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(`The --config-name specified was not found`);
|
25 | }
|
26 |
|
27 | result = found;
|
28 | } else {
|
29 | throw new WebpackCommandError(
|
30 | '--config-name was used but the specified configuration is not an Array'
|
31 | );
|
32 | }
|
33 | }
|
34 |
|
35 | return result;
|
36 | },
|
37 |
|
38 | load(argv, options) {
|
39 | const loaderOptions = {
|
40 | allowMissing: true,
|
41 | configPath: argv.config,
|
42 | require: argv.require
|
43 | };
|
44 |
|
45 | if (argv.config) {
|
46 | if (isAbsolute(argv.config)) {
|
47 | loaderOptions.configPath = argv.config;
|
48 | } else {
|
49 | loaderOptions.configPath = resolve(process.cwd(), argv.config);
|
50 | }
|
51 | }
|
52 |
|
53 | return loader(loaderOptions).then((result) => {
|
54 | const { config } = result;
|
55 | const { distill } = module.exports;
|
56 | const target = distill(argv, config, options);
|
57 |
|
58 | return target;
|
59 | });
|
60 | }
|
61 | };
|