我正在尝试将项目拆分为公共块,使用旧的 commonChunks 插件很容易,因为该项目旨在拆分为各种库......使用新的 splitChunks 我无法获得相同的结果。所以我回到了一个基本的测试来试图理解新的 splitChunks,不幸的是我面临着同样的问题。这里是配置文件:
const webpack = require('webpack'); //to access built-in plugins
const path = require('path');
var PROD = JSON.parse(process.env.PROD_ENV || '0');
const groupsOptions = {chunks: "all", minSize:0, minChunks: 1, reuseExistingChunk: true, enforce: true};
const reactConfig = {
entry: {
"react": ["react", "react-dom"],
"pageA": "./src/A.jsx",
"pageB": "./src/B.jsx",
"pageC": "./src/C.jsx",
"pageD": "./src/D.jsx",
"commonAB": [ "./src/ab_a.js", "./src/ab_b.js"],
"commonCD": ["./src/cd_c.js", "./src/cd_d.js"]
},
output: {
path: __dirname + '/build/',
filename: PROD
? "[name].min.js"
: "[name].js"
},
module: {
rules: [
{
test: /\.jsx$/,
use: {
loader: "babel-loader",
options: {
presets: ["es2015", "react"]
}
}
}
]
},
optimization: {
splitChunks: {
cacheGroups:{
react: {test:'react', name: "react", ...groupsOptions},
AB: {test:'commonAB', name: "commonAB", ...groupsOptions},
CD: {test:'commonCD', name: "commonCD", ...groupsOptions}
}
}
},
plugins: [
// new webpack.optimize.CommonsChunkPlugin({
// name: "react",
// filename: "react.js",
// chunks: ["pageA", "pageB"]
// }),
// new webpack.optimize.CommonsChunkPlugin({
// name: "commonAB",
// chunks: ["pageA", "pageB"]
// })
//new webpack.optimize.UglifyJsPlugin()
]
};
使用该配置,我可以实现此输出,这正是我想要做的:
Asset Size Chunks Chunk Names
commonAB.js 1.67 KiB commonAB [emitted] commonAB
commonCD.js 1.67 KiB commonCD [emitted] commonCD
pageA.js 8.1 KiB pageA [emitted] pageA
pageB.js 8.15 KiB pageB [emitted] pageB
pageC.js 8.1 KiB pageC [emitted] pageC
pageD.js 8.1 KiB pageD [emitted] pageD
react.js 631 KiB react [emitted] react
Entrypoint pageA = react.js commonAB.js pageA.js
Entrypoint pageB = react.js commonAB.js pageB.js
Entrypoint pageC = react.js commonCD.js pageC.js
Entrypoint pageD = react.js commonCD.js pageD.js
[./src/A.jsx] 2.51 KiB {pageA} [built]
[./src/B.jsx] 2.56 KiB {pageB} [built]
[./src/C.jsx] 2.51 KiB {pageC} [built]
[./src/D.jsx] 2.51 KiB {pageD} [built]
[./src/ab_a.js] 122 bytes {commonAB} [built]
[./src/ab_b.js] 122 bytes {commonAB} [built]
[./src/cd_c.js] 122 bytes {commonCD} [built]
[./src/cd_d.js] 122 bytes {commonCD} [built]
[0] multi react react-dom 40 bytes {react} [built]
[1] multi ./src/ab_a.js ./src/ab_b.js 40 bytes {commonAB} [built]
[2] multi ./src/cd_c.js ./src/cd_d.js 40 bytes {commonCD} [built]
+ 23 hidden modules
这个例子很简单,一切正常!
- pageA 取决于 react 和 commonAB -> OK
- pageB 取决于 react 和 commonAB -> OK
- pageC 依赖于 react 和 commonCD -> OK
- pageD 取决于 react 和 commonCD -> OK
但是,当我修改ab_a.js的代码并添加
import React from 'react';
输出是:
> webpack --config src.config.js --colors --progress --watch --mode development
10% building modules 3/3 modules 0 active
Webpack is watching the files…
Hash: 020e818e618248278134
Version: webpack 4.2.0
Time: 1900ms
Built at: 2018-3-22 11:03:21
Asset Size Chunks Chunk Names
commonAB.js 1.94 KiB commonAB [emitted] commonAB
commonCD.js 1.67 KiB commonCD [emitted] commonCD
pageA.js 8.1 KiB pageA [emitted] pageA
pageB.js 8.15 KiB pageB [emitted] pageB
pageC.js 8.11 KiB pageC [emitted] pageC
pageD.js 8.11 KiB pageD [emitted] pageD
react.js 631 KiB react [emitted] react
Entrypoint pageA = commonAB.js react.js pageA.js
Entrypoint pageB = commonAB.js react.js pageB.js
Entrypoint pageC = commonAB.js react.js commonCD.js pageC.js
Entrypoint pageD = commonAB.js react.js commonCD.js pageD.js
[./src/A.jsx] 2.51 KiB {pageA} [built]
[./src/B.jsx] 2.56 KiB {pageB} [built]
[./src/C.jsx] 2.51 KiB {pageC} [built]
[./src/D.jsx] 2.51 KiB {pageD} [built]
[./src/ab_a.js] 152 bytes {commonAB} [built]
[./src/ab_b.js] 122 bytes {commonAB} [built]
[./src/cd_c.js] 122 bytes {commonCD} [built]
[./src/cd_d.js] 122 bytes {commonCD} [built]
[0] multi react react-dom 40 bytes {react} [built]
[1] multi ./src/ab_a.js ./src/ab_b.js 40 bytes {commonAB} [built]
[2] multi ./src/cd_c.js ./src/cd_d.js 40 bytes {commonCD} [built]
+ 23 hidden modules
如您所见, pageC 和 pageD 取决于 commonAB.js,但事实并非如此!这个例子很简单,一切正常!
- pageA 取决于 react 和 commonAB -> OK
- pageB 取决于 react 和 commonAB -> OK
- pageC 取决于 react 和 commonAB 和 commonCD -> 错误
- pageD 取决于 react 和 commonAB 和 commonCD -> 错误
此外,如果我从“react”导入 React;进入 cs_c.js (已经预期,但不是我想要的)我有输出:
Asset Size Chunks Chunk Names
commonAB.js 1.94 KiB commonAB [emitted] commonAB
commonCD.js 1.94 KiB commonCD [emitted] commonCD
pageA.js 8.11 KiB pageA [emitted] pageA
pageB.js 8.16 KiB pageB [emitted] pageB
pageC.js 8.11 KiB pageC [emitted] pageC
pageD.js 8.11 KiB pageD [emitted] pageD
react.js 631 KiB react [emitted] react
Entrypoint pageA = commonCD.js commonAB.js react.js pageA.js
Entrypoint pageB = commonCD.js commonAB.js react.js pageB.js
Entrypoint pageC = commonCD.js commonAB.js react.js pageC.js
Entrypoint pageD = commonCD.js commonAB.js react.js pageD.js
[./src/A.jsx] 2.51 KiB {pageA} [built]
[./src/B.jsx] 2.56 KiB {pageB} [built]
[./src/C.jsx] 2.51 KiB {pageC} [built]
[./src/D.jsx] 2.51 KiB {pageD} [built]
[./src/ab_a.js] 152 bytes {commonAB} [built]
[./src/ab_b.js] 122 bytes {commonAB} [built]
[./src/cd_c.js] 152 bytes {commonCD} [built]
[./src/cd_d.js] 122 bytes {commonCD} [built]
[0] multi react react-dom 40 bytes {react} [built]
[1] multi ./src/ab_a.js ./src/ab_b.js 40 bytes {commonAB} [built]
[2] multi ./src/cd_c.js ./src/cd_d.js 40 bytes {commonCD} [built]
+ 23 hidden modules
- pageA 取决于 react 和 commonAB 和 commonCD -> 错误
- pageB 取决于 react 和 commonAB 和 commonCD -> 错误
- pageC 取决于 react 和 commonAB 和 commonCD -> 错误
- pageD 取决于 react 和 commonAB 和 commonCD -> 错误
感谢您的帮助 !