webpack.js
7.79 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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const Compiler = require("./Compiler");
const MultiCompiler = require("./MultiCompiler");
const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
const WebpackOptionsApply = require("./WebpackOptionsApply");
const WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter");
const validateSchema = require("./validateSchema");
const WebpackOptionsValidationError = require("./WebpackOptionsValidationError");
const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
const RemovedPluginError = require("./RemovedPluginError");
const version = require("../package.json").version;
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
/**
* @param {WebpackOptions} options options object
* @param {function(Error=, Stats=): void=} callback callback
* @returns {Compiler | MultiCompiler} the compiler object
*/
const webpack = (options, callback) => {
const webpackOptionsValidationErrors = validateSchema(
webpackOptionsSchema,
options
);
if (webpackOptionsValidationErrors.length) {
throw new WebpackOptionsValidationError(webpackOptionsValidationErrors);
}
let compiler;
if (Array.isArray(options)) {
compiler = new MultiCompiler(
Array.from(options).map(options => webpack(options))
);
} else if (typeof options === "object") {
options = new WebpackOptionsDefaulter().process(options);
compiler = new Compiler(options.context);
compiler.options = options;
new NodeEnvironmentPlugin({
infrastructureLogging: options.infrastructureLogging
}).apply(compiler);
if (options.plugins && Array.isArray(options.plugins)) {
for (const plugin of options.plugins) {
if (typeof plugin === "function") {
plugin.call(compiler, compiler);
} else {
plugin.apply(compiler);
}
}
}
compiler.hooks.environment.call();
compiler.hooks.afterEnvironment.call();
compiler.options = new WebpackOptionsApply().process(options, compiler);
} else {
throw new Error("Invalid argument: options");
}
if (callback) {
if (typeof callback !== "function") {
throw new Error("Invalid argument: callback");
}
if (
options.watch === true ||
(Array.isArray(options) && options.some(o => o.watch))
) {
const watchOptions = Array.isArray(options)
? options.map(o => o.watchOptions || {})
: options.watchOptions || {};
return compiler.watch(watchOptions, callback);
}
compiler.run(callback);
}
return compiler;
};
exports = module.exports = webpack;
exports.version = version;
webpack.WebpackOptionsDefaulter = WebpackOptionsDefaulter;
webpack.WebpackOptionsApply = WebpackOptionsApply;
webpack.Compiler = Compiler;
webpack.MultiCompiler = MultiCompiler;
webpack.NodeEnvironmentPlugin = NodeEnvironmentPlugin;
// @ts-ignore Global @this directive is not supported
webpack.validate = validateSchema.bind(this, webpackOptionsSchema);
webpack.validateSchema = validateSchema;
webpack.WebpackOptionsValidationError = WebpackOptionsValidationError;
const exportPlugins = (obj, mappings) => {
for (const name of Object.keys(mappings)) {
Object.defineProperty(obj, name, {
configurable: false,
enumerable: true,
get: mappings[name]
});
}
};
exportPlugins(exports, {
AutomaticPrefetchPlugin: () => require("./AutomaticPrefetchPlugin"),
BannerPlugin: () => require("./BannerPlugin"),
CachePlugin: () => require("./CachePlugin"),
ContextExclusionPlugin: () => require("./ContextExclusionPlugin"),
ContextReplacementPlugin: () => require("./ContextReplacementPlugin"),
DefinePlugin: () => require("./DefinePlugin"),
Dependency: () => require("./Dependency"),
DllPlugin: () => require("./DllPlugin"),
DllReferencePlugin: () => require("./DllReferencePlugin"),
EnvironmentPlugin: () => require("./EnvironmentPlugin"),
EvalDevToolModulePlugin: () => require("./EvalDevToolModulePlugin"),
EvalSourceMapDevToolPlugin: () => require("./EvalSourceMapDevToolPlugin"),
ExtendedAPIPlugin: () => require("./ExtendedAPIPlugin"),
ExternalsPlugin: () => require("./ExternalsPlugin"),
HashedModuleIdsPlugin: () => require("./HashedModuleIdsPlugin"),
HotModuleReplacementPlugin: () => require("./HotModuleReplacementPlugin"),
IgnorePlugin: () => require("./IgnorePlugin"),
LibraryTemplatePlugin: () => require("./LibraryTemplatePlugin"),
LoaderOptionsPlugin: () => require("./LoaderOptionsPlugin"),
LoaderTargetPlugin: () => require("./LoaderTargetPlugin"),
MemoryOutputFileSystem: () => require("./MemoryOutputFileSystem"),
Module: () => require("./Module"),
ModuleFilenameHelpers: () => require("./ModuleFilenameHelpers"),
NamedChunksPlugin: () => require("./NamedChunksPlugin"),
NamedModulesPlugin: () => require("./NamedModulesPlugin"),
NoEmitOnErrorsPlugin: () => require("./NoEmitOnErrorsPlugin"),
NormalModuleReplacementPlugin: () =>
require("./NormalModuleReplacementPlugin"),
PrefetchPlugin: () => require("./PrefetchPlugin"),
ProgressPlugin: () => require("./ProgressPlugin"),
ProvidePlugin: () => require("./ProvidePlugin"),
SetVarMainTemplatePlugin: () => require("./SetVarMainTemplatePlugin"),
SingleEntryPlugin: () => require("./SingleEntryPlugin"),
SourceMapDevToolPlugin: () => require("./SourceMapDevToolPlugin"),
Stats: () => require("./Stats"),
Template: () => require("./Template"),
UmdMainTemplatePlugin: () => require("./UmdMainTemplatePlugin"),
WatchIgnorePlugin: () => require("./WatchIgnorePlugin")
});
exportPlugins((exports.dependencies = {}), {
DependencyReference: () => require("./dependencies/DependencyReference")
});
exportPlugins((exports.optimize = {}), {
AggressiveMergingPlugin: () => require("./optimize/AggressiveMergingPlugin"),
AggressiveSplittingPlugin: () =>
require("./optimize/AggressiveSplittingPlugin"),
ChunkModuleIdRangePlugin: () =>
require("./optimize/ChunkModuleIdRangePlugin"),
LimitChunkCountPlugin: () => require("./optimize/LimitChunkCountPlugin"),
MinChunkSizePlugin: () => require("./optimize/MinChunkSizePlugin"),
ModuleConcatenationPlugin: () =>
require("./optimize/ModuleConcatenationPlugin"),
OccurrenceOrderPlugin: () => require("./optimize/OccurrenceOrderPlugin"),
OccurrenceModuleOrderPlugin: () =>
require("./optimize/OccurrenceModuleOrderPlugin"),
OccurrenceChunkOrderPlugin: () =>
require("./optimize/OccurrenceChunkOrderPlugin"),
RuntimeChunkPlugin: () => require("./optimize/RuntimeChunkPlugin"),
SideEffectsFlagPlugin: () => require("./optimize/SideEffectsFlagPlugin"),
SplitChunksPlugin: () => require("./optimize/SplitChunksPlugin")
});
exportPlugins((exports.web = {}), {
FetchCompileWasmTemplatePlugin: () =>
require("./web/FetchCompileWasmTemplatePlugin"),
JsonpTemplatePlugin: () => require("./web/JsonpTemplatePlugin")
});
exportPlugins((exports.webworker = {}), {
WebWorkerTemplatePlugin: () => require("./webworker/WebWorkerTemplatePlugin")
});
exportPlugins((exports.node = {}), {
NodeTemplatePlugin: () => require("./node/NodeTemplatePlugin"),
ReadFileCompileWasmTemplatePlugin: () =>
require("./node/ReadFileCompileWasmTemplatePlugin")
});
exportPlugins((exports.debug = {}), {
ProfilingPlugin: () => require("./debug/ProfilingPlugin")
});
exportPlugins((exports.util = {}), {
createHash: () => require("./util/createHash")
});
const defineMissingPluginError = (namespace, pluginName, errorMessage) => {
Object.defineProperty(namespace, pluginName, {
configurable: false,
enumerable: true,
get() {
throw new RemovedPluginError(errorMessage);
}
});
};
// TODO remove in webpack 5
defineMissingPluginError(
exports.optimize,
"UglifyJsPlugin",
"webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead."
);
// TODO remove in webpack 5
defineMissingPluginError(
exports.optimize,
"CommonsChunkPlugin",
"webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChunks instead."
);