| | |
| | | applyWebpackOptionsBaseDefaults, |
| | | applyWebpackOptionsDefaults |
| | | } = require("./config/defaults"); |
| | | const { getNormalizedWebpackOptions } = require("./config/normalization"); |
| | | const { |
| | | applyWebpackOptionsInterception, |
| | | getNormalizedWebpackOptions |
| | | } = require("./config/normalization"); |
| | | const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin"); |
| | | const memoize = require("./util/memoize"); |
| | | |
| | | /** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */ |
| | | /** @typedef {import("../declarations/WebpackOptions").WebpackPluginFunction} WebpackPluginFunction */ |
| | | /** @typedef {import("./config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptionsNormalizedWithDefaults */ |
| | | /** @typedef {import("./config/normalization").WebpackOptionsInterception} WebpackOptionsInterception */ |
| | | /** @typedef {import("./Compiler").WatchOptions} WatchOptions */ |
| | | /** @typedef {import("./MultiCompiler").MultiCompilerOptions} MultiCompilerOptions */ |
| | | /** @typedef {import("./MultiCompiler").MultiWebpackOptions} MultiWebpackOptions */ |
| | | /** @typedef {import("./MultiStats")} MultiStats */ |
| | | /** @typedef {import("./Stats")} Stats */ |
| | | |
| | | /** @typedef {(this: Compiler, compiler: Compiler) => void} WebpackPluginFunction */ |
| | | /** @typedef {(compiler: Compiler) => void} WebpackPluginInstanceApplyFunction */ |
| | | |
| | | const getValidateSchema = memoize(() => require("./validateSchema")); |
| | | |
| | | /** |
| | | * Defines the callback callback. |
| | | * @template T |
| | | * @template [R=void] |
| | | * @callback Callback |
| | |
| | | /** @typedef {Callback<void>} ErrorCallback */ |
| | | |
| | | /** |
| | | * Creates a multi compiler. |
| | | * @param {ReadonlyArray<WebpackOptions>} childOptions options array |
| | | * @param {MultiCompilerOptions} options options |
| | | * @returns {MultiCompiler} a multi-compiler |
| | |
| | | }; |
| | | |
| | | /** |
| | | * Creates a compiler. |
| | | * @param {WebpackOptions} rawOptions options object |
| | | * @param {number=} compilerIndex index of compiler |
| | | * @returns {Compiler} a compiler |
| | | */ |
| | | const createCompiler = (rawOptions, compilerIndex) => { |
| | | const options = getNormalizedWebpackOptions(rawOptions); |
| | | let options = getNormalizedWebpackOptions(rawOptions); |
| | | applyWebpackOptionsBaseDefaults(options); |
| | | |
| | | /** @type {WebpackOptionsInterception=} */ |
| | | let interception; |
| | | ({ options, interception } = applyWebpackOptionsInterception(options)); |
| | | |
| | | const compiler = new Compiler( |
| | | /** @type {string} */ (options.context), |
| | | options |
| | |
| | | if (resolvedDefaultOptions.platform) { |
| | | compiler.platform = resolvedDefaultOptions.platform; |
| | | } |
| | | if (options.validate) { |
| | | compiler.hooks.validate.call(); |
| | | } |
| | | compiler.hooks.environment.call(); |
| | | compiler.hooks.afterEnvironment.call(); |
| | | new WebpackOptionsApply().process( |
| | | /** @type {WebpackOptionsNormalizedWithDefaults} */ |
| | | (options), |
| | | compiler |
| | | compiler, |
| | | interception |
| | | ); |
| | | compiler.hooks.initialize.call(); |
| | | return compiler; |
| | | }; |
| | | |
| | | /** |
| | | * Returns array of options. |
| | | * @template T |
| | | * @param {T[] | T} options options |
| | | * @returns {T[]} array of 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( |
| | | webpackOptionsSchemaCheck |
| | | !asArray(/** @type {WebpackOptions} */ (options)).every((options) => |
| | | needValidate(options) ? webpackOptionsSchemaCheck(options) : true |
| | | ) |
| | | ) { |
| | | getValidateSchema()(webpackOptionsSchema, options); |
| | | 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.", |
| | |
| | | let watch = false; |
| | | /** @type {WatchOptions | WatchOptions[]} */ |
| | | let watchOptions; |
| | | if (Array.isArray(options)) { |
| | | if (isMultiCompiler) { |
| | | /** @type {MultiCompiler} */ |
| | | compiler = createMultiCompiler( |
| | | options, |