UNPKG

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