webpack 5.81.0 → 5.82.1

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.

Potentially problematic release.


This version of webpack might be problematic. Click here for more details.

Files changed (62) hide show
  1. package/bin/webpack.js +13 -2
  2. package/lib/Compilation.js +4 -1
  3. package/lib/CssModule.js +39 -7
  4. package/lib/DependenciesBlock.js +8 -0
  5. package/lib/FileSystemInfo.js +1 -1
  6. package/lib/HotModuleReplacementPlugin.js +3 -2
  7. package/lib/Module.js +3 -2
  8. package/lib/ModuleTypeConstants.js +90 -0
  9. package/lib/NormalModule.js +2 -1
  10. package/lib/RuntimeModule.js +4 -3
  11. package/lib/Template.js +2 -1
  12. package/lib/WebpackOptionsApply.js +33 -40
  13. package/lib/asset/AssetGenerator.js +4 -3
  14. package/lib/asset/AssetModulesPlugin.js +21 -11
  15. package/lib/asset/RawDataUrlModule.js +2 -1
  16. package/lib/cache/MemoryWithGcCachePlugin.js +2 -0
  17. package/lib/config/defaults.js +4 -2
  18. package/lib/container/FallbackModule.js +2 -1
  19. package/lib/container/RemoteModule.js +2 -1
  20. package/lib/css/CssGenerator.js +4 -0
  21. package/lib/css/CssLoadingRuntimeModule.js +9 -2
  22. package/lib/css/CssModulesPlugin.js +149 -39
  23. package/lib/css/CssParser.js +443 -319
  24. package/lib/css/walkCssTokens.js +118 -27
  25. package/lib/debug/ProfilingPlugin.js +2 -0
  26. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +1 -0
  27. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +4 -2
  28. package/lib/hmr/LazyCompilationPlugin.js +13 -4
  29. package/lib/javascript/BasicEvaluatedExpression.js +108 -1
  30. package/lib/javascript/JavascriptModulesPlugin.js +3 -2
  31. package/lib/javascript/JavascriptParser.js +132 -11
  32. package/lib/json/JsonData.js +25 -0
  33. package/lib/json/JsonGenerator.js +15 -3
  34. package/lib/json/JsonModulesPlugin.js +1 -0
  35. package/lib/json/JsonParser.js +2 -1
  36. package/lib/library/ModuleLibraryPlugin.js +2 -1
  37. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +3 -1
  38. package/lib/runtime/GetChunkFilenameRuntimeModule.js +4 -0
  39. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +22 -3
  40. package/lib/schemes/DataUriPlugin.js +4 -0
  41. package/lib/schemes/HttpUriPlugin.js +38 -0
  42. package/lib/sharing/ConsumeSharedModule.js +5 -2
  43. package/lib/sharing/ProvideSharedModule.js +2 -1
  44. package/lib/sharing/utils.js +293 -7
  45. package/lib/stats/DefaultStatsFactoryPlugin.js +7 -4
  46. package/lib/stats/DefaultStatsPrinterPlugin.js +25 -0
  47. package/lib/util/StackedCacheMap.js +6 -0
  48. package/lib/util/StringXor.js +51 -0
  49. package/lib/util/compileBooleanMatcher.js +31 -0
  50. package/lib/util/createHash.js +4 -3
  51. package/lib/util/deprecation.js +8 -0
  52. package/lib/util/identifier.js +4 -0
  53. package/lib/util/numberHash.js +75 -21
  54. package/lib/util/propertyAccess.js +5 -0
  55. package/lib/wasm/EnableWasmLoadingPlugin.js +4 -0
  56. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +1 -0
  57. package/lib/wasm-async/AsyncWebAssemblyParser.js +1 -1
  58. package/lib/web/JsonpChunkLoadingRuntimeModule.js +8 -4
  59. package/package.json +3 -3
  60. package/schemas/WebpackOptions.check.js +1 -1
  61. package/schemas/WebpackOptions.json +25 -0
  62. package/types.d.ts +181 -39
package/bin/webpack.js CHANGED
@@ -78,8 +78,19 @@ const runCli = cli => {
78
78
  const pkgPath = require.resolve(`${cli.package}/package.json`);
79
79
  // eslint-disable-next-line node/no-missing-require
80
80
  const pkg = require(pkgPath);
81
- // eslint-disable-next-line node/no-missing-require
82
- require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));
81
+
82
+ if (pkg.type === "module" || /\.mjs/i.test(pkg.bin[cli.binName])) {
83
+ // eslint-disable-next-line node/no-unsupported-features/es-syntax
84
+ import(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName])).catch(
85
+ error => {
86
+ console.error(error);
87
+ process.exitCode = 1;
88
+ }
89
+ );
90
+ } else {
91
+ // eslint-disable-next-line node/no-missing-require
92
+ require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));
93
+ }
83
94
  };
84
95
 
85
96
  /**
@@ -49,6 +49,7 @@ const ModuleProfile = require("./ModuleProfile");
49
49
  const ModuleRestoreError = require("./ModuleRestoreError");
50
50
  const ModuleStoreError = require("./ModuleStoreError");
51
51
  const ModuleTemplate = require("./ModuleTemplate");
52
+ const { WEBPACK_MODULE_TYPE_RUNTIME } = require("./ModuleTypeConstants");
52
53
  const RuntimeGlobals = require("./RuntimeGlobals");
53
54
  const RuntimeTemplate = require("./RuntimeTemplate");
54
55
  const Stats = require("./Stats");
@@ -2578,6 +2579,8 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
2578
2579
  p.calculate();
2579
2580
 
2580
2581
  const logger = this.getLogger("webpack.Compilation.ModuleProfile");
2582
+ // Avoid coverage problems due indirect changes
2583
+ /* istanbul ignore next */
2581
2584
  const logByValue = (value, msg) => {
2582
2585
  if (value > 1000) {
2583
2586
  logger.error(msg);
@@ -5119,7 +5122,7 @@ This prevents using hashes of each other and should be avoided.`);
5119
5122
  const usedIds = new Set();
5120
5123
 
5121
5124
  for (const module of this.modules) {
5122
- if (module.type === "runtime") continue;
5125
+ if (module.type === WEBPACK_MODULE_TYPE_RUNTIME) continue;
5123
5126
  const moduleId = chunkGraph.getModuleId(module);
5124
5127
  if (moduleId === null) continue;
5125
5128
  if (usedIds.has(moduleId)) {
package/lib/CssModule.js CHANGED
@@ -14,7 +14,13 @@ const makeSerializable = require("./util/makeSerializable");
14
14
  /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
15
15
  /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
16
16
 
17
- /** @typedef {NormalModuleCreateData & { cssLayer: string|undefined|null, supports: string|undefined|null, media: string|undefined|null }} CSSModuleCreateData */
17
+ /** @typedef {string|undefined} CssLayer */
18
+ /** @typedef {string|undefined} Supports */
19
+ /** @typedef {string|undefined} Media */
20
+ /** @typedef {[CssLayer?, Supports?, Media?]} InheritanceItem */
21
+ /** @typedef {Array<InheritanceItem>} Inheritance */
22
+
23
+ /** @typedef {NormalModuleCreateData & { cssLayer: CssLayer|null, supports: Supports|null, media: Media|null, inheritance: Inheritance|null }} CSSModuleCreateData */
18
24
 
19
25
  class CssModule extends NormalModule {
20
26
  /**
@@ -27,6 +33,7 @@ class CssModule extends NormalModule {
27
33
  this.cssLayer = options.cssLayer;
28
34
  this.supports = options.supports;
29
35
  this.media = options.media;
36
+ this.inheritance = options.inheritance;
30
37
  }
31
38
 
32
39
  /**
@@ -47,6 +54,17 @@ class CssModule extends NormalModule {
47
54
  identifier += `|${this.media}`;
48
55
  }
49
56
 
57
+ if (this.inheritance) {
58
+ const inheritance = this.inheritance.map(
59
+ (item, index) =>
60
+ `inheritance_${index}|${item[0] || ""}|${item[1] || ""}|${
61
+ item[2] || ""
62
+ }`
63
+ );
64
+
65
+ identifier += `|${inheritance.join("|")}`;
66
+ }
67
+
50
68
  return identifier;
51
69
  }
52
70
 
@@ -57,11 +75,21 @@ class CssModule extends NormalModule {
57
75
  readableIdentifier(requestShortener) {
58
76
  const readableIdentifier = super.readableIdentifier(requestShortener);
59
77
 
60
- return `css ${readableIdentifier}${
61
- this.cssLayer ? ` (layer ${this.cssLayer || ""})` : ""
62
- }${this.supports ? ` (supports ${this.supports || ""})` : ""}${
63
- this.media ? ` (media ${this.media || ""})` : ""
64
- }`;
78
+ let identifier = `css ${readableIdentifier}`;
79
+
80
+ if (this.cssLayer) {
81
+ identifier += ` (layer: ${this.cssLayer})`;
82
+ }
83
+
84
+ if (this.supports) {
85
+ identifier += ` (supports: ${this.supports})`;
86
+ }
87
+
88
+ if (this.media) {
89
+ identifier += ` (media: ${this.media})`;
90
+ }
91
+
92
+ return identifier;
65
93
  }
66
94
 
67
95
  /**
@@ -77,6 +105,7 @@ class CssModule extends NormalModule {
77
105
  this.cssLayer = m.cssLayer;
78
106
  this.supports = m.supports;
79
107
  this.media = m.media;
108
+ this.inheritance = m.inheritance;
80
109
  }
81
110
 
82
111
  /**
@@ -87,6 +116,7 @@ class CssModule extends NormalModule {
87
116
  write(this.cssLayer);
88
117
  write(this.supports);
89
118
  write(this.media);
119
+ write(this.inheritance);
90
120
  super.serialize(context);
91
121
  }
92
122
 
@@ -114,7 +144,8 @@ class CssModule extends NormalModule {
114
144
  resolveOptions: null,
115
145
  cssLayer: null,
116
146
  supports: null,
117
- media: null
147
+ media: null,
148
+ inheritance: null
118
149
  });
119
150
  obj.deserialize(context);
120
151
  return obj;
@@ -128,6 +159,7 @@ class CssModule extends NormalModule {
128
159
  this.cssLayer = read();
129
160
  this.supports = read();
130
161
  this.media = read();
162
+ this.inheritance = read();
131
163
  super.deserialize(context);
132
164
  }
133
165
  }
@@ -18,6 +18,14 @@ const makeSerializable = require("./util/makeSerializable");
18
18
 
19
19
  /** @typedef {(d: Dependency) => boolean} DependencyFilterFunction */
20
20
 
21
+ /**
22
+ * DependenciesBlock is the base class for all Module classes in webpack. It describes a
23
+ * "block" of dependencies which are pointers to other DependenciesBlock instances. For example
24
+ * when a Module has a CommonJs require statement, the DependencyBlock for the CommonJs module
25
+ * would be added as a dependency to the Module. DependenciesBlock is inherited by two types of classes:
26
+ * Module subclasses and AsyncDependenciesBlock subclasses. The only difference between the two is that
27
+ * AsyncDependenciesBlock subclasses are used for code-splitting (async boundary) and Module subclasses are not.
28
+ */
21
29
  class DependenciesBlock {
22
30
  constructor() {
23
31
  /** @type {Dependency[]} */
@@ -1464,7 +1464,7 @@ class FileSystemInfo {
1464
1464
  push({
1465
1465
  type: RBDT_DIRECTORY,
1466
1466
  context: undefined,
1467
- path: resultPath,
1467
+ path: /** @type {string} */ (resultPath),
1468
1468
  expected: undefined,
1469
1469
  issuer: job
1470
1470
  });
@@ -38,7 +38,8 @@ const {
38
38
  const {
39
39
  JAVASCRIPT_MODULE_TYPE_AUTO,
40
40
  JAVASCRIPT_MODULE_TYPE_DYNAMIC,
41
- JAVASCRIPT_MODULE_TYPE_ESM
41
+ JAVASCRIPT_MODULE_TYPE_ESM,
42
+ WEBPACK_MODULE_TYPE_RUNTIME
42
43
  } = require("./ModuleTypeConstants");
43
44
 
44
45
  /** @typedef {import("./Chunk")} Chunk */
@@ -564,7 +565,7 @@ class HotModuleReplacementPlugin {
564
565
  newRuntime
565
566
  );
566
567
  if (hash !== oldHash) {
567
- if (module.type === "runtime") {
568
+ if (module.type === WEBPACK_MODULE_TYPE_RUNTIME) {
568
569
  newRuntimeModules = newRuntimeModules || [];
569
570
  newRuntimeModules.push(
570
571
  /** @type {RuntimeModule} */ (module)
package/lib/Module.js CHANGED
@@ -28,6 +28,7 @@ const makeSerializable = require("./util/makeSerializable");
28
28
  /** @typedef {import("./ExportsInfo").UsageStateType} UsageStateType */
29
29
  /** @typedef {import("./FileSystemInfo")} FileSystemInfo */
30
30
  /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
31
+ /** @typedef {import("./ModuleTypeConstants").ModuleTypes} ModuleTypes */
31
32
  /** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
32
33
  /** @typedef {import("./RequestShortener")} RequestShortener */
33
34
  /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
@@ -129,14 +130,14 @@ const deprecatedNeedRebuild = util.deprecate(
129
130
 
130
131
  class Module extends DependenciesBlock {
131
132
  /**
132
- * @param {string} type the module type
133
+ * @param {ModuleTypes | ""} type the module type, when deserializing the type is not known and is an empty string
133
134
  * @param {string=} context an optional context
134
135
  * @param {string=} layer an optional layer in which the module is
135
136
  */
136
137
  constructor(type, context = null, layer = null) {
137
138
  super();
138
139
 
139
- /** @type {string} */
140
+ /** @type {ModuleTypes | ""} */
140
141
  this.type = type;
141
142
  /** @type {string | null} */
142
143
  this.context = context;
@@ -60,6 +60,88 @@ const CSS_MODULE_TYPE_GLOBAL = "css/global";
60
60
  */
61
61
  const CSS_MODULE_TYPE_MODULE = "css/module";
62
62
 
63
+ /**
64
+ * @type {Readonly<"asset">}
65
+ * This is the module type used for automatically choosing between `asset/inline`, `asset/resource` based on asset size limit (8096).
66
+ */
67
+ const ASSET_MODULE_TYPE = "asset";
68
+
69
+ /**
70
+ * @type {Readonly<"asset/inline">}
71
+ * This is the module type used for assets that are inlined as a data URI. This is the equivalent of `url-loader`.
72
+ */
73
+ const ASSET_MODULE_TYPE_INLINE = "asset/inline";
74
+
75
+ /**
76
+ * @type {Readonly<"asset/resource">}
77
+ * This is the module type used for assets that are copied to the output directory. This is the equivalent of `file-loader`.
78
+ */
79
+ const ASSET_MODULE_TYPE_RESOURCE = "asset/resource";
80
+
81
+ /**
82
+ * @type {Readonly<"asset/source">}
83
+ * This is the module type used for assets that are imported as source code. This is the equivalent of `raw-loader`.
84
+ */
85
+ const ASSET_MODULE_TYPE_SOURCE = "asset/source";
86
+
87
+ /**
88
+ * @type {Readonly<"asset/raw-data-url">}
89
+ * TODO: Document what this asset type is for. See css-loader tests for its usage.
90
+ */
91
+ const ASSET_MODULE_TYPE_RAW_DATA_URL = "asset/raw-data-url";
92
+
93
+ /**
94
+ * @type {Readonly<"runtime">}
95
+ * This is the module type used for the webpack runtime abstractions.
96
+ */
97
+ const WEBPACK_MODULE_TYPE_RUNTIME = "runtime";
98
+
99
+ /**
100
+ * @type {Readonly<"fallback-module">}
101
+ * This is the module type used for the ModuleFederation feature's FallbackModule class.
102
+ * TODO: Document this better.
103
+ */
104
+ const WEBPACK_MODULE_TYPE_FALLBACK = "fallback-module";
105
+
106
+ /**
107
+ * @type {Readonly<"remote-module">}
108
+ * This is the module type used for the ModuleFederation feature's RemoteModule class.
109
+ * TODO: Document this better.
110
+ */
111
+ const WEBPACK_MODULE_TYPE_REMOTE = "remote-module";
112
+
113
+ /**
114
+ * @type {Readonly<"provide-module">}
115
+ * This is the module type used for the ModuleFederation feature's ProvideModule class.
116
+ * TODO: Document this better.
117
+ */
118
+ const WEBPACK_MODULE_TYPE_PROVIDE = "provide-module";
119
+
120
+ /**
121
+ * @type {Readonly<"consume-shared-module">}
122
+ * This is the module type used for the ModuleFederation feature's ConsumeSharedModule class.
123
+ */
124
+ const WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE = "consume-shared-module";
125
+
126
+ /**
127
+ * @type {Readonly<"lazy-compilation-proxy">}
128
+ * Module type used for `experiments.lazyCompilation` feature. See `LazyCompilationPlugin` for more information.
129
+ */
130
+ const WEBPACK_MODULE_TYPE_LAZY_COMPILATION_PROXY = "lazy-compilation-proxy";
131
+
132
+ /** @typedef {"javascript/auto" | "javascript/dynamic" | "javascript/esm"} JavaScriptModuleTypes */
133
+ /** @typedef {"json"} JSONModuleType */
134
+ /** @typedef {"webassembly/async" | "webassembly/sync"} WebAssemblyModuleTypes */
135
+ /** @typedef {"css" | "css/global" | "css/module"} CSSModuleTypes */
136
+ /** @typedef {"asset" | "asset/inline" | "asset/resource" | "asset/source" | "asset/raw-data-url"} AssetModuleTypes */
137
+ /** @typedef {"runtime" | "fallback-module" | "remote-module" | "provide-module" | "consume-shared-module" | "lazy-compilation-proxy"} WebpackModuleTypes */
138
+ /** @typedef {JavaScriptModuleTypes | JSONModuleType | WebAssemblyModuleTypes | CSSModuleTypes | AssetModuleTypes | WebpackModuleTypes} ModuleTypes */
139
+
140
+ exports.ASSET_MODULE_TYPE = ASSET_MODULE_TYPE;
141
+ exports.ASSET_MODULE_TYPE_RAW_DATA_URL = ASSET_MODULE_TYPE_RAW_DATA_URL;
142
+ exports.ASSET_MODULE_TYPE_SOURCE = ASSET_MODULE_TYPE_SOURCE;
143
+ exports.ASSET_MODULE_TYPE_RESOURCE = ASSET_MODULE_TYPE_RESOURCE;
144
+ exports.ASSET_MODULE_TYPE_INLINE = ASSET_MODULE_TYPE_INLINE;
63
145
  exports.JAVASCRIPT_MODULE_TYPE_AUTO = JAVASCRIPT_MODULE_TYPE_AUTO;
64
146
  exports.JAVASCRIPT_MODULE_TYPE_DYNAMIC = JAVASCRIPT_MODULE_TYPE_DYNAMIC;
65
147
  exports.JAVASCRIPT_MODULE_TYPE_ESM = JAVASCRIPT_MODULE_TYPE_ESM;
@@ -69,3 +151,11 @@ exports.WEBASSEMBLY_MODULE_TYPE_SYNC = WEBASSEMBLY_MODULE_TYPE_SYNC;
69
151
  exports.CSS_MODULE_TYPE = CSS_MODULE_TYPE;
70
152
  exports.CSS_MODULE_TYPE_GLOBAL = CSS_MODULE_TYPE_GLOBAL;
71
153
  exports.CSS_MODULE_TYPE_MODULE = CSS_MODULE_TYPE_MODULE;
154
+ exports.WEBPACK_MODULE_TYPE_RUNTIME = WEBPACK_MODULE_TYPE_RUNTIME;
155
+ exports.WEBPACK_MODULE_TYPE_FALLBACK = WEBPACK_MODULE_TYPE_FALLBACK;
156
+ exports.WEBPACK_MODULE_TYPE_REMOTE = WEBPACK_MODULE_TYPE_REMOTE;
157
+ exports.WEBPACK_MODULE_TYPE_PROVIDE = WEBPACK_MODULE_TYPE_PROVIDE;
158
+ exports.WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE =
159
+ WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE;
160
+ exports.WEBPACK_MODULE_TYPE_LAZY_COMPILATION_PROXY =
161
+ WEBPACK_MODULE_TYPE_LAZY_COMPILATION_PROXY;
@@ -65,6 +65,7 @@ const memoize = require("./util/memoize");
65
65
  /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
66
66
  /** @typedef {import("./ModuleGraph")} ModuleGraph */
67
67
  /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
68
+ /** @typedef {import("./ModuleTypeConstants").JavaScriptModuleTypes} JavaScriptModuleTypes */
68
69
  /** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
69
70
  /** @typedef {import("./Parser")} Parser */
70
71
  /** @typedef {import("./RequestShortener")} RequestShortener */
@@ -201,7 +202,7 @@ makeSerializable(
201
202
  /**
202
203
  * @typedef {Object} NormalModuleCreateData
203
204
  * @property {string=} layer an optional layer in which the module is
204
- * @property {string} type module type
205
+ * @property {JavaScriptModuleTypes | ""} type module type. When deserializing, this is set to an empty string "".
205
206
  * @property {string} request request string
206
207
  * @property {string} userRequest request intended by user (without loaders from config)
207
208
  * @property {string} rawRequest request without resolving
@@ -8,6 +8,7 @@
8
8
  const { RawSource } = require("webpack-sources");
9
9
  const OriginalSource = require("webpack-sources").OriginalSource;
10
10
  const Module = require("./Module");
11
+ const { WEBPACK_MODULE_TYPE_RUNTIME } = require("./ModuleTypeConstants");
11
12
 
12
13
  /** @typedef {import("webpack-sources").Source} Source */
13
14
  /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
@@ -24,7 +25,7 @@ const Module = require("./Module");
24
25
  /** @typedef {import("./util/Hash")} Hash */
25
26
  /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
26
27
 
27
- const TYPES = new Set(["runtime"]);
28
+ const TYPES = new Set([WEBPACK_MODULE_TYPE_RUNTIME]);
28
29
 
29
30
  class RuntimeModule extends Module {
30
31
  /**
@@ -32,7 +33,7 @@ class RuntimeModule extends Module {
32
33
  * @param {number=} stage an optional stage
33
34
  */
34
35
  constructor(name, stage = 0) {
35
- super("runtime");
36
+ super(WEBPACK_MODULE_TYPE_RUNTIME);
36
37
  this.name = name;
37
38
  this.stage = stage;
38
39
  this.buildMeta = {};
@@ -137,7 +138,7 @@ class RuntimeModule extends Module {
137
138
  const generatedCode = this.getGeneratedCode();
138
139
  if (generatedCode) {
139
140
  sources.set(
140
- "runtime",
141
+ WEBPACK_MODULE_TYPE_RUNTIME,
141
142
  this.useSourceMap || this.useSimpleSourceMap
142
143
  ? new OriginalSource(generatedCode, this.identifier())
143
144
  : new RawSource(generatedCode)
package/lib/Template.js CHANGED
@@ -6,6 +6,7 @@
6
6
  "use strict";
7
7
 
8
8
  const { ConcatSource, PrefixSource } = require("webpack-sources");
9
+ const { WEBPACK_MODULE_TYPE_RUNTIME } = require("./ModuleTypeConstants");
9
10
 
10
11
  /** @typedef {import("webpack-sources").Source} Source */
11
12
  /** @typedef {import("../declarations/WebpackOptions").Output} OutputOptions */
@@ -362,7 +363,7 @@ class Template {
362
363
  runtimeSource = codeGenerationResults.getSource(
363
364
  module,
364
365
  renderContext.chunk.runtime,
365
- "runtime"
366
+ WEBPACK_MODULE_TYPE_RUNTIME
366
367
  );
367
368
  } else {
368
369
  const codeGenResult = module.codeGeneration({
@@ -119,47 +119,40 @@ class WebpackOptionsApply extends OptionsApply {
119
119
  if (options.externalsPresets.webAsync) {
120
120
  //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
121
121
  const ExternalsPlugin = require("./ExternalsPlugin");
122
- new ExternalsPlugin(
123
- "import",
124
- options.experiments.css
125
- ? ({ request, dependencyType }, callback) => {
126
- if (dependencyType === "url") {
127
- if (/^(\/\/|https?:\/\/)/.test(request))
128
- return callback(null, `asset ${request}`);
129
- } else if (dependencyType === "css-import") {
130
- if (/^(\/\/|https?:\/\/)/.test(request))
131
- return callback(null, `css-import ${request}`);
132
- } else if (/^(\/\/|https?:\/\/|std:)/.test(request)) {
133
- if (/^\.css(\?|$)/.test(request))
134
- return callback(null, `css-import ${request}`);
135
- return callback(null, `import ${request}`);
136
- }
137
- callback();
138
- }
139
- : /^(\/\/|https?:\/\/|std:)/
140
- ).apply(compiler);
122
+ new ExternalsPlugin("import", ({ request, dependencyType }, callback) => {
123
+ if (dependencyType === "url") {
124
+ if (/^(\/\/|https?:\/\/|#)/.test(request))
125
+ return callback(null, `asset ${request}`);
126
+ } else if (options.experiments.css && dependencyType === "css-import") {
127
+ if (/^(\/\/|https?:\/\/|#)/.test(request))
128
+ return callback(null, `css-import ${request}`);
129
+ } else if (
130
+ options.experiments.css &&
131
+ /^(\/\/|https?:\/\/|std:)/.test(request)
132
+ ) {
133
+ if (/^\.css(\?|$)/.test(request))
134
+ return callback(null, `css-import ${request}`);
135
+ return callback(null, `import ${request}`);
136
+ }
137
+ callback();
138
+ }).apply(compiler);
141
139
  } else if (options.externalsPresets.web) {
142
140
  //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
143
141
  const ExternalsPlugin = require("./ExternalsPlugin");
144
- new ExternalsPlugin(
145
- "module",
146
- options.experiments.css
147
- ? ({ request, dependencyType }, callback) => {
148
- if (dependencyType === "url") {
149
- if (/^(\/\/|https?:\/\/)/.test(request))
150
- return callback(null, `asset ${request}`);
151
- } else if (dependencyType === "css-import") {
152
- if (/^(\/\/|https?:\/\/)/.test(request))
153
- return callback(null, `css-import ${request}`);
154
- } else if (/^(\/\/|https?:\/\/|std:)/.test(request)) {
155
- if (/^\.css(\?|$)/.test(request))
156
- return callback(null, `css-import ${request}`);
157
- return callback(null, `module ${request}`);
158
- }
159
- callback();
160
- }
161
- : /^(\/\/|https?:\/\/|std:)/
162
- ).apply(compiler);
142
+ new ExternalsPlugin("module", ({ request, dependencyType }, callback) => {
143
+ if (dependencyType === "url") {
144
+ if (/^(\/\/|https?:\/\/|#)/.test(request))
145
+ return callback(null, `asset ${request}`);
146
+ } else if (options.experiments.css && dependencyType === "css-import") {
147
+ if (/^(\/\/|https?:\/\/|#)/.test(request))
148
+ return callback(null, `css-import ${request}`);
149
+ } else if (/^(\/\/|https?:\/\/|std:)/.test(request)) {
150
+ if (options.experiments.css && /^\.css((\?)|$)/.test(request))
151
+ return callback(null, `css-import ${request}`);
152
+ return callback(null, `module ${request}`);
153
+ }
154
+ callback();
155
+ }).apply(compiler);
163
156
  } else if (options.externalsPresets.node) {
164
157
  if (options.experiments.css) {
165
158
  //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
@@ -168,10 +161,10 @@ class WebpackOptionsApply extends OptionsApply {
168
161
  "module",
169
162
  ({ request, dependencyType }, callback) => {
170
163
  if (dependencyType === "url") {
171
- if (/^(\/\/|https?:\/\/)/.test(request))
164
+ if (/^(\/\/|https?:\/\/|#)/.test(request))
172
165
  return callback(null, `asset ${request}`);
173
166
  } else if (dependencyType === "css-import") {
174
- if (/^(\/\/|https?:\/\/)/.test(request))
167
+ if (/^(\/\/|https?:\/\/|#)/.test(request))
175
168
  return callback(null, `css-import ${request}`);
176
169
  } else if (/^(\/\/|https?:\/\/|std:)/.test(request)) {
177
170
  if (/^\.css(\?|$)/.test(request))
@@ -10,6 +10,7 @@ const path = require("path");
10
10
  const { RawSource } = require("webpack-sources");
11
11
  const ConcatenationScope = require("../ConcatenationScope");
12
12
  const Generator = require("../Generator");
13
+ const { ASSET_MODULE_TYPE } = require("../ModuleTypeConstants");
13
14
  const RuntimeGlobals = require("../RuntimeGlobals");
14
15
  const createHash = require("../util/createHash");
15
16
  const { makePathsRelative } = require("../util/identifier");
@@ -122,7 +123,7 @@ const decodeDataUriContent = (encoding, content) => {
122
123
  };
123
124
 
124
125
  const JS_TYPES = new Set(["javascript"]);
125
- const JS_AND_ASSET_TYPES = new Set(["javascript", "asset"]);
126
+ const JS_AND_ASSET_TYPES = new Set(["javascript", ASSET_MODULE_TYPE]);
126
127
  const DEFAULT_ENCODING = "base64";
127
128
 
128
129
  class AssetGenerator extends Generator {
@@ -228,7 +229,7 @@ class AssetGenerator extends Generator {
228
229
  }
229
230
  ) {
230
231
  switch (type) {
231
- case "asset":
232
+ case ASSET_MODULE_TYPE:
232
233
  return module.originalSource();
233
234
  default: {
234
235
  let content;
@@ -406,7 +407,7 @@ class AssetGenerator extends Generator {
406
407
  */
407
408
  getSize(module, type) {
408
409
  switch (type) {
409
- case "asset": {
410
+ case ASSET_MODULE_TYPE: {
410
411
  const originalSource = module.originalSource();
411
412
 
412
413
  if (!originalSource) {
@@ -5,6 +5,12 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const {
9
+ ASSET_MODULE_TYPE_RESOURCE,
10
+ ASSET_MODULE_TYPE_INLINE,
11
+ ASSET_MODULE_TYPE,
12
+ ASSET_MODULE_TYPE_SOURCE
13
+ } = require("../ModuleTypeConstants");
8
14
  const { cleverMerge } = require("../util/cleverMerge");
9
15
  const { compareModulesByIdentifier } = require("../util/comparators");
10
16
  const createSchemaValidation = require("../util/create-schema-validation");
@@ -61,7 +67,7 @@ const getAssetSourceGenerator = memoize(() =>
61
67
  require("./AssetSourceGenerator")
62
68
  );
63
69
 
64
- const type = "asset";
70
+ const type = ASSET_MODULE_TYPE;
65
71
  const plugin = "AssetModulesPlugin";
66
72
 
67
73
  class AssetModulesPlugin {
@@ -75,7 +81,7 @@ class AssetModulesPlugin {
75
81
  plugin,
76
82
  (compilation, { normalModuleFactory }) => {
77
83
  normalModuleFactory.hooks.createParser
78
- .for("asset")
84
+ .for(ASSET_MODULE_TYPE)
79
85
  .tap(plugin, parserOptions => {
80
86
  validateParserOptions(parserOptions);
81
87
  parserOptions = cleverMerge(
@@ -96,35 +102,39 @@ class AssetModulesPlugin {
96
102
  return new AssetParser(dataUrlCondition);
97
103
  });
98
104
  normalModuleFactory.hooks.createParser
99
- .for("asset/inline")
105
+ .for(ASSET_MODULE_TYPE_INLINE)
100
106
  .tap(plugin, parserOptions => {
101
107
  const AssetParser = getAssetParser();
102
108
 
103
109
  return new AssetParser(true);
104
110
  });
105
111
  normalModuleFactory.hooks.createParser
106
- .for("asset/resource")
112
+ .for(ASSET_MODULE_TYPE_RESOURCE)
107
113
  .tap(plugin, parserOptions => {
108
114
  const AssetParser = getAssetParser();
109
115
 
110
116
  return new AssetParser(false);
111
117
  });
112
118
  normalModuleFactory.hooks.createParser
113
- .for("asset/source")
119
+ .for(ASSET_MODULE_TYPE_SOURCE)
114
120
  .tap(plugin, parserOptions => {
115
121
  const AssetSourceParser = getAssetSourceParser();
116
122
 
117
123
  return new AssetSourceParser();
118
124
  });
119
125
 
120
- for (const type of ["asset", "asset/inline", "asset/resource"]) {
126
+ for (const type of [
127
+ ASSET_MODULE_TYPE,
128
+ ASSET_MODULE_TYPE_INLINE,
129
+ ASSET_MODULE_TYPE_RESOURCE
130
+ ]) {
121
131
  normalModuleFactory.hooks.createGenerator
122
132
  .for(type)
123
133
  .tap(plugin, generatorOptions => {
124
134
  validateGeneratorOptions[type](generatorOptions);
125
135
 
126
136
  let dataUrl = undefined;
127
- if (type !== "asset/resource") {
137
+ if (type !== ASSET_MODULE_TYPE_RESOURCE) {
128
138
  dataUrl = generatorOptions.dataUrl;
129
139
  if (!dataUrl || typeof dataUrl === "object") {
130
140
  dataUrl = {
@@ -138,7 +148,7 @@ class AssetModulesPlugin {
138
148
  let filename = undefined;
139
149
  let publicPath = undefined;
140
150
  let outputPath = undefined;
141
- if (type !== "asset/inline") {
151
+ if (type !== ASSET_MODULE_TYPE_INLINE) {
142
152
  filename = generatorOptions.filename;
143
153
  publicPath = generatorOptions.publicPath;
144
154
  outputPath = generatorOptions.outputPath;
@@ -156,7 +166,7 @@ class AssetModulesPlugin {
156
166
  });
157
167
  }
158
168
  normalModuleFactory.hooks.createGenerator
159
- .for("asset/source")
169
+ .for(ASSET_MODULE_TYPE_SOURCE)
160
170
  .tap(plugin, () => {
161
171
  const AssetSourceGenerator = getAssetSourceGenerator();
162
172
 
@@ -169,7 +179,7 @@ class AssetModulesPlugin {
169
179
 
170
180
  const modules = chunkGraph.getOrderedChunkModulesIterableBySourceType(
171
181
  chunk,
172
- "asset",
182
+ ASSET_MODULE_TYPE,
173
183
  compareModulesByIdentifier
174
184
  );
175
185
  if (modules) {
@@ -207,7 +217,7 @@ class AssetModulesPlugin {
207
217
  "AssetModulesPlugin",
208
218
  (options, context) => {
209
219
  const { codeGenerationResult } = options;
210
- const source = codeGenerationResult.sources.get("asset");
220
+ const source = codeGenerationResult.sources.get(ASSET_MODULE_TYPE);
211
221
  if (source === undefined) return;
212
222
  context.assets.set(codeGenerationResult.data.get("filename"), {
213
223
  source,
@@ -7,6 +7,7 @@
7
7
 
8
8
  const { RawSource } = require("webpack-sources");
9
9
  const Module = require("../Module");
10
+ const { ASSET_MODULE_TYPE_RAW_DATA_URL } = require("../ModuleTypeConstants");
10
11
  const RuntimeGlobals = require("../RuntimeGlobals");
11
12
  const makeSerializable = require("../util/makeSerializable");
12
13
 
@@ -33,7 +34,7 @@ class RawDataUrlModule extends Module {
33
34
  * @param {string=} readableIdentifier readable identifier
34
35
  */
35
36
  constructor(url, identifier, readableIdentifier) {
36
- super("asset/raw-data-url", null);
37
+ super(ASSET_MODULE_TYPE_RAW_DATA_URL, null);
37
38
  this.url = url;
38
39
  this.urlBuffer = url ? Buffer.from(url) : undefined;
39
40
  this.identifierStr = identifier || this.url;
@@ -34,6 +34,8 @@ class MemoryWithGcCachePlugin {
34
34
  generation++;
35
35
  let clearedEntries = 0;
36
36
  let lastClearedIdentifier;
37
+ // Avoid coverage problems due indirect changes
38
+ /* istanbul ignore next */
37
39
  for (const [identifier, entry] of oldCache) {
38
40
  if (entry.until > generation) break;
39
41