| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const util = require("util");
|
|---|
| 9 | const webpackOptionsSchemaCheck = require("../schemas/WebpackOptions.check");
|
|---|
| 10 | const webpackOptionsSchema =
|
|---|
| 11 | /** @type {EXPECTED_ANY} */
|
|---|
| 12 | (require("../schemas/WebpackOptions.json"));
|
|---|
| 13 | const Compiler = require("./Compiler");
|
|---|
| 14 | const MultiCompiler = require("./MultiCompiler");
|
|---|
| 15 | const WebpackOptionsApply = require("./WebpackOptionsApply");
|
|---|
| 16 | const {
|
|---|
| 17 | applyWebpackOptionsBaseDefaults,
|
|---|
| 18 | applyWebpackOptionsDefaults
|
|---|
| 19 | } = require("./config/defaults");
|
|---|
| 20 | const {
|
|---|
| 21 | applyWebpackOptionsInterception,
|
|---|
| 22 | getNormalizedWebpackOptions
|
|---|
| 23 | } = require("./config/normalization");
|
|---|
| 24 | const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
|
|---|
| 25 | const memoize = require("./util/memoize");
|
|---|
| 26 |
|
|---|
| 27 | /** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
|
|---|
| 28 | /** @typedef {import("./config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptionsNormalizedWithDefaults */
|
|---|
| 29 | /** @typedef {import("./config/normalization").WebpackOptionsInterception} WebpackOptionsInterception */
|
|---|
| 30 | /** @typedef {import("./Compiler").WatchOptions} WatchOptions */
|
|---|
| 31 | /** @typedef {import("./MultiCompiler").MultiCompilerOptions} MultiCompilerOptions */
|
|---|
| 32 | /** @typedef {import("./MultiCompiler").MultiWebpackOptions} MultiWebpackOptions */
|
|---|
| 33 | /** @typedef {import("./MultiStats")} MultiStats */
|
|---|
| 34 | /** @typedef {import("./Stats")} Stats */
|
|---|
| 35 |
|
|---|
| 36 | /** @typedef {(this: Compiler, compiler: Compiler) => void} WebpackPluginFunction */
|
|---|
| 37 | /** @typedef {(compiler: Compiler) => void} WebpackPluginInstanceApplyFunction */
|
|---|
| 38 |
|
|---|
| 39 | const getValidateSchema = memoize(() => require("./validateSchema"));
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Defines the callback callback.
|
|---|
| 43 | * @template T
|
|---|
| 44 | * @template [R=void]
|
|---|
| 45 | * @callback Callback
|
|---|
| 46 | * @param {Error | null} err
|
|---|
| 47 | * @param {T=} result
|
|---|
| 48 | * @returns {R}
|
|---|
| 49 | */
|
|---|
| 50 |
|
|---|
| 51 | /** @typedef {Callback<void>} ErrorCallback */
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Creates a multi compiler.
|
|---|
| 55 | * @param {ReadonlyArray<WebpackOptions>} childOptions options array
|
|---|
| 56 | * @param {MultiCompilerOptions} options options
|
|---|
| 57 | * @returns {MultiCompiler} a multi-compiler
|
|---|
| 58 | */
|
|---|
| 59 | const createMultiCompiler = (childOptions, options) => {
|
|---|
| 60 | const compilers = childOptions.map((options, index) =>
|
|---|
| 61 | createCompiler(options, index)
|
|---|
| 62 | );
|
|---|
| 63 | const compiler = new MultiCompiler(compilers, options);
|
|---|
| 64 | for (const childCompiler of compilers) {
|
|---|
| 65 | if (childCompiler.options.dependencies) {
|
|---|
| 66 | compiler.setDependencies(
|
|---|
| 67 | childCompiler,
|
|---|
| 68 | childCompiler.options.dependencies
|
|---|
| 69 | );
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | return compiler;
|
|---|
| 73 | };
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * Creates a compiler.
|
|---|
| 77 | * @param {WebpackOptions} rawOptions options object
|
|---|
| 78 | * @param {number=} compilerIndex index of compiler
|
|---|
| 79 | * @returns {Compiler} a compiler
|
|---|
| 80 | */
|
|---|
| 81 | const createCompiler = (rawOptions, compilerIndex) => {
|
|---|
| 82 | let options = getNormalizedWebpackOptions(rawOptions);
|
|---|
| 83 | applyWebpackOptionsBaseDefaults(options);
|
|---|
| 84 |
|
|---|
| 85 | /** @type {WebpackOptionsInterception=} */
|
|---|
| 86 | let interception;
|
|---|
| 87 | ({ options, interception } = applyWebpackOptionsInterception(options));
|
|---|
| 88 |
|
|---|
| 89 | const compiler = new Compiler(
|
|---|
| 90 | /** @type {string} */ (options.context),
|
|---|
| 91 | options
|
|---|
| 92 | );
|
|---|
| 93 | new NodeEnvironmentPlugin({
|
|---|
| 94 | infrastructureLogging: options.infrastructureLogging
|
|---|
| 95 | }).apply(compiler);
|
|---|
| 96 | if (Array.isArray(options.plugins)) {
|
|---|
| 97 | for (const plugin of options.plugins) {
|
|---|
| 98 | if (typeof plugin === "function") {
|
|---|
| 99 | /** @type {WebpackPluginFunction} */
|
|---|
| 100 | (plugin).call(compiler, compiler);
|
|---|
| 101 | } else if (plugin) {
|
|---|
| 102 | plugin.apply(compiler);
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | const resolvedDefaultOptions = applyWebpackOptionsDefaults(
|
|---|
| 107 | options,
|
|---|
| 108 | compilerIndex
|
|---|
| 109 | );
|
|---|
| 110 | if (resolvedDefaultOptions.platform) {
|
|---|
| 111 | compiler.platform = resolvedDefaultOptions.platform;
|
|---|
| 112 | }
|
|---|
| 113 | if (options.validate) {
|
|---|
| 114 | compiler.hooks.validate.call();
|
|---|
| 115 | }
|
|---|
| 116 | compiler.hooks.environment.call();
|
|---|
| 117 | compiler.hooks.afterEnvironment.call();
|
|---|
| 118 | new WebpackOptionsApply().process(
|
|---|
| 119 | /** @type {WebpackOptionsNormalizedWithDefaults} */
|
|---|
| 120 | (options),
|
|---|
| 121 | compiler,
|
|---|
| 122 | interception
|
|---|
| 123 | );
|
|---|
| 124 | compiler.hooks.initialize.call();
|
|---|
| 125 | return compiler;
|
|---|
| 126 | };
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Returns array of options.
|
|---|
| 130 | * @template T
|
|---|
| 131 | * @param {T[] | T} options options
|
|---|
| 132 | * @returns {T[]} array of options
|
|---|
| 133 | */
|
|---|
| 134 | const asArray = (options) =>
|
|---|
| 135 | Array.isArray(options) ? [...options] : [options];
|
|---|
| 136 |
|
|---|
| 137 | /**
|
|---|
| 138 | * Checks whether it needs validate.
|
|---|
| 139 | * @param {WebpackOptions | null | undefined} options options
|
|---|
| 140 | * @returns {boolean} true when need to validate, otherwise false
|
|---|
| 141 | */
|
|---|
| 142 | const needValidate = (options) => {
|
|---|
| 143 | if (
|
|---|
| 144 | options &&
|
|---|
| 145 | (options.validate === false ||
|
|---|
| 146 | (options.experiments &&
|
|---|
| 147 | options.experiments.futureDefaults === true &&
|
|---|
| 148 | (options.mode === "production" || !options.mode)))
|
|---|
| 149 | ) {
|
|---|
| 150 | return false;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | return true;
|
|---|
| 154 | };
|
|---|
| 155 |
|
|---|
| 156 | /**
|
|---|
| 157 | * Returns the compiler object.
|
|---|
| 158 | * @overload
|
|---|
| 159 | * @param {WebpackOptions} options options object
|
|---|
| 160 | * @param {Callback<Stats>} callback callback
|
|---|
| 161 | * @returns {Compiler | null} the compiler object
|
|---|
| 162 | */
|
|---|
| 163 | /**
|
|---|
| 164 | * Returns the compiler object.
|
|---|
| 165 | * @overload
|
|---|
| 166 | * @param {WebpackOptions} options options object
|
|---|
| 167 | * @returns {Compiler} the compiler object
|
|---|
| 168 | */
|
|---|
| 169 | /**
|
|---|
| 170 | * Returns the multi compiler object.
|
|---|
| 171 | * @overload
|
|---|
| 172 | * @param {MultiWebpackOptions} options options objects
|
|---|
| 173 | * @param {Callback<MultiStats>} callback callback
|
|---|
| 174 | * @returns {MultiCompiler | null} the multi compiler object
|
|---|
| 175 | */
|
|---|
| 176 | /**
|
|---|
| 177 | * Returns the multi compiler object.
|
|---|
| 178 | * @overload
|
|---|
| 179 | * @param {MultiWebpackOptions} options options objects
|
|---|
| 180 | * @returns {MultiCompiler} the multi compiler object
|
|---|
| 181 | */
|
|---|
| 182 | /**
|
|---|
| 183 | * Returns compiler or MultiCompiler.
|
|---|
| 184 | * @param {WebpackOptions | MultiWebpackOptions} options options
|
|---|
| 185 | * @param {Callback<Stats> & Callback<MultiStats>=} callback callback
|
|---|
| 186 | * @returns {Compiler | MultiCompiler | null} Compiler or MultiCompiler
|
|---|
| 187 | */
|
|---|
| 188 | const webpack = (options, callback) => {
|
|---|
| 189 | const create = () => {
|
|---|
| 190 | const isMultiCompiler = Array.isArray(options);
|
|---|
| 191 |
|
|---|
| 192 | if (
|
|---|
| 193 | !asArray(/** @type {WebpackOptions} */ (options)).every((options) =>
|
|---|
| 194 | needValidate(options) ? webpackOptionsSchemaCheck(options) : true
|
|---|
| 195 | )
|
|---|
| 196 | ) {
|
|---|
| 197 | getValidateSchema()(
|
|---|
| 198 | webpackOptionsSchema,
|
|---|
| 199 | isMultiCompiler
|
|---|
| 200 | ? options.map((options) => (needValidate(options) ? options : {}))
|
|---|
| 201 | : needValidate(options)
|
|---|
| 202 | ? options
|
|---|
| 203 | : {}
|
|---|
| 204 | );
|
|---|
| 205 | util.deprecate(
|
|---|
| 206 | () => {},
|
|---|
| 207 | "webpack bug: Pre-compiled schema reports error while real schema is happy. This has performance drawbacks.",
|
|---|
| 208 | "DEP_WEBPACK_PRE_COMPILED_SCHEMA_INVALID"
|
|---|
| 209 | )();
|
|---|
| 210 | }
|
|---|
| 211 | /** @type {MultiCompiler | Compiler} */
|
|---|
| 212 | let compiler;
|
|---|
| 213 | /** @type {boolean | undefined} */
|
|---|
| 214 | let watch = false;
|
|---|
| 215 | /** @type {WatchOptions | WatchOptions[]} */
|
|---|
| 216 | let watchOptions;
|
|---|
| 217 | if (isMultiCompiler) {
|
|---|
| 218 | /** @type {MultiCompiler} */
|
|---|
| 219 | compiler = createMultiCompiler(
|
|---|
| 220 | options,
|
|---|
| 221 | /** @type {MultiCompilerOptions} */
|
|---|
| 222 | (options)
|
|---|
| 223 | );
|
|---|
| 224 | watch = options.some((options) => options.watch);
|
|---|
| 225 | watchOptions = options.map((options) => options.watchOptions || {});
|
|---|
| 226 | } else {
|
|---|
| 227 | const webpackOptions = /** @type {WebpackOptions} */ (options);
|
|---|
| 228 | /** @type {Compiler} */
|
|---|
| 229 | compiler = createCompiler(webpackOptions);
|
|---|
| 230 | watch = webpackOptions.watch;
|
|---|
| 231 | watchOptions = webpackOptions.watchOptions || {};
|
|---|
| 232 | }
|
|---|
| 233 | return { compiler, watch, watchOptions };
|
|---|
| 234 | };
|
|---|
| 235 | if (callback) {
|
|---|
| 236 | try {
|
|---|
| 237 | const { compiler, watch, watchOptions } = create();
|
|---|
| 238 | if (watch) {
|
|---|
| 239 | compiler.watch(watchOptions, callback);
|
|---|
| 240 | } else {
|
|---|
| 241 | compiler.run((err, stats) => {
|
|---|
| 242 | compiler.close((err2) => {
|
|---|
| 243 | callback(
|
|---|
| 244 | err || err2,
|
|---|
| 245 | /** @type {options extends WebpackOptions ? Stats : MultiStats} */
|
|---|
| 246 | (stats)
|
|---|
| 247 | );
|
|---|
| 248 | });
|
|---|
| 249 | });
|
|---|
| 250 | }
|
|---|
| 251 | return compiler;
|
|---|
| 252 | } catch (err) {
|
|---|
| 253 | process.nextTick(() => callback(/** @type {Error} */ (err)));
|
|---|
| 254 | return null;
|
|---|
| 255 | }
|
|---|
| 256 | } else {
|
|---|
| 257 | const { compiler, watch } = create();
|
|---|
| 258 | if (watch) {
|
|---|
| 259 | util.deprecate(
|
|---|
| 260 | () => {},
|
|---|
| 261 | "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.",
|
|---|
| 262 | "DEP_WEBPACK_WATCH_WITHOUT_CALLBACK"
|
|---|
| 263 | )();
|
|---|
| 264 | }
|
|---|
| 265 | return compiler;
|
|---|
| 266 | }
|
|---|
| 267 | };
|
|---|
| 268 |
|
|---|
| 269 | module.exports = webpack;
|
|---|