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