/
webpack-cli.js
529 lines (422 loc) · 18.1 KB
/
webpack-cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
const path = require('path');
const packageExists = require('./utils/package-exists');
const webpack = packageExists('webpack') ? require('webpack') : undefined;
const webpackMerge = require('webpack-merge');
const { createWriteStream, existsSync } = require('fs');
const { options: coloretteOptions, yellow } = require('colorette');
const { stringifyStream: createJsonStringifyStream } = require('@discoveryjs/json-ext');
const logger = require('./utils/logger');
const { cli, flags } = require('./utils/cli-flags');
const argParser = require('./utils/arg-parser');
const CLIPlugin = require('./plugins/CLIPlugin');
const promptInstallation = require('./utils/prompt-installation');
const { extensions, jsVariants } = require('interpret');
const rechoir = require('rechoir');
const toKebabCase = require('./utils/to-kebab-case');
const { resolve, extname } = path;
class WebpackCLI {
constructor() {}
async resolveConfig(args) {
const loadConfig = (configPath) => {
const ext = extname(configPath);
const interpreted = Object.keys(jsVariants).find((variant) => variant === ext);
if (interpreted) {
rechoir.prepare(extensions, configPath);
}
let options;
try {
options = require(configPath);
} catch (error) {
logger.error(`Failed to load '${configPath}'`);
logger.error(error);
process.exit(2);
}
if (options.default) {
options = options.default;
}
return { options, path: configPath };
};
const evaluateConfig = async (loadedConfig, args) => {
const isMultiCompiler = Array.isArray(loadedConfig.options);
const config = isMultiCompiler ? loadedConfig.options : [loadedConfig.options];
let evaluatedConfig = await Promise.all(
config.map(async (rawConfig) => {
if (typeof rawConfig.then === 'function') {
rawConfig = await rawConfig;
}
// `Promise` may return `Function`
if (typeof rawConfig === 'function') {
// when config is a function, pass the env from args to the config function
rawConfig = await rawConfig(args.env, args);
}
return rawConfig;
}),
);
loadedConfig.options = isMultiCompiler ? evaluatedConfig : evaluatedConfig[0];
const isObject = (value) => typeof value === 'object' && value !== null;
if (!isObject(loadedConfig.options) && !Array.isArray(loadedConfig.options)) {
logger.error(`Invalid configuration in '${loadedConfig.path}'`);
process.exit(2);
}
return loadedConfig;
};
let config = { options: {}, path: new WeakMap() };
if (args.config && args.config.length > 0) {
const evaluatedConfigs = await Promise.all(
args.config.map(async (value) => {
const configPath = resolve(value);
if (!existsSync(configPath)) {
logger.error(`The specified config file doesn't exist in '${configPath}'`);
process.exit(2);
}
const loadedConfig = loadConfig(configPath);
return evaluateConfig(loadedConfig, args);
}),
);
config.options = [];
evaluatedConfigs.forEach((evaluatedConfig) => {
if (Array.isArray(evaluatedConfig.options)) {
evaluatedConfig.options.forEach((options) => {
config.options.push(options);
config.path.set(options, evaluatedConfig.path);
});
} else {
config.options.push(evaluatedConfig.options);
config.path.set(evaluatedConfig.options, evaluatedConfig.path);
}
});
config.options = config.options.length === 1 ? config.options[0] : config.options;
} else {
// Order defines the priority, in increasing order
const defaultConfigFiles = ['webpack.config', '.webpack/webpack.config', '.webpack/webpackfile']
// .filter((value) => value.includes(args.mode))
.map((filename) =>
// Since .cjs is not available on interpret side add it manually to default config extension list
[...Object.keys(extensions), '.cjs'].map((ext) => ({
path: resolve(filename + ext),
ext: ext,
module: extensions[ext],
})),
)
.reduce((accumulator, currentValue) => accumulator.concat(currentValue), []);
let foundDefaultConfigFile;
for (const defaultConfigFile of defaultConfigFiles) {
if (existsSync(defaultConfigFile.path)) {
foundDefaultConfigFile = defaultConfigFile;
break;
}
}
if (foundDefaultConfigFile) {
const loadedConfig = loadConfig(foundDefaultConfigFile.path);
const evaluatedConfig = await evaluateConfig(loadedConfig, args);
config.options = evaluatedConfig.options;
if (Array.isArray(config.options)) {
config.options.forEach((options) => {
config.path.set(options, evaluatedConfig.path);
});
} else {
config.path.set(evaluatedConfig.options, evaluatedConfig.path);
}
}
}
if (args.configName) {
const notfoundConfigNames = [];
config.options = args.configName.map((configName) => {
let found;
if (Array.isArray(config.options)) {
found = config.options.find((options) => options.name === configName);
} else {
found = config.options.name === configName ? config.options : undefined;
}
if (!found) {
notfoundConfigNames.push(configName);
}
return found;
});
if (notfoundConfigNames.length > 0) {
logger.error(
notfoundConfigNames.map((configName) => `Configuration with the "${configName}" name was not found.`).join(' '),
);
process.exit(2);
}
}
if (args.merge) {
// we can only merge when there are multiple configurations
// either by passing multiple configs by flags or passing a
// single config exporting an array
if (!Array.isArray(config.options) || config.options.length <= 1) {
logger.error('At least two configurations are required for merge.');
process.exit(2);
}
const mergedConfigPaths = [];
config.options = config.options.reduce((accumulator, options) => {
const configPath = config.path.get(options);
const mergedOptions = webpackMerge(accumulator, options);
mergedConfigPaths.push(configPath);
return mergedOptions;
}, {});
config.path.set(config.options, mergedConfigPaths);
}
return config;
}
// TODO refactor
async resolveArguments(config, args) {
if (args.analyze) {
if (!packageExists('webpack-bundle-analyzer')) {
try {
await promptInstallation('webpack-bundle-analyzer', () => {
logger.error(`It looks like ${yellow('webpack-bundle-analyzer')} is not installed.`);
});
} catch (error) {
logger.error(`Action Interrupted, Please try once again or install ${yellow('webpack-bundle-analyzer')} manually.`);
process.exit(2);
}
logger.success(`${yellow('webpack-bundle-analyzer')} was installed successfully.`);
}
}
if (typeof args.progress === 'string' && args.progress !== 'profile') {
logger.error(`'${args.progress}' is an invalid value for the --progress option. Only 'profile' is allowed.`);
process.exit(2);
}
if (Object.keys(args).length === 0 && !process.env.NODE_ENV) {
return config;
}
if (cli) {
const processArguments = (options) => {
const coreFlagMap = flags
.filter((flag) => flag.group === 'core')
.reduce((accumulator, flag) => {
accumulator[flag.name] = flag;
return accumulator;
}, {});
const coreConfig = Object.keys(args).reduce((accumulator, name) => {
const kebabName = toKebabCase(name);
if (coreFlagMap[kebabName]) {
accumulator[kebabName] = args[name];
}
return accumulator;
}, {});
const problems = cli.processArguments(coreFlagMap, options, coreConfig);
if (problems) {
problems.forEach((problem) => {
logger.error(
`Found the '${problem.type}' problem with the '--${problem.argument}' argument${
problem.path ? ` by path '${problem.path}'` : ''
}`,
);
});
process.exit(2);
}
return options;
};
config.options = Array.isArray(config.options)
? config.options.map((options) => processArguments(options))
: processArguments(config.options);
}
const setupDefaultOptions = (options) => {
// No need to run for webpack@4
if (cli && options.cache && options.cache.type === 'filesystem') {
const configPath = config.path.get(options);
if (configPath) {
if (!options.cache.buildDependencies) {
options.cache.buildDependencies = {};
}
if (!options.cache.buildDependencies.config) {
options.cache.buildDependencies.config = [];
}
if (Array.isArray(configPath)) {
configPath.forEach((item) => {
options.cache.buildDependencies.config.push(item);
});
} else {
options.cache.buildDependencies.config.push(configPath);
}
}
}
return options;
};
config.options = Array.isArray(config.options)
? config.options.map((options) => setupDefaultOptions(options))
: setupDefaultOptions(config.options);
// Logic for webpack@4
// TODO remove after drop webpack@4
const processLegacyArguments = (options) => {
if (args.entry) {
options.entry = args.entry;
}
if (args.outputPath) {
options.output = { ...options.output, ...{ path: path.resolve(args.outputPath) } };
}
if (args.target) {
options.target = args.target;
}
if (typeof args.devtool !== 'undefined') {
options.devtool = args.devtool;
}
if (args.mode) {
options.mode = args.mode;
} else if (
!options.mode &&
process.env &&
process.env.NODE_ENV &&
(process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'node')
) {
options.mode = process.env.NODE_ENV;
}
if (args.name) {
options.name = args.name;
}
if (typeof args.stats !== 'undefined') {
options.stats = args.stats;
}
if (typeof args.watch !== 'undefined') {
options.watch = args.watch;
}
return options;
};
config.options = Array.isArray(config.options)
? config.options.map((options) => processLegacyArguments(options))
: processLegacyArguments(config.options);
return config;
}
async resolveCLIPlugin(config, args) {
const addCLIPlugin = (options) => {
if (!options.plugins) {
options.plugins = [];
}
options.plugins.unshift(
new CLIPlugin({
configPath: config.path,
helpfulOutput: !args.json,
hot: args.hot,
progress: args.progress,
prefetch: args.prefetch,
analyze: args.analyze,
}),
);
return options;
};
config.options = Array.isArray(config.options)
? config.options.map((options) => addCLIPlugin(options))
: addCLIPlugin(config.options);
return config;
}
async resolve(parsedArgs) {
let config = await this.resolveConfig(parsedArgs);
config = await this.resolveArguments(config, parsedArgs);
config = await this.resolveCLIPlugin(config, parsedArgs);
return config;
}
/**
* Expose commander argParser
* @param {...any} args args for argParser
*/
argParser(...args) {
return argParser(...args);
}
getCoreFlags() {
return flags;
}
handleError(error) {
// https://github.com/webpack/webpack/blob/master/lib/index.js#L267
// https://github.com/webpack/webpack/blob/v4.44.2/lib/webpack.js#L90
const ValidationError = webpack.ValidationError || webpack.WebpackOptionsValidationError;
// In case of schema errors print and exit process
// For webpack@4 and webpack@5
if (error instanceof ValidationError) {
logger.error(error.message);
} else {
logger.error(error);
}
}
createCompiler(options, callback) {
let compiler;
try {
compiler = webpack(options, callback);
} catch (error) {
this.handleError(error);
process.exit(2);
}
return compiler;
}
async getCompiler(args) {
// TODO test with serve
const config = await this.resolve(args);
return this.createCompiler(config.options);
}
async run(args) {
let compiler;
const callback = (error, stats) => {
if (error) {
this.handleError(error);
process.exit(2);
}
if (stats.hasErrors()) {
process.exitCode = 1;
}
const getStatsOptions = (stats) => {
// TODO remove after drop webpack@4
if (webpack.Stats && webpack.Stats.presetToOptions) {
if (!stats) {
stats = {};
} else if (typeof stats === 'boolean' || typeof stats === 'string') {
stats = webpack.Stats.presetToOptions(stats);
}
}
let colors;
// From flags
if (typeof args.color !== 'undefined') {
colors = args.color;
}
// From stats
else if (typeof stats.colors !== 'undefined') {
colors = stats.colors;
}
// Default
else {
colors = coloretteOptions.enabled;
}
stats.colors = colors;
return stats;
};
const getStatsOptionsFromCompiler = (compiler) => getStatsOptions(compiler.options ? compiler.options.stats : undefined);
if (!compiler) {
return;
}
const foundStats = compiler.compilers
? { children: compiler.compilers.map(getStatsOptionsFromCompiler) }
: getStatsOptionsFromCompiler(compiler);
const handleWriteError = (error) => {
logger.error(error);
process.exit(2);
};
if (args.json === true) {
createJsonStringifyStream(stats.toJson(foundStats))
.on('error', handleWriteError)
.pipe(process.stdout)
.on('error', handleWriteError)
.on('close', () => process.stdout.write('\n'));
} else if (typeof args.json === 'string') {
createJsonStringifyStream(stats.toJson(foundStats))
.on('error', handleWriteError)
.pipe(createWriteStream(args.json))
.on('error', handleWriteError)
.on('close', () => logger.success(`stats are successfully stored as json to ${args.json}`));
} else {
const printedStats = stats.toString(foundStats);
// Avoid extra empty line when `stats: 'none'`
if (printedStats) {
logger.raw(`${stats.toString(foundStats)}`);
}
}
};
const config = await this.resolve(args);
compiler = this.createCompiler(config.options, callback);
// TODO webpack@4 return Watching and MultiWathing instead Compiler and MultiCompiler, remove this after drop webpack@4
if (compiler && compiler.compiler) {
compiler = compiler.compiler;
}
return Promise.resolve();
}
}
module.exports = WebpackCLI;