我有两个入口点,并且使用Webpack 4,但是用于生产的构建大约需要3-5分钟,并为两个入口点创建两个大型vendor
输出js文件。看来我有:
对于一个条目:
main.4b8e36a69d37fde00916.js 1.15 MiB 0 [emitted] [big] main
vendors~main.ebc7e19767bc133dd354.js 5.36 MiB 1 [emitted] [big] vendors~main
第二次:
../identityServerModel.cc17842eb2ee86c9a5a7.js 17.2 KiB 0 [emitted] identitySer
../vendors~main.6adcd3c3d6680968397f.js 4.24 MiB 2 [emitted] [big] vendors~main
如何优化它?您有任何建议,解决方法或技巧吗?
当我启动build
脚本时,我为此入口点运行两个构建。
package.json:
...
"build": "webpack --colors --config webpack/prod.js && webpack --config webpack/identityServer.js && cpx \"dist/**/*\" ../SomeFolder/Ui --clean",
...
我的webpack:
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'}},
}
拳头入口点配置:
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,
}),
],
})
第二个入口点:
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: '/',
},
})
我有一个主要项目,我在prod.js
中捆绑了所有vendros,在identetyServer.js
中捆绑了服务器页面,每个条目都有相似的供应商(react,redux和所有这些东西)。对我来说,两个条目的供应商捆绑包看起来都很相似。
答案 0 :(得分:0)
有很多方法可以加快Webpack的运行速度,但是首先应该考虑的一个选项:
如果要使用extract-text-webpack-plugin
从捆绑中“删除”,请从webpack中删除scss / postcc处理。如果要使用link
在html中引用css,我不知道在webpack中处理css的任何正当理由。
或者,您可以对“增量”样式的webpack构建过程执行相同的操作。但是恕我直言,“退一步”是最简单的方法。