Skip to content

Commit c428c4b

Browse files
committed
update webpack-common-chunk
1 parent 616ac6f commit c428c4b

File tree

11 files changed

+50
-19
lines changed

11 files changed

+50
-19
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# CommonsChunkPlugin
1+
# CommonsChunkPlugin
2+
3+
CommonsChunkPlugin插件会生成公共chunk,这个公共chunk会变成entry chunk,即包含webpack runtime,而且会包含其他公共的模块。
4+
5+
应尽量将长期不变动的类库打包到一个vendor chunk文件中,而且这个文件中不应该包含webpack runtime以及其他公共模块,因为webpack runtime和公共模块的代码都会变化,这会导致vendor chunk缓存失效。

tutorials/webpack-common-chunk/example2.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ module.exports = {
1515

1616
// plugins: [
1717
// new webpack.optimize.CommonsChunkPlugin({
18-
// //本例中init.js = webpack runtime + polyfill + utility2
18+
// //init.js = webpack runtime + polyfill + utility2
1919
// name: "init"
2020
// })
2121
// ]
2222

2323
plugins: [
2424
new webpack.optimize.CommonsChunkPlugin({
25-
//本例中init.js = webpack runtime
25+
//init.js = webpack runtime
2626
name: "init",
2727
minChunks: Infinity
2828
})

tutorials/webpack-common-chunk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"reference": [
2222
"https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin",
2323
"http://stackoverflow.com/questions/39548175/can-someone-explain-webpacks-commonschunkplugin",
24+
"http://stackoverflow.com/questions/35908253/webpack-how-to-bundle-entries-to-multiple-common-chunks-with-commonschunkplugin",
2425
"https://webpack.js.org/plugins/commons-chunk-plugin/",
2526
"https://github.com/webpack/webpack/tree/master/examples/common-chunk-and-vendor-chunk",
2627
"https://github.com/webpack/webpack/tree/master/examples/multiple-entry-points-commons-chunk-css-bundle",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "jQuery";

tutorials/webpack-common-chunk/src/pageA.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var polyfill = require('./polyfill');
2+
var jQuery = require('./jQuery');
3+
var underscore = require('./underscore');
24
var utility1 = require('./utility1');
35
var utility2 = require('./utility2');
46

tutorials/webpack-common-chunk/src/pageB.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var polyfill = require('./polyfill');
2+
var jQuery = require('./jQuery');
3+
var underscore = require('./underscore');
24
var utility2 = require('./utility2');
35
var utility3 = require('./utility3');
46

tutorials/webpack-common-chunk/src/pageC.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var polyfill = require('./polyfill');
2+
var jQuery = require('./jQuery');
3+
var underscore = require('./underscore');
24
var utility2 = require('./utility2');
35
var utility3 = require('./utility3');
46

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "underscore";

tutorials/webpack-common-chunk/src/vendor1.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

tutorials/webpack-common-chunk/src/vendor2.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

tutorials/webpack-common-chunk/webpack.config.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var webpack = require("webpack");
1313

1414
module.exports = {
1515
entry: {
16-
vendor: ["./src/polyfill", "./src/vendor1", "./src/vendor2"],
16+
vendor: ["./src/polyfill", "./src/jQuery", "./src/underscore"],
1717
pageA: "./src/pageA",
1818
pageB: "./src/pageB",
1919
pageC: "./src/pageC"
@@ -49,30 +49,38 @@ module.exports = {
4949
// })
5050
// ]
5151

52-
//http://stackoverflow.com/questions/39548175/can-someone-explain-webpacks-commonschunkplugin
53-
plugins: [
54-
new webpack.optimize.CommonsChunkPlugin({
55-
name: "common",
56-
minChunks: 2
57-
}),
58-
59-
new webpack.optimize.CommonsChunkPlugin({
60-
name: "init",
61-
minChunks: 2
62-
})
63-
]
52+
// plugins: [
53+
// new webpack.optimize.CommonsChunkPlugin({
54+
// // init.js只用于存储webpack runtime的代码,即init.js = webpack runtime
55+
// name: "init",
56+
// minChunks: Infinity
57+
// })
58+
// ]
6459

6560
// plugins: [
6661
// new webpack.optimize.CommonsChunkPlugin({
6762
// //顺序很重要
68-
// //If an array of strings is passed this is equal to invoking the plugin multiple times for each chunk name.
63+
// //If an array of strings is passed this is equal to invoking the plugin multiple times for each chunk name.
6964
// //common.js用于至少被2个entry都使用的公共模块,即common.js = polyfill + utility2.js + utility3.js
7065
// //init.js用于存储webpack runtime的代码,即init.js = webpack runtime
7166
// names: ["common", "init"],
7267
// minChunks: 2
7368
// })
7469
// ]
7570

71+
//http://stackoverflow.com/questions/39548175/can-someone-explain-webpacks-commonschunkplugin
72+
// plugins: [
73+
// new webpack.optimize.CommonsChunkPlugin({
74+
// name: "common",
75+
// minChunks: 2
76+
// }),
77+
78+
// new webpack.optimize.CommonsChunkPlugin({
79+
// name: "init",
80+
// minChunks: 2
81+
// })
82+
// ]
83+
7684
// plugins: [
7785
// new webpack.optimize.CommonsChunkPlugin({
7886
// //顺序很重要
@@ -82,4 +90,16 @@ module.exports = {
8290
// minChunks: 2
8391
// })
8492
// ]
93+
94+
//http://stackoverflow.com/questions/35908253/webpack-how-to-bundle-entries-to-multiple-common-chunks-with-commonschunkplugin
95+
plugins: [
96+
new webpack.optimize.CommonsChunkPlugin({
97+
name: "vendor",
98+
chunks: ["vendor"]
99+
}),
100+
new webpack.optimize.CommonsChunkPlugin({
101+
name: "init",
102+
chunks: ["pageA", "pageB", "pageC"]
103+
})
104+
]
85105
};

0 commit comments

Comments
 (0)