| 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 APIPlugin = require("./APIPlugin");
|
|---|
| 9 |
|
|---|
| 10 | const CompatibilityPlugin = require("./CompatibilityPlugin");
|
|---|
| 11 |
|
|---|
| 12 | const ConstPlugin = require("./ConstPlugin");
|
|---|
| 13 |
|
|---|
| 14 | const EntryOptionPlugin = require("./EntryOptionPlugin");
|
|---|
| 15 |
|
|---|
| 16 | const ExportsInfoApiPlugin = require("./ExportsInfoApiPlugin");
|
|---|
| 17 | const FlagDependencyExportsPlugin = require("./FlagDependencyExportsPlugin");
|
|---|
| 18 |
|
|---|
| 19 | const JavascriptMetaInfoPlugin = require("./JavascriptMetaInfoPlugin");
|
|---|
| 20 |
|
|---|
| 21 | const NodeStuffPlugin = require("./NodeStuffPlugin");
|
|---|
| 22 | const OptionsApply = require("./OptionsApply");
|
|---|
| 23 |
|
|---|
| 24 | const RecordIdsPlugin = require("./RecordIdsPlugin");
|
|---|
| 25 |
|
|---|
| 26 | const RuntimePlugin = require("./RuntimePlugin");
|
|---|
| 27 |
|
|---|
| 28 | const TemplatedPathPlugin = require("./TemplatedPathPlugin");
|
|---|
| 29 |
|
|---|
| 30 | const UseStrictPlugin = require("./UseStrictPlugin");
|
|---|
| 31 |
|
|---|
| 32 | const WarnCaseSensitiveModulesPlugin = require("./WarnCaseSensitiveModulesPlugin");
|
|---|
| 33 |
|
|---|
| 34 | const WebpackIsIncludedPlugin = require("./WebpackIsIncludedPlugin");
|
|---|
| 35 |
|
|---|
| 36 | const AssetModulesPlugin = require("./asset/AssetModulesPlugin");
|
|---|
| 37 |
|
|---|
| 38 | const InferAsyncModulesPlugin = require("./async-modules/InferAsyncModulesPlugin");
|
|---|
| 39 |
|
|---|
| 40 | const ResolverCachePlugin = require("./cache/ResolverCachePlugin");
|
|---|
| 41 |
|
|---|
| 42 | const CommonJsPlugin = require("./dependencies/CommonJsPlugin");
|
|---|
| 43 |
|
|---|
| 44 | const HarmonyModulesPlugin = require("./dependencies/HarmonyModulesPlugin");
|
|---|
| 45 |
|
|---|
| 46 | const ImportMetaContextPlugin = require("./dependencies/ImportMetaContextPlugin");
|
|---|
| 47 | const ImportMetaPlugin = require("./dependencies/ImportMetaPlugin");
|
|---|
| 48 |
|
|---|
| 49 | const ImportPlugin = require("./dependencies/ImportPlugin");
|
|---|
| 50 | const LoaderPlugin = require("./dependencies/LoaderPlugin");
|
|---|
| 51 |
|
|---|
| 52 | const RequireContextPlugin = require("./dependencies/RequireContextPlugin");
|
|---|
| 53 | const RequireEnsurePlugin = require("./dependencies/RequireEnsurePlugin");
|
|---|
| 54 | const RequireIncludePlugin = require("./dependencies/RequireIncludePlugin");
|
|---|
| 55 |
|
|---|
| 56 | const SystemPlugin = require("./dependencies/SystemPlugin");
|
|---|
| 57 |
|
|---|
| 58 | const URLPlugin = require("./dependencies/URLPlugin");
|
|---|
| 59 |
|
|---|
| 60 | const WorkerPlugin = require("./dependencies/WorkerPlugin");
|
|---|
| 61 |
|
|---|
| 62 | const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
|
|---|
| 63 | const JavascriptParser = require("./javascript/JavascriptParser");
|
|---|
| 64 |
|
|---|
| 65 | const JsonModulesPlugin = require("./json/JsonModulesPlugin");
|
|---|
| 66 |
|
|---|
| 67 | const ChunkPrefetchPreloadPlugin = require("./prefetch/ChunkPrefetchPreloadPlugin");
|
|---|
| 68 |
|
|---|
| 69 | const DataUriPlugin = require("./schemes/DataUriPlugin");
|
|---|
| 70 | const FileUriPlugin = require("./schemes/FileUriPlugin");
|
|---|
| 71 |
|
|---|
| 72 | const DefaultStatsFactoryPlugin = require("./stats/DefaultStatsFactoryPlugin");
|
|---|
| 73 | const DefaultStatsPresetPlugin = require("./stats/DefaultStatsPresetPlugin");
|
|---|
| 74 | const DefaultStatsPrinterPlugin = require("./stats/DefaultStatsPrinterPlugin");
|
|---|
| 75 |
|
|---|
| 76 | const { cleverMerge } = require("./util/cleverMerge");
|
|---|
| 77 |
|
|---|
| 78 | /** @typedef {import("./webpack").WebpackPluginFunction} WebpackPluginFunction */
|
|---|
| 79 | /** @typedef {import("./config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
|
|---|
| 80 | /** @typedef {import("./config/normalization").WebpackOptionsInterception} WebpackOptionsInterception */
|
|---|
| 81 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 82 | /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
|
|---|
| 83 | /** @typedef {import("./util/fs").IntermediateFileSystem} IntermediateFileSystem */
|
|---|
| 84 |
|
|---|
| 85 | const CLASS_NAME = "WebpackOptionsApply";
|
|---|
| 86 |
|
|---|
| 87 | class WebpackOptionsApply extends OptionsApply {
|
|---|
| 88 | constructor() {
|
|---|
| 89 | super();
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /**
|
|---|
| 93 | * Returns options object.
|
|---|
| 94 | * @param {WebpackOptions} options options object
|
|---|
| 95 | * @param {Compiler} compiler compiler object
|
|---|
| 96 | * @param {WebpackOptionsInterception=} interception intercepted options
|
|---|
| 97 | * @returns {WebpackOptions} options object
|
|---|
| 98 | */
|
|---|
| 99 | process(options, compiler, interception) {
|
|---|
| 100 | compiler.outputPath = options.output.path;
|
|---|
| 101 | compiler.recordsInputPath = options.recordsInputPath || null;
|
|---|
| 102 | compiler.recordsOutputPath = options.recordsOutputPath || null;
|
|---|
| 103 | compiler.name = options.name;
|
|---|
| 104 |
|
|---|
| 105 | if (options.externals) {
|
|---|
| 106 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 107 | const ExternalsPlugin = require("./ExternalsPlugin");
|
|---|
| 108 |
|
|---|
| 109 | new ExternalsPlugin(options.externalsType, options.externals).apply(
|
|---|
| 110 | compiler
|
|---|
| 111 | );
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | if (options.externalsPresets.node) {
|
|---|
| 115 | const NodeTargetPlugin = require("./node/NodeTargetPlugin");
|
|---|
| 116 |
|
|---|
| 117 | // Some older versions of Node.js don't support all built-in modules via import, only via `require`,
|
|---|
| 118 | // but it seems like there shouldn't be a warning here since these versions are rarely used in real applications
|
|---|
| 119 | new NodeTargetPlugin(
|
|---|
| 120 | options.output.module ? "module-import" : "node-commonjs"
|
|---|
| 121 | ).apply(compiler);
|
|---|
| 122 |
|
|---|
| 123 | // Handle external CSS `@import` and `url()`
|
|---|
| 124 | if (options.experiments.css) {
|
|---|
| 125 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 126 | const ExternalsPlugin = require("./ExternalsPlugin");
|
|---|
| 127 |
|
|---|
| 128 | new ExternalsPlugin(
|
|---|
| 129 | "module",
|
|---|
| 130 | ({ request, dependencyType, contextInfo }, callback) => {
|
|---|
| 131 | if (
|
|---|
| 132 | /\.css(?:\?|$)/.test(contextInfo.issuer) &&
|
|---|
| 133 | /^(?:\/\/|https?:\/\/|#)/.test(request)
|
|---|
| 134 | ) {
|
|---|
| 135 | if (dependencyType === "url") {
|
|---|
| 136 | return callback(null, `asset ${request}`);
|
|---|
| 137 | } else if (
|
|---|
| 138 | (dependencyType === "css-import" ||
|
|---|
| 139 | dependencyType === "css-import-local-module" ||
|
|---|
| 140 | dependencyType === "css-import-global-module") &&
|
|---|
| 141 | options.experiments.css
|
|---|
| 142 | ) {
|
|---|
| 143 | return callback(null, `css-import ${request}`);
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | callback();
|
|---|
| 148 | }
|
|---|
| 149 | ).apply(compiler);
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 | if (options.externalsPresets.webAsync || options.externalsPresets.web) {
|
|---|
| 153 | const type = options.externalsPresets.webAsync ? "import" : "module";
|
|---|
| 154 |
|
|---|
| 155 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 156 | const ExternalsPlugin = require("./ExternalsPlugin");
|
|---|
| 157 |
|
|---|
| 158 | new ExternalsPlugin(type, ({ request, dependencyType }, callback) => {
|
|---|
| 159 | if (/^(?:\/\/|https?:\/\/|#|std:|jsr:|npm:)/.test(request)) {
|
|---|
| 160 | if (dependencyType === "url") {
|
|---|
| 161 | return callback(null, `asset ${request}`);
|
|---|
| 162 | } else if (
|
|---|
| 163 | (dependencyType === "css-import" ||
|
|---|
| 164 | dependencyType === "css-import-local-module" ||
|
|---|
| 165 | dependencyType === "css-import-global-module") &&
|
|---|
| 166 | options.experiments.css
|
|---|
| 167 | ) {
|
|---|
| 168 | return callback(null, `css-import ${request}`);
|
|---|
| 169 | } else if (/^(?:\/\/|https?:\/\/|std:|jsr:|npm:)/.test(request)) {
|
|---|
| 170 | return callback(null, `${type} ${request}`);
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | callback();
|
|---|
| 175 | }).apply(compiler);
|
|---|
| 176 | }
|
|---|
| 177 | if (options.externalsPresets.electron) {
|
|---|
| 178 | if (options.externalsPresets.electronMain) {
|
|---|
| 179 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 180 | const ElectronTargetPlugin = require("./electron/ElectronTargetPlugin");
|
|---|
| 181 |
|
|---|
| 182 | new ElectronTargetPlugin("main").apply(compiler);
|
|---|
| 183 | }
|
|---|
| 184 | if (options.externalsPresets.electronPreload) {
|
|---|
| 185 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 186 | const ElectronTargetPlugin = require("./electron/ElectronTargetPlugin");
|
|---|
| 187 |
|
|---|
| 188 | new ElectronTargetPlugin("preload").apply(compiler);
|
|---|
| 189 | }
|
|---|
| 190 | if (options.externalsPresets.electronRenderer) {
|
|---|
| 191 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 192 | const ElectronTargetPlugin = require("./electron/ElectronTargetPlugin");
|
|---|
| 193 |
|
|---|
| 194 | new ElectronTargetPlugin("renderer").apply(compiler);
|
|---|
| 195 | }
|
|---|
| 196 | if (
|
|---|
| 197 | !options.externalsPresets.electronMain &&
|
|---|
| 198 | !options.externalsPresets.electronPreload &&
|
|---|
| 199 | !options.externalsPresets.electronRenderer
|
|---|
| 200 | ) {
|
|---|
| 201 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 202 | const ElectronTargetPlugin = require("./electron/ElectronTargetPlugin");
|
|---|
| 203 |
|
|---|
| 204 | new ElectronTargetPlugin().apply(compiler);
|
|---|
| 205 | }
|
|---|
| 206 | }
|
|---|
| 207 | if (options.externalsPresets.nwjs) {
|
|---|
| 208 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 209 | const ExternalsPlugin = require("./ExternalsPlugin");
|
|---|
| 210 |
|
|---|
| 211 | new ExternalsPlugin("node-commonjs", "nw.gui").apply(compiler);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | new ChunkPrefetchPreloadPlugin().apply(compiler);
|
|---|
| 215 |
|
|---|
| 216 | if (typeof options.output.chunkFormat === "string") {
|
|---|
| 217 | switch (options.output.chunkFormat) {
|
|---|
| 218 | case "array-push": {
|
|---|
| 219 | const ArrayPushCallbackChunkFormatPlugin = require("./javascript/ArrayPushCallbackChunkFormatPlugin");
|
|---|
| 220 |
|
|---|
| 221 | new ArrayPushCallbackChunkFormatPlugin().apply(compiler);
|
|---|
| 222 | break;
|
|---|
| 223 | }
|
|---|
| 224 | case "commonjs": {
|
|---|
| 225 | const CommonJsChunkFormatPlugin = require("./javascript/CommonJsChunkFormatPlugin");
|
|---|
| 226 |
|
|---|
| 227 | new CommonJsChunkFormatPlugin().apply(compiler);
|
|---|
| 228 | break;
|
|---|
| 229 | }
|
|---|
| 230 | case "module": {
|
|---|
| 231 | const ModuleChunkFormatPlugin = require("./esm/ModuleChunkFormatPlugin");
|
|---|
| 232 |
|
|---|
| 233 | new ModuleChunkFormatPlugin().apply(compiler);
|
|---|
| 234 | break;
|
|---|
| 235 | }
|
|---|
| 236 | default:
|
|---|
| 237 | throw new Error(
|
|---|
| 238 | `Unsupported chunk format '${options.output.chunkFormat}'.`
|
|---|
| 239 | );
|
|---|
| 240 | }
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | const enabledChunkLoadingTypes =
|
|---|
| 244 | /** @type {NonNullable<WebpackOptions["output"]["enabledChunkLoadingTypes"]>} */
|
|---|
| 245 | (options.output.enabledChunkLoadingTypes);
|
|---|
| 246 |
|
|---|
| 247 | if (enabledChunkLoadingTypes.length > 0) {
|
|---|
| 248 | for (const type of enabledChunkLoadingTypes) {
|
|---|
| 249 | const EnableChunkLoadingPlugin = require("./javascript/EnableChunkLoadingPlugin");
|
|---|
| 250 |
|
|---|
| 251 | new EnableChunkLoadingPlugin(type).apply(compiler);
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | const enabledWasmLoadingTypes =
|
|---|
| 256 | /** @type {NonNullable<WebpackOptions["output"]["enabledWasmLoadingTypes"]>} */
|
|---|
| 257 | (options.output.enabledWasmLoadingTypes);
|
|---|
| 258 |
|
|---|
| 259 | if (enabledWasmLoadingTypes.length > 0) {
|
|---|
| 260 | for (const type of enabledWasmLoadingTypes) {
|
|---|
| 261 | const EnableWasmLoadingPlugin = require("./wasm/EnableWasmLoadingPlugin");
|
|---|
| 262 |
|
|---|
| 263 | new EnableWasmLoadingPlugin(type).apply(compiler);
|
|---|
| 264 | }
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | const enabledLibraryTypes =
|
|---|
| 268 | /** @type {NonNullable<WebpackOptions["output"]["enabledLibraryTypes"]>} */
|
|---|
| 269 | (options.output.enabledLibraryTypes);
|
|---|
| 270 |
|
|---|
| 271 | if (enabledLibraryTypes.length > 0) {
|
|---|
| 272 | let once = true;
|
|---|
| 273 | for (const type of enabledLibraryTypes) {
|
|---|
| 274 | const EnableLibraryPlugin = require("./library/EnableLibraryPlugin");
|
|---|
| 275 |
|
|---|
| 276 | new EnableLibraryPlugin(type, {
|
|---|
| 277 | // eslint-disable-next-line no-loop-func
|
|---|
| 278 | additionalApply: () => {
|
|---|
| 279 | if (!once) return;
|
|---|
| 280 | once = false;
|
|---|
| 281 | // We rely on `exportInfo` to generate the `export statement` in certain library bundles.
|
|---|
| 282 | // Therefore, we ignore the disabling of `optimization.providedExport` and continue to apply `FlagDependencyExportsPlugin`.
|
|---|
| 283 | if (
|
|---|
| 284 | ["module", "commonjs-static", "modern-module"].includes(type) &&
|
|---|
| 285 | !options.optimization.providedExports
|
|---|
| 286 | ) {
|
|---|
| 287 | new FlagDependencyExportsPlugin().apply(compiler);
|
|---|
| 288 | }
|
|---|
| 289 | }
|
|---|
| 290 | }).apply(compiler);
|
|---|
| 291 | }
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | if (options.output.pathinfo) {
|
|---|
| 295 | const ModuleInfoHeaderPlugin = require("./ModuleInfoHeaderPlugin");
|
|---|
| 296 |
|
|---|
| 297 | new ModuleInfoHeaderPlugin(options.output.pathinfo !== true).apply(
|
|---|
| 298 | compiler
|
|---|
| 299 | );
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | if (options.output.clean) {
|
|---|
| 303 | const CleanPlugin = require("./CleanPlugin");
|
|---|
| 304 |
|
|---|
| 305 | new CleanPlugin(
|
|---|
| 306 | options.output.clean === true ? {} : options.output.clean
|
|---|
| 307 | ).apply(compiler);
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | if (options.dotenv) {
|
|---|
| 311 | const DotenvPlugin = require("./DotenvPlugin");
|
|---|
| 312 |
|
|---|
| 313 | new DotenvPlugin(
|
|---|
| 314 | typeof options.dotenv === "boolean" ? {} : options.dotenv
|
|---|
| 315 | ).apply(compiler);
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | let devtool =
|
|---|
| 319 | interception === undefined ? options.devtool : interception.devtool;
|
|---|
| 320 | devtool = Array.isArray(devtool)
|
|---|
| 321 | ? devtool
|
|---|
| 322 | : typeof devtool === "string"
|
|---|
| 323 | ? [{ type: "all", use: devtool }]
|
|---|
| 324 | : [];
|
|---|
| 325 |
|
|---|
| 326 | for (const item of devtool) {
|
|---|
| 327 | const { type, use } = item;
|
|---|
| 328 |
|
|---|
| 329 | if (use) {
|
|---|
| 330 | if (use.includes("source-map")) {
|
|---|
| 331 | const hidden = use.includes("hidden");
|
|---|
| 332 | const inline = use.includes("inline");
|
|---|
| 333 | const evalWrapped = use.includes("eval");
|
|---|
| 334 | const cheap = use.includes("cheap");
|
|---|
| 335 | const moduleMaps = use.includes("module");
|
|---|
| 336 | const noSources = use.includes("nosources");
|
|---|
| 337 | const debugIds = use.includes("debugids");
|
|---|
| 338 | const Plugin = evalWrapped
|
|---|
| 339 | ? require("./EvalSourceMapDevToolPlugin")
|
|---|
| 340 | : require("./SourceMapDevToolPlugin");
|
|---|
| 341 | const assetExt =
|
|---|
| 342 | type === "javascript"
|
|---|
| 343 | ? /\.((c|m)?js)($|\?)/i
|
|---|
| 344 | : type === "css"
|
|---|
| 345 | ? /\.(css)($|\?)/i
|
|---|
| 346 | : /\.((c|m)?js|css)($|\?)/i;
|
|---|
| 347 |
|
|---|
| 348 | new Plugin({
|
|---|
| 349 | test: evalWrapped ? undefined : assetExt,
|
|---|
| 350 | filename: inline ? null : options.output.sourceMapFilename,
|
|---|
| 351 | moduleFilenameTemplate:
|
|---|
| 352 | options.output.devtoolModuleFilenameTemplate,
|
|---|
| 353 | fallbackModuleFilenameTemplate:
|
|---|
| 354 | options.output.devtoolFallbackModuleFilenameTemplate,
|
|---|
| 355 | append: hidden ? false : undefined,
|
|---|
| 356 | module: moduleMaps ? true : !cheap,
|
|---|
| 357 | columns: !cheap,
|
|---|
| 358 | noSources,
|
|---|
| 359 | namespace: options.output.devtoolNamespace,
|
|---|
| 360 | debugIds
|
|---|
| 361 | }).apply(compiler);
|
|---|
| 362 | } else if (use.includes("eval")) {
|
|---|
| 363 | const EvalDevToolModulePlugin = require("./EvalDevToolModulePlugin");
|
|---|
| 364 |
|
|---|
| 365 | new EvalDevToolModulePlugin({
|
|---|
| 366 | moduleFilenameTemplate:
|
|---|
| 367 | options.output.devtoolModuleFilenameTemplate,
|
|---|
| 368 | namespace: options.output.devtoolNamespace
|
|---|
| 369 | }).apply(compiler);
|
|---|
| 370 | }
|
|---|
| 371 | }
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | new JavascriptModulesPlugin().apply(compiler);
|
|---|
| 375 | new JsonModulesPlugin().apply(compiler);
|
|---|
| 376 | new AssetModulesPlugin({
|
|---|
| 377 | sideEffectFree: options.experiments.futureDefaults
|
|---|
| 378 | }).apply(compiler);
|
|---|
| 379 |
|
|---|
| 380 | if (!options.experiments.outputModule) {
|
|---|
| 381 | if (options.output.module) {
|
|---|
| 382 | throw new Error(
|
|---|
| 383 | "'output.module: true' is only allowed when 'experiments.outputModule' is enabled"
|
|---|
| 384 | );
|
|---|
| 385 | }
|
|---|
| 386 | if (options.output.enabledLibraryTypes.includes("module")) {
|
|---|
| 387 | throw new Error(
|
|---|
| 388 | "library type \"module\" is only allowed when 'experiments.outputModule' is enabled"
|
|---|
| 389 | );
|
|---|
| 390 | }
|
|---|
| 391 | if (options.output.enabledLibraryTypes.includes("modern-module")) {
|
|---|
| 392 | throw new Error(
|
|---|
| 393 | "library type \"modern-module\" is only allowed when 'experiments.outputModule' is enabled"
|
|---|
| 394 | );
|
|---|
| 395 | }
|
|---|
| 396 | if (
|
|---|
| 397 | options.externalsType === "module" ||
|
|---|
| 398 | options.externalsType === "module-import"
|
|---|
| 399 | ) {
|
|---|
| 400 | throw new Error(
|
|---|
| 401 | "'externalsType: \"module\"' is only allowed when 'experiments.outputModule' is enabled"
|
|---|
| 402 | );
|
|---|
| 403 | }
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | if (options.experiments.syncWebAssembly) {
|
|---|
| 407 | const WebAssemblyModulesPlugin = require("./wasm-sync/WebAssemblyModulesPlugin");
|
|---|
| 408 |
|
|---|
| 409 | new WebAssemblyModulesPlugin({
|
|---|
| 410 | mangleImports: options.optimization.mangleWasmImports
|
|---|
| 411 | }).apply(compiler);
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | if (options.experiments.asyncWebAssembly) {
|
|---|
| 415 | const AsyncWebAssemblyModulesPlugin = require("./wasm-async/AsyncWebAssemblyModulesPlugin");
|
|---|
| 416 |
|
|---|
| 417 | new AsyncWebAssemblyModulesPlugin({
|
|---|
| 418 | mangleImports: options.optimization.mangleWasmImports
|
|---|
| 419 | }).apply(compiler);
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | if (options.experiments.css) {
|
|---|
| 423 | const CssModulesPlugin = require("./css/CssModulesPlugin");
|
|---|
| 424 |
|
|---|
| 425 | new CssModulesPlugin().apply(compiler);
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | if (options.experiments.html) {
|
|---|
| 429 | const HtmlModulesPlugin = require("./html/HtmlModulesPlugin");
|
|---|
| 430 |
|
|---|
| 431 | new HtmlModulesPlugin().apply(compiler);
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | if (options.experiments.typescript) {
|
|---|
| 435 | const TypeScriptPlugin = require("./typescript/TypeScriptPlugin");
|
|---|
| 436 |
|
|---|
| 437 | new TypeScriptPlugin().apply(compiler);
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | if (options.experiments.lazyCompilation) {
|
|---|
| 441 | const LazyCompilationPlugin = require("./hmr/LazyCompilationPlugin");
|
|---|
| 442 |
|
|---|
| 443 | const lazyOptions =
|
|---|
| 444 | typeof options.experiments.lazyCompilation === "object"
|
|---|
| 445 | ? options.experiments.lazyCompilation
|
|---|
| 446 | : {};
|
|---|
| 447 | const isUniversalTarget =
|
|---|
| 448 | options.output.module &&
|
|---|
| 449 | compiler.platform.node === null &&
|
|---|
| 450 | compiler.platform.web === null;
|
|---|
| 451 |
|
|---|
| 452 | if (isUniversalTarget) {
|
|---|
| 453 | const emitter = require.resolve("../hot/emitter-event-target.js");
|
|---|
| 454 |
|
|---|
| 455 | const NormalModuleReplacementPlugin = require("./NormalModuleReplacementPlugin");
|
|---|
| 456 |
|
|---|
| 457 | // Override emitter that using `EventEmitter` to `EventTarget`
|
|---|
| 458 | // TODO webpack 6 - migrate to `EventTarget` by default
|
|---|
| 459 | new NormalModuleReplacementPlugin(/emitter(\.js)?$/, (result) => {
|
|---|
| 460 | if (
|
|---|
| 461 | /webpack[/\\]hot|webpack-dev-server[/\\]client|webpack-hot-middleware[/\\]client/.test(
|
|---|
| 462 | result.context
|
|---|
| 463 | )
|
|---|
| 464 | ) {
|
|---|
| 465 | result.request = emitter;
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 | return result;
|
|---|
| 469 | }).apply(compiler);
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | const backend = require.resolve(
|
|---|
| 473 | isUniversalTarget
|
|---|
| 474 | ? "../hot/lazy-compilation-universal.js"
|
|---|
| 475 | : `../hot/lazy-compilation-${
|
|---|
| 476 | options.externalsPresets.node ? "node" : "web"
|
|---|
| 477 | }.js`
|
|---|
| 478 | );
|
|---|
| 479 |
|
|---|
| 480 | new LazyCompilationPlugin({
|
|---|
| 481 | backend:
|
|---|
| 482 | typeof lazyOptions.backend === "function"
|
|---|
| 483 | ? lazyOptions.backend
|
|---|
| 484 | : require("./hmr/lazyCompilationBackend")({
|
|---|
| 485 | ...lazyOptions.backend,
|
|---|
| 486 | client:
|
|---|
| 487 | (lazyOptions.backend && lazyOptions.backend.client) || backend
|
|---|
| 488 | }),
|
|---|
| 489 | entries: !lazyOptions || lazyOptions.entries !== false,
|
|---|
| 490 | imports: !lazyOptions || lazyOptions.imports !== false,
|
|---|
| 491 | test: (lazyOptions && lazyOptions.test) || undefined
|
|---|
| 492 | }).apply(compiler);
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | if (options.experiments.buildHttp) {
|
|---|
| 496 | const HttpUriPlugin = require("./schemes/HttpUriPlugin");
|
|---|
| 497 |
|
|---|
| 498 | const httpOptions = options.experiments.buildHttp;
|
|---|
| 499 | new HttpUriPlugin(httpOptions).apply(compiler);
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | if (
|
|---|
| 503 | !(
|
|---|
| 504 | /** @type {typeof JavascriptParser & { __importPhasesExtended?: true }} */
|
|---|
| 505 | (JavascriptParser).__importPhasesExtended
|
|---|
| 506 | ) &&
|
|---|
| 507 | (options.experiments.deferImport || options.experiments.sourceImport)
|
|---|
| 508 | ) {
|
|---|
| 509 | const importPhases = require("acorn-import-phases");
|
|---|
| 510 |
|
|---|
| 511 | JavascriptParser.extend(importPhases({ source: true, defer: true }));
|
|---|
| 512 | /** @type {typeof JavascriptParser & { __importPhasesExtended?: true }} */
|
|---|
| 513 | (JavascriptParser).__importPhasesExtended = true;
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | new EntryOptionPlugin().apply(compiler);
|
|---|
| 517 | compiler.hooks.entryOption.call(options.context, options.entry);
|
|---|
| 518 |
|
|---|
| 519 | new RuntimePlugin().apply(compiler);
|
|---|
| 520 |
|
|---|
| 521 | new InferAsyncModulesPlugin().apply(compiler);
|
|---|
| 522 |
|
|---|
| 523 | new DataUriPlugin().apply(compiler);
|
|---|
| 524 | new FileUriPlugin().apply(compiler);
|
|---|
| 525 |
|
|---|
| 526 | new CompatibilityPlugin().apply(compiler);
|
|---|
| 527 | new HarmonyModulesPlugin({
|
|---|
| 528 | deferImport: options.experiments.deferImport
|
|---|
| 529 | }).apply(compiler);
|
|---|
| 530 | if (options.amd !== false) {
|
|---|
| 531 | const AMDPlugin = require("./dependencies/AMDPlugin");
|
|---|
| 532 | const RequireJsStuffPlugin = require("./dependencies/RequireJsStuffPlugin");
|
|---|
| 533 |
|
|---|
| 534 | new AMDPlugin(options.amd || {}).apply(compiler);
|
|---|
| 535 | new RequireJsStuffPlugin().apply(compiler);
|
|---|
| 536 | }
|
|---|
| 537 | new CommonJsPlugin().apply(compiler);
|
|---|
| 538 | new LoaderPlugin().apply(compiler);
|
|---|
| 539 | new NodeStuffPlugin({
|
|---|
| 540 | global: options.node ? options.node.global : false,
|
|---|
| 541 | __dirname: options.node ? options.node.__dirname : false,
|
|---|
| 542 | __filename: options.node ? options.node.__filename : false
|
|---|
| 543 | }).apply(compiler);
|
|---|
| 544 | new APIPlugin().apply(compiler);
|
|---|
| 545 | new ExportsInfoApiPlugin().apply(compiler);
|
|---|
| 546 | new WebpackIsIncludedPlugin().apply(compiler);
|
|---|
| 547 | new ConstPlugin().apply(compiler);
|
|---|
| 548 | new UseStrictPlugin().apply(compiler);
|
|---|
| 549 | new RequireIncludePlugin().apply(compiler);
|
|---|
| 550 | new RequireEnsurePlugin().apply(compiler);
|
|---|
| 551 | new RequireContextPlugin().apply(compiler);
|
|---|
| 552 | new ImportPlugin().apply(compiler);
|
|---|
| 553 | new ImportMetaContextPlugin().apply(compiler);
|
|---|
| 554 | new SystemPlugin().apply(compiler);
|
|---|
| 555 | new ImportMetaPlugin().apply(compiler);
|
|---|
| 556 | new URLPlugin().apply(compiler);
|
|---|
| 557 | new WorkerPlugin(
|
|---|
| 558 | options.output.workerChunkLoading,
|
|---|
| 559 | options.output.workerWasmLoading,
|
|---|
| 560 | options.output.module,
|
|---|
| 561 | options.output.workerPublicPath
|
|---|
| 562 | ).apply(compiler);
|
|---|
| 563 |
|
|---|
| 564 | new DefaultStatsFactoryPlugin().apply(compiler);
|
|---|
| 565 | new DefaultStatsPresetPlugin().apply(compiler);
|
|---|
| 566 | new DefaultStatsPrinterPlugin().apply(compiler);
|
|---|
| 567 |
|
|---|
| 568 | new JavascriptMetaInfoPlugin().apply(compiler);
|
|---|
| 569 |
|
|---|
| 570 | if (typeof options.mode !== "string") {
|
|---|
| 571 | const WarnNoModeSetPlugin = require("./WarnNoModeSetPlugin");
|
|---|
| 572 |
|
|---|
| 573 | new WarnNoModeSetPlugin().apply(compiler);
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | const EnsureChunkConditionsPlugin = require("./optimize/EnsureChunkConditionsPlugin");
|
|---|
| 577 |
|
|---|
| 578 | new EnsureChunkConditionsPlugin().apply(compiler);
|
|---|
| 579 | if (options.optimization.removeAvailableModules) {
|
|---|
| 580 | const RemoveParentModulesPlugin = require("./optimize/RemoveParentModulesPlugin");
|
|---|
| 581 |
|
|---|
| 582 | new RemoveParentModulesPlugin().apply(compiler);
|
|---|
| 583 | }
|
|---|
| 584 | if (options.optimization.removeEmptyChunks) {
|
|---|
| 585 | const RemoveEmptyChunksPlugin = require("./optimize/RemoveEmptyChunksPlugin");
|
|---|
| 586 |
|
|---|
| 587 | new RemoveEmptyChunksPlugin().apply(compiler);
|
|---|
| 588 | }
|
|---|
| 589 | if (options.optimization.mergeDuplicateChunks) {
|
|---|
| 590 | const MergeDuplicateChunksPlugin = require("./optimize/MergeDuplicateChunksPlugin");
|
|---|
| 591 |
|
|---|
| 592 | new MergeDuplicateChunksPlugin().apply(compiler);
|
|---|
| 593 | }
|
|---|
| 594 | if (options.optimization.flagIncludedChunks) {
|
|---|
| 595 | const FlagIncludedChunksPlugin = require("./optimize/FlagIncludedChunksPlugin");
|
|---|
| 596 |
|
|---|
| 597 | new FlagIncludedChunksPlugin().apply(compiler);
|
|---|
| 598 | }
|
|---|
| 599 | if (options.optimization.sideEffects) {
|
|---|
| 600 | const SideEffectsFlagPlugin = require("./optimize/SideEffectsFlagPlugin");
|
|---|
| 601 |
|
|---|
| 602 | new SideEffectsFlagPlugin(
|
|---|
| 603 | options.optimization.sideEffects === true
|
|---|
| 604 | ).apply(compiler);
|
|---|
| 605 | }
|
|---|
| 606 | if (options.optimization.providedExports) {
|
|---|
| 607 | new FlagDependencyExportsPlugin().apply(compiler);
|
|---|
| 608 | }
|
|---|
| 609 | if (options.optimization.usedExports) {
|
|---|
| 610 | const FlagDependencyUsagePlugin = require("./FlagDependencyUsagePlugin");
|
|---|
| 611 |
|
|---|
| 612 | new FlagDependencyUsagePlugin(
|
|---|
| 613 | options.optimization.usedExports === "global"
|
|---|
| 614 | ).apply(compiler);
|
|---|
| 615 | }
|
|---|
| 616 | if (options.optimization.innerGraph) {
|
|---|
| 617 | const InnerGraphPlugin = require("./optimize/InnerGraphPlugin");
|
|---|
| 618 |
|
|---|
| 619 | new InnerGraphPlugin().apply(compiler);
|
|---|
| 620 | }
|
|---|
| 621 | if (options.optimization.mangleExports) {
|
|---|
| 622 | const MangleExportsPlugin = require("./optimize/MangleExportsPlugin");
|
|---|
| 623 |
|
|---|
| 624 | new MangleExportsPlugin(
|
|---|
| 625 | options.optimization.mangleExports !== "size"
|
|---|
| 626 | ).apply(compiler);
|
|---|
| 627 | }
|
|---|
| 628 | if (options.optimization.concatenateModules) {
|
|---|
| 629 | const ModuleConcatenationPlugin = require("./optimize/ModuleConcatenationPlugin");
|
|---|
| 630 |
|
|---|
| 631 | new ModuleConcatenationPlugin().apply(compiler);
|
|---|
| 632 | }
|
|---|
| 633 | if (options.optimization.splitChunks) {
|
|---|
| 634 | const SplitChunksPlugin = require("./optimize/SplitChunksPlugin");
|
|---|
| 635 |
|
|---|
| 636 | new SplitChunksPlugin(options.optimization.splitChunks).apply(compiler);
|
|---|
| 637 | }
|
|---|
| 638 | if (options.optimization.runtimeChunk) {
|
|---|
| 639 | const RuntimeChunkPlugin = require("./optimize/RuntimeChunkPlugin");
|
|---|
| 640 |
|
|---|
| 641 | new RuntimeChunkPlugin(options.optimization.runtimeChunk).apply(compiler);
|
|---|
| 642 | }
|
|---|
| 643 | if (!options.optimization.emitOnErrors) {
|
|---|
| 644 | const NoEmitOnErrorsPlugin = require("./NoEmitOnErrorsPlugin");
|
|---|
| 645 |
|
|---|
| 646 | new NoEmitOnErrorsPlugin().apply(compiler);
|
|---|
| 647 | }
|
|---|
| 648 | if (options.optimization.realContentHash) {
|
|---|
| 649 | const RealContentHashPlugin = require("./optimize/RealContentHashPlugin");
|
|---|
| 650 |
|
|---|
| 651 | new RealContentHashPlugin({
|
|---|
| 652 | hashFunction:
|
|---|
| 653 | /** @type {NonNullable<WebpackOptions["output"]["hashFunction"]>} */
|
|---|
| 654 | (options.output.hashFunction),
|
|---|
| 655 | hashDigest:
|
|---|
| 656 | /** @type {NonNullable<WebpackOptions["output"]["hashDigest"]>} */
|
|---|
| 657 | (options.output.hashDigest)
|
|---|
| 658 | }).apply(compiler);
|
|---|
| 659 | }
|
|---|
| 660 | if (options.optimization.checkWasmTypes) {
|
|---|
| 661 | const WasmFinalizeExportsPlugin = require("./wasm-sync/WasmFinalizeExportsPlugin");
|
|---|
| 662 |
|
|---|
| 663 | new WasmFinalizeExportsPlugin().apply(compiler);
|
|---|
| 664 | }
|
|---|
| 665 | const moduleIds = options.optimization.moduleIds;
|
|---|
| 666 | if (moduleIds) {
|
|---|
| 667 | switch (moduleIds) {
|
|---|
| 668 | case "natural": {
|
|---|
| 669 | const NaturalModuleIdsPlugin = require("./ids/NaturalModuleIdsPlugin");
|
|---|
| 670 |
|
|---|
| 671 | new NaturalModuleIdsPlugin().apply(compiler);
|
|---|
| 672 | break;
|
|---|
| 673 | }
|
|---|
| 674 | case "named": {
|
|---|
| 675 | const NamedModuleIdsPlugin = require("./ids/NamedModuleIdsPlugin");
|
|---|
| 676 |
|
|---|
| 677 | new NamedModuleIdsPlugin().apply(compiler);
|
|---|
| 678 | break;
|
|---|
| 679 | }
|
|---|
| 680 | case "hashed": {
|
|---|
| 681 | const WarnDeprecatedOptionPlugin = require("./WarnDeprecatedOptionPlugin");
|
|---|
| 682 | const HashedModuleIdsPlugin = require("./ids/HashedModuleIdsPlugin");
|
|---|
| 683 |
|
|---|
| 684 | new WarnDeprecatedOptionPlugin(
|
|---|
| 685 | "optimization.moduleIds",
|
|---|
| 686 | "hashed",
|
|---|
| 687 | "deterministic"
|
|---|
| 688 | ).apply(compiler);
|
|---|
| 689 | new HashedModuleIdsPlugin({
|
|---|
| 690 | hashFunction: options.output.hashFunction
|
|---|
| 691 | }).apply(compiler);
|
|---|
| 692 | break;
|
|---|
| 693 | }
|
|---|
| 694 | case "deterministic": {
|
|---|
| 695 | const DeterministicModuleIdsPlugin = require("./ids/DeterministicModuleIdsPlugin");
|
|---|
| 696 |
|
|---|
| 697 | new DeterministicModuleIdsPlugin().apply(compiler);
|
|---|
| 698 | break;
|
|---|
| 699 | }
|
|---|
| 700 | case "size": {
|
|---|
| 701 | const OccurrenceModuleIdsPlugin = require("./ids/OccurrenceModuleIdsPlugin");
|
|---|
| 702 |
|
|---|
| 703 | new OccurrenceModuleIdsPlugin({
|
|---|
| 704 | prioritiseInitial: true
|
|---|
| 705 | }).apply(compiler);
|
|---|
| 706 | break;
|
|---|
| 707 | }
|
|---|
| 708 | default:
|
|---|
| 709 | throw new Error(
|
|---|
| 710 | `webpack bug: moduleIds: ${moduleIds} is not implemented`
|
|---|
| 711 | );
|
|---|
| 712 | }
|
|---|
| 713 | }
|
|---|
| 714 | const chunkIds = options.optimization.chunkIds;
|
|---|
| 715 | if (chunkIds) {
|
|---|
| 716 | switch (chunkIds) {
|
|---|
| 717 | case "natural": {
|
|---|
| 718 | const NaturalChunkIdsPlugin = require("./ids/NaturalChunkIdsPlugin");
|
|---|
| 719 |
|
|---|
| 720 | new NaturalChunkIdsPlugin().apply(compiler);
|
|---|
| 721 | break;
|
|---|
| 722 | }
|
|---|
| 723 | case "named": {
|
|---|
| 724 | const NamedChunkIdsPlugin = require("./ids/NamedChunkIdsPlugin");
|
|---|
| 725 |
|
|---|
| 726 | new NamedChunkIdsPlugin().apply(compiler);
|
|---|
| 727 | break;
|
|---|
| 728 | }
|
|---|
| 729 | case "deterministic": {
|
|---|
| 730 | const DeterministicChunkIdsPlugin = require("./ids/DeterministicChunkIdsPlugin");
|
|---|
| 731 |
|
|---|
| 732 | new DeterministicChunkIdsPlugin().apply(compiler);
|
|---|
| 733 | break;
|
|---|
| 734 | }
|
|---|
| 735 | case "size": {
|
|---|
| 736 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 737 | const OccurrenceChunkIdsPlugin = require("./ids/OccurrenceChunkIdsPlugin");
|
|---|
| 738 |
|
|---|
| 739 | new OccurrenceChunkIdsPlugin({
|
|---|
| 740 | prioritiseInitial: true
|
|---|
| 741 | }).apply(compiler);
|
|---|
| 742 | break;
|
|---|
| 743 | }
|
|---|
| 744 | case "total-size": {
|
|---|
| 745 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 746 | const OccurrenceChunkIdsPlugin = require("./ids/OccurrenceChunkIdsPlugin");
|
|---|
| 747 |
|
|---|
| 748 | new OccurrenceChunkIdsPlugin({
|
|---|
| 749 | prioritiseInitial: false
|
|---|
| 750 | }).apply(compiler);
|
|---|
| 751 | break;
|
|---|
| 752 | }
|
|---|
| 753 | default:
|
|---|
| 754 | throw new Error(
|
|---|
| 755 | `webpack bug: chunkIds: ${chunkIds} is not implemented`
|
|---|
| 756 | );
|
|---|
| 757 | }
|
|---|
| 758 | }
|
|---|
| 759 | if (options.optimization.nodeEnv) {
|
|---|
| 760 | const DefinePlugin = require("./DefinePlugin");
|
|---|
| 761 |
|
|---|
| 762 | const defValue = JSON.stringify(options.optimization.nodeEnv);
|
|---|
| 763 |
|
|---|
| 764 | new DefinePlugin({
|
|---|
| 765 | "process.env.NODE_ENV": defValue,
|
|---|
| 766 | "import.meta.env.NODE_ENV": defValue
|
|---|
| 767 | }).apply(compiler);
|
|---|
| 768 | }
|
|---|
| 769 | if (options.optimization.minimize) {
|
|---|
| 770 | for (const minimizer of options.optimization.minimizer) {
|
|---|
| 771 | if (typeof minimizer === "function") {
|
|---|
| 772 | /** @type {WebpackPluginFunction} */
|
|---|
| 773 | (minimizer).call(compiler, compiler);
|
|---|
| 774 | } else if (minimizer !== "..." && minimizer) {
|
|---|
| 775 | minimizer.apply(compiler);
|
|---|
| 776 | }
|
|---|
| 777 | }
|
|---|
| 778 | }
|
|---|
| 779 |
|
|---|
| 780 | if (options.performance) {
|
|---|
| 781 | const SizeLimitsPlugin = require("./performance/SizeLimitsPlugin");
|
|---|
| 782 |
|
|---|
| 783 | new SizeLimitsPlugin(options.performance).apply(compiler);
|
|---|
| 784 | }
|
|---|
| 785 |
|
|---|
| 786 | new TemplatedPathPlugin().apply(compiler);
|
|---|
| 787 |
|
|---|
| 788 | new RecordIdsPlugin({
|
|---|
| 789 | portableIds: options.optimization.portableRecords
|
|---|
| 790 | }).apply(compiler);
|
|---|
| 791 |
|
|---|
| 792 | new WarnCaseSensitiveModulesPlugin().apply(compiler);
|
|---|
| 793 |
|
|---|
| 794 | const AddManagedPathsPlugin = require("./cache/AddManagedPathsPlugin");
|
|---|
| 795 |
|
|---|
| 796 | new AddManagedPathsPlugin(
|
|---|
| 797 | /** @type {NonNullable<WebpackOptions["snapshot"]["managedPaths"]>} */
|
|---|
| 798 | (options.snapshot.managedPaths),
|
|---|
| 799 | /** @type {NonNullable<WebpackOptions["snapshot"]["managedPaths"]>} */
|
|---|
| 800 | (options.snapshot.immutablePaths),
|
|---|
| 801 | /** @type {NonNullable<WebpackOptions["snapshot"]["managedPaths"]>} */
|
|---|
| 802 | (options.snapshot.unmanagedPaths)
|
|---|
| 803 | ).apply(compiler);
|
|---|
| 804 |
|
|---|
| 805 | if (options.cache && typeof options.cache === "object") {
|
|---|
| 806 | const cacheOptions = options.cache;
|
|---|
| 807 | switch (cacheOptions.type) {
|
|---|
| 808 | case "memory": {
|
|---|
| 809 | if (Number.isFinite(cacheOptions.maxGenerations)) {
|
|---|
| 810 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 811 | const MemoryWithGcCachePlugin = require("./cache/MemoryWithGcCachePlugin");
|
|---|
| 812 |
|
|---|
| 813 | new MemoryWithGcCachePlugin({
|
|---|
| 814 | maxGenerations:
|
|---|
| 815 | /** @type {number} */
|
|---|
| 816 | (cacheOptions.maxGenerations)
|
|---|
| 817 | }).apply(compiler);
|
|---|
| 818 | } else {
|
|---|
| 819 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 820 | const MemoryCachePlugin = require("./cache/MemoryCachePlugin");
|
|---|
| 821 |
|
|---|
| 822 | new MemoryCachePlugin().apply(compiler);
|
|---|
| 823 | }
|
|---|
| 824 | if (cacheOptions.cacheUnaffected) {
|
|---|
| 825 | if (!options.experiments.cacheUnaffected) {
|
|---|
| 826 | throw new Error(
|
|---|
| 827 | "'cache.cacheUnaffected: true' is only allowed when 'experiments.cacheUnaffected' is enabled"
|
|---|
| 828 | );
|
|---|
| 829 | }
|
|---|
| 830 | compiler.moduleMemCaches = new Map();
|
|---|
| 831 | }
|
|---|
| 832 | break;
|
|---|
| 833 | }
|
|---|
| 834 | case "filesystem": {
|
|---|
| 835 | const AddBuildDependenciesPlugin = require("./cache/AddBuildDependenciesPlugin");
|
|---|
| 836 |
|
|---|
| 837 | for (const key in cacheOptions.buildDependencies) {
|
|---|
| 838 | const list = cacheOptions.buildDependencies[key];
|
|---|
| 839 | new AddBuildDependenciesPlugin(list).apply(compiler);
|
|---|
| 840 | }
|
|---|
| 841 | if (!Number.isFinite(cacheOptions.maxMemoryGenerations)) {
|
|---|
| 842 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 843 | const MemoryCachePlugin = require("./cache/MemoryCachePlugin");
|
|---|
| 844 |
|
|---|
| 845 | new MemoryCachePlugin().apply(compiler);
|
|---|
| 846 | } else if (cacheOptions.maxMemoryGenerations !== 0) {
|
|---|
| 847 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
|---|
| 848 | const MemoryWithGcCachePlugin = require("./cache/MemoryWithGcCachePlugin");
|
|---|
| 849 |
|
|---|
| 850 | new MemoryWithGcCachePlugin({
|
|---|
| 851 | maxGenerations:
|
|---|
| 852 | /** @type {number} */
|
|---|
| 853 | (cacheOptions.maxMemoryGenerations)
|
|---|
| 854 | }).apply(compiler);
|
|---|
| 855 | }
|
|---|
| 856 | if (cacheOptions.memoryCacheUnaffected) {
|
|---|
| 857 | if (!options.experiments.cacheUnaffected) {
|
|---|
| 858 | throw new Error(
|
|---|
| 859 | "'cache.memoryCacheUnaffected: true' is only allowed when 'experiments.cacheUnaffected' is enabled"
|
|---|
| 860 | );
|
|---|
| 861 | }
|
|---|
| 862 | compiler.moduleMemCaches = new Map();
|
|---|
| 863 | }
|
|---|
| 864 | switch (cacheOptions.store) {
|
|---|
| 865 | case "pack": {
|
|---|
| 866 | const IdleFileCachePlugin = require("./cache/IdleFileCachePlugin");
|
|---|
| 867 | const PackFileCacheStrategy = require("./cache/PackFileCacheStrategy");
|
|---|
| 868 |
|
|---|
| 869 | new IdleFileCachePlugin(
|
|---|
| 870 | new PackFileCacheStrategy({
|
|---|
| 871 | compiler,
|
|---|
| 872 | fs:
|
|---|
| 873 | /** @type {IntermediateFileSystem} */
|
|---|
| 874 | (compiler.intermediateFileSystem),
|
|---|
| 875 | context: options.context,
|
|---|
| 876 | cacheLocation:
|
|---|
| 877 | /** @type {string} */
|
|---|
| 878 | (cacheOptions.cacheLocation),
|
|---|
| 879 | version: /** @type {string} */ (cacheOptions.version),
|
|---|
| 880 | logger: compiler.getInfrastructureLogger(
|
|---|
| 881 | "webpack.cache.PackFileCacheStrategy"
|
|---|
| 882 | ),
|
|---|
| 883 | snapshot: options.snapshot,
|
|---|
| 884 | maxAge: /** @type {number} */ (cacheOptions.maxAge),
|
|---|
| 885 | profile: cacheOptions.profile,
|
|---|
| 886 | allowCollectingMemory: cacheOptions.allowCollectingMemory,
|
|---|
| 887 | compression: cacheOptions.compression,
|
|---|
| 888 | readonly: cacheOptions.readonly
|
|---|
| 889 | }),
|
|---|
| 890 | /** @type {number} */
|
|---|
| 891 | (cacheOptions.idleTimeout),
|
|---|
| 892 | /** @type {number} */
|
|---|
| 893 | (cacheOptions.idleTimeoutForInitialStore),
|
|---|
| 894 | /** @type {number} */
|
|---|
| 895 | (cacheOptions.idleTimeoutAfterLargeChanges)
|
|---|
| 896 | ).apply(compiler);
|
|---|
| 897 | break;
|
|---|
| 898 | }
|
|---|
| 899 | default:
|
|---|
| 900 | throw new Error("Unhandled value for cache.store");
|
|---|
| 901 | }
|
|---|
| 902 | break;
|
|---|
| 903 | }
|
|---|
| 904 | default:
|
|---|
| 905 | // @ts-expect-error Property 'type' does not exist on type 'never'. ts(2339)
|
|---|
| 906 | throw new Error(`Unknown cache type ${cacheOptions.type}`);
|
|---|
| 907 | }
|
|---|
| 908 | }
|
|---|
| 909 | new ResolverCachePlugin().apply(compiler);
|
|---|
| 910 |
|
|---|
| 911 | if (options.ignoreWarnings && options.ignoreWarnings.length > 0) {
|
|---|
| 912 | const IgnoreWarningsPlugin = require("./IgnoreWarningsPlugin");
|
|---|
| 913 |
|
|---|
| 914 | new IgnoreWarningsPlugin(options.ignoreWarnings).apply(compiler);
|
|---|
| 915 | }
|
|---|
| 916 |
|
|---|
| 917 | compiler.hooks.afterPlugins.call(compiler);
|
|---|
| 918 | if (!compiler.inputFileSystem) {
|
|---|
| 919 | throw new Error("No input filesystem provided");
|
|---|
| 920 | }
|
|---|
| 921 | compiler.resolverFactory.hooks.resolveOptions
|
|---|
| 922 | .for("normal")
|
|---|
| 923 | .tap(CLASS_NAME, (resolveOptions) => {
|
|---|
| 924 | resolveOptions = cleverMerge(options.resolve, resolveOptions);
|
|---|
| 925 | resolveOptions.fileSystem =
|
|---|
| 926 | /** @type {InputFileSystem} */
|
|---|
| 927 | (compiler.inputFileSystem);
|
|---|
| 928 | return resolveOptions;
|
|---|
| 929 | });
|
|---|
| 930 | compiler.resolverFactory.hooks.resolveOptions
|
|---|
| 931 | .for("context")
|
|---|
| 932 | .tap(CLASS_NAME, (resolveOptions) => {
|
|---|
| 933 | resolveOptions = cleverMerge(options.resolve, resolveOptions);
|
|---|
| 934 | resolveOptions.fileSystem =
|
|---|
| 935 | /** @type {InputFileSystem} */
|
|---|
| 936 | (compiler.inputFileSystem);
|
|---|
| 937 | resolveOptions.resolveToContext = true;
|
|---|
| 938 | return resolveOptions;
|
|---|
| 939 | });
|
|---|
| 940 | compiler.resolverFactory.hooks.resolveOptions
|
|---|
| 941 | .for("loader")
|
|---|
| 942 | .tap(CLASS_NAME, (resolveOptions) => {
|
|---|
| 943 | resolveOptions = cleverMerge(options.resolveLoader, resolveOptions);
|
|---|
| 944 | resolveOptions.fileSystem =
|
|---|
| 945 | /** @type {InputFileSystem} */
|
|---|
| 946 | (compiler.inputFileSystem);
|
|---|
| 947 | return resolveOptions;
|
|---|
| 948 | });
|
|---|
| 949 | compiler.hooks.afterResolvers.call(compiler);
|
|---|
| 950 | return options;
|
|---|
| 951 | }
|
|---|
| 952 | }
|
|---|
| 953 |
|
|---|
| 954 | module.exports = WebpackOptionsApply;
|
|---|