Skip to content

Commit 6dc571b

Browse files
committed
feat: use optimization config
1 parent e2bc0a8 commit 6dc571b

7 files changed

Lines changed: 151 additions & 84 deletions

File tree

config/plugin.js

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -55,69 +55,69 @@ exports.define = {
5555
}
5656
};
5757

58-
exports.commonsChunk = {
59-
enable() {
60-
const config = this.config;
61-
return !config.dll && !(config.optimization && config.optimization.splitChunks);
62-
},
63-
type: 'client',
64-
name: webpack.optimize.SplitChunksPlugin,
65-
action: 'merge',
66-
args() {
67-
const packKeys = Object.keys(this.packs || {});
68-
const chunks = Object.keys(this.webpackConfig.entry || {}).filter(entry => {
69-
return !packKeys.includes(entry);
70-
});
71-
const lib = this.utils.isObject(this.config.lib) ? this.config.lib : {};
72-
const name = lib.name || 'common';
73-
return {
74-
name,
75-
chunks
76-
};
77-
}
78-
};
58+
// exports.commonsChunk = {
59+
// enable() {
60+
// const config = this.config;
61+
// return !config.dll && !(config.optimization && config.optimization.splitChunks);
62+
// },
63+
// type: 'client',
64+
// name: webpack.optimize.SplitChunksPlugin,
65+
// action: 'merge',
66+
// args() {
67+
// const packKeys = Object.keys(this.packs || {});
68+
// const chunks = Object.keys(this.webpackConfig.entry || {}).filter(entry => {
69+
// return !packKeys.includes(entry);
70+
// });
71+
// const lib = this.utils.isObject(this.config.lib) ? this.config.lib : {};
72+
// const name = lib.name || 'common';
73+
// return {
74+
// name,
75+
// chunks
76+
// };
77+
// }
78+
// };
7979

80-
exports.runtime = {
81-
enable() {
82-
return this.isUse('commonsChunk');
83-
},
84-
type: 'client',
85-
name: webpack.optimize.RuntimeChunkPlugin,
86-
action: 'merge',
87-
args() {
88-
const config = this.config;
89-
const runtimeChunk = config.optimization && config.optimization.runtimeChunk;
90-
const name = this.utils.isObject(runtimeChunk) && runtimeChunk.name;
91-
return {
92-
name: name || 'runtime'
93-
};
94-
}
95-
};
80+
// exports.runtime = {
81+
// enable() {
82+
// return this.isUse('commonsChunk');
83+
// },
84+
// type: 'client',
85+
// name: webpack.optimize.RuntimeChunkPlugin,
86+
// action: 'merge',
87+
// args() {
88+
// const config = this.config;
89+
// const runtimeChunk = config.optimization && config.optimization.runtimeChunk;
90+
// const name = this.utils.isObject(runtimeChunk) && runtimeChunk.name;
91+
// return {
92+
// name: name || 'runtime'
93+
// };
94+
// }
95+
// };
9696

97-
exports.uglifyJs = {
98-
enable() {
99-
const config = this.config;
100-
return !(config.optimization && config.optimization.minimize);
101-
},
102-
env: ['prod'],
103-
name: 'uglifyjs-webpack-plugin',
104-
args: {
105-
cache: true,
106-
parallel: 2,
107-
sourceMap: true,
108-
uglifyOptions: {
109-
warnings: false,
110-
compress: {
111-
dead_code: true,
112-
drop_console: true,
113-
drop_debugger: true
114-
},
115-
output: {
116-
comments: false
117-
}
118-
}
119-
}
120-
};
97+
// exports.uglifyJs = {
98+
// enable() {
99+
// const config = this.config;
100+
// return !(config.optimization && config.optimization.minimize);
101+
// },
102+
// env: ['prod'],
103+
// name: 'uglifyjs-webpack-plugin',
104+
// args: {
105+
// cache: true,
106+
// parallel: 2,
107+
// sourceMap: true,
108+
// uglifyOptions: {
109+
// warnings: false,
110+
// compress: {
111+
// dead_code: true,
112+
// drop_console: true,
113+
// drop_debugger: true
114+
// },
115+
// output: {
116+
// comments: false
117+
// }
118+
// }
119+
// }
120+
// };
121121

122122
exports.manifest = {
123123
enable: true,

lib/base.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -218,22 +218,7 @@ class WebpackBaseBuilder extends Config {
218218
}
219219

220220
getCommonsChunk(useRuntime = true) {
221-
let commonsChunks = [];
222-
if (useRuntime) {
223-
const runtime = this.getPluginByName('runtime');
224-
if (runtime && runtime.enable) {
225-
if (runtime.args) {
226-
commonsChunks = commonsChunks.concat(runtime.args.names || runtime.args.name || []);
227-
}
228-
}
229-
}
230-
const common = this.getPluginByName('commonsChunk');
231-
if (common && common.enable) {
232-
if (common.args) {
233-
commonsChunks = commonsChunks.concat(common.args.names || common.args.name || []);
234-
}
235-
}
236-
return commonsChunks;
221+
return this.webpackOptimize.getCommonsChunk();
237222
}
238223

239224

@@ -539,14 +524,19 @@ class WebpackBaseBuilder extends Config {
539524
});
540525
}
541526

527+
createOptimization() {
528+
return this.webpackOptimize.getOptimization();
529+
}
530+
542531
combineWebpackConfig(config) {
543532
this.t3 = Date.now();
544533
const buildWebpackConfig = {
545534
output: this.createOutput(config),
546535
module: {
547536
rules: this.createWebpackLoader(config)
548537
},
549-
plugins: this.createWebpackPlugin(config)
538+
plugins: this.createWebpackPlugin(config),
539+
optimization: this.createOptimization(config)
550540
};
551541
const webpackConfig = this.mergeWebpackConfig(this.nativeWebpackConfig, buildWebpackConfig);
552542
this.t4 = Date.now();

lib/client.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class WebpackClientBuilder extends WebpackBaseBuilder {
5454
}
5555
}
5656

57+
createOptimization() {
58+
return this.webpackOptimize.getWebOptimization();
59+
}
60+
5761
createHotEntry() {
5862
if (this.hot) {
5963
const hotMiddleware = require.resolve('webpack-hot-middleware').split(path.sep);
@@ -131,8 +135,9 @@ class WebpackClientBuilder extends WebpackBaseBuilder {
131135
} : false;
132136
const commonsChunk = this.getCommonsChunk();
133137
const dllChunks = this.getDLLChunk();
134-
const chunks = [].concat(dllChunks.names).concat(commonsChunk).concat(entryName);
138+
const chunks = [].concat(dllChunks.names).concat(commonsChunk);
135139
if (!commonsChunk.includes(entryName)) {
140+
chunks.push(entryName);
136141
this.plugins[entryName] = this.merge({ args: { minify, chunks, filename, template, css, js } }, plugin);
137142
}
138143
});

lib/config.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const webpack = WebpackTool.webpack;
77
const merge = WebpackTool.merge;
88
const utils = require('../utils/utils');
99
const uniq = require('lodash.uniq');
10+
const WebpackOptimize = require('./optimize');
1011
const Logger = require('../utils/logger');
1112
const Adapter = require('../utils/adapter');
1213
const zero = require('./zero');
@@ -31,7 +32,7 @@ class Config {
3132
this.beforeCreateQueue = [];
3233
this.webpackNodeList = [
3334
'context', 'mode', 'target', 'node', 'output', 'externals', 'resolve', 'watch', 'watchOptions', 'amd',
34-
'resolveLoader', 'devServer', 'performance', 'module', 'profile', 'stats', 'cache', 'optimization'
35+
'resolveLoader', 'devServer', 'performance', 'module', 'profile', 'stats', 'cache'
3536
];
3637
this.webpackModuleRuleKeys = [
3738
'test', 'use', 'include', 'exclude', 'issuer', 'loader', 'oneOf', 'options', 'parser', 'resource',
@@ -52,6 +53,7 @@ class Config {
5253
};
5354
this.logger = new Logger(this.config.logger, this);
5455
this.adapter = new Adapter(this);
56+
this.webpackOptimize = new WebpackOptimize(this);
5557
this.initZero(this.config);
5658
this.initEntry(this.config);
5759
this.initialize(this.config);
@@ -220,7 +222,7 @@ class Config {
220222
this.dev = true;
221223
this.mergeConfig(defaultConfig.devConfig);
222224
}
223-
this.webpackConfig.mode = this.test || this.prod ? 'production' : 'development';
225+
this.webpackConfig.mode = this.prod ? 'production' : 'development';
224226
}
225227

226228
initEntry(config) {
@@ -453,8 +455,14 @@ class Config {
453455
this.webpackConfig.devtool = cliDevtool;
454456
} else if (cliDevtool === true) {
455457
this.webpackConfig.devtool = 'source-map';
456-
} else if (devtool && this.dev) { /* istanbul ignore next */
457-
this.webpackConfig.devtool = devtool;
458+
} else if (devtool) { /* istanbul ignore next */
459+
if (this.dev) {
460+
this.webpackConfig.devtool = devtool;
461+
} else {
462+
this.webpackConfig.devtool = false;
463+
}
464+
} else {
465+
this.webpackConfig.devtool = false;
458466
}
459467
}
460468

lib/optimize.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
module.exports = class WebpackOptimize {
3+
constructor(ctx) {
4+
this.ctx = ctx;
5+
}
6+
7+
getCommonsChunk() {
8+
const commonsChunks = [];
9+
const optimization = this.getWebOptimization();
10+
const { runtimeChunk = {}, splitChunks = {} } = optimization;
11+
const { cacheGroups = {} } = splitChunks;
12+
if (runtimeChunk.name) {
13+
commonsChunks.push(runtimeChunk.name);
14+
}
15+
Object.keys(cacheGroups).forEach(key => {
16+
const group = cacheGroups[key];
17+
const name = group.name || key;
18+
if (!commonsChunks.includes(name)) {
19+
commonsChunks.push(name);
20+
}
21+
});
22+
return commonsChunks;
23+
}
24+
25+
getOptimization() {
26+
return {};
27+
}
28+
29+
getWebOptimization() {
30+
return {
31+
runtimeChunk: {
32+
name: 'runtime'
33+
},
34+
splitChunks: {
35+
chunks: 'async',
36+
minSize: 30000,
37+
maxAsyncRequests: 5,
38+
maxInitialRequests: 3,
39+
name: false,
40+
cacheGroups: {
41+
common: {
42+
name: 'common',
43+
chunks: 'initial',
44+
minChunks: 2,
45+
test: /node_modules\/(.*)\.js/
46+
},
47+
styles: {
48+
name: 'common',
49+
chunks: 'all',
50+
minChunks: 2,
51+
test: /\.(css|less|scss|stylus)$/
52+
}
53+
}
54+
}
55+
};
56+
}
57+
58+
getNodeOptimization() {
59+
return this.getOptimization();
60+
}
61+
};

lib/server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class WebpackServerBuilder extends WebpackBaseBuilder {
4343
}
4444
return super.createOutput();
4545
}
46+
47+
createOptimization() {
48+
return this.webpackOptimize.getNodeOptimization();
49+
}
4650
}
4751
WebpackServerBuilder.TYPE = 'server';
4852
WebpackServerBuilder.TARGET = 'node';

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
"less": "^2.7.2",
7272
"less-loader": "^4.0.5",
7373
"mocha": "^3.4.2",
74-
"node-sass": "^4.5.3",
7574
"node-tool-utils": "^1.0.0",
7675
"nyc": "^11.1.0",
7776
"sass-loader": "^6.0.6",

0 commit comments

Comments
 (0)