【发布时间】:2019-10-25 11:08:02
【问题描述】:
我想优化所提供的 javascript 页面。我正在使用 webpack 4 并具有以下配置。我使用webpack -p --mode=production webpack。当我在生产模式下使用 webpack 时,我仍然得到大于 1 mb 的文件。我想尽可能减少文件大小以提高性能。
module.exports = {
entry : {
app: [...
],
vendor: ['react','moment','lodash/core']
},
output : {
filename : '[name].bundle.js',
path : __dirname + '/docs',
publicPath : '/'
},
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
chunks: 'initial',
name: 'vendor',
test: 'vendor',
enforce: true
},
}
},
runtimeChunk: true,
minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
},
module : {
rules : [ {
test : /\.js[x]?$/,
loader : 'babel-loader',
exclude : {
test: /node_modules/,
not: [/@babel\/register/],
},
options : {
cacheDirectory : false,
presets : [ '@babel/preset-react' ],
plugins : [ '@babel/plugin-transform-runtime' ],
}
}, {
test : /\.css$/,
use : [ {
loader : MiniCssExtractPlugin.loader,
options : {
publicPath : __dirname + '/docs/stylesheets',
hmr : process.env.NODE_ENV === 'development',
},
}, 'css-loader', ]
}, ...]
},...
plugins : [
new HtmlWebpackPlugin({
template : __dirname + '/src/index.html',
filename : 'index.html',
inject : true
}),...
new MiniCssExtractPlugin({
filename : '[name].css',
chunkFilename : '[id].css',
ignoreOrder : false,
}),
new LodashModuleReplacementPlugin
],
};
【问题讨论】:
标签: optimization webpack code-splitting