[英]Decrease and Optimize bundle with two entry points with Webpack 4
I have two entry points and use Webpack 4, but the build for production takes about 3-5 minutes and create for both entry points two large vendor
output js files. 我有两个入口点,并使用Webpack 4,但是用于生产的构建大约需要3-5分钟,并为两个入口点创建两个大型vendor
输出js文件。 Looks like I have: 看起来我有:
For one entry: 对于一个条目:
main.4b8e36a69d37fde00916.js 1.15 MiB 0 [emitted] [big] main
vendors~main.ebc7e19767bc133dd354.js 5.36 MiB 1 [emitted] [big] vendors~main
For second: 第二:
../identityServerModel.cc17842eb2ee86c9a5a7.js 17.2 KiB 0 [emitted] identitySer
../vendors~main.6adcd3c3d6680968397f.js 4.24 MiB 2 [emitted] [big] vendors~main
How to optimize it? 如何优化呢? Do you have any suggestions and workaround or tricks? 您有任何建议,解决方法或技巧吗? When I start my build
script I run two builds for this entry points. 启动build
脚本时,我为此入口点运行两个构建。
package.json: package.json:
...
"build": "webpack --colors --config webpack/prod.js && webpack --config webpack/identityServer.js && cpx \"dist/**/*\" ../SomeFolder/Ui --clean",
...
My webpack: 我的webpack:
common.js: common.js:
const extractTextPlugin = require('extract-text-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const path = require('path')
// initialize version.js
require('child_process').exec('node ' + path.resolve('./../scripts/setAppVersion.js'), {cwd: '../'}, function (err, stdout, stderr) {
console.log(err)
})
module.exports = {
mode: 'development',
devtool: 'source-map',
entry: './src/main.js',
plugins: [
// TODO remove after deleting *.scss files across app
new extractTextPlugin({
filename: 'bundle.css',
disable: false,
allChunks: true,
}),
new CleanWebpackPlugin(['dist'], {
root: path.join(__dirname, '..'),
}),
],
output: {
filename: '[name].[chunkhash].js',
path: path.resolve(__dirname, '../dist'),
publicPath: '/',
},
module: {
rules: [
{
test: /\.js$/,
use: 'eslint-loader?{fix:true}',
exclude: /node_modules/,
enforce: 'pre',
},
{
test: /\.html$/,
exclude: /node_modules/,
use: 'file-loader?name=[name].[ext]',
},
{
test: /\.css$/,
loader: 'style-loader!css-loader',
},
// TODO remove extractTextPlugin after delete all .scss in react-components
{
test: /\.scss$/,
use: extractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader!sass-loader',
}),
},
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader?name=fonts/[name].[ext]',
},
{
test: /\.(png|mp4)$/,
use: 'file-loader',
},
],
},
performance: {hints: false},
optimization: {splitChunks: {chunks: 'all'}},
}
fist entry point config: 拳头入口点配置:
prod.js: prod.js:
const merge = require('webpack-merge')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const common = require('./common.js')
module.exports = merge(common, {
mode: 'production',
devtool: false,
stats: 'normal',
plugins: [
new webpack.optimize.ModuleConcatenationPlugin(),
new HtmlWebpackPlugin({
template: './src/index.ejs',
hash: true,
}),
],
})
second entry point: 第二个入口点:
identityServer.js: identityServer.js:
const merge = require('webpack-merge')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const common = require('./common.js')
const path = require('path')
const templates = {
index: {excludeChunks: ['identityServerModel']},
logout: {
excludeChunks: ['main'],
chunks: ['identityServerModel'],
inject: 'head',
},
error: {
excludeChunks: ['main'],
chunks: ['identityServerModel'],
},
}
const entryHtmlPlugins = Object.keys(templates).map(template => new HtmlWebpackPlugin({
template: `./src/components/loginPage/${template}.ejs`,
filename: `${template}.html`,
hash: true,
...templates[template],
}))
const commonFiltered = {
...common,
}
commonFiltered.plugins = commonFiltered.plugins.filter(plugin => !(plugin instanceof CleanWebpackPlugin))
module.exports = merge(commonFiltered, {
mode: 'production',
devtool: false,
stats: 'normal',
entry: {
identityServerModel: './src/components/loginPage/identityServerModel.js',
main: './src/components/loginPage/login.js',
},
plugins: entryHtmlPlugins,
output: {
filename: '../[name].[chunkhash].js',
path: path.resolve(__dirname, '../dist/login'),
publicPath: '/',
},
})
I have main project and I bundle all vendros in prod.js
and server page in identetyServer.js
each of this entries has similar vendors (react, redux and all this stuff). 我主要的项目,我捆绑在所有vendros prod.js
和服务器页面中identetyServer.js
各该条目也有类似的供应商(反应,终极版和所有的东西)。 For me vendor bundles looks similar for both entries. 对我来说,两个条目的供应商捆绑包看起来都很相似。
There are many options to speed up webpack, but there is one that should be considered first: 有很多选项可以加快Webpack的运行速度,但是首先应该考虑的一个选项是:
Remove scss/postcc processing from webpack if you are going to "drop it out" from bundle with extract-text-webpack-plugin
. 如果要使用extract-text-webpack-plugin
从捆绑中“删除”,请从webpack中删除scss / postcc处理。 I do not know any good reason to process css in webpack if you are going to reference it in the html with link
. 如果您要在具有link
的html中引用它,我不知道在Webpack中处理css的任何正当理由。
Alternatively you can do something the same with "incremental" style webpack build processes. 另外,您也可以使用“增量”样式的webpack构建过程执行相同的操作。 But IMHO one "step back" is the most simple way. 但是恕我直言,“退一步”是最简单的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.