1 | const { isAbsolute, resolve } = require('path');
|
2 |
|
3 | const loader = require('@webpack-contrib/config-loader');
|
4 | const merge = require('merge-options');
|
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) => {
|
14 | const res = merge(conf, options);
|
15 | const { plugins } = conf;
|
16 |
|
17 | if (plugins && Array.isArray(plugins) && options.plugins) {
|
18 | res.plugins = plugins.concat(options.plugins);
|
19 | }
|
20 |
|
21 | return res;
|
22 | });
|
23 | } else {
|
24 | result = merge(options, config);
|
25 | const { plugins } = result;
|
26 |
|
27 | if (plugins && Array.isArray(plugins) && options.plugins) {
|
28 | result.plugins = plugins.concat(options.plugins);
|
29 | }
|
30 | }
|
31 |
|
32 | if (argv.configName) {
|
33 | if (Array.isArray(config)) {
|
34 | const found = config.find((conf) => conf.name === argv.configName);
|
35 |
|
36 | if (!found) {
|
37 | throw new WebpackCommandError(
|
38 | `The --config-name specified was not found`
|
39 | );
|
40 | }
|
41 |
|
42 | result = found;
|
43 | } else {
|
44 | throw new WebpackCommandError(
|
45 | '--config-name was used but the specified configuration is not an Array'
|
46 | );
|
47 | }
|
48 | }
|
49 |
|
50 | return result;
|
51 | },
|
52 |
|
53 | load(argv, options) {
|
54 | const loaderOptions = {
|
55 | allowMissing: true,
|
56 | configPath: argv.config,
|
57 | require: argv.require,
|
58 | };
|
59 |
|
60 | if (argv.config) {
|
61 | if (isAbsolute(argv.config)) {
|
62 | loaderOptions.configPath = argv.config;
|
63 | } else {
|
64 | loaderOptions.configPath = resolve(process.cwd(), argv.config);
|
65 | }
|
66 | }
|
67 |
|
68 | return loader(loaderOptions).then((result) => {
|
69 | const { config } = result;
|
70 | const { distill } = module.exports;
|
71 | const target = distill(argv, config, options);
|
72 |
|
73 | return target;
|
74 | });
|
75 | },
|
76 | };
|