webpack 5.84.1 → 5.85.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 (121) hide show
  1. package/lib/AsyncDependenciesBlock.js +2 -2
  2. package/lib/AutomaticPrefetchPlugin.js +1 -0
  3. package/lib/BannerPlugin.js +4 -0
  4. package/lib/CacheFacade.js +4 -0
  5. package/lib/ChunkGroup.js +5 -1
  6. package/lib/CleanPlugin.js +30 -7
  7. package/lib/CompatibilityPlugin.js +1 -1
  8. package/lib/Compilation.js +1 -1
  9. package/lib/Compiler.js +1 -1
  10. package/lib/ContextModuleFactory.js +1 -1
  11. package/lib/DllModuleFactory.js +1 -1
  12. package/lib/HookWebpackError.js +1 -1
  13. package/lib/IgnoreErrorModuleFactory.js +1 -1
  14. package/lib/Module.js +21 -9
  15. package/lib/ModuleFactory.js +1 -1
  16. package/lib/ModuleTypeConstants.js +2 -1
  17. package/lib/NormalModuleFactory.js +1 -1
  18. package/lib/NormalModuleReplacementPlugin.js +4 -2
  19. package/lib/NullFactory.js +1 -1
  20. package/lib/RecordIdsPlugin.js +4 -4
  21. package/lib/RuntimeModule.js +5 -5
  22. package/lib/RuntimePlugin.js +4 -0
  23. package/lib/RuntimeTemplate.js +2 -2
  24. package/lib/SelfModuleFactory.js +1 -1
  25. package/lib/Template.js +1 -1
  26. package/lib/UseStrictPlugin.js +4 -0
  27. package/lib/WarnDeprecatedOptionPlugin.js +7 -0
  28. package/lib/WatchIgnorePlugin.js +4 -0
  29. package/lib/WebpackOptionsApply.js +2 -1
  30. package/lib/asset/AssetGenerator.js +7 -2
  31. package/lib/asset/AssetModulesPlugin.js +7 -1
  32. package/lib/asset/AssetParser.js +4 -1
  33. package/lib/asset/RawDataUrlModule.js +5 -3
  34. package/lib/async-modules/InferAsyncModulesPlugin.js +1 -1
  35. package/lib/cache/PackFileCacheStrategy.js +7 -1
  36. package/lib/config/defaults.js +38 -10
  37. package/lib/config/normalization.js +2 -1
  38. package/lib/container/ContainerEntryModule.js +4 -0
  39. package/lib/container/ContainerEntryModuleFactory.js +1 -1
  40. package/lib/container/FallbackDependency.js +7 -0
  41. package/lib/container/FallbackItemDependency.js +3 -0
  42. package/lib/container/FallbackModule.js +4 -0
  43. package/lib/container/FallbackModuleFactory.js +1 -1
  44. package/lib/container/RemoteModule.js +4 -0
  45. package/lib/container/RemoteRuntimeModule.js +1 -0
  46. package/lib/container/RemoteToExternalDependency.js +3 -0
  47. package/lib/dependencies/CommonJsPlugin.js +14 -12
  48. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +1 -1
  49. package/lib/dependencies/HarmonyExports.js +3 -1
  50. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +18 -5
  51. package/lib/dependencies/HarmonyImportSpecifierDependency.js +75 -6
  52. package/lib/dependencies/WorkerPlugin.js +25 -1
  53. package/lib/esm/ModuleChunkFormatPlugin.js +13 -5
  54. package/lib/esm/ModuleChunkLoadingPlugin.js +9 -0
  55. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +10 -8
  56. package/lib/hmr/LazyCompilationPlugin.js +1 -1
  57. package/lib/ids/ChunkModuleIdRangePlugin.js +11 -0
  58. package/lib/ids/DeterministicChunkIdsPlugin.js +11 -2
  59. package/lib/ids/DeterministicModuleIdsPlugin.js +11 -7
  60. package/lib/ids/HashedModuleIdsPlugin.js +8 -1
  61. package/lib/ids/IdHelpers.js +6 -0
  62. package/lib/ids/NamedChunkIdsPlugin.js +13 -1
  63. package/lib/ids/NamedModuleIdsPlugin.js +14 -3
  64. package/lib/ids/OccurrenceChunkIdsPlugin.js +6 -2
  65. package/lib/ids/SyncModuleIdsPlugin.js +2 -2
  66. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +1 -1
  67. package/lib/javascript/BasicEvaluatedExpression.js +11 -1
  68. package/lib/javascript/ChunkHelpers.js +2 -2
  69. package/lib/javascript/EnableChunkLoadingPlugin.js +4 -0
  70. package/lib/javascript/JavascriptModulesPlugin.js +24 -19
  71. package/lib/javascript/JavascriptParser.js +40 -16
  72. package/lib/javascript/StartupHelpers.js +1 -1
  73. package/lib/json/JsonGenerator.js +7 -5
  74. package/lib/json/JsonParser.js +1 -1
  75. package/lib/library/AbstractLibraryPlugin.js +4 -0
  76. package/lib/library/AssignLibraryPlugin.js +6 -4
  77. package/lib/library/ExportPropertyLibraryPlugin.js +4 -1
  78. package/lib/library/ModuleLibraryPlugin.js +6 -3
  79. package/lib/library/SystemLibraryPlugin.js +1 -0
  80. package/lib/library/UmdLibraryPlugin.js +4 -0
  81. package/lib/node/CommonJsChunkLoadingPlugin.js +17 -2
  82. package/lib/node/NodeTemplatePlugin.js +10 -2
  83. package/lib/node/NodeWatchFileSystem.js +1 -1
  84. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +4 -1
  85. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +8 -0
  86. package/lib/node/ReadFileCompileWasmPlugin.js +19 -2
  87. package/lib/node/RequireChunkLoadingRuntimeModule.js +4 -1
  88. package/lib/runtime/AsyncModuleRuntimeModule.js +1 -1
  89. package/lib/runtime/AutoPublicPathRuntimeModule.js +5 -1
  90. package/lib/runtime/BaseUriRuntimeModule.js +5 -1
  91. package/lib/runtime/EnsureChunkRuntimeModule.js +3 -0
  92. package/lib/runtime/GetChunkFilenameRuntimeModule.js +35 -16
  93. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +1 -1
  94. package/lib/runtime/LoadScriptRuntimeModule.js +1 -1
  95. package/lib/runtime/PublicPathRuntimeModule.js +5 -0
  96. package/lib/runtime/StartupChunkDependenciesPlugin.js +15 -0
  97. package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +3 -0
  98. package/lib/runtime/StartupEntrypointRuntimeModule.js +3 -0
  99. package/lib/sharing/ProvideSharedModuleFactory.js +1 -1
  100. package/lib/util/createHash.js +1 -1
  101. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +9 -0
  102. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +9 -1
  103. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +13 -3
  104. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +19 -2
  105. package/lib/wasm-async/AsyncWebAssemblyParser.js +3 -0
  106. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +25 -1
  107. package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +14 -5
  108. package/lib/wasm-sync/WebAssemblyGenerator.js +18 -5
  109. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +1 -1
  110. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +12 -1
  111. package/lib/wasm-sync/WebAssemblyParser.js +9 -2
  112. package/lib/wasm-sync/WebAssemblyUtils.js +1 -1
  113. package/lib/web/FetchCompileAsyncWasmPlugin.js +9 -0
  114. package/lib/web/FetchCompileWasmPlugin.js +19 -2
  115. package/lib/web/JsonpChunkLoadingPlugin.js +9 -0
  116. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +9 -0
  117. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +1 -1
  118. package/package.json +1 -1
  119. package/schemas/WebpackOptions.check.js +1 -1
  120. package/schemas/WebpackOptions.json +12 -0
  121. package/types.d.ts +186 -141
@@ -51,7 +51,7 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
51
51
  /** @type {Map<string | FilenameFunction, Set<Chunk>>} */
52
52
  const chunkFilenames = new Map();
53
53
  let maxChunks = 0;
54
- /** @type {string} */
54
+ /** @type {string | undefined} */
55
55
  let dynamicFilename;
56
56
 
57
57
  /**
@@ -69,9 +69,20 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
69
69
  if (typeof chunkFilename === "string") {
70
70
  if (set.size < maxChunks) return;
71
71
  if (set.size === maxChunks) {
72
- if (chunkFilename.length < dynamicFilename.length) return;
73
- if (chunkFilename.length === dynamicFilename.length) {
74
- if (chunkFilename < dynamicFilename) return;
72
+ if (
73
+ chunkFilename.length <
74
+ /** @type {string} */ (dynamicFilename).length
75
+ ) {
76
+ return;
77
+ }
78
+
79
+ if (
80
+ chunkFilename.length ===
81
+ /** @type {string} */ (dynamicFilename).length
82
+ ) {
83
+ if (chunkFilename < /** @type {string} */ (dynamicFilename)) {
84
+ return;
85
+ }
75
86
  }
76
87
  }
77
88
  maxChunks = set.size;
@@ -108,7 +119,7 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
108
119
  addChunk(entrypoint.chunks[entrypoint.chunks.length - 1]);
109
120
  }
110
121
 
111
- /** @type {Map<string, Set<string | number>>} */
122
+ /** @type {Map<string, Set<string | number | null>>} */
112
123
  const staticUrls = new Map();
113
124
  /** @type {Set<Chunk>} */
114
125
  const dynamicUrlChunks = new Set();
@@ -152,10 +163,14 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
152
163
  hashWithLength: length =>
153
164
  `" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
154
165
  chunk: {
155
- id: unquotedStringify(c.id),
156
- hash: unquotedStringify(c.renderedHash),
157
- hashWithLength: unquotedStringifyWithLength(c.renderedHash),
158
- name: unquotedStringify(c.name || c.id),
166
+ id: unquotedStringify(/** @type {number | string} */ (c.id)),
167
+ hash: unquotedStringify(/** @type {string} */ (c.renderedHash)),
168
+ hashWithLength: unquotedStringifyWithLength(
169
+ /** @type {string} */ (c.renderedHash)
170
+ ),
171
+ name: unquotedStringify(
172
+ c.name || /** @type {number | string} */ (c.id)
173
+ ),
159
174
  contentHash: {
160
175
  [contentType]: unquotedStringify(c.contentHash[contentType])
161
176
  },
@@ -187,8 +202,10 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
187
202
  * @returns {string} code with static mapping of results of fn
188
203
  */
189
204
  const createMap = fn => {
205
+ /** @type {Record<number | string, number | string>} */
190
206
  const obj = {};
191
207
  let useId = false;
208
+ /** @type {number | string | undefined} */
192
209
  let lastKey;
193
210
  let entries = 0;
194
211
  for (const c of dynamicUrlChunks) {
@@ -196,8 +213,8 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
196
213
  if (value === c.id) {
197
214
  useId = true;
198
215
  } else {
199
- obj[c.id] = value;
200
- lastKey = c.id;
216
+ obj[/** @type {number | string} */ (c.id)] = value;
217
+ lastKey = /** @type {number | string} */ (c.id);
201
218
  entries++;
202
219
  }
203
220
  }
@@ -205,9 +222,9 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
205
222
  if (entries === 1) {
206
223
  return useId
207
224
  ? `(chunkId === ${JSON.stringify(lastKey)} ? ${JSON.stringify(
208
- obj[lastKey]
225
+ obj[/** @type {number | string} */ (lastKey)]
209
226
  )} : chunkId)`
210
- : JSON.stringify(obj[lastKey]);
227
+ : JSON.stringify(obj[/** @type {number | string} */ (lastKey)]);
211
228
  }
212
229
  return useId
213
230
  ? `(${JSON.stringify(obj)}[chunkId] || chunkId)`
@@ -238,9 +255,11 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
238
255
  `" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
239
256
  chunk: {
240
257
  id: `" + chunkId + "`,
241
- hash: mapExpr(c => c.renderedHash),
242
- hashWithLength: mapExprWithLength(c => c.renderedHash),
243
- name: mapExpr(c => c.name || c.id),
258
+ hash: mapExpr(c => /** @type {string} */ (c.renderedHash)),
259
+ hashWithLength: mapExprWithLength(
260
+ c => /** @type {string} */ (c.renderedHash)
261
+ ),
262
+ name: mapExpr(c => c.name || /** @type {number | string} */ (c.id)),
244
263
  contentHash: {
245
264
  [contentType]: mapExpr(c => c.contentHash[contentType])
246
265
  },
@@ -10,7 +10,7 @@ const HelperRuntimeModule = require("./HelperRuntimeModule");
10
10
 
11
11
  class GetTrustedTypesPolicyRuntimeModule extends HelperRuntimeModule {
12
12
  /**
13
- * @param {Set<string>} runtimeRequirements runtime requirements
13
+ * @param {ReadonlySet<string>} runtimeRequirements runtime requirements
14
14
  */
15
15
  constructor(runtimeRequirements) {
16
16
  super("trusted types policy");
@@ -72,7 +72,7 @@ class LoadScriptRuntimeModule extends HelperRuntimeModule {
72
72
  "script = document.createElement('script');",
73
73
  scriptType ? `script.type = ${JSON.stringify(scriptType)};` : "",
74
74
  charset ? "script.charset = 'utf-8';" : "",
75
- `script.timeout = ${loadTimeout / 1000};`,
75
+ `script.timeout = ${/** @type {number} */ (loadTimeout) / 1000};`,
76
76
  `if (${RuntimeGlobals.scriptNonce}) {`,
77
77
  Template.indent(
78
78
  `script.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
@@ -7,7 +7,12 @@
7
7
  const RuntimeGlobals = require("../RuntimeGlobals");
8
8
  const RuntimeModule = require("../RuntimeModule");
9
9
 
10
+ /** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} OutputOptions */
11
+
10
12
  class PublicPathRuntimeModule extends RuntimeModule {
13
+ /**
14
+ * @param {OutputOptions["publicPath"]} publicPath public path
15
+ */
11
16
  constructor(publicPath) {
12
17
  super("publicPath", RuntimeModule.STAGE_BASIC);
13
18
  this.publicPath = publicPath;
@@ -8,9 +8,20 @@ const RuntimeGlobals = require("../RuntimeGlobals");
8
8
  const StartupChunkDependenciesRuntimeModule = require("./StartupChunkDependenciesRuntimeModule");
9
9
  const StartupEntrypointRuntimeModule = require("./StartupEntrypointRuntimeModule");
10
10
 
11
+ /** @typedef {import("../../declarations/WebpackOptions").ChunkLoadingType} ChunkLoadingType */
12
+ /** @typedef {import("../Chunk")} Chunk */
11
13
  /** @typedef {import("../Compiler")} Compiler */
12
14
 
15
+ /**
16
+ * @typedef {Object} Options
17
+ * @property {ChunkLoadingType} chunkLoading
18
+ * @property {boolean=} asyncChunkLoading
19
+ */
20
+
13
21
  class StartupChunkDependenciesPlugin {
22
+ /**
23
+ * @param {Options} options options
24
+ */
14
25
  constructor(options) {
15
26
  this.chunkLoading = options.chunkLoading;
16
27
  this.asyncChunkLoading =
@@ -29,6 +40,10 @@ class StartupChunkDependenciesPlugin {
29
40
  "StartupChunkDependenciesPlugin",
30
41
  compilation => {
31
42
  const globalChunkLoading = compilation.outputOptions.chunkLoading;
43
+ /**
44
+ * @param {Chunk} chunk chunk to check
45
+ * @returns {boolean} true, when the plugin is enabled for the chunk
46
+ */
32
47
  const isEnabledForChunk = chunk => {
33
48
  const options = chunk.getEntryOptions();
34
49
  const chunkLoading =
@@ -10,6 +10,9 @@ const RuntimeModule = require("../RuntimeModule");
10
10
  const Template = require("../Template");
11
11
 
12
12
  class StartupChunkDependenciesRuntimeModule extends RuntimeModule {
13
+ /**
14
+ * @param {boolean} asyncChunkLoading use async chunk loading
15
+ */
13
16
  constructor(asyncChunkLoading) {
14
17
  super("startup chunk dependencies", RuntimeModule.STAGE_TRIGGER);
15
18
  this.asyncChunkLoading = asyncChunkLoading;
@@ -10,6 +10,9 @@ const RuntimeModule = require("../RuntimeModule");
10
10
  /** @typedef {import("../MainTemplate")} MainTemplate */
11
11
 
12
12
  class StartupEntrypointRuntimeModule extends RuntimeModule {
13
+ /**
14
+ * @param {boolean} asyncChunkLoading use async chunk loading
15
+ */
13
16
  constructor(asyncChunkLoading) {
14
17
  super("startup entrypoint");
15
18
  this.asyncChunkLoading = asyncChunkLoading;
@@ -15,7 +15,7 @@ const ProvideSharedModule = require("./ProvideSharedModule");
15
15
  class ProvideSharedModuleFactory extends ModuleFactory {
16
16
  /**
17
17
  * @param {ModuleFactoryCreateData} data data object
18
- * @param {function(Error=, ModuleFactoryResult=): void} callback callback
18
+ * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback
19
19
  * @returns {void}
20
20
  */
21
21
  create(data, callback) {
@@ -132,7 +132,7 @@ let BatchedHash = undefined;
132
132
 
133
133
  /**
134
134
  * Creates a hash by name or function
135
- * @param {string | typeof Hash} algorithm the algorithm name or a constructor creating a hash
135
+ * @param {string | typeof Hash | undefined} algorithm the algorithm name or a constructor creating a hash
136
136
  * @returns {Hash} the hash
137
137
  */
138
138
  module.exports = algorithm => {
@@ -9,7 +9,16 @@ const RuntimeGlobals = require("../RuntimeGlobals");
9
9
  const RuntimeModule = require("../RuntimeModule");
10
10
  const Template = require("../Template");
11
11
 
12
+ /**
13
+ * @typedef {Object} AsyncWasmLoadingRuntimeModuleOptions
14
+ * @property {function(string): string} generateLoadBinaryCode
15
+ * @property {boolean} supportsStreaming
16
+ */
17
+
12
18
  class AsyncWasmLoadingRuntimeModule extends RuntimeModule {
19
+ /**
20
+ * @param {AsyncWasmLoadingRuntimeModuleOptions} options options
21
+ */
13
22
  constructor({ generateLoadBinaryCode, supportsStreaming }) {
14
23
  super("wasm loading", RuntimeModule.STAGE_NORMAL);
15
24
  this.generateLoadBinaryCode = generateLoadBinaryCode;
@@ -13,7 +13,15 @@ const Generator = require("../Generator");
13
13
 
14
14
  const TYPES = new Set(["webassembly"]);
15
15
 
16
+ /**
17
+ * @typedef {Object} AsyncWebAssemblyGeneratorOptions
18
+ * @property {boolean} [mangleImports] mangle imports
19
+ */
20
+
16
21
  class AsyncWebAssemblyGenerator extends Generator {
22
+ /**
23
+ * @param {AsyncWebAssemblyGeneratorOptions} options options
24
+ */
17
25
  constructor(options) {
18
26
  super();
19
27
  this.options = options;
@@ -46,7 +54,7 @@ class AsyncWebAssemblyGenerator extends Generator {
46
54
  * @returns {Source} generated code
47
55
  */
48
56
  generate(module, generateContext) {
49
- return module.originalSource();
57
+ return /** @type {Source} */ (module.originalSource());
50
58
  }
51
59
  }
52
60
 
@@ -13,6 +13,7 @@ const Template = require("../Template");
13
13
  const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
14
14
 
15
15
  /** @typedef {import("webpack-sources").Source} Source */
16
+ /** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} OutputOptions */
16
17
  /** @typedef {import("../DependencyTemplates")} DependencyTemplates */
17
18
  /** @typedef {import("../Generator").GenerateContext} GenerateContext */
18
19
  /** @typedef {import("../Module")} Module */
@@ -21,7 +22,14 @@ const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDe
21
22
 
22
23
  const TYPES = new Set(["webassembly"]);
23
24
 
25
+ /**
26
+ * @typedef {{ request: string, importVar: string }} ImportObjRequestItem
27
+ */
28
+
24
29
  class AsyncWebAssemblyJavascriptGenerator extends Generator {
30
+ /**
31
+ * @param {OutputOptions["webassemblyModuleFilename"]} filenameTemplate template for the WebAssembly module filename
32
+ */
25
33
  constructor(filenameTemplate) {
26
34
  super();
27
35
  this.filenameTemplate = filenameTemplate;
@@ -61,9 +69,9 @@ class AsyncWebAssemblyJavascriptGenerator extends Generator {
61
69
  runtimeRequirements.add(RuntimeGlobals.moduleId);
62
70
  runtimeRequirements.add(RuntimeGlobals.exports);
63
71
  runtimeRequirements.add(RuntimeGlobals.instantiateWasm);
64
- /** @type {InitFragment[]} */
72
+ /** @type {InitFragment<InitFragment<string>>[]} */
65
73
  const initFragments = [];
66
- /** @type {Map<Module, { request: string, importVar: string }>} */
74
+ /** @type {Map<Module, ImportObjRequestItem>} */
67
75
  const depModules = new Map();
68
76
  /** @type {Map<string, WebAssemblyImportDependency[]>} */
69
77
  const wasmDepsByRequest = new Map();
@@ -113,7 +121,9 @@ class AsyncWebAssemblyJavascriptGenerator extends Generator {
113
121
  ([request, deps]) => {
114
122
  const exportItems = deps.map(dep => {
115
123
  const importedModule = moduleGraph.getModule(dep);
116
- const importVar = depModules.get(importedModule).importVar;
124
+ const importVar =
125
+ /** @type {ImportObjRequestItem} */
126
+ (depModules.get(importedModule)).importVar;
117
127
  return `${JSON.stringify(
118
128
  dep.name
119
129
  )}: ${runtimeTemplate.exportFromImport({
@@ -15,6 +15,7 @@ const { compareModulesByIdentifier } = require("../util/comparators");
15
15
  const memoize = require("../util/memoize");
16
16
 
17
17
  /** @typedef {import("webpack-sources").Source} Source */
18
+ /** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} OutputOptions */
18
19
  /** @typedef {import("../Chunk")} Chunk */
19
20
  /** @typedef {import("../ChunkGraph")} ChunkGraph */
20
21
  /** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
@@ -25,6 +26,7 @@ const memoize = require("../util/memoize");
25
26
  /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
26
27
  /** @typedef {import("../Template").RenderManifestEntry} RenderManifestEntry */
27
28
  /** @typedef {import("../Template").RenderManifestOptions} RenderManifestOptions */
29
+ /** @typedef {import("../WebpackError")} WebpackError */
28
30
 
29
31
  const getAsyncWebAssemblyGenerator = memoize(() =>
30
32
  require("./AsyncWebAssemblyGenerator")
@@ -51,6 +53,11 @@ const getAsyncWebAssemblyParser = memoize(() =>
51
53
  * @property {SyncWaterfallHook<[Source, Module, WebAssemblyRenderContext]>} renderModuleContent
52
54
  */
53
55
 
56
+ /**
57
+ * @typedef {Object} AsyncWebAssemblyModulesPluginOptions
58
+ * @property {boolean} [mangleImports] mangle imports
59
+ */
60
+
54
61
  /** @type {WeakMap<Compilation, CompilationHooks>} */
55
62
  const compilationHooksMap = new WeakMap();
56
63
 
@@ -81,6 +88,9 @@ class AsyncWebAssemblyModulesPlugin {
81
88
  return hooks;
82
89
  }
83
90
 
91
+ /**
92
+ * @param {AsyncWebAssemblyModulesPluginOptions} options options
93
+ */
84
94
  constructor(options) {
85
95
  this.options = options;
86
96
  }
@@ -140,7 +150,8 @@ class AsyncWebAssemblyModulesPlugin {
140
150
  )) {
141
151
  if (module.type === WEBASSEMBLY_MODULE_TYPE_ASYNC) {
142
152
  const filenameTemplate =
143
- outputOptions.webassemblyModuleFilename;
153
+ /** @type {NonNullable<OutputOptions["webassemblyModuleFilename"]>} */
154
+ (outputOptions.webassemblyModuleFilename);
144
155
 
145
156
  result.push({
146
157
  render: () =>
@@ -178,6 +189,12 @@ class AsyncWebAssemblyModulesPlugin {
178
189
  );
179
190
  }
180
191
 
192
+ /**
193
+ * @param {Module} module the rendered module
194
+ * @param {WebAssemblyRenderContext} renderContext options object
195
+ * @param {CompilationHooks} hooks hooks
196
+ * @returns {Source} the newly generated source from rendering
197
+ */
181
198
  renderModule(module, renderContext, hooks) {
182
199
  const { codeGenerationResults, chunk } = renderContext;
183
200
  try {
@@ -192,7 +209,7 @@ class AsyncWebAssemblyModulesPlugin {
192
209
  "AsyncWebAssemblyModulesPlugin.getCompilationHooks().renderModuleContent"
193
210
  );
194
211
  } catch (e) {
195
- e.module = module;
212
+ /** @type {WebpackError} */ (e).module = module;
196
213
  throw e;
197
214
  }
198
215
  }
@@ -23,6 +23,9 @@ const decoderOpts = {
23
23
  };
24
24
 
25
25
  class WebAssemblyParser extends Parser {
26
+ /**
27
+ * @param {{}=} options parser options
28
+ */
26
29
  constructor(options) {
27
30
  super();
28
31
  this.hooks = Object.freeze({});
@@ -10,14 +10,22 @@ const Template = require("../Template");
10
10
  const { compareModulesByIdentifier } = require("../util/comparators");
11
11
  const WebAssemblyUtils = require("./WebAssemblyUtils");
12
12
 
13
+ /** @typedef {import("../Chunk")} Chunk */
13
14
  /** @typedef {import("../ChunkGraph")} ChunkGraph */
14
15
  /** @typedef {import("../Compilation")} Compilation */
15
16
  /** @typedef {import("../Module")} Module */
17
+ /** @typedef {import("../ModuleGraph")} ModuleGraph */
16
18
  /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
17
19
 
18
20
  // TODO webpack 6 remove the whole folder
19
21
 
20
22
  // Get all wasm modules
23
+ /**
24
+ * @param {ModuleGraph} moduleGraph the module graph
25
+ * @param {ChunkGraph} chunkGraph the chunk graph
26
+ * @param {Chunk} chunk the chunk
27
+ * @returns {Module[]} all wasm modules
28
+ */
21
29
  const getAllWasmModules = (moduleGraph, chunkGraph, chunk) => {
22
30
  const wasmModules = chunk.getAllAsyncChunks();
23
31
  const array = [];
@@ -39,7 +47,7 @@ const getAllWasmModules = (moduleGraph, chunkGraph, chunk) => {
39
47
  * generates the import object function for a module
40
48
  * @param {ChunkGraph} chunkGraph the chunk graph
41
49
  * @param {Module} module the module
42
- * @param {boolean} mangle mangle imports
50
+ * @param {boolean | undefined} mangle mangle imports
43
51
  * @param {string[]} declarations array where declarations are pushed to
44
52
  * @param {RuntimeSpec} runtime the runtime
45
53
  * @returns {string} source code
@@ -188,7 +196,18 @@ const generateImportObject = (
188
196
  }
189
197
  };
190
198
 
199
+ /**
200
+ * @typedef {Object} WasmChunkLoadingRuntimeModuleOptions
201
+ * @property {(path: string) => string} generateLoadBinaryCode
202
+ * @property {boolean} [supportsStreaming]
203
+ * @property {boolean} [mangleImports]
204
+ * @property {Set<string>} runtimeRequirements
205
+ */
206
+
191
207
  class WasmChunkLoadingRuntimeModule extends RuntimeModule {
208
+ /**
209
+ * @param {WasmChunkLoadingRuntimeModuleOptions} options options
210
+ */
192
211
  constructor({
193
212
  generateLoadBinaryCode,
194
213
  supportsStreaming,
@@ -213,6 +232,7 @@ class WasmChunkLoadingRuntimeModule extends RuntimeModule {
213
232
  RuntimeGlobals.hmrDownloadUpdateHandlers
214
233
  );
215
234
  const wasmModules = getAllWasmModules(moduleGraph, chunkGraph, chunk);
235
+ /** @type {string[]} */
216
236
  const declarations = [];
217
237
  const importObjects = wasmModules.map(module => {
218
238
  return generateImportObject(
@@ -226,6 +246,10 @@ class WasmChunkLoadingRuntimeModule extends RuntimeModule {
226
246
  const chunkModuleIdMap = chunkGraph.getChunkModuleIdMap(chunk, m =>
227
247
  m.type.startsWith("webassembly")
228
248
  );
249
+ /**
250
+ * @param {string} content content
251
+ * @returns {string} created import object
252
+ */
229
253
  const createImportObject = content =>
230
254
  mangleImports
231
255
  ? `{ ${JSON.stringify(WebAssemblyUtils.MANGLED_MODULE)}: ${content} }`
@@ -9,6 +9,8 @@ const formatLocation = require("../formatLocation");
9
9
  const UnsupportedWebAssemblyFeatureError = require("./UnsupportedWebAssemblyFeatureError");
10
10
 
11
11
  /** @typedef {import("../Compiler")} Compiler */
12
+ /** @typedef {import("../Dependency")} Dependency */
13
+ /** @typedef {import("../Module")} Module */
12
14
 
13
15
  class WasmFinalizeExportsPlugin {
14
16
  /**
@@ -37,12 +39,13 @@ class WasmFinalizeExportsPlugin {
37
39
  // 2. is active and referenced by a non-WebAssembly module
38
40
  if (
39
41
  connection.isTargetActive(undefined) &&
40
- connection.originModule.type.startsWith("webassembly") ===
42
+ /** @type {Module} */
43
+ (connection.originModule).type.startsWith("webassembly") ===
41
44
  false
42
45
  ) {
43
46
  const referencedExports =
44
47
  compilation.getDependencyReferencedExports(
45
- connection.dependency,
48
+ /** @type {Dependency} */ (connection.dependency),
46
49
  undefined
47
50
  );
48
51
 
@@ -61,9 +64,15 @@ class WasmFinalizeExportsPlugin {
61
64
  // 4. error
62
65
  const error = new UnsupportedWebAssemblyFeatureError(
63
66
  `Export "${name}" with ${jsIncompatibleExports[name]} can only be used for direct wasm to wasm dependencies\n` +
64
- `It's used from ${connection.originModule.readableIdentifier(
65
- compilation.requestShortener
66
- )} at ${formatLocation(connection.dependency.loc)}.`
67
+ `It's used from ${
68
+ /** @type {Module} */
69
+ (connection.originModule).readableIdentifier(
70
+ compilation.requestShortener
71
+ )
72
+ } at ${formatLocation(
73
+ /** @type {Dependency} */ (connection.dependency)
74
+ .loc
75
+ )}.`
67
76
  );
68
77
  error.module = module;
69
78
  compilation.errors.push(error);
@@ -27,7 +27,7 @@ const WebAssemblyExportImportedDependency = require("../dependencies/WebAssembly
27
27
  /** @typedef {import("./WebAssemblyUtils").UsedWasmDependency} UsedWasmDependency */
28
28
 
29
29
  /**
30
- * @typedef {(ArrayBuffer) => ArrayBuffer} ArrayBufferTransform
30
+ * @typedef {(buf: ArrayBuffer) => ArrayBuffer} ArrayBufferTransform
31
31
  */
32
32
 
33
33
  /**
@@ -62,9 +62,10 @@ const removeStartFunc = state => bin => {
62
62
  * Get imported globals
63
63
  *
64
64
  * @param {Object} ast Module's AST
65
- * @returns {Array<t.ModuleImport>} - nodes
65
+ * @returns {t.ModuleImport[]} - nodes
66
66
  */
67
67
  const getImportedGlobals = ast => {
68
+ /** @type {t.ModuleImport[]} */
68
69
  const importedGlobals = [];
69
70
 
70
71
  t.traverse(ast, {
@@ -318,7 +319,10 @@ const addInitFunction =
318
319
  `${importedGlobal.module}.${importedGlobal.name}`
319
320
  );
320
321
 
321
- return t.funcParam(importedGlobal.descr.valtype, id);
322
+ return t.funcParam(
323
+ /** @type {string} */ (importedGlobal.descr.valtype),
324
+ id
325
+ );
322
326
  });
323
327
 
324
328
  const funcBody = [];
@@ -344,6 +348,7 @@ const addInitFunction =
344
348
 
345
349
  funcBody.push(t.instruction("end"));
346
350
 
351
+ /** @type {string[]} */
347
352
  const funcResults = [];
348
353
 
349
354
  // Code section
@@ -369,7 +374,7 @@ const addInitFunction =
369
374
  * Extract mangle mappings from module
370
375
  * @param {ModuleGraph} moduleGraph module graph
371
376
  * @param {Module} module current module
372
- * @param {boolean} mangle mangle imports
377
+ * @param {boolean | undefined} mangle mangle imports
373
378
  * @returns {Map<string, UsedWasmDependency>} mappings to mangled names
374
379
  */
375
380
  const getUsedDependencyMap = (moduleGraph, module, mangle) => {
@@ -390,7 +395,15 @@ const getUsedDependencyMap = (moduleGraph, module, mangle) => {
390
395
 
391
396
  const TYPES = new Set(["webassembly"]);
392
397
 
398
+ /**
399
+ * @typedef {Object} WebAssemblyGeneratorOptions
400
+ * @property {boolean} [mangleImports] mangle imports
401
+ */
402
+
393
403
  class WebAssemblyGenerator extends Generator {
404
+ /**
405
+ * @param {WebAssemblyGeneratorOptions} options options
406
+ */
394
407
  constructor(options) {
395
408
  super();
396
409
  this.options = options;
@@ -423,7 +436,7 @@ class WebAssemblyGenerator extends Generator {
423
436
  * @returns {Source} generated code
424
437
  */
425
438
  generate(module, { moduleGraph, runtime }) {
426
- const bin = module.originalSource().source();
439
+ const bin = /** @type {Source} */ (module.originalSource()).source();
427
440
 
428
441
  const initFuncId = t.identifier("");
429
442
 
@@ -55,7 +55,7 @@ class WebAssemblyJavascriptGenerator extends Generator {
55
55
  runtimeRequirements,
56
56
  runtime
57
57
  } = generateContext;
58
- /** @type {InitFragment[]} */
58
+ /** @type {InitFragment<InitFragment<string>>[]} */
59
59
  const initFragments = [];
60
60
 
61
61
  const exportsInfo = moduleGraph.getExportsInfo(module);
@@ -14,6 +14,7 @@ const memoize = require("../util/memoize");
14
14
  const WebAssemblyInInitialChunkError = require("./WebAssemblyInInitialChunkError");
15
15
 
16
16
  /** @typedef {import("webpack-sources").Source} Source */
17
+ /** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} OutputOptions */
17
18
  /** @typedef {import("../Compiler")} Compiler */
18
19
  /** @typedef {import("../Module")} Module */
19
20
  /** @typedef {import("../ModuleTemplate")} ModuleTemplate */
@@ -29,7 +30,15 @@ const getWebAssemblyParser = memoize(() => require("./WebAssemblyParser"));
29
30
 
30
31
  const PLUGIN_NAME = "WebAssemblyModulesPlugin";
31
32
 
33
+ /**
34
+ * @typedef {Object} WebAssemblyModulesPluginOptions
35
+ * @property {boolean} [mangleImports] mangle imports
36
+ */
37
+
32
38
  class WebAssemblyModulesPlugin {
39
+ /**
40
+ * @param {WebAssemblyModulesPluginOptions} options options
41
+ */
33
42
  constructor(options) {
34
43
  this.options = options;
35
44
  }
@@ -83,7 +92,9 @@ class WebAssemblyModulesPlugin {
83
92
  compareModulesByIdentifier
84
93
  )) {
85
94
  if (module.type === WEBASSEMBLY_MODULE_TYPE_SYNC) {
86
- const filenameTemplate = outputOptions.webassemblyModuleFilename;
95
+ const filenameTemplate =
96
+ /** @type {NonNullable<OutputOptions["webassemblyModuleFilename"]>} */
97
+ (outputOptions.webassemblyModuleFilename);
87
98
 
88
99
  result.push({
89
100
  render: () =>
@@ -61,6 +61,9 @@ const decoderOpts = {
61
61
  };
62
62
 
63
63
  class WebAssemblyParser extends Parser {
64
+ /**
65
+ * @param {{}=} options parser options
66
+ */
64
67
  constructor(options) {
65
68
  super();
66
69
  this.hooks = Object.freeze({});
@@ -88,10 +91,12 @@ class WebAssemblyParser extends Parser {
88
91
  const moduleContext = moduleContextFromModuleAST(module);
89
92
 
90
93
  // extract imports and exports
94
+ /** @type {string[]} */
91
95
  const exports = [];
92
96
  let jsIncompatibleExports = (state.module.buildMeta.jsIncompatibleExports =
93
97
  undefined);
94
98
 
99
+ /** @type {TODO[]} */
95
100
  const importedGlobals = [];
96
101
  t.traverse(module, {
97
102
  ModuleExport({ node }) {
@@ -157,12 +162,14 @@ class WebAssemblyParser extends Parser {
157
162
  } else if (t.isTable(node.descr) === true) {
158
163
  onlyDirectImport = "Table";
159
164
  } else if (t.isFuncImportDescr(node.descr) === true) {
160
- const incompatibleType = getJsIncompatibleType(node.descr.signature);
165
+ const incompatibleType = getJsIncompatibleType(
166
+ /** @type {t.Signature} */ (node.descr.signature)
167
+ );
161
168
  if (incompatibleType) {
162
169
  onlyDirectImport = `Non-JS-compatible Func Signature (${incompatibleType})`;
163
170
  }
164
171
  } else if (t.isGlobalType(node.descr) === true) {
165
- const type = node.descr.valtype;
172
+ const type = /** @type {string} */ (node.descr.valtype);
166
173
  if (!JS_COMPAT_TYPES.has(type)) {
167
174
  onlyDirectImport = `Non-JS-compatible Global Type (${type})`;
168
175
  }
@@ -22,7 +22,7 @@ const MANGLED_MODULE = "a";
22
22
  /**
23
23
  * @param {ModuleGraph} moduleGraph the module graph
24
24
  * @param {Module} module the module
25
- * @param {boolean} mangle mangle module and export names
25
+ * @param {boolean | undefined} mangle mangle module and export names
26
26
  * @returns {UsedWasmDependency[]} used dependencies and (mangled) name
27
27
  */
28
28
  const getUsedDependencies = (moduleGraph, module, mangle) => {