webpack 5.108.1 → 5.108.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,7 +8,7 @@
8
8
  const path = require("path");
9
9
  const asyncLib = require("neo-async");
10
10
  const { SyncBailHook } = require("tapable");
11
- const Compilation = require("./Compilation");
11
+ const createHooksRegistry = require("./util/createHooksRegistry");
12
12
  const { join } = require("./util/fs");
13
13
  const { ABSOLUTE_PATH_REGEXP } = require("./util/identifier");
14
14
  const processAsyncTree = require("./util/processAsyncTree");
@@ -335,33 +335,9 @@ const applyDiff = (fs, outputPath, dry, logger, diff, isKept, callback) => {
335
335
  );
336
336
  };
337
337
 
338
- /** @type {WeakMap<Compilation, CleanPluginCompilationHooks>} */
339
- const compilationHooksMap = new WeakMap();
340
-
341
338
  const PLUGIN_NAME = "CleanPlugin";
342
339
 
343
340
  class CleanPlugin {
344
- /**
345
- * Returns the attached hooks.
346
- * @param {Compilation} compilation the compilation
347
- * @returns {CleanPluginCompilationHooks} the attached hooks
348
- */
349
- static getCompilationHooks(compilation) {
350
- if (!(compilation instanceof Compilation)) {
351
- throw new TypeError(
352
- "The 'compilation' argument must be an instance of Compilation"
353
- );
354
- }
355
- let hooks = compilationHooksMap.get(compilation);
356
- if (hooks === undefined) {
357
- hooks = {
358
- keep: new SyncBailHook(["ignore"])
359
- };
360
- compilationHooksMap.set(compilation, hooks);
361
- }
362
- return hooks;
363
- }
364
-
365
341
  /** @param {CleanOptions} options options */
366
342
  constructor(options = {}) {
367
343
  /** @type {CleanOptions} */
@@ -508,5 +484,12 @@ class CleanPlugin {
508
484
  }
509
485
  }
510
486
 
487
+ CleanPlugin.getCompilationHooks = createHooksRegistry(
488
+ () =>
489
+ /** @type {CleanPluginCompilationHooks} */ ({
490
+ keep: new SyncBailHook(["ignore"])
491
+ })
492
+ );
493
+
511
494
  module.exports = CleanPlugin;
512
495
  module.exports._getDirectories = getDirectories;
@@ -2377,7 +2377,6 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
2377
2377
  applyFactoryResultDependencies();
2378
2378
  return callback();
2379
2379
  }
2380
-
2381
2380
  if (currentProfile !== undefined) {
2382
2381
  moduleGraph.setProfile(newModule, currentProfile);
2383
2382
  }
@@ -21,6 +21,7 @@ const {
21
21
  toConstantDependency
22
22
  } = require("./javascript/JavascriptParserHelpers");
23
23
  const createHash = require("./util/createHash");
24
+ const createHooksRegistry = require("./util/createHooksRegistry");
24
25
 
25
26
  /** @typedef {import("estree").Expression} Expression */
26
27
  /** @typedef {import("./Compiler")} Compiler */
@@ -362,26 +363,7 @@ const WEBPACK_REQUIRE_IDENTIFIER_REGEXP = new RegExp(RuntimeGlobals.require);
362
363
 
363
364
  /** @typedef {Record<string, CodeValue>} Definitions */
364
365
 
365
- /** @type {WeakMap<Compilation, DefinePluginHooks>} */
366
- const compilationHooksMap = new WeakMap();
367
-
368
366
  class DefinePlugin {
369
- /**
370
- * Returns the attached hooks.
371
- * @param {Compilation} compilation the compilation
372
- * @returns {DefinePluginHooks} the attached hooks
373
- */
374
- static getCompilationHooks(compilation) {
375
- let hooks = compilationHooksMap.get(compilation);
376
- if (hooks === undefined) {
377
- hooks = {
378
- definitions: new SyncWaterfallHook(["definitions"])
379
- };
380
- compilationHooksMap.set(compilation, hooks);
381
- }
382
- return hooks;
383
- }
384
-
385
367
  /**
386
368
  * Create a new define plugin
387
369
  * @param {Definitions} definitions A map of global object definitions
@@ -962,4 +944,11 @@ class DefinePlugin {
962
944
  }
963
945
  }
964
946
 
947
+ DefinePlugin.getCompilationHooks = createHooksRegistry(
948
+ () =>
949
+ /** @type {DefinePluginHooks} */ ({
950
+ definitions: new SyncWaterfallHook(["definitions"])
951
+ })
952
+ );
953
+
965
954
  module.exports = DefinePlugin;
@@ -25,7 +25,9 @@ const { DEFAULTS } = require("./config/defaults");
25
25
  const { ImportPhaseUtils } = require("./dependencies/ImportPhase");
26
26
  const StaticExportsDependency = require("./dependencies/StaticExportsDependency");
27
27
  const EnvironmentNotSupportAsyncWarning = require("./errors/EnvironmentNotSupportAsyncWarning");
28
+ const { coreModules } = require("./node/nodeBuiltins");
28
29
  const createHash = require("./util/createHash");
30
+ const createHooksRegistry = require("./util/createHooksRegistry");
29
31
  const extractUrlAndGlobal = require("./util/extractUrlAndGlobal");
30
32
  const makeSerializable = require("./util/makeSerializable");
31
33
  const { propertyAccess } = require("./util/property");
@@ -101,6 +103,56 @@ const RUNTIME_REQUIREMENTS_FOR_MODULE = new Set([
101
103
  /** @type {RuntimeRequirements} */
102
104
  const EMPTY_RUNTIME_REQUIREMENTS = new Set();
103
105
 
106
+ const NODE_PREFIX = "node:";
107
+ // External types that emit a node.js-resolvable specifier (`require`/`import`).
108
+ const NODE_RESOLVING_EXTERNAL_TYPES = new Set([
109
+ "commonjs",
110
+ "commonjs2",
111
+ "commonjs-module",
112
+ "commonjs-static",
113
+ "node-commonjs",
114
+ "import",
115
+ "module"
116
+ ]);
117
+
118
+ /**
119
+ * Normalizes a node.js core module specifier for the target. Strips `node:`
120
+ * when the target can't resolve a prefixed request, adds `node:` for a universal
121
+ * bundle (which may run on deno/bun where the prefix is required), and otherwise
122
+ * keeps the author's original notation.
123
+ * @param {string | string[]} request the external request
124
+ * @param {ExternalsType} externalType the resolved external type
125
+ * @param {RuntimeTemplate} runtimeTemplate the runtime template
126
+ * @returns {string | string[]} the normalized request
127
+ */
128
+ const normalizeNodeCoreModuleRequest = (
129
+ request,
130
+ externalType,
131
+ runtimeTemplate
132
+ ) => {
133
+ if (!NODE_RESOLVING_EXTERNAL_TYPES.has(externalType)) return request;
134
+ const name = Array.isArray(request) ? request[0] : request;
135
+ if (typeof name !== "string") return request;
136
+ /**
137
+ * @param {string} next replacement specifier
138
+ * @returns {string | string[]} request with the specifier swapped
139
+ */
140
+ const withName = (next) =>
141
+ Array.isArray(request) ? [next, ...request.slice(1)] : next;
142
+
143
+ if (
144
+ runtimeTemplate.outputOptions.environment.nodePrefixForCoreModules === false
145
+ ) {
146
+ if (!name.startsWith(NODE_PREFIX)) return request;
147
+ const stripped = name.slice(NODE_PREFIX.length);
148
+ return coreModules.has(stripped) ? withName(stripped) : request;
149
+ }
150
+ if (runtimeTemplate.isUniversalTarget() && !name.startsWith(NODE_PREFIX)) {
151
+ return coreModules.has(name) ? withName(NODE_PREFIX + name) : request;
152
+ }
153
+ return request;
154
+ };
155
+
104
156
  /**
105
157
  * Gets source for global variable external.
106
158
  * @param {string | string[]} variableName the variable name or path
@@ -716,9 +768,6 @@ const getSourceForDefaultCase = (optional, request, runtimeTemplate) => {
716
768
  * @property {SyncBailHook<[Chunk, Compilation], boolean>} chunkCondition
717
769
  */
718
770
 
719
- /** @type {WeakMap<Compilation, ExternalModuleHooks>} */
720
- const compilationHooksMap = new WeakMap();
721
-
722
771
  /**
723
772
  * Defines the build info properties specific to external modules.
724
773
  * @typedef {object} KnownExternalModuleBuildInfo
@@ -753,22 +802,6 @@ class ExternalModule extends Module {
753
802
  this.dependencyMeta = dependencyMeta;
754
803
  }
755
804
 
756
- /**
757
- * Returns the attached hooks.
758
- * @param {Compilation} compilation the compilation
759
- * @returns {ExternalModuleHooks} the attached hooks
760
- */
761
- static getCompilationHooks(compilation) {
762
- let hooks = compilationHooksMap.get(compilation);
763
- if (hooks === undefined) {
764
- hooks = {
765
- chunkCondition: new SyncBailHook(["chunk", "compilation"])
766
- };
767
- compilationHooksMap.set(compilation, hooks);
768
- }
769
- return hooks;
770
- }
771
-
772
805
  /**
773
806
  * Returns the source types this module can generate.
774
807
  * @returns {SourceTypes} types available (do not mutate)
@@ -1042,6 +1075,11 @@ class ExternalModule extends Module {
1042
1075
  dependencyMeta,
1043
1076
  concatenationScope
1044
1077
  ) {
1078
+ request = normalizeNodeCoreModuleRequest(
1079
+ request,
1080
+ externalType,
1081
+ runtimeTemplate
1082
+ );
1045
1083
  switch (externalType) {
1046
1084
  case "this":
1047
1085
  case "window":
@@ -1345,6 +1383,13 @@ class ExternalModule extends Module {
1345
1383
  }
1346
1384
  }
1347
1385
 
1386
+ ExternalModule.getCompilationHooks = createHooksRegistry(
1387
+ () =>
1388
+ /** @type {ExternalModuleHooks} */ ({
1389
+ chunkCondition: new SyncBailHook(["chunk", "compilation"])
1390
+ })
1391
+ );
1392
+
1348
1393
  makeSerializable(ExternalModule, "webpack/lib/ExternalModule");
1349
1394
 
1350
1395
  module.exports = ExternalModule;
@@ -13,6 +13,7 @@ const CssImportDependency = require("./dependencies/CssImportDependency");
13
13
  const CssUrlDependency = require("./dependencies/CssUrlDependency");
14
14
  const HarmonyImportDependency = require("./dependencies/HarmonyImportDependency");
15
15
  const ImportDependency = require("./dependencies/ImportDependency");
16
+ const { coreModules } = require("./node/nodeBuiltins");
16
17
  const { cachedSetProperty, resolveByProperty } = require("./util/cleverMerge");
17
18
 
18
19
  /** @typedef {import("enhanced-resolve").ResolveContext} ResolveContext */
@@ -48,6 +49,21 @@ const { cachedSetProperty, resolveByProperty } = require("./util/cleverMerge");
48
49
 
49
50
  const UNSPECIFIED_EXTERNAL_TYPE_REGEXP = /^[a-z0-9-]+ /;
50
51
  const EMPTY_RESOLVE_OPTIONS = {};
52
+ const NODE_PREFIX = "node:";
53
+
54
+ /**
55
+ * For a node.js core module request, returns its `node:`-prefixed/unprefixed
56
+ * counterpart so externals match regardless of which form the code uses.
57
+ * @param {string} request the request as written in the import/require
58
+ * @returns {string | undefined} the alternate form, or undefined when not a core module
59
+ */
60
+ const getAlternateCoreModuleRequest = (request) => {
61
+ if (request.startsWith(NODE_PREFIX)) {
62
+ const name = request.slice(NODE_PREFIX.length);
63
+ return coreModules.has(name) ? name : undefined;
64
+ }
65
+ return coreModules.has(request) ? NODE_PREFIX + request : undefined;
66
+ };
51
67
 
52
68
  // TODO webpack 6 remove this
53
69
  const callDeprecatedExternals = util.deprecate(
@@ -236,7 +252,10 @@ class ExternalModuleFactoryPlugin {
236
252
  */
237
253
  const handleExternals = (externals, callback) => {
238
254
  if (typeof externals === "string") {
239
- if (externals === dependency.request) {
255
+ if (
256
+ externals === dependency.request ||
257
+ externals === getAlternateCoreModuleRequest(dependency.request)
258
+ ) {
240
259
  return handleExternal(dependency.request, undefined, callback);
241
260
  }
242
261
  } else if (Array.isArray(externals)) {
@@ -374,6 +393,23 @@ class ExternalModuleFactoryPlugin {
374
393
  callback
375
394
  );
376
395
  }
396
+ // Fall back to the `node:`-prefixed/unprefixed core module form.
397
+ const alternateRequest = getAlternateCoreModuleRequest(
398
+ dependency.request
399
+ );
400
+ if (
401
+ alternateRequest !== undefined &&
402
+ Object.prototype.hasOwnProperty.call(
403
+ resolvedExternals,
404
+ alternateRequest
405
+ )
406
+ ) {
407
+ return handleExternal(
408
+ resolvedExternals[alternateRequest],
409
+ undefined,
410
+ callback
411
+ );
412
+ }
377
413
  }
378
414
  callback();
379
415
  };
package/lib/LazyBarrel.js CHANGED
@@ -73,7 +73,7 @@ class LazyBarrelInfo {
73
73
  }
74
74
 
75
75
  /**
76
- * Defers a dependency under its request key.
76
+ * Records a deferred dependency under its request key.
77
77
  * @param {string} requestKey request key
78
78
  * @param {Dependency} dependency dependency to defer
79
79
  * @param {ModuleFactory} factory factory for the request
@@ -86,7 +86,6 @@ class LazyBarrelInfo {
86
86
  this._requestToDepGroup.set(requestKey, group);
87
87
  }
88
88
  group.dependencies.push(dependency);
89
- dependency.setLazy(true);
90
89
  }
91
90
 
92
91
  /**
@@ -251,7 +250,9 @@ class LazyBarrelController {
251
250
  ? resourceIdent
252
251
  : `${category}${resourceIdent}`;
253
252
  if (info === undefined) info = new LazyBarrelInfo();
254
- info.addLazy(requestKey, dep, factory, dep.getContext());
253
+ if (dep.isLazy()) {
254
+ info.addLazy(requestKey, dep, factory, dep.getContext());
255
+ }
255
256
  if (until === Dependency.LAZY_UNTIL_ID) {
256
257
  info.addForwardId(
257
258
  /** @type {string} */ (dep.getLazyName()),
@@ -19,7 +19,6 @@ const {
19
19
  RawSource,
20
20
  SourceMapSource
21
21
  } = require("webpack-sources");
22
- const Compilation = require("./Compilation");
23
22
  const Dependency = require("./Dependency");
24
23
  const Module = require("./Module");
25
24
  const ModuleGraphConnection = require("./ModuleGraphConnection");
@@ -41,6 +40,7 @@ const {
41
40
  sortWithSourceOrder
42
41
  } = require("./util/comparators");
43
42
  const createHash = require("./util/createHash");
43
+ const createHooksRegistry = require("./util/createHooksRegistry");
44
44
  const { createFakeHook } = require("./util/deprecation");
45
45
  const formatLocation = require("./util/formatLocation");
46
46
  const { join } = require("./util/fs");
@@ -60,6 +60,7 @@ const parseJson = require("./util/parseJson");
60
60
  /** @typedef {import("webpack-sources").RawSourceMap} RawSourceMap */
61
61
  /** @typedef {import("../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
62
62
  /** @typedef {import("../declarations/WebpackOptions").NoParse} NoParse */
63
+ /** @typedef {import("./Compilation")} Compilation */
63
64
  /** @typedef {import("./config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
64
65
  /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
65
66
  /** @typedef {import("./Generator")} Generator */
@@ -762,8 +763,52 @@ const asBuffer = (input) => {
762
763
 
763
764
  /** @typedef {BuildInfo & KnownNormalModuleBuildInfo} NormalModuleBuildInfo */
764
765
 
765
- /** @type {WeakMap<Compilation, NormalModuleCompilationHooks>} */
766
- const compilationHooksMap = new WeakMap();
766
+ const normalModuleHooksRegistry = createHooksRegistry(() => {
767
+ // TODO webpack 6 deprecate
768
+ /** @type {Partial<NormalModuleCompilationHooks>} */
769
+ const hooks = {};
770
+ hooks.readResourceForScheme = new HookMap((scheme) => {
771
+ const hook =
772
+ /** @type {NormalModuleCompilationHooks} */
773
+ (hooks).readResource.for(scheme);
774
+ return createFakeHook(
775
+ /** @type {AsyncSeriesBailHook<[string, NormalModule], string | Buffer | null>} */ ({
776
+ tap: (options, fn) =>
777
+ hook.tap(options, (loaderContext) =>
778
+ fn(
779
+ loaderContext.resource,
780
+ /** @type {NormalModule} */ (loaderContext._module)
781
+ )
782
+ ),
783
+ tapAsync: (options, fn) =>
784
+ hook.tapAsync(options, (loaderContext, callback) =>
785
+ fn(
786
+ loaderContext.resource,
787
+ /** @type {NormalModule} */ (loaderContext._module),
788
+ callback
789
+ )
790
+ ),
791
+ tapPromise: (options, fn) =>
792
+ hook.tapPromise(options, (loaderContext) =>
793
+ fn(
794
+ loaderContext.resource,
795
+ /** @type {NormalModule} */ (loaderContext._module)
796
+ )
797
+ )
798
+ })
799
+ );
800
+ });
801
+ hooks.readResource = new HookMap(
802
+ () => new AsyncSeriesBailHook(["loaderContext"])
803
+ );
804
+ hooks.loader = new SyncHook(["loaderContext", "module"]);
805
+ hooks.beforeLoaders = new SyncHook(["loaders", "module", "loaderContext"]);
806
+ hooks.beforeParse = new SyncHook(["module"]);
807
+ hooks.beforeSnapshot = new SyncHook(["module"]);
808
+ hooks.processResult = new SyncWaterfallHook(["result", "module"]);
809
+ hooks.needBuild = new AsyncSeriesBailHook(["module", "context"]);
810
+ return /** @type {NormalModuleCompilationHooks} */ (hooks);
811
+ });
767
812
 
768
813
  class NormalModule extends Module {
769
814
  /**
@@ -771,62 +816,7 @@ class NormalModule extends Module {
771
816
  * @returns {NormalModuleCompilationHooks} the attached hooks
772
817
  */
773
818
  static getCompilationHooks(compilation) {
774
- if (!(compilation instanceof Compilation)) {
775
- throw new TypeError(
776
- "The 'compilation' argument must be an instance of Compilation"
777
- );
778
- }
779
- let hooks = compilationHooksMap.get(compilation);
780
- if (hooks === undefined) {
781
- hooks = {
782
- loader: new SyncHook(["loaderContext", "module"]),
783
- beforeLoaders: new SyncHook(["loaders", "module", "loaderContext"]),
784
- beforeParse: new SyncHook(["module"]),
785
- beforeSnapshot: new SyncHook(["module"]),
786
- // TODO webpack 6 deprecate
787
- readResourceForScheme: new HookMap((scheme) => {
788
- const hook =
789
- /** @type {NormalModuleCompilationHooks} */
790
- (hooks).readResource.for(scheme);
791
- return createFakeHook(
792
- /** @type {AsyncSeriesBailHook<[string, NormalModule], string | Buffer | null>} */ ({
793
- tap: (options, fn) =>
794
- hook.tap(options, (loaderContext) =>
795
- fn(
796
- loaderContext.resource,
797
- /** @type {NormalModule} */ (loaderContext._module)
798
- )
799
- ),
800
- tapAsync: (options, fn) =>
801
- hook.tapAsync(options, (loaderContext, callback) =>
802
- fn(
803
- loaderContext.resource,
804
- /** @type {NormalModule} */ (loaderContext._module),
805
- callback
806
- )
807
- ),
808
- tapPromise: (options, fn) =>
809
- hook.tapPromise(options, (loaderContext) =>
810
- fn(
811
- loaderContext.resource,
812
- /** @type {NormalModule} */ (loaderContext._module)
813
- )
814
- )
815
- })
816
- );
817
- }),
818
- readResource: new HookMap(
819
- () => new AsyncSeriesBailHook(["loaderContext"])
820
- ),
821
- processResult: new SyncWaterfallHook(["result", "module"]),
822
- needBuild: new AsyncSeriesBailHook(["module", "context"])
823
- };
824
- compilationHooksMap.set(
825
- compilation,
826
- /** @type {NormalModuleCompilationHooks} */ (hooks)
827
- );
828
- }
829
- return /** @type {NormalModuleCompilationHooks} */ (hooks);
819
+ return normalModuleHooksRegistry(compilation);
830
820
  }
831
821
 
832
822
  /**
@@ -6,18 +6,12 @@
6
6
  "use strict";
7
7
 
8
8
  const ExternalsPlugin = require("../ExternalsPlugin");
9
- const NodeTargetPlugin = require("../node/NodeTargetPlugin");
9
+ const { coreModules } = require("../node/nodeBuiltins");
10
10
 
11
11
  /** @typedef {import("../Compiler")} Compiler */
12
12
 
13
13
  // Bun exposes the node.js core modules; externalize them with the `node:`
14
14
  // specifier (Bun resolves both forms) like the deno target.
15
- // cspell:word pnpapi
16
- const coreModules = new Set(
17
- NodeTargetPlugin.builtins.filter(
18
- (builtin) => typeof builtin === "string" && builtin !== "pnpapi"
19
- )
20
- );
21
15
 
22
16
  // Bun's own built-in modules (`bun`, `bun:sqlite`, ...) are provided by the
23
17
  // runtime, never bundled.
@@ -7,8 +7,8 @@
7
7
 
8
8
  const { SyncHook } = require("tapable");
9
9
  const isValidExternalsType = require("../../schemas/plugins/container/ExternalsType.check");
10
- const Compilation = require("../Compilation");
11
10
  const SharePlugin = require("../sharing/SharePlugin");
11
+ const createHooksRegistry = require("../util/createHooksRegistry");
12
12
  const ContainerPlugin = require("./ContainerPlugin");
13
13
  const ContainerReferencePlugin = require("./ContainerReferencePlugin");
14
14
  const HoistContainerReferences = require("./HoistContainerReferencesPlugin");
@@ -25,8 +25,6 @@ const HoistContainerReferences = require("./HoistContainerReferencesPlugin");
25
25
  * @property {SyncHook<Dependency>} addFederationRuntimeDependency
26
26
  */
27
27
 
28
- /** @type {WeakMap<Compilation, CompilationHooks>} */
29
- const compilationHooksMap = new WeakMap();
30
28
  const PLUGIN_NAME = "ModuleFederationPlugin";
31
29
 
32
30
  class ModuleFederationPlugin {
@@ -39,28 +37,6 @@ class ModuleFederationPlugin {
39
37
  this.options = options;
40
38
  }
41
39
 
42
- /**
43
- * Get the compilation hooks associated with this plugin.
44
- * @param {Compilation} compilation The compilation instance.
45
- * @returns {CompilationHooks} The hooks for the compilation.
46
- */
47
- static getCompilationHooks(compilation) {
48
- if (!(compilation instanceof Compilation)) {
49
- throw new TypeError(
50
- "The 'compilation' argument must be an instance of Compilation"
51
- );
52
- }
53
- let hooks = compilationHooksMap.get(compilation);
54
- if (!hooks) {
55
- hooks = {
56
- addContainerEntryDependency: new SyncHook(["dependency"]),
57
- addFederationRuntimeDependency: new SyncHook(["dependency"])
58
- };
59
- compilationHooksMap.set(compilation, hooks);
60
- }
61
- return hooks;
62
- }
63
-
64
40
  /**
65
41
  * Applies the plugin by registering its hooks on the compiler.
66
42
  * @param {Compiler} compiler the compiler instance
@@ -134,4 +110,12 @@ class ModuleFederationPlugin {
134
110
  }
135
111
  }
136
112
 
113
+ ModuleFederationPlugin.getCompilationHooks = createHooksRegistry(
114
+ () =>
115
+ /** @type {CompilationHooks} */ ({
116
+ addContainerEntryDependency: new SyncHook(["dependency"]),
117
+ addFederationRuntimeDependency: new SyncHook(["dependency"])
118
+ })
119
+ );
120
+
137
121
  module.exports = ModuleFederationPlugin;
@@ -6,10 +6,11 @@
6
6
  "use strict";
7
7
 
8
8
  const { SyncWaterfallHook } = require("tapable");
9
- const Compilation = require("../Compilation");
9
+ /** @typedef {import("../Compilation")} Compilation */
10
10
  const RuntimeGlobals = require("../RuntimeGlobals");
11
11
  const RuntimeModule = require("../RuntimeModule");
12
12
  const Template = require("../Template");
13
+ const createHooksRegistry = require("../util/createHooksRegistry");
13
14
 
14
15
  /** @typedef {import("../Chunk")} Chunk */
15
16
  /** @typedef {import("../Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
@@ -19,30 +20,7 @@ const Template = require("../Template");
19
20
  * @property {SyncWaterfallHook<[string, Chunk]>} createStyle
20
21
  */
21
22
 
22
- /** @type {WeakMap<Compilation, CssInjectCompilationHooks>} */
23
- const compilationHooksMap = new WeakMap();
24
-
25
23
  class CssInjectStyleRuntimeModule extends RuntimeModule {
26
- /**
27
- * @param {Compilation} compilation the compilation
28
- * @returns {CssInjectCompilationHooks} hooks
29
- */
30
- static getCompilationHooks(compilation) {
31
- if (!(compilation instanceof Compilation)) {
32
- throw new TypeError(
33
- "The 'compilation' argument must be an instance of Compilation"
34
- );
35
- }
36
- let hooks = compilationHooksMap.get(compilation);
37
- if (hooks === undefined) {
38
- hooks = {
39
- createStyle: new SyncWaterfallHook(["source", "chunk"])
40
- };
41
- compilationHooksMap.set(compilation, hooks);
42
- }
43
- return hooks;
44
- }
45
-
46
24
  /**
47
25
  * @param {ReadOnlyRuntimeRequirements} runtimeRequirements runtime requirements
48
26
  */
@@ -207,4 +185,11 @@ class CssInjectStyleRuntimeModule extends RuntimeModule {
207
185
  }
208
186
  }
209
187
 
188
+ CssInjectStyleRuntimeModule.getCompilationHooks = createHooksRegistry(
189
+ () =>
190
+ /** @type {CssInjectCompilationHooks} */ ({
191
+ createStyle: new SyncWaterfallHook(["source", "chunk"])
192
+ })
193
+ );
194
+
210
195
  module.exports = CssInjectStyleRuntimeModule;
@@ -6,12 +6,13 @@
6
6
  "use strict";
7
7
 
8
8
  const { SyncWaterfallHook } = require("tapable");
9
- const Compilation = require("../Compilation");
9
+ /** @typedef {import("../Compilation")} Compilation */
10
10
  const { CSS_TYPE } = require("../ModuleSourceTypeConstants");
11
11
  const RuntimeGlobals = require("../RuntimeGlobals");
12
12
  const RuntimeModule = require("../RuntimeModule");
13
13
  const Template = require("../Template");
14
14
  const compileBooleanMatcher = require("../util/compileBooleanMatcher");
15
+ const createHooksRegistry = require("../util/createHooksRegistry");
15
16
  const { chunkHasCss } = require("./CssModulesPlugin");
16
17
 
17
18
  /** @typedef {import("../Chunk")} Chunk */
@@ -27,33 +28,7 @@ const { chunkHasCss } = require("./CssModulesPlugin");
27
28
  * @property {SyncWaterfallHook<[string, Chunk]>} linkInsert
28
29
  */
29
30
 
30
- /** @type {WeakMap<Compilation, CssLoadingRuntimeModulePluginHooks>} */
31
- const compilationHooksMap = new WeakMap();
32
-
33
31
  class CssLoadingRuntimeModule extends RuntimeModule {
34
- /**
35
- * @param {Compilation} compilation the compilation
36
- * @returns {CssLoadingRuntimeModulePluginHooks} hooks
37
- */
38
- static getCompilationHooks(compilation) {
39
- if (!(compilation instanceof Compilation)) {
40
- throw new TypeError(
41
- "The 'compilation' argument must be an instance of Compilation"
42
- );
43
- }
44
- let hooks = compilationHooksMap.get(compilation);
45
- if (hooks === undefined) {
46
- hooks = {
47
- createStylesheet: new SyncWaterfallHook(["source", "chunk"]),
48
- linkPreload: new SyncWaterfallHook(["source", "chunk"]),
49
- linkPrefetch: new SyncWaterfallHook(["source", "chunk"]),
50
- linkInsert: new SyncWaterfallHook(["source", "chunk"])
51
- };
52
- compilationHooksMap.set(compilation, hooks);
53
- }
54
- return hooks;
55
- }
56
-
57
32
  /**
58
33
  * @param {ReadOnlyRuntimeRequirements} runtimeRequirements runtime requirements
59
34
  */
@@ -597,4 +572,14 @@ class CssLoadingRuntimeModule extends RuntimeModule {
597
572
  }
598
573
  }
599
574
 
575
+ CssLoadingRuntimeModule.getCompilationHooks = createHooksRegistry(
576
+ () =>
577
+ /** @type {CssLoadingRuntimeModulePluginHooks} */ ({
578
+ createStylesheet: new SyncWaterfallHook(["source", "chunk"]),
579
+ linkPreload: new SyncWaterfallHook(["source", "chunk"]),
580
+ linkPrefetch: new SyncWaterfallHook(["source", "chunk"]),
581
+ linkInsert: new SyncWaterfallHook(["source", "chunk"])
582
+ })
583
+ );
584
+
600
585
  module.exports = CssLoadingRuntimeModule;