forked from webpack/webpack-cli
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack-cli.js
297 lines (278 loc) · 10.9 KB
/
webpack-cli.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
const GroupHelper = require('./utils/GroupHelper');
const { Compiler } = require('./utils/Compiler');
const { groups, core } = require('./utils/cli-flags');
const webpackMerge = require('webpack-merge');
const { toKebabCase } = require('./utils/helpers');
const argParser = require('./utils/arg-parser');
class WebpackCLI extends GroupHelper {
constructor() {
super();
this.groupMap = new Map();
this.groups = [];
this.args = {};
this.processingMessageBuffer = [];
this.compilation = new Compiler();
this.defaultEntry = 'index';
this.possibleFileNames = [
`./${this.defaultEntry}`,
`./${this.defaultEntry}.js`,
`${this.defaultEntry}.js`,
this.defaultEntry,
`./src/${this.defaultEntry}`,
`./src/${this.defaultEntry}.js`,
`src/${this.defaultEntry}.js`,
];
this.compilerConfiguration = {};
this.outputConfiguration = {};
}
setMappedGroups(args, inlineOptions) {
Object.keys(args).forEach((key) => {
this.setGroupMap(toKebabCase(key), args[key], inlineOptions);
});
}
setGroupMap(key, val, inlineOptions) {
if (val === undefined) return;
const opt = inlineOptions.find((opt) => opt.name === key);
const groupName = opt.group;
if (this.groupMap.has(groupName)) {
const pushToMap = this.groupMap.get(groupName);
pushToMap.push({ [opt.name]: val });
} else {
this.groupMap.set(groupName, [{ [opt.name]: val }]);
}
}
/**
* Responsible for handling flags coming from webpack/webpack
* @private\
* @returns {void}
*/
_handleCoreFlags() {
if (!this.groupMap.has('core')) {
return;
}
const coreFlags = this.groupMap.get('core');
// convert all the flags from map to single object
const coreConfig = coreFlags.reduce((allFlag, curFlag) => ({ ...allFlag, ...curFlag }), {});
const coreCliHelper = require('webpack').cli;
const coreCliArgs = coreCliHelper.getArguments();
// Merge the core flag config with the compilerConfiguration
coreCliHelper.processArguments(coreCliArgs, this.compilerConfiguration, coreConfig);
}
/**
* Expose commander argParser
* @param {...any} args args for argParser
*/
argParser(...args) {
return argParser(...args);
}
getCoreFlags() {
return core;
}
/**
* Based on the parsed keys, the function will import and create
* a group that handles respective values
*
* @returns {void}
*/
resolveGroups() {
let mode;
// determine the passed mode for ConfigGroup
if (this.groupMap.has(groups.ZERO_CONFIG_GROUP)) {
const modePresent = this.groupMap.get(groups.ZERO_CONFIG_GROUP).find((t) => !!t.mode);
if (modePresent) mode = modePresent.mode;
}
for (const [key, value] of this.groupMap.entries()) {
switch (key) {
case groups.ZERO_CONFIG_GROUP: {
const ZeroConfigGroup = require('./groups/ZeroConfigGroup');
this.zeroConfigGroup = new ZeroConfigGroup(value);
break;
}
case groups.BASIC_GROUP: {
const BasicGroup = require('./groups/BasicGroup');
this.basicGroup = new BasicGroup(value);
break;
}
case groups.ADVANCED_GROUP: {
const AdvancedGroup = require('./groups/AdvancedGroup');
this.advancedGroup = new AdvancedGroup(value);
break;
}
case groups.CONFIG_GROUP: {
const ConfigGroup = require('./groups/ConfigGroup');
this.configGroup = new ConfigGroup([...value, { mode }]);
break;
}
case groups.DISPLAY_GROUP: {
const StatsGroup = require('./groups/StatsGroup');
this.statsGroup = new StatsGroup(value);
break;
}
case groups.HELP_GROUP: {
const HelpGroup = require('./groups/HelpGroup');
this.helpGroup = new HelpGroup();
break;
}
case groups.OUTPUT_GROUP: {
const OutputGroup = require('./groups/OutputGroup');
this.outputGroup = new OutputGroup(value);
this.groups.push(this.outputGroup);
break;
}
}
}
}
/**
* Responsible to override webpack options.
* @param {Object} options The options returned by a group helper
* @param {Object} strategy The strategy to pass to webpack-merge. The strategy
* is implemented inside the group helper
* @private
* @returns {void}
*/
_mergeOptionsToConfiguration(options, strategy) {
/**
* options is an array (multiple configuration) so we create a new
* configuration where each element is individually merged
*/
if (Array.isArray(options)) {
this.compilerConfiguration = options.map((configuration) => {
if (strategy) {
return webpackMerge.strategy(strategy)(this.compilerConfiguration, configuration);
}
return webpackMerge(this.compilerConfiguration, configuration);
});
} else {
/**
* The compiler configuration is already an array, so for each element
* we merge the options
*/
if (Array.isArray(this.compilerConfiguration)) {
this.compilerConfiguration = this.compilerConfiguration.map((thisConfiguration) => {
if (strategy) {
return webpackMerge.strategy(strategy)(thisConfiguration, options);
}
return webpackMerge(thisConfiguration, options);
});
} else {
if (strategy) {
this.compilerConfiguration = webpackMerge.strategy(strategy)(this.compilerConfiguration, options);
} else {
this.compilerConfiguration = webpackMerge(this.compilerConfiguration, options);
}
}
}
}
/**
* Responsible for creating and updating the new output configuration
*
* @param {Object} options Output options emitted by the group helper
* @private
* @returns {void}
*/
_mergeOptionsToOutputConfiguration(options) {
if (options) {
this.outputConfiguration = Object.assign(this.outputConfiguration, options);
}
}
/**
* Responsible for updating the buffer
*
* @param {string[]} messageBuffer The current buffer message
* @private
* @returns {void}
*/
_mergeProcessingMessageBuffer(messageBuffer) {
if (messageBuffer) {
this.processingMessageBuffer = this.processingMessageBuffer.concat(...messageBuffer);
}
}
/**
* It receives a group helper, it runs and it merges its result inside
* the file result that will be passed to the compiler
*
* @param {GroupHelper?} groupHelper A group helper
* @private
* @returns {void}
*/
async _handleGroupHelper(groupHelper) {
if (groupHelper) {
const result = await groupHelper.run();
this._mergeOptionsToConfiguration(result.options, groupHelper.strategy);
this._mergeOptionsToOutputConfiguration(result.outputOptions);
this._mergeProcessingMessageBuffer(result.processingMessageBuffer);
}
}
/**
* Get the defaultEntry for merge with config rightly
* @private
* @returns {void}
*/
_handleDefaultEntry() {
if (!this.basicGroup) {
const BasicGroup = require('./groups/BasicGroup');
this.basicGroup = new BasicGroup();
}
const defaultEntry = this.basicGroup.resolveFilePath(null, 'index.js');
const options = { entry: defaultEntry };
this._mergeOptionsToConfiguration(options);
}
/**
* It runs in a fancy order all the expected groups.
* Zero config and configuration goes first.
*
* The next groups will override existing parameters
* @returns {Promise<void>} A Promise
*/
async runOptionGroups() {
await Promise.resolve()
.then(() => this._handleGroupHelper(this.zeroConfigGroup))
.then(() => this._handleDefaultEntry())
.then(() => this._handleGroupHelper(this.configGroup))
.then(() => this._handleGroupHelper(this.outputGroup))
.then(() => this._handleCoreFlags())
.then(() => this._handleGroupHelper(this.basicGroup))
.then(() => this._handleGroupHelper(this.advancedGroup))
.then(() => this._handleGroupHelper(this.statsGroup))
.then(() => this._handleGroupHelper(this.helpGroup));
}
async processArgs(args, cliOptions) {
this.setMappedGroups(args, cliOptions);
this.resolveGroups();
const groupResult = await this.runOptionGroups();
return groupResult;
}
async getCompiler(args, cliOptions) {
await this.processArgs(args, cliOptions);
await this.compilation.createCompiler(this.compilerConfiguration);
return this.compilation.compiler;
}
async run(args, cliOptions) {
await this.processArgs(args, cliOptions);
await this.compilation.createCompiler(this.compilerConfiguration);
const webpack = await this.compilation.webpackInstance({
options: this.compilerConfiguration,
outputOptions: this.outputConfiguration,
processingMessageBuffer: this.processingMessageBuffer,
});
return webpack;
}
runHelp(args) {
const HelpGroup = require('./groups/HelpGroup');
const { commands, allNames, hasUnknownArgs } = require('./utils/unknown-args');
const subject = allNames.filter((name) => {
return args.includes(name);
})[0];
const invalidArgs = hasUnknownArgs(args.slice(2), ...allNames);
const isCommand = commands.includes(subject);
return new HelpGroup().outputHelp(isCommand, subject, invalidArgs);
}
runVersion(args, externalPkg) {
const HelpGroup = require('./groups/HelpGroup');
const { commands, allNames, hasUnknownArgs } = require('./utils/unknown-args');
const commandsUsed = args.filter((val) => commands.includes(val));
const invalidArgs = hasUnknownArgs(args.slice(2), ...allNames);
return new HelpGroup().outputVersion(externalPkg, commandsUsed, invalidArgs);
}
}
module.exports = WebpackCLI;