1 | const camelcase = require('camelcase');
|
2 | const merge = require('merge-options');
|
3 | const { LoaderOptionsPlugin, optimize } = require('webpack');
|
4 |
|
5 | const { MinChunkSizePlugin, LimitChunkCountPlugin } = optimize;
|
6 |
|
7 | module.exports = {
|
8 | apply(argv, options) {
|
9 | let plugins = [];
|
10 |
|
11 | if (options.plugins) {
|
12 | plugins = plugins.concat(options.plugins);
|
13 | }
|
14 |
|
15 | const { flags } = module.exports;
|
16 | const keys = Object.keys(flags);
|
17 |
|
18 | for (const key of keys) {
|
19 | const arg = camelcase(key);
|
20 | const flag = flags[key];
|
21 | const value = argv[arg];
|
22 |
|
23 | if (value) {
|
24 | const plugin = flag.apply(value);
|
25 | plugins.unshift(plugin);
|
26 | }
|
27 | }
|
28 |
|
29 | return merge(options, plugins.length ? { plugins } : {});
|
30 | },
|
31 |
|
32 | flags: {
|
33 | 'optimize-max-chunks': {
|
34 | apply: (value) => new LimitChunkCountPlugin({ maxChunks: parseInt(value, 10) }),
|
35 | desc: 'Try to keep the chunk count below a limit',
|
36 | type: 'number'
|
37 | },
|
38 | 'optimize-min-chunk-size': {
|
39 | apply: (value) => new MinChunkSizePlugin({ minChunkSize: parseInt(value, 10) }),
|
40 | desc: 'Try to keep the chunk size above a limit',
|
41 | type: 'number'
|
42 | },
|
43 | 'optimize-minimize': {
|
44 | apply: () => new LoaderOptionsPlugin({ minimize: true }),
|
45 | desc: 'Minimize javascript and switches loaders to minimizing',
|
46 | type: 'boolean'
|
47 | }
|
48 | },
|
49 |
|
50 | name: 'Optimization'
|
51 | };
|