1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
const { isAbsolute, resolve } = require('path');
const loader = require('@webpack-contrib/config-loader');
const isPlainObject = require('lodash/isPlainObject');
const TimeFixPlugin = require('time-fix-plugin');
const { MultiCompiler } = require('webpack');
// automagically wrap hot-client-invalid entry values in an array
function toArray(config) {
const result = config;
if (typeof result.entry === 'string') {
result.entry = [result.entry];
} else if (typeof result.entry === 'undefined') {
// webpack v4 defaults an empty config to { entry: './src' }. but since we
// need an array, we'll mimic that default config.
result.entry = ['./src'];
} else if (isPlainObject(result.entry)) {
for (const key of Object.keys(result.entry)) {
const entry = result.entry[key];
if (!Array.isArray(entry)) {
result.entry[key] = [entry];
}
}
}
return result;
}
// adds https://github.com/egoist/time-fix-plugin if not already added
// to the config.
function timeFix(config) {
const result = config;
if (result.plugins) {
let timeFixFound = false;
for (const plugin of result.plugins) {
if (!timeFixFound && plugin instanceof TimeFixPlugin) {
timeFixFound = true;
}
}
if (!timeFixFound) {
result.plugins.unshift(new TimeFixPlugin());
}
} else {
result.plugins = [new TimeFixPlugin()];
}
return result;
}
function prepare(config) {
return timeFix(toArray(config));
}
module.exports = {
load(options) {
const { compiler, config } = options;
const flags = options.flags || {};
const require = flags.require || options.require;
// if someone passed us a config Object, then just resolve that object
// we're only going to load the config from file when we're given a file
// to load
if (isPlainObject(config) || Array.isArray(config)) {
const result = [].concat(config).map((conf) => prepare(conf));
return Promise.resolve(result);
}
// if a user has passed us a compiler directly, then pull the config from
// the compiler and use that
if (compiler) {
let configs = [];
if (compiler instanceof MultiCompiler) {
const { compilers } = compiler;
configs = compilers.map((c) => c.options);
} else {
configs = [compiler.options];
}
return Promise.resolve(configs);
}
const loaderOptions = {
allowMissing: true,
require,
schema: {
properties: {
serve: {
additionalProperties: true,
type: 'object',
},
},
},
};
if (typeof config === 'string') {
if (isAbsolute(config)) {
loaderOptions.configPath = config;
} else {
loaderOptions.configPath = resolve(process.cwd(), config);
}
}
return loader(loaderOptions).then((result) => {
let found = result.config;
if (!found) {
// webpack v4+ defaults an empty config to { entry: './src' }. but since we
// need an array, we'll mimic that default config.
found = { entry: ['./src'] };
}
found = [].concat(found).map((conf) => prepare(conf));
return found;
});
},
};
|