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