webpack 5.90.2 → 5.91.0

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 (182) hide show
  1. package/README.md +5 -5
  2. package/bin/webpack.js +5 -1
  3. package/lib/APIPlugin.js +8 -4
  4. package/lib/AutomaticPrefetchPlugin.js +1 -1
  5. package/lib/BannerPlugin.js +3 -1
  6. package/lib/Cache.js +7 -1
  7. package/lib/CacheFacade.js +3 -3
  8. package/lib/ChunkGraph.js +32 -18
  9. package/lib/ChunkGroup.js +14 -14
  10. package/lib/CleanPlugin.js +7 -5
  11. package/lib/Compilation.js +263 -94
  12. package/lib/Compiler.js +199 -83
  13. package/lib/ConditionalInitFragment.js +4 -5
  14. package/lib/ContextModule.js +4 -1
  15. package/lib/ContextModuleFactory.js +4 -2
  16. package/lib/ContextReplacementPlugin.js +3 -2
  17. package/lib/CssModule.js +0 -3
  18. package/lib/DefinePlugin.js +4 -2
  19. package/lib/DelegatedModule.js +2 -1
  20. package/lib/Dependency.js +4 -2
  21. package/lib/DependencyTemplate.js +7 -2
  22. package/lib/DllModule.js +3 -1
  23. package/lib/DllReferencePlugin.js +6 -2
  24. package/lib/EntryOptionPlugin.js +4 -1
  25. package/lib/EntryPlugin.js +6 -1
  26. package/lib/Entrypoint.js +1 -1
  27. package/lib/ExportsInfo.js +1 -4
  28. package/lib/ExternalModule.js +120 -25
  29. package/lib/ExternalModuleFactoryPlugin.js +37 -2
  30. package/lib/FileSystemInfo.js +1 -1
  31. package/lib/Generator.js +2 -1
  32. package/lib/HookWebpackError.js +2 -2
  33. package/lib/InitFragment.js +5 -3
  34. package/lib/LibManifestPlugin.js +15 -7
  35. package/lib/Module.js +54 -5
  36. package/lib/ModuleFilenameHelpers.js +1 -1
  37. package/lib/ModuleGraph.js +56 -27
  38. package/lib/ModuleGraphConnection.js +2 -1
  39. package/lib/MultiCompiler.js +61 -17
  40. package/lib/NodeStuffPlugin.js +14 -3
  41. package/lib/NormalModule.js +239 -86
  42. package/lib/NormalModuleFactory.js +163 -32
  43. package/lib/NormalModuleReplacementPlugin.js +5 -1
  44. package/lib/ProvidePlugin.js +3 -1
  45. package/lib/RawModule.js +4 -2
  46. package/lib/ResolverFactory.js +5 -1
  47. package/lib/RuntimeModule.js +6 -5
  48. package/lib/RuntimePlugin.js +1 -0
  49. package/lib/RuntimeTemplate.js +102 -34
  50. package/lib/SourceMapDevToolPlugin.js +4 -1
  51. package/lib/Stats.js +12 -5
  52. package/lib/TemplatedPathPlugin.js +32 -6
  53. package/lib/Watching.js +67 -60
  54. package/lib/WebpackError.js +6 -6
  55. package/lib/WebpackOptionsApply.js +18 -5
  56. package/lib/asset/RawDataUrlModule.js +5 -2
  57. package/lib/async-modules/AwaitDependenciesInitFragment.js +2 -2
  58. package/lib/buildChunkGraph.js +220 -392
  59. package/lib/cache/IdleFileCachePlugin.js +8 -3
  60. package/lib/cache/MemoryCachePlugin.js +1 -1
  61. package/lib/cache/MemoryWithGcCachePlugin.js +6 -2
  62. package/lib/cache/PackFileCacheStrategy.js +49 -16
  63. package/lib/cache/ResolverCachePlugin.js +14 -6
  64. package/lib/cache/getLazyHashedEtag.js +1 -1
  65. package/lib/config/defaults.js +24 -1
  66. package/lib/config/normalization.js +3 -1
  67. package/lib/container/ContainerEntryDependency.js +2 -1
  68. package/lib/container/ContainerEntryModule.js +5 -2
  69. package/lib/container/ContainerPlugin.js +14 -10
  70. package/lib/container/FallbackModule.js +3 -2
  71. package/lib/container/RemoteModule.js +2 -1
  72. package/lib/container/RemoteRuntimeModule.js +12 -3
  73. package/lib/css/CssExportsGenerator.js +34 -17
  74. package/lib/css/CssGenerator.js +20 -2
  75. package/lib/css/CssLoadingRuntimeModule.js +212 -96
  76. package/lib/css/CssModulesPlugin.js +47 -13
  77. package/lib/debug/ProfilingPlugin.js +27 -2
  78. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +9 -5
  79. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +4 -1
  80. package/lib/dependencies/CommonJsDependencyHelpers.js +2 -1
  81. package/lib/dependencies/CommonJsExportRequireDependency.js +33 -18
  82. package/lib/dependencies/CommonJsExportsDependency.js +13 -5
  83. package/lib/dependencies/CommonJsExportsParserPlugin.js +20 -15
  84. package/lib/dependencies/CommonJsImportsParserPlugin.js +1 -2
  85. package/lib/dependencies/ContextDependencyHelpers.js +49 -29
  86. package/lib/dependencies/ContextElementDependency.js +8 -1
  87. package/lib/dependencies/CssLocalIdentifierDependency.js +63 -8
  88. package/lib/dependencies/CssUrlDependency.js +5 -3
  89. package/lib/dependencies/ExportsInfoDependency.js +4 -3
  90. package/lib/dependencies/ExternalModuleInitFragment.js +5 -3
  91. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +4 -4
  92. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +43 -23
  93. package/lib/dependencies/HarmonyExportHeaderDependency.js +1 -1
  94. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +73 -32
  95. package/lib/dependencies/HarmonyExportInitFragment.js +10 -2
  96. package/lib/dependencies/HarmonyImportDependency.js +28 -12
  97. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +44 -16
  98. package/lib/dependencies/HarmonyImportSideEffectDependency.js +7 -6
  99. package/lib/dependencies/HarmonyImportSpecifierDependency.js +21 -10
  100. package/lib/dependencies/ImportDependency.js +9 -2
  101. package/lib/dependencies/ImportEagerDependency.js +4 -2
  102. package/lib/dependencies/ImportMetaContextDependency.js +7 -0
  103. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +25 -14
  104. package/lib/dependencies/ImportParserPlugin.js +12 -4
  105. package/lib/dependencies/ImportWeakDependency.js +4 -2
  106. package/lib/dependencies/LoaderDependency.js +2 -1
  107. package/lib/dependencies/LoaderImportDependency.js +2 -1
  108. package/lib/dependencies/ModuleDependency.js +4 -5
  109. package/lib/dependencies/PureExpressionDependency.js +4 -1
  110. package/lib/dependencies/RequireContextPlugin.js +1 -1
  111. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +26 -14
  112. package/lib/dependencies/RequireEnsureDependency.js +1 -1
  113. package/lib/dependencies/URLDependency.js +7 -4
  114. package/lib/dependencies/WorkerDependency.js +1 -1
  115. package/lib/dependencies/WorkerPlugin.js +6 -5
  116. package/lib/dependencies/getFunctionExpression.js +3 -1
  117. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +2 -1
  118. package/lib/hmr/LazyCompilationPlugin.js +2 -1
  119. package/lib/javascript/BasicEvaluatedExpression.js +2 -2
  120. package/lib/javascript/ChunkHelpers.js +2 -2
  121. package/lib/javascript/JavascriptParser.js +169 -57
  122. package/lib/javascript/JavascriptParserHelpers.js +1 -1
  123. package/lib/javascript/StartupHelpers.js +22 -5
  124. package/lib/logging/Logger.js +27 -2
  125. package/lib/logging/createConsoleLogger.js +11 -7
  126. package/lib/node/NodeEnvironmentPlugin.js +13 -7
  127. package/lib/node/NodeWatchFileSystem.js +37 -26
  128. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +2 -1
  129. package/lib/node/RequireChunkLoadingRuntimeModule.js +2 -1
  130. package/lib/node/nodeConsole.js +24 -1
  131. package/lib/optimize/AggressiveSplittingPlugin.js +1 -0
  132. package/lib/optimize/ConcatenatedModule.js +140 -55
  133. package/lib/optimize/EnsureChunkConditionsPlugin.js +1 -1
  134. package/lib/optimize/InnerGraph.js +7 -2
  135. package/lib/optimize/InnerGraphPlugin.js +36 -13
  136. package/lib/optimize/ModuleConcatenationPlugin.js +12 -2
  137. package/lib/optimize/RemoveParentModulesPlugin.js +1 -0
  138. package/lib/optimize/RuntimeChunkPlugin.js +6 -1
  139. package/lib/optimize/SideEffectsFlagPlugin.js +46 -15
  140. package/lib/optimize/SplitChunksPlugin.js +2 -2
  141. package/lib/performance/SizeLimitsPlugin.js +11 -0
  142. package/lib/rules/ObjectMatcherRulePlugin.js +4 -0
  143. package/lib/runtime/EnsureChunkRuntimeModule.js +2 -1
  144. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +2 -1
  145. package/lib/serialization/FileMiddleware.js +1 -0
  146. package/lib/serialization/ObjectMiddleware.js +2 -0
  147. package/lib/serialization/Serializer.js +19 -0
  148. package/lib/sharing/ConsumeSharedModule.js +3 -2
  149. package/lib/sharing/ConsumeSharedPlugin.js +17 -3
  150. package/lib/sharing/ConsumeSharedRuntimeModule.js +9 -2
  151. package/lib/sharing/ProvideSharedModule.js +2 -1
  152. package/lib/sharing/ProvideSharedPlugin.js +12 -5
  153. package/lib/sharing/resolveMatchedConfigs.js +2 -2
  154. package/lib/sharing/utils.js +13 -6
  155. package/lib/util/StackedCacheMap.js +26 -0
  156. package/lib/util/WeakTupleMap.js +57 -13
  157. package/lib/util/cleverMerge.js +24 -11
  158. package/lib/util/comparators.js +34 -14
  159. package/lib/util/conventions.js +129 -0
  160. package/lib/util/fs.js +379 -65
  161. package/lib/util/hash/BatchedHash.js +3 -0
  162. package/lib/util/hash/xxhash64.js +2 -2
  163. package/lib/util/memoize.js +2 -0
  164. package/lib/util/runtime.js +1 -1
  165. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +4 -2
  166. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +15 -6
  167. package/lib/wasm-sync/WebAssemblyGenerator.js +27 -6
  168. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +2 -1
  169. package/lib/wasm-sync/WebAssemblyParser.js +7 -4
  170. package/lib/web/JsonpChunkLoadingRuntimeModule.js +2 -1
  171. package/lib/webpack.js +11 -5
  172. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +2 -1
  173. package/package.json +25 -26
  174. package/schemas/WebpackOptions.check.js +1 -1
  175. package/schemas/WebpackOptions.json +69 -8
  176. package/schemas/plugins/BannerPlugin.check.js +1 -1
  177. package/schemas/plugins/BannerPlugin.json +5 -1
  178. package/schemas/plugins/css/CssAutoGeneratorOptions.check.js +1 -1
  179. package/schemas/plugins/css/CssGeneratorOptions.check.js +1 -1
  180. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.js +1 -1
  181. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +1 -1
  182. package/types.d.ts +1772 -673
@@ -38,11 +38,13 @@ const makeSerializable = require("./util/makeSerializable");
38
38
  /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
39
39
  /** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
40
40
  /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
41
+ /** @typedef {import("./Module").SourceTypes} SourceTypes */
41
42
  /** @typedef {import("./ModuleGraph")} ModuleGraph */
42
43
  /** @typedef {import("./RequestShortener")} RequestShortener */
43
44
  /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
44
45
  /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
45
46
  /** @typedef {import("./dependencies/ContextElementDependency")} ContextElementDependency */
47
+ /** @typedef {import("./javascript/JavascriptParser").Attributes} Attributes */
46
48
  /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
47
49
  /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
48
50
  /** @template T @typedef {import("./util/LazySet")<T>} LazySet<T> */
@@ -65,6 +67,7 @@ const makeSerializable = require("./util/makeSerializable");
65
67
  * @property {string=} category
66
68
  * @property {(string[][] | null)=} referencedExports exports referenced from modules (won't be mangled)
67
69
  * @property {string=} layer
70
+ * @property {Attributes=} attributes
68
71
  */
69
72
 
70
73
  /**
@@ -144,7 +147,7 @@ class ContextModule extends Module {
144
147
  }
145
148
 
146
149
  /**
147
- * @returns {Set<string>} types available (do not mutate)
150
+ * @returns {SourceTypes} types available (do not mutate)
148
151
  */
149
152
  getSourceTypes() {
150
153
  return TYPES;
@@ -289,7 +289,8 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
289
289
  exclude,
290
290
  referencedExports,
291
291
  category,
292
- typePrefix
292
+ typePrefix,
293
+ attributes
293
294
  } = options;
294
295
  if (!regExp || !resource) return callback(null, []);
295
296
 
@@ -365,7 +366,8 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
365
366
  typePrefix,
366
367
  category,
367
368
  referencedExports,
368
- obj.context
369
+ obj.context,
370
+ attributes
369
371
  );
370
372
  dep.optional = true;
371
373
  return dep;
@@ -9,6 +9,7 @@ const ContextElementDependency = require("./dependencies/ContextElementDependenc
9
9
  const { join } = require("./util/fs");
10
10
 
11
11
  /** @typedef {import("./Compiler")} Compiler */
12
+ /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
12
13
 
13
14
  class ContextReplacementPlugin {
14
15
  /**
@@ -104,7 +105,7 @@ class ContextReplacementPlugin {
104
105
  result.resource = newContentResource;
105
106
  } else {
106
107
  result.resource = join(
107
- compiler.inputFileSystem,
108
+ /** @type {InputFileSystem} */ (compiler.inputFileSystem),
108
109
  result.resource,
109
110
  newContentResource
110
111
  );
@@ -132,7 +133,7 @@ class ContextReplacementPlugin {
132
133
  ) {
133
134
  // When the function changed it to an relative path
134
135
  result.resource = join(
135
- compiler.inputFileSystem,
136
+ /** @type {InputFileSystem} */ (compiler.inputFileSystem),
136
137
  origResource,
137
138
  result.resource
138
139
  );
package/lib/CssModule.js CHANGED
@@ -151,9 +151,6 @@ class CssModule extends NormalModule {
151
151
  return obj;
152
152
  }
153
153
 
154
- /**
155
- * @param {ObjectDeserializerContext} context context
156
- */
157
154
  deserialize(context) {
158
155
  const { read } = context;
159
156
  this.cssLayer = read();
@@ -308,8 +308,10 @@ const PLUGIN_NAME = "DefinePlugin";
308
308
  const VALUE_DEP_PREFIX = `webpack/${PLUGIN_NAME} `;
309
309
  const VALUE_DEP_MAIN = `webpack/${PLUGIN_NAME}_hash`;
310
310
  const TYPEOF_OPERATOR_REGEXP = /^typeof\s+/;
311
- const WEBPACK_REQUIRE_FUNCTION_REGEXP = /__webpack_require__\s*(!?\.)/;
312
- const WEBPACK_REQUIRE_IDENTIFIER_REGEXP = /__webpack_require__/;
311
+ const WEBPACK_REQUIRE_FUNCTION_REGEXP = new RegExp(
312
+ `${RuntimeGlobals.require}\\s*(!?\\.)`
313
+ );
314
+ const WEBPACK_REQUIRE_IDENTIFIER_REGEXP = new RegExp(RuntimeGlobals.require);
313
315
 
314
316
  class DefinePlugin {
315
317
  /**
@@ -25,6 +25,7 @@ const makeSerializable = require("./util/makeSerializable");
25
25
  /** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
26
26
  /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
27
27
  /** @typedef {import("./Module").SourceContext} SourceContext */
28
+ /** @typedef {import("./Module").SourceTypes} SourceTypes */
28
29
  /** @typedef {import("./RequestShortener")} RequestShortener */
29
30
  /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
30
31
  /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
@@ -66,7 +67,7 @@ class DelegatedModule extends Module {
66
67
  }
67
68
 
68
69
  /**
69
- * @returns {Set<string>} types available (do not mutate)
70
+ * @returns {SourceTypes} types available (do not mutate)
70
71
  */
71
72
  getSourceTypes() {
72
73
  return TYPES;
package/lib/Dependency.js CHANGED
@@ -5,6 +5,7 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const RawModule = require("./RawModule");
8
9
  const memoize = require("./util/memoize");
9
10
 
10
11
  /** @typedef {import("webpack-sources").Source} Source */
@@ -80,10 +81,11 @@ const memoize = require("./util/memoize");
80
81
  * @property {boolean=} canMangle when false, referenced export can not be mangled, defaults to true
81
82
  */
82
83
 
84
+ /** @typedef {function(ModuleGraphConnection, RuntimeSpec): ConnectionState} GetConditionFn */
85
+
83
86
  const TRANSITIVE = Symbol("transitive");
84
87
 
85
88
  const getIgnoredModule = memoize(() => {
86
- const RawModule = require("./RawModule");
87
89
  return new RawModule("/* (ignored) */", `ignored`, `(ignored)`);
88
90
  });
89
91
 
@@ -235,7 +237,7 @@ class Dependency {
235
237
 
236
238
  /**
237
239
  * @param {ModuleGraph} moduleGraph module graph
238
- * @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
240
+ * @returns {null | false | GetConditionFn} function to determine if the connection is active
239
241
  */
240
242
  getCondition(moduleGraph) {
241
243
  return null;
@@ -13,18 +13,23 @@
13
13
  /** @typedef {import("./Dependency").RuntimeSpec} RuntimeSpec */
14
14
  /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
15
15
  /** @typedef {import("./Generator").GenerateContext} GenerateContext */
16
- /** @template T @typedef {import("./InitFragment")<T>} InitFragment */
17
16
  /** @typedef {import("./Module")} Module */
17
+ /** @typedef {import("./Module").RuntimeRequirements} RuntimeRequirements */
18
18
  /** @typedef {import("./ModuleGraph")} ModuleGraph */
19
19
  /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
20
20
 
21
+ /**
22
+ * @template T
23
+ * @typedef {import("./InitFragment")<T>} InitFragment
24
+ */
25
+
21
26
  /**
22
27
  * @typedef {Object} DependencyTemplateContext
23
28
  * @property {RuntimeTemplate} runtimeTemplate the runtime template
24
29
  * @property {DependencyTemplates} dependencyTemplates the dependency templates
25
30
  * @property {ModuleGraph} moduleGraph the module graph
26
31
  * @property {ChunkGraph} chunkGraph the chunk graph
27
- * @property {Set<string>} runtimeRequirements the requirements for runtime
32
+ * @property {RuntimeRequirements} runtimeRequirements the requirements for runtime
28
33
  * @property {Module} module current module
29
34
  * @property {RuntimeSpec} runtime current runtime, for which code is generated
30
35
  * @property {RuntimeSpec[]} [runtimes] current runtimes, for which code is generated
package/lib/DllModule.js CHANGED
@@ -22,6 +22,7 @@ const makeSerializable = require("./util/makeSerializable");
22
22
  /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
23
23
  /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
24
24
  /** @typedef {import("./Module").SourceContext} SourceContext */
25
+ /** @typedef {import("./Module").SourceTypes} SourceTypes */
25
26
  /** @typedef {import("./RequestShortener")} RequestShortener */
26
27
  /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
27
28
  /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
@@ -47,12 +48,13 @@ class DllModule extends Module {
47
48
  super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, context);
48
49
 
49
50
  // Info from Factory
51
+ /** @type {Dependency[]} */
50
52
  this.dependencies = dependencies;
51
53
  this.name = name;
52
54
  }
53
55
 
54
56
  /**
55
- * @returns {Set<string>} types available (do not mutate)
57
+ * @returns {SourceTypes} types available (do not mutate)
56
58
  */
57
59
  getSourceTypes() {
58
60
  return TYPES;
@@ -17,6 +17,7 @@ const makePathsRelative = require("./util/identifier").makePathsRelative;
17
17
  /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */
18
18
  /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsManifest} DllReferencePluginOptionsManifest */
19
19
  /** @typedef {import("./Compiler")} Compiler */
20
+ /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
20
21
 
21
22
  const validate = createSchemaValidation(
22
23
  require("../schemas/plugins/DllReferencePlugin.check.js"),
@@ -60,7 +61,8 @@ class DllReferencePlugin {
60
61
  if ("manifest" in this.options) {
61
62
  const manifest = this.options.manifest;
62
63
  if (typeof manifest === "string") {
63
- compiler.inputFileSystem.readFile(manifest, (err, result) => {
64
+ /** @type {InputFileSystem} */
65
+ (compiler.inputFileSystem).readFile(manifest, (err, result) => {
64
66
  if (err) return callback(err);
65
67
  const data = {
66
68
  path: manifest,
@@ -70,7 +72,9 @@ class DllReferencePlugin {
70
72
  // Catch errors parsing the manifest so that blank
71
73
  // or malformed manifest files don't kill the process.
72
74
  try {
73
- data.data = parseJson(result.toString("utf-8"));
75
+ data.data = parseJson(
76
+ /** @type {Buffer} */ (result).toString("utf-8")
77
+ );
74
78
  } catch (e) {
75
79
  // Store the error in the params so that it can
76
80
  // be added as a compilation error later on.
@@ -41,7 +41,10 @@ class EntryOptionPlugin {
41
41
  name,
42
42
  desc
43
43
  );
44
- for (const entry of desc.import) {
44
+ const descImport =
45
+ /** @type {Exclude<EntryDescription["import"], undefined>} */
46
+ (desc.import);
47
+ for (const entry of descImport) {
45
48
  new EntryPlugin(context, entry, options).apply(compiler);
46
49
  }
47
50
  }
@@ -59,7 +59,12 @@ class EntryPlugin {
59
59
  static createDependency(entry, options) {
60
60
  const dep = new EntryDependency(entry);
61
61
  // TODO webpack 6 remove string option
62
- dep.loc = { name: typeof options === "object" ? options.name : options };
62
+ dep.loc = {
63
+ name:
64
+ typeof options === "object"
65
+ ? /** @type {string} */ (options.name)
66
+ : options
67
+ };
63
68
  return dep;
64
69
  }
65
70
  }
package/lib/Entrypoint.js CHANGED
@@ -89,7 +89,7 @@ class Entrypoint extends ChunkGroup {
89
89
  /**
90
90
  * @param {Chunk} oldChunk chunk to be replaced
91
91
  * @param {Chunk} newChunk New chunk that will be replaced with
92
- * @returns {boolean} returns true if the replacement was successful
92
+ * @returns {boolean | undefined} returns true if the replacement was successful
93
93
  */
94
94
  replaceChunk(oldChunk, newChunk) {
95
95
  if (this._runtimeChunk === oldChunk) this._runtimeChunk = newChunk;
@@ -207,9 +207,6 @@ class ExportsInfo {
207
207
  this._redirectTo.setHasUseInfo();
208
208
  } else {
209
209
  this._otherExportsInfo.setHasUseInfo();
210
- if (this._otherExportsInfo.canMangleUse === undefined) {
211
- this._otherExportsInfo.canMangleUse = true;
212
- }
213
210
  }
214
211
  }
215
212
 
@@ -670,7 +667,7 @@ class ExportsInfo {
670
667
  }
671
668
 
672
669
  /**
673
- * @param {string | string[]} name the export name
670
+ * @param {string | string[] | undefined} name the export name
674
671
  * @param {RuntimeSpec} runtime check usage for this runtime only
675
672
  * @returns {string | string[] | false} the used name
676
673
  */
@@ -29,17 +29,23 @@ const { register } = require("./util/serialization");
29
29
  /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
30
30
  /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
31
31
  /** @typedef {import("./ExportsInfo")} ExportsInfo */
32
+ /** @typedef {import("./Generator").GenerateContext} GenerateContext */
33
+ /** @typedef {import("./Module").BuildInfo} BuildInfo */
32
34
  /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
33
35
  /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
34
36
  /** @typedef {import("./Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
35
37
  /** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
36
38
  /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
39
+ /** @typedef {import("./Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
40
+ /** @typedef {import("./Module").SourceTypes} SourceTypes */
41
+ /** @typedef {import("./ModuleGraph")} ModuleGraph */
37
42
  /** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
38
43
  /** @typedef {import("./RequestShortener")} RequestShortener */
39
44
  /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
40
45
  /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
41
46
  /** @typedef {import("./WebpackError")} WebpackError */
42
47
  /** @typedef {import("./javascript/JavascriptModulesPlugin").ChunkRenderContext} ChunkRenderContext */
48
+ /** @typedef {import("./javascript/JavascriptParser").Attributes} Attributes */
43
49
  /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
44
50
  /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
45
51
  /** @typedef {import("./util/Hash")} Hash */
@@ -47,13 +53,18 @@ const { register } = require("./util/serialization");
47
53
  /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
48
54
  /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
49
55
 
56
+ /** @typedef {{ attributes?: Attributes }} ImportDependencyMeta */
57
+ /** @typedef {{ layer?: string, supports?: string, media?: string }} CssImportDependencyMeta */
58
+
59
+ /** @typedef {ImportDependencyMeta | CssImportDependencyMeta} DependencyMeta */
60
+
50
61
  /**
51
62
  * @typedef {Object} SourceData
52
63
  * @property {boolean=} iife
53
64
  * @property {string=} init
54
65
  * @property {string} expression
55
66
  * @property {InitFragment<ChunkRenderContext>[]=} chunkInitFragments
56
- * @property {ReadonlySet<string>=} runtimeRequirements
67
+ * @property {ReadOnlyRuntimeRequirements=} runtimeRequirements
57
68
  */
58
69
 
59
70
  const TYPES = new Set(["javascript"]);
@@ -140,43 +151,60 @@ const getSourceForCommonJsExternalInNodeModule = (
140
151
  /**
141
152
  * @param {string|string[]} moduleAndSpecifiers the module request
142
153
  * @param {RuntimeTemplate} runtimeTemplate the runtime template
154
+ * @param {ImportDependencyMeta=} dependencyMeta the dependency meta
143
155
  * @returns {SourceData} the generated source
144
156
  */
145
- const getSourceForImportExternal = (moduleAndSpecifiers, runtimeTemplate) => {
157
+ const getSourceForImportExternal = (
158
+ moduleAndSpecifiers,
159
+ runtimeTemplate,
160
+ dependencyMeta
161
+ ) => {
146
162
  const importName = runtimeTemplate.outputOptions.importFunctionName;
147
163
  if (!runtimeTemplate.supportsDynamicImport() && importName === "import") {
148
164
  throw new Error(
149
165
  "The target environment doesn't support 'import()' so it's not possible to use external type 'import'"
150
166
  );
151
167
  }
168
+ const attributes =
169
+ dependencyMeta && dependencyMeta.attributes
170
+ ? `, ${JSON.stringify(dependencyMeta.attributes)}`
171
+ : "";
152
172
  if (!Array.isArray(moduleAndSpecifiers)) {
153
173
  return {
154
- expression: `${importName}(${JSON.stringify(moduleAndSpecifiers)});`
174
+ expression: `${importName}(${JSON.stringify(
175
+ moduleAndSpecifiers
176
+ )}${attributes});`
155
177
  };
156
178
  }
157
179
  if (moduleAndSpecifiers.length === 1) {
158
180
  return {
159
- expression: `${importName}(${JSON.stringify(moduleAndSpecifiers[0])});`
181
+ expression: `${importName}(${JSON.stringify(
182
+ moduleAndSpecifiers[0]
183
+ )}${attributes});`
160
184
  };
161
185
  }
162
186
  const moduleName = moduleAndSpecifiers[0];
163
187
  return {
164
188
  expression: `${importName}(${JSON.stringify(
165
189
  moduleName
166
- )}).then(${runtimeTemplate.returningFunction(
190
+ )}${attributes}).then(${runtimeTemplate.returningFunction(
167
191
  `module${propertyAccess(moduleAndSpecifiers, 1)}`,
168
192
  "module"
169
193
  )});`
170
194
  };
171
195
  };
172
196
 
197
+ /**
198
+ * @extends {InitFragment<ChunkRenderContext>}
199
+ */
173
200
  class ModuleExternalInitFragment extends InitFragment {
174
201
  /**
175
202
  * @param {string} request import source
176
203
  * @param {string=} ident recomputed ident
204
+ * @param {ImportDependencyMeta=} dependencyMeta the dependency meta
177
205
  * @param {string | HashConstructor=} hashFunction the hash function to use
178
206
  */
179
- constructor(request, ident, hashFunction = "md4") {
207
+ constructor(request, ident, dependencyMeta, hashFunction = "md4") {
180
208
  if (ident === undefined) {
181
209
  ident = Template.toIdentifier(request);
182
210
  if (ident !== request) {
@@ -188,14 +216,19 @@ class ModuleExternalInitFragment extends InitFragment {
188
216
  }
189
217
  const identifier = `__WEBPACK_EXTERNAL_MODULE_${ident}__`;
190
218
  super(
191
- `import * as ${identifier} from ${JSON.stringify(request)};\n`,
219
+ `import * as ${identifier} from ${JSON.stringify(request)}${
220
+ dependencyMeta && dependencyMeta.attributes
221
+ ? ` assert ${JSON.stringify(dependencyMeta.attributes)}`
222
+ : ""
223
+ };\n`,
192
224
  InitFragment.STAGE_HARMONY_IMPORTS,
193
225
  0,
194
226
  `external module import ${ident}`
195
227
  );
196
228
  this._ident = ident;
197
- this._identifier = identifier;
198
229
  this._request = request;
230
+ this._dependencyMeta = request;
231
+ this._identifier = identifier;
199
232
  }
200
233
 
201
234
  getNamespaceIdentifier() {
@@ -211,13 +244,21 @@ register(
211
244
  serialize(obj, { write }) {
212
245
  write(obj._request);
213
246
  write(obj._ident);
247
+ write(obj._dependencyMeta);
214
248
  },
215
249
  deserialize({ read }) {
216
- return new ModuleExternalInitFragment(read(), read());
250
+ return new ModuleExternalInitFragment(read(), read(), read());
217
251
  }
218
252
  }
219
253
  );
220
254
 
255
+ /**
256
+ * @param {string} input input
257
+ * @param {ExportsInfo} exportsInfo the exports info
258
+ * @param {RuntimeSpec=} runtime the runtime
259
+ * @param {RuntimeTemplate=} runtimeTemplate the runtime template
260
+ * @returns {string | undefined} the module remapping
261
+ */
221
262
  const generateModuleRemapping = (
222
263
  input,
223
264
  exportsInfo,
@@ -241,9 +282,11 @@ const generateModuleRemapping = (
241
282
  }
242
283
  }
243
284
  properties.push(
244
- `[${JSON.stringify(used)}]: ${runtimeTemplate.returningFunction(
245
- `${input}${propertyAccess([exportInfo.name])}`
246
- )}`
285
+ `[${JSON.stringify(used)}]: ${
286
+ /** @type {RuntimeTemplate} */ (runtimeTemplate).returningFunction(
287
+ `${input}${propertyAccess([exportInfo.name])}`
288
+ )
289
+ }`
247
290
  );
248
291
  }
249
292
  return `x({ ${properties.join(", ")} })`;
@@ -255,19 +298,22 @@ const generateModuleRemapping = (
255
298
  * @param {ExportsInfo} exportsInfo exports info of this module
256
299
  * @param {RuntimeSpec} runtime the runtime
257
300
  * @param {RuntimeTemplate} runtimeTemplate the runtime template
301
+ * @param {ImportDependencyMeta} dependencyMeta the dependency meta
258
302
  * @returns {SourceData} the generated source
259
303
  */
260
304
  const getSourceForModuleExternal = (
261
305
  moduleAndSpecifiers,
262
306
  exportsInfo,
263
307
  runtime,
264
- runtimeTemplate
308
+ runtimeTemplate,
309
+ dependencyMeta
265
310
  ) => {
266
311
  if (!Array.isArray(moduleAndSpecifiers))
267
312
  moduleAndSpecifiers = [moduleAndSpecifiers];
268
313
  const initFragment = new ModuleExternalInitFragment(
269
314
  moduleAndSpecifiers[0],
270
315
  undefined,
316
+ dependencyMeta,
271
317
  runtimeTemplate.outputOptions.hashFunction
272
318
  );
273
319
  const baseAccess = `${initFragment.getNamespaceIdentifier()}${propertyAccess(
@@ -399,10 +445,11 @@ const getSourceForDefaultCase = (optional, request, runtimeTemplate) => {
399
445
  class ExternalModule extends Module {
400
446
  /**
401
447
  * @param {string | string[] | Record<string, string | string[]>} request request
402
- * @param {TODO} type type
448
+ * @param {string} type type
403
449
  * @param {string} userRequest user request
450
+ * @param {DependencyMeta=} dependencyMeta dependency meta
404
451
  */
405
- constructor(request, type, userRequest) {
452
+ constructor(request, type, userRequest, dependencyMeta) {
406
453
  super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, null);
407
454
 
408
455
  // Info from Factory
@@ -412,10 +459,12 @@ class ExternalModule extends Module {
412
459
  this.externalType = type;
413
460
  /** @type {string} */
414
461
  this.userRequest = userRequest;
462
+ /** @type {DependencyMeta=} */
463
+ this.dependencyMeta = dependencyMeta;
415
464
  }
416
465
 
417
466
  /**
418
- * @returns {Set<string>} types available (do not mutate)
467
+ * @returns {SourceTypes} types available (do not mutate)
419
468
  */
420
469
  getSourceTypes() {
421
470
  return this.externalType === "css-import" ? CSS_TYPES : TYPES;
@@ -548,6 +597,11 @@ class ExternalModule extends Module {
548
597
  callback();
549
598
  }
550
599
 
600
+ /**
601
+ * restore unsafe cache data
602
+ * @param {object} unsafeCacheData data from getUnsafeCacheData
603
+ * @param {NormalModuleFactory} normalModuleFactory the normal module factory handling the unsafe caching
604
+ */
551
605
  restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory) {
552
606
  this._restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory);
553
607
  }
@@ -576,13 +630,25 @@ class ExternalModule extends Module {
576
630
  return { request, externalType };
577
631
  }
578
632
 
633
+ /**
634
+ * @private
635
+ * @param {string | string[]} request request
636
+ * @param {string} externalType the external type
637
+ * @param {RuntimeTemplate} runtimeTemplate the runtime template
638
+ * @param {ModuleGraph} moduleGraph the module graph
639
+ * @param {ChunkGraph} chunkGraph the chunk graph
640
+ * @param {RuntimeSpec} runtime the runtime
641
+ * @param {DependencyMeta | undefined} dependencyMeta the dependency meta
642
+ * @returns {SourceData} the source data
643
+ */
579
644
  _getSourceData(
580
645
  request,
581
646
  externalType,
582
647
  runtimeTemplate,
583
648
  moduleGraph,
584
649
  chunkGraph,
585
- runtime
650
+ runtime,
651
+ dependencyMeta
586
652
  ) {
587
653
  switch (externalType) {
588
654
  case "this":
@@ -600,10 +666,12 @@ class ExternalModule extends Module {
600
666
  case "commonjs-static":
601
667
  return getSourceForCommonJsExternal(request);
602
668
  case "node-commonjs":
603
- return this.buildInfo.module
669
+ return /** @type {BuildInfo} */ (this.buildInfo).module
604
670
  ? getSourceForCommonJsExternalInNodeModule(
605
671
  request,
606
- runtimeTemplate.outputOptions.importMetaName
672
+ /** @type {string} */ (
673
+ runtimeTemplate.outputOptions.importMetaName
674
+ )
607
675
  )
608
676
  : getSourceForCommonJsExternal(request);
609
677
  case "amd":
@@ -621,11 +689,15 @@ class ExternalModule extends Module {
621
689
  );
622
690
  }
623
691
  case "import":
624
- return getSourceForImportExternal(request, runtimeTemplate);
692
+ return getSourceForImportExternal(
693
+ request,
694
+ runtimeTemplate,
695
+ /** @type {ImportDependencyMeta} */ (dependencyMeta)
696
+ );
625
697
  case "script":
626
698
  return getSourceForScriptExternal(request, runtimeTemplate);
627
699
  case "module": {
628
- if (!this.buildInfo.module) {
700
+ if (!(/** @type {BuildInfo} */ (this.buildInfo).module)) {
629
701
  if (!runtimeTemplate.supportsDynamicImport()) {
630
702
  throw new Error(
631
703
  "The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script" +
@@ -634,7 +706,11 @@ class ExternalModule extends Module {
634
706
  : "")
635
707
  );
636
708
  }
637
- return getSourceForImportExternal(request, runtimeTemplate);
709
+ return getSourceForImportExternal(
710
+ request,
711
+ runtimeTemplate,
712
+ /** @type {ImportDependencyMeta} */ (dependencyMeta)
713
+ );
638
714
  }
639
715
  if (!runtimeTemplate.supportsEcmaScriptModuleSyntax()) {
640
716
  throw new Error(
@@ -645,7 +721,8 @@ class ExternalModule extends Module {
645
721
  request,
646
722
  moduleGraph.getExportsInfo(this),
647
723
  runtime,
648
- runtimeTemplate
724
+ runtimeTemplate,
725
+ /** @type {ImportDependencyMeta} */ (dependencyMeta)
649
726
  );
650
727
  }
651
728
  case "var":
@@ -687,9 +764,24 @@ class ExternalModule extends Module {
687
764
  }
688
765
  case "css-import": {
689
766
  const sources = new Map();
767
+ const dependencyMeta = /** @type {CssImportDependencyMeta} */ (
768
+ this.dependencyMeta
769
+ );
770
+ const layer =
771
+ dependencyMeta.layer !== undefined
772
+ ? ` layer(${dependencyMeta.layer})`
773
+ : "";
774
+ const supports = dependencyMeta.supports
775
+ ? ` supports(${dependencyMeta.supports})`
776
+ : "";
777
+ const media = dependencyMeta.media ? ` ${dependencyMeta.media}` : "";
690
778
  sources.set(
691
779
  "css-import",
692
- new RawSource(`@import url(${JSON.stringify(request)});`)
780
+ new RawSource(
781
+ `@import url(${JSON.stringify(
782
+ request
783
+ )})${layer}${supports}${media};`
784
+ )
693
785
  );
694
786
  return {
695
787
  sources,
@@ -703,7 +795,8 @@ class ExternalModule extends Module {
703
795
  runtimeTemplate,
704
796
  moduleGraph,
705
797
  chunkGraph,
706
- runtime
798
+ runtime,
799
+ this.dependencyMeta
707
800
  );
708
801
 
709
802
  let sourceString = sourceData.expression;
@@ -791,6 +884,7 @@ class ExternalModule extends Module {
791
884
  write(this.request);
792
885
  write(this.externalType);
793
886
  write(this.userRequest);
887
+ write(this.dependencyMeta);
794
888
 
795
889
  super.serialize(context);
796
890
  }
@@ -804,6 +898,7 @@ class ExternalModule extends Module {
804
898
  this.request = read();
805
899
  this.externalType = read();
806
900
  this.userRequest = read();
901
+ this.dependencyMeta = read();
807
902
 
808
903
  super.deserialize(context);
809
904
  }