webpack/lib/webpack.js

215 lines
6.5 KiB
JavaScript
Raw Normal View History

2012-03-12 04:50:55 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
const util = require("util");
const webpackOptionsSchemaCheck = require("../schemas/WebpackOptions.check");
2018-07-30 23:08:51 +08:00
const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
const Compiler = require("./Compiler");
const MultiCompiler = require("./MultiCompiler");
const WebpackOptionsApply = require("./WebpackOptionsApply");
const {
2025-07-03 17:06:45 +08:00
applyWebpackOptionsBaseDefaults,
applyWebpackOptionsDefaults
} = require("./config/defaults");
const { getNormalizedWebpackOptions } = require("./config/normalization");
2018-07-30 23:08:51 +08:00
const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
const memoize = require("./util/memoize");
2018-02-27 01:22:25 +08:00
2018-09-19 19:05:22 +08:00
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
2024-02-17 01:39:12 +08:00
/** @typedef {import("../declarations/WebpackOptions").WebpackPluginFunction} WebpackPluginFunction */
2025-08-20 18:50:12 +08:00
/** @typedef {import("./config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptionsNormalizedWithDefaults */
/** @typedef {import("./Compiler").WatchOptions} WatchOptions */
/** @typedef {import("./MultiCompiler").MultiCompilerOptions} MultiCompilerOptions */
2025-07-18 20:28:16 +08:00
/** @typedef {import("./MultiCompiler").MultiWebpackOptions} MultiWebpackOptions */
2018-12-10 18:34:59 +08:00
/** @typedef {import("./MultiStats")} MultiStats */
2025-08-22 06:40:41 +08:00
/** @typedef {import("./Watching")} Watching */
/** @typedef {import("./MultiWatching")} MultiWatching */
/** @typedef {import("./Stats")} Stats */
2018-09-19 19:05:22 +08:00
const getValidateSchema = memoize(() => require("./validateSchema"));
2018-12-09 21:26:35 +08:00
/**
2018-12-10 18:34:59 +08:00
* @template T
2025-08-22 06:40:41 +08:00
* @template [R=void]
2018-12-10 18:34:59 +08:00
* @callback Callback
2024-02-21 22:05:29 +08:00
* @param {Error | null} err
2025-08-22 06:40:41 +08:00
* @param {T=} result
* @returns {R}
2018-12-09 21:26:35 +08:00
*/
2025-08-22 06:40:41 +08:00
/** @typedef {Callback<void>} ErrorCallback */
2018-12-09 21:26:35 +08:00
/**
* @param {ReadonlyArray<WebpackOptions>} childOptions options array
* @param {MultiCompilerOptions} options options
2018-12-09 21:26:35 +08:00
* @returns {MultiCompiler} a multi-compiler
*/
const createMultiCompiler = (childOptions, options) => {
2024-05-08 00:01:41 +08:00
const compilers = childOptions.map((options, index) =>
createCompiler(options, index)
);
const compiler = new MultiCompiler(compilers, options);
2018-12-09 21:26:35 +08:00
for (const childCompiler of compilers) {
if (childCompiler.options.dependencies) {
compiler.setDependencies(
childCompiler,
childCompiler.options.dependencies
);
}
}
return compiler;
};
2018-09-19 19:05:22 +08:00
/**
* @param {WebpackOptions} rawOptions options object
2025-04-16 22:04:11 +08:00
* @param {number=} compilerIndex index of compiler
2018-12-09 21:26:35 +08:00
* @returns {Compiler} a compiler
*/
2024-05-08 00:03:46 +08:00
const createCompiler = (rawOptions, compilerIndex) => {
const options = getNormalizedWebpackOptions(rawOptions);
applyWebpackOptionsBaseDefaults(options);
2023-06-12 22:21:21 +08:00
const compiler = new Compiler(
/** @type {string} */ (options.context),
options
);
2019-07-24 16:51:04 +08:00
new NodeEnvironmentPlugin({
infrastructureLogging: options.infrastructureLogging
}).apply(compiler);
2018-12-09 21:26:35 +08:00
if (Array.isArray(options.plugins)) {
for (const plugin of options.plugins) {
if (typeof plugin === "function") {
2024-02-17 01:39:12 +08:00
/** @type {WebpackPluginFunction} */
(plugin).call(compiler, compiler);
} else if (plugin) {
2018-12-09 21:26:35 +08:00
plugin.apply(compiler);
}
}
}
2024-06-07 22:29:10 +08:00
const resolvedDefaultOptions = applyWebpackOptionsDefaults(
options,
compilerIndex
);
if (resolvedDefaultOptions.platform) {
compiler.platform = resolvedDefaultOptions.platform;
}
2018-12-09 21:26:35 +08:00
compiler.hooks.environment.call();
compiler.hooks.afterEnvironment.call();
2025-08-20 18:50:12 +08:00
new WebpackOptionsApply().process(
/** @type {WebpackOptionsNormalizedWithDefaults} */
(options),
compiler
);
compiler.hooks.initialize.call();
2018-12-09 21:26:35 +08:00
return compiler;
};
/**
2020-04-21 14:54:59 +08:00
* @callback WebpackFunctionSingle
* @param {WebpackOptions} options options object
* @param {Callback<Stats>=} callback callback
2025-07-18 20:28:16 +08:00
* @returns {Compiler | null} the compiler object
2018-09-19 19:05:22 +08:00
*/
2020-04-21 14:54:59 +08:00
/**
* @callback WebpackFunctionMulti
2025-07-18 20:28:16 +08:00
* @param {MultiWebpackOptions} options options objects
2020-04-21 14:54:59 +08:00
* @param {Callback<MultiStats>=} callback callback
2025-07-18 20:28:16 +08:00
* @returns {MultiCompiler | null} the multi compiler object
2020-04-21 14:54:59 +08:00
*/
2023-06-12 22:21:21 +08:00
/**
* @template T
* @param {Array<T> | T} options options
* @returns {Array<T>} array of options
*/
const asArray = (options) =>
Array.isArray(options) ? [...options] : [options];
2025-07-18 20:28:16 +08:00
/**
* @callback WebpackCallback
* @param {WebpackOptions | MultiWebpackOptions} options options
* @param {Callback<Stats> & Callback<MultiStats>=} callback callback
* @returns {Compiler | MultiCompiler | null} Compiler or MultiCompiler
*/
const webpack = /** @type {WebpackFunctionSingle & WebpackFunctionMulti} */ (
2025-07-18 20:28:16 +08:00
/** @type {WebpackCallback} */
(options, callback) => {
const create = () => {
2025-08-20 18:50:12 +08:00
if (
!asArray(/** @type {WebpackOptions} */ (options)).every(
webpackOptionsSchemaCheck
)
) {
getValidateSchema()(webpackOptionsSchema, 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;
2023-06-12 22:21:21 +08:00
/** @type {boolean | undefined} */
let watch = false;
2025-08-20 18:50:12 +08:00
/** @type {WatchOptions | WatchOptions[]} */
let watchOptions;
if (Array.isArray(options)) {
/** @type {MultiCompiler} */
2021-05-18 23:10:36 +08:00
compiler = createMultiCompiler(
options,
2025-08-20 18:50:12 +08:00
/** @type {MultiCompilerOptions} */
(options)
2021-05-18 23:10:36 +08:00
);
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) => {
2024-02-21 22:55:02 +08:00
callback(
err || err2,
/** @type {options extends WebpackOptions ? Stats : MultiStats} */
(stats)
);
});
});
}
return compiler;
} catch (err) {
2024-02-17 01:39:12 +08:00
process.nextTick(() => callback(/** @type {Error} */ (err)));
return null;
}
} else {
const { compiler, watch } = create();
if (watch) {
util.deprecate(
() => {},
2021-07-12 18:15:59 +08:00
"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;
2018-12-09 21:26:35 +08:00
}
2012-10-20 21:08:12 +08:00
}
);
2017-11-08 18:32:05 +08:00
2018-12-09 21:26:35 +08:00
module.exports = webpack;