1 |
3a515b92
|
cagy
|
/*
|
2 |
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3 |
|
|
Author Tobias Koppers @sokra
|
4 |
|
|
*/
|
5 |
|
|
"use strict";
|
6 |
|
|
|
7 |
|
|
const Compiler = require("./Compiler");
|
8 |
|
|
const MultiCompiler = require("./MultiCompiler");
|
9 |
|
|
const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
|
10 |
|
|
const WebpackOptionsApply = require("./WebpackOptionsApply");
|
11 |
|
|
const WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter");
|
12 |
|
|
const validateSchema = require("./validateSchema");
|
13 |
|
|
const WebpackOptionsValidationError = require("./WebpackOptionsValidationError");
|
14 |
|
|
const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
|
15 |
|
|
const RemovedPluginError = require("./RemovedPluginError");
|
16 |
|
|
const version = require("../package.json").version;
|
17 |
|
|
|
18 |
|
|
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
|
19 |
|
|
|
20 |
|
|
/**
|
21 |
|
|
* @param {WebpackOptions} options options object
|
22 |
|
|
* @param {function(Error=, Stats=): void=} callback callback
|
23 |
|
|
* @returns {Compiler | MultiCompiler} the compiler object
|
24 |
|
|
*/
|
25 |
|
|
const webpack = (options, callback) => {
|
26 |
|
|
const webpackOptionsValidationErrors = validateSchema(
|
27 |
|
|
webpackOptionsSchema,
|
28 |
|
|
options
|
29 |
|
|
);
|
30 |
|
|
if (webpackOptionsValidationErrors.length) {
|
31 |
|
|
throw new WebpackOptionsValidationError(webpackOptionsValidationErrors);
|
32 |
|
|
}
|
33 |
|
|
let compiler;
|
34 |
|
|
if (Array.isArray(options)) {
|
35 |
|
|
compiler = new MultiCompiler(
|
36 |
|
|
Array.from(options).map(options => webpack(options))
|
37 |
|
|
);
|
38 |
|
|
} else if (typeof options === "object") {
|
39 |
|
|
options = new WebpackOptionsDefaulter().process(options);
|
40 |
|
|
|
41 |
|
|
compiler = new Compiler(options.context);
|
42 |
|
|
compiler.options = options;
|
43 |
|
|
new NodeEnvironmentPlugin({
|
44 |
|
|
infrastructureLogging: options.infrastructureLogging
|
45 |
|
|
}).apply(compiler);
|
46 |
|
|
if (options.plugins && Array.isArray(options.plugins)) {
|
47 |
|
|
for (const plugin of options.plugins) {
|
48 |
|
|
if (typeof plugin === "function") {
|
49 |
|
|
plugin.call(compiler, compiler);
|
50 |
|
|
} else {
|
51 |
|
|
plugin.apply(compiler);
|
52 |
|
|
}
|
53 |
|
|
}
|
54 |
|
|
}
|
55 |
|
|
compiler.hooks.environment.call();
|
56 |
|
|
compiler.hooks.afterEnvironment.call();
|
57 |
|
|
compiler.options = new WebpackOptionsApply().process(options, compiler);
|
58 |
|
|
} else {
|
59 |
|
|
throw new Error("Invalid argument: options");
|
60 |
|
|
}
|
61 |
|
|
if (callback) {
|
62 |
|
|
if (typeof callback !== "function") {
|
63 |
|
|
throw new Error("Invalid argument: callback");
|
64 |
|
|
}
|
65 |
|
|
if (
|
66 |
|
|
options.watch === true ||
|
67 |
|
|
(Array.isArray(options) && options.some(o => o.watch))
|
68 |
|
|
) {
|
69 |
|
|
const watchOptions = Array.isArray(options)
|
70 |
|
|
? options.map(o => o.watchOptions || {})
|
71 |
|
|
: options.watchOptions || {};
|
72 |
|
|
return compiler.watch(watchOptions, callback);
|
73 |
|
|
}
|
74 |
|
|
compiler.run(callback);
|
75 |
|
|
}
|
76 |
|
|
return compiler;
|
77 |
|
|
};
|
78 |
|
|
|
79 |
|
|
exports = module.exports = webpack;
|
80 |
|
|
exports.version = version;
|
81 |
|
|
|
82 |
|
|
webpack.WebpackOptionsDefaulter = WebpackOptionsDefaulter;
|
83 |
|
|
webpack.WebpackOptionsApply = WebpackOptionsApply;
|
84 |
|
|
webpack.Compiler = Compiler;
|
85 |
|
|
webpack.MultiCompiler = MultiCompiler;
|
86 |
|
|
webpack.NodeEnvironmentPlugin = NodeEnvironmentPlugin;
|
87 |
|
|
// @ts-ignore Global @this directive is not supported
|
88 |
|
|
webpack.validate = validateSchema.bind(this, webpackOptionsSchema);
|
89 |
|
|
webpack.validateSchema = validateSchema;
|
90 |
|
|
webpack.WebpackOptionsValidationError = WebpackOptionsValidationError;
|
91 |
|
|
|
92 |
|
|
const exportPlugins = (obj, mappings) => {
|
93 |
|
|
for (const name of Object.keys(mappings)) {
|
94 |
|
|
Object.defineProperty(obj, name, {
|
95 |
|
|
configurable: false,
|
96 |
|
|
enumerable: true,
|
97 |
|
|
get: mappings[name]
|
98 |
|
|
});
|
99 |
|
|
}
|
100 |
|
|
};
|
101 |
|
|
|
102 |
|
|
exportPlugins(exports, {
|
103 |
|
|
AutomaticPrefetchPlugin: () => require("./AutomaticPrefetchPlugin"),
|
104 |
|
|
BannerPlugin: () => require("./BannerPlugin"),
|
105 |
|
|
CachePlugin: () => require("./CachePlugin"),
|
106 |
|
|
ContextExclusionPlugin: () => require("./ContextExclusionPlugin"),
|
107 |
|
|
ContextReplacementPlugin: () => require("./ContextReplacementPlugin"),
|
108 |
|
|
DefinePlugin: () => require("./DefinePlugin"),
|
109 |
|
|
Dependency: () => require("./Dependency"),
|
110 |
|
|
DllPlugin: () => require("./DllPlugin"),
|
111 |
|
|
DllReferencePlugin: () => require("./DllReferencePlugin"),
|
112 |
|
|
EnvironmentPlugin: () => require("./EnvironmentPlugin"),
|
113 |
|
|
EvalDevToolModulePlugin: () => require("./EvalDevToolModulePlugin"),
|
114 |
|
|
EvalSourceMapDevToolPlugin: () => require("./EvalSourceMapDevToolPlugin"),
|
115 |
|
|
ExtendedAPIPlugin: () => require("./ExtendedAPIPlugin"),
|
116 |
|
|
ExternalsPlugin: () => require("./ExternalsPlugin"),
|
117 |
|
|
HashedModuleIdsPlugin: () => require("./HashedModuleIdsPlugin"),
|
118 |
|
|
HotModuleReplacementPlugin: () => require("./HotModuleReplacementPlugin"),
|
119 |
|
|
IgnorePlugin: () => require("./IgnorePlugin"),
|
120 |
|
|
LibraryTemplatePlugin: () => require("./LibraryTemplatePlugin"),
|
121 |
|
|
LoaderOptionsPlugin: () => require("./LoaderOptionsPlugin"),
|
122 |
|
|
LoaderTargetPlugin: () => require("./LoaderTargetPlugin"),
|
123 |
|
|
MemoryOutputFileSystem: () => require("./MemoryOutputFileSystem"),
|
124 |
|
|
Module: () => require("./Module"),
|
125 |
|
|
ModuleFilenameHelpers: () => require("./ModuleFilenameHelpers"),
|
126 |
|
|
NamedChunksPlugin: () => require("./NamedChunksPlugin"),
|
127 |
|
|
NamedModulesPlugin: () => require("./NamedModulesPlugin"),
|
128 |
|
|
NoEmitOnErrorsPlugin: () => require("./NoEmitOnErrorsPlugin"),
|
129 |
|
|
NormalModuleReplacementPlugin: () =>
|
130 |
|
|
require("./NormalModuleReplacementPlugin"),
|
131 |
|
|
PrefetchPlugin: () => require("./PrefetchPlugin"),
|
132 |
|
|
ProgressPlugin: () => require("./ProgressPlugin"),
|
133 |
|
|
ProvidePlugin: () => require("./ProvidePlugin"),
|
134 |
|
|
SetVarMainTemplatePlugin: () => require("./SetVarMainTemplatePlugin"),
|
135 |
|
|
SingleEntryPlugin: () => require("./SingleEntryPlugin"),
|
136 |
|
|
SourceMapDevToolPlugin: () => require("./SourceMapDevToolPlugin"),
|
137 |
|
|
Stats: () => require("./Stats"),
|
138 |
|
|
Template: () => require("./Template"),
|
139 |
|
|
UmdMainTemplatePlugin: () => require("./UmdMainTemplatePlugin"),
|
140 |
|
|
WatchIgnorePlugin: () => require("./WatchIgnorePlugin")
|
141 |
|
|
});
|
142 |
|
|
exportPlugins((exports.dependencies = {}), {
|
143 |
|
|
DependencyReference: () => require("./dependencies/DependencyReference")
|
144 |
|
|
});
|
145 |
|
|
exportPlugins((exports.optimize = {}), {
|
146 |
|
|
AggressiveMergingPlugin: () => require("./optimize/AggressiveMergingPlugin"),
|
147 |
|
|
AggressiveSplittingPlugin: () =>
|
148 |
|
|
require("./optimize/AggressiveSplittingPlugin"),
|
149 |
|
|
ChunkModuleIdRangePlugin: () =>
|
150 |
|
|
require("./optimize/ChunkModuleIdRangePlugin"),
|
151 |
|
|
LimitChunkCountPlugin: () => require("./optimize/LimitChunkCountPlugin"),
|
152 |
|
|
MinChunkSizePlugin: () => require("./optimize/MinChunkSizePlugin"),
|
153 |
|
|
ModuleConcatenationPlugin: () =>
|
154 |
|
|
require("./optimize/ModuleConcatenationPlugin"),
|
155 |
|
|
OccurrenceOrderPlugin: () => require("./optimize/OccurrenceOrderPlugin"),
|
156 |
|
|
OccurrenceModuleOrderPlugin: () =>
|
157 |
|
|
require("./optimize/OccurrenceModuleOrderPlugin"),
|
158 |
|
|
OccurrenceChunkOrderPlugin: () =>
|
159 |
|
|
require("./optimize/OccurrenceChunkOrderPlugin"),
|
160 |
|
|
RuntimeChunkPlugin: () => require("./optimize/RuntimeChunkPlugin"),
|
161 |
|
|
SideEffectsFlagPlugin: () => require("./optimize/SideEffectsFlagPlugin"),
|
162 |
|
|
SplitChunksPlugin: () => require("./optimize/SplitChunksPlugin")
|
163 |
|
|
});
|
164 |
|
|
exportPlugins((exports.web = {}), {
|
165 |
|
|
FetchCompileWasmTemplatePlugin: () =>
|
166 |
|
|
require("./web/FetchCompileWasmTemplatePlugin"),
|
167 |
|
|
JsonpTemplatePlugin: () => require("./web/JsonpTemplatePlugin")
|
168 |
|
|
});
|
169 |
|
|
exportPlugins((exports.webworker = {}), {
|
170 |
|
|
WebWorkerTemplatePlugin: () => require("./webworker/WebWorkerTemplatePlugin")
|
171 |
|
|
});
|
172 |
|
|
exportPlugins((exports.node = {}), {
|
173 |
|
|
NodeTemplatePlugin: () => require("./node/NodeTemplatePlugin"),
|
174 |
|
|
ReadFileCompileWasmTemplatePlugin: () =>
|
175 |
|
|
require("./node/ReadFileCompileWasmTemplatePlugin")
|
176 |
|
|
});
|
177 |
|
|
exportPlugins((exports.debug = {}), {
|
178 |
|
|
ProfilingPlugin: () => require("./debug/ProfilingPlugin")
|
179 |
|
|
});
|
180 |
|
|
exportPlugins((exports.util = {}), {
|
181 |
|
|
createHash: () => require("./util/createHash")
|
182 |
|
|
});
|
183 |
|
|
|
184 |
|
|
const defineMissingPluginError = (namespace, pluginName, errorMessage) => {
|
185 |
|
|
Object.defineProperty(namespace, pluginName, {
|
186 |
|
|
configurable: false,
|
187 |
|
|
enumerable: true,
|
188 |
|
|
get() {
|
189 |
|
|
throw new RemovedPluginError(errorMessage);
|
190 |
|
|
}
|
191 |
|
|
});
|
192 |
|
|
};
|
193 |
|
|
|
194 |
|
|
// TODO remove in webpack 5
|
195 |
|
|
defineMissingPluginError(
|
196 |
|
|
exports.optimize,
|
197 |
|
|
"UglifyJsPlugin",
|
198 |
|
|
"webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead."
|
199 |
|
|
);
|
200 |
|
|
|
201 |
|
|
// TODO remove in webpack 5
|
202 |
|
|
defineMissingPluginError(
|
203 |
|
|
exports.optimize,
|
204 |
|
|
"CommonsChunkPlugin",
|
205 |
|
|
"webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChunks instead."
|
206 |
|
|
);
|