1 | const { resolve } = require('path');
|
2 |
|
3 | const camelcase = require('camelcase');
|
4 | const chalk = require('chalk');
|
5 | const isObject = require('isobject');
|
6 | const merge = require('merge-options');
|
7 | const {
|
8 | DefinePlugin,
|
9 | HotModuleReplacementPlugin,
|
10 | PrefetchPlugin,
|
11 | ProvidePlugin
|
12 | } = require('webpack');
|
13 |
|
14 | const { loadPlugin } = require('./util');
|
15 |
|
16 | module.exports = {
|
17 | apply(argv, options) {
|
18 | let plugins = [];
|
19 | const output = { watchOptions: {} };
|
20 |
|
21 | if (options.plugins) {
|
22 | plugins = plugins.concat(options.plugins);
|
23 | }
|
24 |
|
25 | for (const flag of ['bail', 'cache', 'profile', 'target']) {
|
26 | if (argv[flag]) {
|
27 | output[flag] = argv[flag];
|
28 | }
|
29 | }
|
30 |
|
31 | for (const name of ['records-input-path', 'records-output-path', 'records-path']) {
|
32 | const flag = camelcase(name);
|
33 | if (argv[flag]) {
|
34 | output[flag] = resolve(argv[flag]);
|
35 | }
|
36 | }
|
37 |
|
38 |
|
39 | if (argv.define && isObject(argv.define)) {
|
40 | const plugin = new DefinePlugin(argv.define);
|
41 | plugins.unshift(plugin);
|
42 | }
|
43 |
|
44 | if (argv.hot) {
|
45 | const plugin = new HotModuleReplacementPlugin();
|
46 | plugins.unshift(plugin);
|
47 | }
|
48 |
|
49 | if (argv.plugin) {
|
50 | const plugin = loadPlugin(argv.plugin);
|
51 | plugins.unshift(plugin);
|
52 | }
|
53 |
|
54 | if (argv.prefetch) {
|
55 | const plugin = new PrefetchPlugin(argv.prefetch);
|
56 | plugins.unshift(plugin);
|
57 | }
|
58 |
|
59 | if (argv.provide && isObject(argv.provide)) {
|
60 | const plugin = new ProvidePlugin(argv.provide);
|
61 | plugins.unshift(plugin);
|
62 | }
|
63 |
|
64 | if (argv.watchAggregateTimeout) {
|
65 | output.watchOptions.aggregateTimeout = +argv.watchAggregateTimeout;
|
66 | }
|
67 |
|
68 | if (argv.watchPoll) {
|
69 | const value = typeof argv.watchPoll === 'boolean' ? true : +argv.watchPoll;
|
70 | output.watchOptions.poll = value;
|
71 | }
|
72 |
|
73 | if (argv.watchStdin) {
|
74 | output.watchOptions.stdin = true;
|
75 | output.watch = true;
|
76 | }
|
77 |
|
78 | if (!Object.keys(output.watchOptions).length) {
|
79 | delete output.watchOptions;
|
80 | }
|
81 |
|
82 | const result = merge(options, output, plugins.length ? { plugins } : {});
|
83 |
|
84 | return result;
|
85 | },
|
86 |
|
87 | flags: {
|
88 | bail: {
|
89 | type: 'boolean',
|
90 | desc: 'Abort the compilation on first error'
|
91 | },
|
92 | cache: {
|
93 | type: 'boolean',
|
94 | desc: 'Enable in memory caching'
|
95 | },
|
96 | define: {
|
97 | type: 'object',
|
98 | desc: `Used to redefine or replace a variable or key in the bundle. Complex
|
99 | keys should be set in config. {dim e.g. --define.batman robin}`
|
100 | },
|
101 | hot: {
|
102 | type: 'boolean',
|
103 | desc: 'Enables Hot Module Replacement'
|
104 | },
|
105 | plugin: {
|
106 | type: 'string',
|
107 | desc: 'Load this plugin'
|
108 | },
|
109 | prefetch: {
|
110 | desc: chalk`Prefetch this request
|
111 | {dim e.g. --prefetch ./file.js}`,
|
112 | type: 'string'
|
113 | },
|
114 | profile: {
|
115 | desc: 'Profile the compilation and include information in stats',
|
116 | type: 'boolean'
|
117 | },
|
118 | provide: {
|
119 | desc: chalk`Provide these modules as free vars in all modules
|
120 | {dim e.g. --provide.jQuery jquery}`,
|
121 | type: 'object'
|
122 | },
|
123 | 'records-input-path': {
|
124 | desc: 'Path to the records file (reading)',
|
125 | type: 'string'
|
126 | },
|
127 | 'records-output-path': {
|
128 | desc: 'Path to the records file (writing)',
|
129 | type: 'string'
|
130 | },
|
131 | 'records-path': {
|
132 | desc: 'Path to the records file',
|
133 | type: 'string'
|
134 | },
|
135 | target: {
|
136 | desc: 'The targeted execution environment',
|
137 | type: 'string'
|
138 | },
|
139 | 'watch-aggregate-timeout': {
|
140 | desc: 'Timeout for gathering changes while watching',
|
141 | type: ['string', 'number']
|
142 | },
|
143 | 'watch-poll': {
|
144 | desc: 'The polling interval for watching (also enable polling)',
|
145 | type: ['string', 'number']
|
146 | },
|
147 | 'watch-stdin': {
|
148 | alias: 'stdin',
|
149 | desc: 'Exit the process when stdin is closed',
|
150 | type: 'boolean'
|
151 | }
|
152 | },
|
153 |
|
154 | name: 'Advanced'
|
155 | };
|