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