This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 233
/
Copy pathConfig.js
176 lines (158 loc) · 5.11 KB
/
Config.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const ChainedMap = require('./ChainedMap');
const ChainedSet = require('./ChainedSet');
const Resolve = require('./Resolve');
const ResolveLoader = require('./ResolveLoader');
const Output = require('./Output');
const DevServer = require('./DevServer');
const Plugin = require('./Plugin');
const Module = require('./Module');
const Optimization = require('./Optimization');
const Performance = require('./Performance');
module.exports = class extends ChainedMap {
constructor() {
super();
this.devServer = new DevServer(this);
this.entryPoints = new ChainedMap(this);
this.module = new Module(this);
this.node = new ChainedMap(this);
this.optimization = new Optimization(this);
this.output = new Output(this);
this.performance = new Performance(this);
this.plugins = new ChainedMap(this);
this.resolve = new Resolve(this);
this.resolveLoader = new ResolveLoader(this);
this.extend([
'amd',
'bail',
'cache',
'context',
'devtool',
'externals',
'loader',
'mode',
'name',
'parallelism',
'profile',
'recordsInputPath',
'recordsPath',
'recordsOutputPath',
'stats',
'target',
'watch',
'watchOptions',
]);
}
static toString(config, { verbose = false, configPrefix = 'config' } = {}) {
// eslint-disable-next-line global-require
const { stringify } = require('javascript-stringify');
return stringify(
config,
(value, indent, stringify) => {
// improve plugin output
if (value && value.__pluginName) {
const prefix = `/* ${configPrefix}.${value.__pluginType}('${value.__pluginName}') */\n`;
const constructorExpression = value.__pluginPath
? // The path is stringified to ensure special characters are escaped
// (such as the backslashes in Windows-style paths).
`(require(${stringify(value.__pluginPath)}))`
: value.__pluginConstructorName;
if (constructorExpression) {
// get correct indentation for args by stringifying the args array and
// discarding the square brackets.
const args = stringify(value.__pluginArgs).slice(1, -1);
return `${prefix}new ${constructorExpression}(${args})`;
}
return (
prefix +
stringify(
value.__pluginArgs && value.__pluginArgs.length
? { args: value.__pluginArgs }
: {},
)
);
}
// improve rule/use output
if (value && value.__ruleNames) {
const ruleTypes = value.__ruleTypes;
const prefix = `/* ${configPrefix}.module${value.__ruleNames
.map(
(r, index) => `.${ruleTypes ? ruleTypes[index] : 'rule'}('${r}')`,
)
.join('')}${
value.__useName ? `.use('${value.__useName}')` : ``
} */\n`;
return prefix + stringify(value);
}
if (value && value.__expression) {
return value.__expression;
}
// shorten long functions
if (typeof value === 'function') {
if (!verbose && value.toString().length > 100) {
return `function () { /* omitted long function */ }`;
}
}
return stringify(value);
},
2,
);
}
entry(name) {
return this.entryPoints.getOrCompute(name, () => new ChainedSet(this));
}
plugin(name) {
return this.plugins.getOrCompute(name, () => new Plugin(this, name));
}
toConfig() {
const entryPoints = this.entryPoints.entries() || {};
return this.clean(
Object.assign(this.entries() || {}, {
node: this.node.entries(),
output: this.output.entries(),
resolve: this.resolve.toConfig(),
resolveLoader: this.resolveLoader.toConfig(),
devServer: this.devServer.toConfig(),
module: this.module.toConfig(),
optimization: this.optimization.toConfig(),
plugins: this.plugins.values().map((plugin) => plugin.toConfig()),
performance: this.performance.entries(),
entry: Object.keys(entryPoints).reduce(
(acc, key) =>
Object.assign(acc, { [key]: entryPoints[key].values() }),
{},
),
}),
);
}
toString(options) {
return module.exports.toString(this.toConfig(), options);
}
merge(obj = {}, omit = []) {
const omissions = [
'node',
'output',
'resolve',
'resolveLoader',
'devServer',
'optimization',
'performance',
'module',
];
if (!omit.includes('entry') && 'entry' in obj) {
Object.keys(obj.entry).forEach((name) =>
this.entry(name).merge([].concat(obj.entry[name])),
);
}
if (!omit.includes('plugin') && 'plugin' in obj) {
Object.keys(obj.plugin).forEach((name) =>
this.plugin(name).merge(obj.plugin[name]),
);
}
omissions.forEach((key) => {
if (!omit.includes(key) && key in obj) {
this[key].merge(obj[key]);
}
});
return super.merge(obj, [...omit, ...omissions, 'entry', 'plugin']);
}
};