UNPKG

1.24 kBJavaScriptView Raw
1const loader = require('@webpack-contrib/config-loader');
2const merge = require('merge-options');
3
4const WebpackCommandError = require('./WebpackCommandError');
5
6module.exports = {
7 distill(argv, config, options) {
8 let result;
9
10 if (Array.isArray(config)) {
11 result = config.map((conf) => merge(conf, options));
12 } else {
13 result = merge(options, config);
14 }
15
16 if (argv.configName) {
17 if (Array.isArray(config)) {
18 const found = config.find((conf) => conf.name === argv.configName);
19
20 if (!found) {
21 throw new WebpackCommandError(
22 `The --config-name specified was not found`
23 );
24 }
25
26 result = found;
27 } else {
28 throw new WebpackCommandError(
29 '--config-name was used but the specified configuration is not an Array'
30 );
31 }
32 }
33
34 return result;
35 },
36
37 load(argv, options) {
38 const loaderOptions = {
39 allowZero: true,
40 configPath: argv.config,
41 require: argv.require,
42 };
43
44 return loader(loaderOptions).then((result) => {
45 const { config } = result;
46 const { distill } = module.exports;
47 const target = distill(argv, config, options);
48
49 return target;
50 });
51 },
52};