Repository navigation
Expand file tree
/
Copy pathwebpack.js
More file actions
345 lines (324 loc) · 10.2 KB
/
Copy pathwebpack.js
File metadata and controls
345 lines (324 loc) · 10.2 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const util = require("util");
const webpackOptionsSchemaCheck = require("../schemas/WebpackOptions.check");
const webpackOptionsSchema =
/** @type {EXPECTED_ANY} */
(require("../schemas/WebpackOptions.json"));
const Compiler = require("./Compiler");
const WebpackOptionsApply = require("./WebpackOptionsApply");
const {
applyWebpackOptionsBaseDefaults,
applyWebpackOptionsDefaults
} = require("./config/defaults");
const {
applyWebpackOptionsInterception,
getNormalizedWebpackOptions
} = require("./config/normalization");
const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
const memoize = require("./util/memoize");
const getValidateSchema = memoize(() => require("./validateSchema"));
const getMultiCompiler = memoize(() => require("./MultiCompiler"));
const getProgressPlugin = memoize(() => require("./ProgressPlugin"));
/**
* @import {
* WebpackOptions,
* WatchOptions
* } from "../declarations/WebpackOptions"
*/
/** @import { WebpackOptionsNormalizedWithDefaults } from "./config/defaults" */
/** @import { WebpackOptionsInterception } from "./config/normalization" */
/**
* @import MultiCompiler, {
* MultiCompilerOptions,
* MultiWebpackOptions
* } from "./MultiCompiler"
*/
/** @import MultiStats from "./MultiStats" */
/** @import Stats from "./Stats" */
/** @typedef {(this: Compiler, compiler: Compiler) => void} WebpackPluginFunction */
/** @typedef {(compiler: Compiler) => void} WebpackPluginInstanceApplyFunction */
// only an array config reaches the multi compiler
/**
* Whether core should auto-apply progress, from `infrastructureLogging.progress`.
* Like other bundlers, `"auto"` shows it in a TTY and stays silent in CI; `true`
* forces it on (unless logging is off), `false` disables it.
* @param {WebpackOptionsNormalizedWithDefaults} options resolved options
* @returns {boolean} true when progress should be applied by default
*/
const isDefaultProgressEnabled = (options) => {
const infrastructureLogging = options.infrastructureLogging;
if (!infrastructureLogging) return false;
const progress = infrastructureLogging.progress;
if (!progress) return false;
if (
infrastructureLogging.level === "none" ||
/** @type {EXPECTED_ANY} */ (infrastructureLogging.level) === false
) {
return false;
}
return progress === "auto" ? !infrastructureLogging.appendOnly : true;
};
/**
* @param {WebpackOptionsNormalizedWithDefaults} options resolved options
* @returns {boolean} true when the user already added a ProgressPlugin
*/
const hasUserProgressPlugin = (options) =>
Array.isArray(options.plugins) &&
options.plugins.some((p) => p instanceof getProgressPlugin());
/**
* Auto-applies a default `ProgressPlugin` driven by `infrastructureLogging.progress`,
* unless the user already added one. `hasUserPlugin` is lazy so the plugin scan
* only runs when progress is actually enabled.
* @param {Compiler | MultiCompiler} compiler compiler to apply to
* @param {WebpackOptionsNormalizedWithDefaults} options options deciding whether progress is on
* @param {() => boolean} hasUserPlugin whether a ProgressPlugin is already present
* @returns {void}
*/
const applyDefaultProgressPlugin = (compiler, options, hasUserPlugin) => {
if (!isDefaultProgressEnabled(options) || hasUserPlugin()) return;
const ProgressPlugin = getProgressPlugin();
new ProgressPlugin({ progressBar: "auto" }).apply(compiler);
};
/**
* Defines the callback callback.
* @template T
* @template [R=void]
* @callback Callback
* @param {Error | null} err
* @param {T=} result
* @returns {R}
*/
/** @typedef {Callback<void>} ErrorCallback */
/**
* Creates a multi compiler.
* @param {ReadonlyArray<WebpackOptions>} childOptions options array
* @param {MultiCompilerOptions} options options
* @returns {MultiCompiler} a multi-compiler
*/
const createMultiCompiler = (childOptions, options) => {
const MultiCompiler = getMultiCompiler();
const compilers = childOptions.map((options, index) =>
createCompiler(options, index)
);
const compiler = new MultiCompiler(compilers, options);
for (const childCompiler of compilers) {
if (childCompiler.options.dependencies) {
compiler.setDependencies(
childCompiler,
childCompiler.options.dependencies
);
}
}
const firstOptions =
compilers.length > 0 &&
/** @type {WebpackOptionsNormalizedWithDefaults} */ (compilers[0].options);
if (firstOptions) {
applyDefaultProgressPlugin(compiler, firstOptions, () =>
compilers.some((c) =>
hasUserProgressPlugin(
/** @type {WebpackOptionsNormalizedWithDefaults} */ (c.options)
)
)
);
}
return compiler;
};
/**
* Creates a compiler.
* @param {WebpackOptions} rawOptions options object
* @param {number=} compilerIndex index of compiler
* @returns {Compiler} a compiler
*/
const createCompiler = (rawOptions, compilerIndex) => {
let options = getNormalizedWebpackOptions(rawOptions);
applyWebpackOptionsBaseDefaults(options);
/** @type {WebpackOptionsInterception=} */
let interception;
({ options, interception } = applyWebpackOptionsInterception(options));
const compiler = new Compiler(
/** @type {string} */ (options.context),
options
);
new NodeEnvironmentPlugin({
infrastructureLogging: options.infrastructureLogging
}).apply(compiler);
if (Array.isArray(options.plugins)) {
for (const plugin of options.plugins) {
if (typeof plugin === "function") {
/** @type {WebpackPluginFunction} */
(plugin).call(compiler, compiler);
} else if (plugin) {
plugin.apply(compiler);
}
}
}
const resolvedDefaultOptions = applyWebpackOptionsDefaults(
options,
compilerIndex
);
if (resolvedDefaultOptions.platform) {
compiler.platform = resolvedDefaultOptions.platform;
}
// Child compilers skip this; the MultiCompiler applies one aggregated reporter.
if (compilerIndex === undefined) {
const resolvedOptions =
/** @type {WebpackOptionsNormalizedWithDefaults} */ (options);
applyDefaultProgressPlugin(compiler, resolvedOptions, () =>
hasUserProgressPlugin(resolvedOptions)
);
}
if (options.validate) {
compiler.hooks.validate.call();
}
compiler.hooks.environment.call();
compiler.hooks.afterEnvironment.call();
new WebpackOptionsApply().process(
/** @type {WebpackOptionsNormalizedWithDefaults} */
(options),
compiler,
interception
);
compiler.hooks.initialize.call();
return compiler;
};
/**
* Returns array of options.
* @template T
* @param {T[] | T} options options
* @returns {T[]} array of options
*/
const asArray = (options) =>
Array.isArray(options) ? [...options] : [options];
/**
* Checks whether it needs validate.
* @param {WebpackOptions | null | undefined} options options
* @returns {boolean} true when need to validate, otherwise false
*/
const needValidate = (options) => {
if (
options &&
(options.validate === false ||
(options.experiments &&
options.experiments.futureDefaults === true &&
(options.mode === "production" || !options.mode)))
) {
return false;
}
return true;
};
/**
* Returns the compiler object.
* @overload
* @param {WebpackOptions} options options object
* @param {Callback<Stats>} callback callback
* @returns {Compiler | null} the compiler object
*/
/**
* Returns the compiler object.
* @overload
* @param {WebpackOptions} options options object
* @returns {Compiler} the compiler object
*/
/**
* Returns the multi compiler object.
* @overload
* @param {MultiWebpackOptions} options options objects
* @param {Callback<MultiStats>} callback callback
* @returns {MultiCompiler | null} the multi compiler object
*/
/**
* Returns the multi compiler object.
* @overload
* @param {MultiWebpackOptions} options options objects
* @returns {MultiCompiler} the multi compiler object
*/
/**
* Returns compiler or MultiCompiler.
* @param {WebpackOptions | MultiWebpackOptions} options options
* @param {Callback<Stats> & Callback<MultiStats>=} callback callback
* @returns {Compiler | MultiCompiler | null} Compiler or MultiCompiler
*/
const webpack = (options, callback) => {
const create = () => {
const isMultiCompiler = Array.isArray(options);
if (
!asArray(/** @type {WebpackOptions} */ (options)).every((options) =>
needValidate(options) ? webpackOptionsSchemaCheck(options) : true
)
) {
getValidateSchema()(
webpackOptionsSchema,
isMultiCompiler
? options.map((options) => (needValidate(options) ? options : {}))
: needValidate(options)
? options
: {}
);
util.deprecate(
() => {},
"webpack bug: Pre-compiled schema reports error while real schema is happy. This has performance drawbacks.",
"DEP_WEBPACK_PRE_COMPILED_SCHEMA_INVALID"
)();
}
/** @type {MultiCompiler | Compiler} */
let compiler;
/** @type {boolean | undefined} */
let watch = false;
/** @type {WatchOptions | WatchOptions[]} */
let watchOptions;
if (isMultiCompiler) {
/** @type {MultiCompiler} */
compiler = createMultiCompiler(
options,
/** @type {MultiCompilerOptions} */
(options)
);
watch = options.some((options) => options.watch);
watchOptions = options.map((options) => options.watchOptions || {});
} else {
const webpackOptions = /** @type {WebpackOptions} */ (options);
/** @type {Compiler} */
compiler = createCompiler(webpackOptions);
watch = webpackOptions.watch;
watchOptions = webpackOptions.watchOptions || {};
}
return { compiler, watch, watchOptions };
};
if (callback) {
try {
const { compiler, watch, watchOptions } = create();
if (watch) {
compiler.watch(watchOptions, callback);
} else {
compiler.run((err, stats) => {
compiler.close((err2) => {
callback(
err || err2,
/** @type {options extends WebpackOptions ? Stats : MultiStats} */
(stats)
);
});
});
}
return compiler;
} catch (err) {
process.nextTick(() => callback(/** @type {Error} */ (err)));
return null;
}
} else {
const { compiler, watch } = create();
if (watch) {
util.deprecate(
() => {},
"A 'callback' argument needs to be provided to the 'webpack(options, callback)' function when the 'watch' option is set. There is no way to handle the 'watch' option without a callback.",
"DEP_WEBPACK_WATCH_WITHOUT_CALLBACK"
)();
}
return compiler;
}
};
module.exports = webpack;