我最近在ReactJS上完成了一个项目,该项目的唯一问题是其生产bundle.js大小约为 30MB ,这真是太庞大了。我已经使用BundleAnalyzerPlugin插件进行了分析,看起来像这样
块越大,大小越大。
node_modules = 10 MB 每个可见的小块几乎都 = 1MB
我的webpack配置如下
webpack.base.config.js
var webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = {
entry: [
'./src/index.js'
],
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
noParse:[
'./~/xlsx/jszip.js',
],
externals:[
{'./jszip': 'jszip'}
],
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['react', 'es2015', 'stage-1']
}
},
{
test: /\.json$/,
loader: "json-loader"
},
{
test: /\.(html)$/,
loader: "html-loader",
options:{
attrs:[':data-src!./src/components/preview/preview_html',':data-src!./src/components/landing/loader/index.html']
}
},
{
test: /\.(ttf|gif|eot|svg|woff|woff2|png)(\?.+)?$/,
loader: 'file-loader?name=[hash:12].[ext]'
},
{
test: /\.css?$/,
loader: 'style-loader!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'
},
{
test: /\.scss?$/,
loader: 'style-loader!css-loader!sass-loader'
}]
},
resolve: {
root: __dirname,
alias:{
//Have set of aliases
},
extensions: ['','.json', '.js', '.jsx','.ejs']
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new BundleAnalyzerPlugin()
],
devServer: {
historyApiFallback: true,
contentBase: './',
watchContentBase: true,
},
devtool:'cheap-module-eval-source-map'
};
webpack.prod.config.js
const merge = require('webpack-merge');
const baseConfig = require('./webpack.base.config.js');
var webpack = require('webpack');
var CompressionPlugin = require('compression-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = merge(baseConfig,{
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
}),
new HtmlWebpackPlugin({
template:'index.ejs',
hash: false,
baseHref: 'xxxx',
scripts: [
{
src: 'bundle.js',
type: 'text/javascript'
}
]
})
]
});
我已经直接从node_modules中导入了少量CSS到 index.ejs 文件中,该文件如下所示:
<link rel="stylesheet" href="/node_modules/react-bootstrap-switch/dist/css/bootstrap3/react-bootstrap-switch.css">
<link rel="stylesheet" href="/node_modules/draft-js/dist/Draft.css">
<link rel="stylesheet" href="/node_modules/react-select-plus/dist/react-select-plus.css">
<link rel="stylesheet" href="/node_modules/react-day-picker/lib/style.css">
<link rel="stylesheet" href="/node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css">
<script src="node_modules/xlsx/dist/shim.min.js"></script>
<script src="node_modules/xlsx/dist/xlsx.full.min.js"></script>
需要帮助来减小尺寸,并发现配置或实现中的错误。
谢谢。
答案 0 :(得分:1)
我假设您正在使用webpack4。我注意到您尚未在生产Webpack配置中设置mode: 'production'
。该选项将进行优化,包括使用terser
最小化捆绑输出。另外,将NODE_ENV设置为production
,某些库直接依赖于该变量的值。
我建议从此开始,并阅读以下内容:https://webpack.js.org/guides/production/
完成此操作后,您可以开始确定在供应商捆绑包中占据最大容量的第三方模块。然后,您可以考虑对它们进行代码拆分,以便它们将位于各自独立的块中,并且仅在需要时才可以加载它们。您可以在这里阅读有关代码拆分的信息:https://webpack.js.org/guides/code-splitting/