webpack 5.85.1 → 5.87.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 (140) hide show
  1. package/README.md +5 -2
  2. package/lib/APIPlugin.js +150 -99
  3. package/lib/CaseSensitiveModulesWarning.js +3 -1
  4. package/lib/Chunk.js +35 -17
  5. package/lib/ChunkGroup.js +11 -6
  6. package/lib/Compiler.js +4 -3
  7. package/lib/ConcatenationScope.js +2 -2
  8. package/lib/ConditionalInitFragment.js +11 -1
  9. package/lib/ConstPlugin.js +57 -29
  10. package/lib/ContextModule.js +4 -2
  11. package/lib/ContextModuleFactory.js +1 -0
  12. package/lib/DependenciesBlock.js +1 -1
  13. package/lib/DllModule.js +6 -0
  14. package/lib/EvalSourceMapDevToolPlugin.js +2 -1
  15. package/lib/ExternalModule.js +15 -8
  16. package/lib/FlagEntryExportAsUsedPlugin.js +4 -0
  17. package/lib/Module.js +7 -1
  18. package/lib/ModuleDependencyError.js +4 -2
  19. package/lib/ModuleDependencyWarning.js +4 -2
  20. package/lib/ModuleGraph.js +31 -24
  21. package/lib/ModuleGraphConnection.js +19 -6
  22. package/lib/ModuleInfoHeaderPlugin.js +9 -2
  23. package/lib/ModuleNotFoundError.js +5 -2
  24. package/lib/ModuleStoreError.js +2 -1
  25. package/lib/ModuleTypeConstants.js +7 -0
  26. package/lib/MultiWatching.js +4 -0
  27. package/lib/ProgressPlugin.js +71 -15
  28. package/lib/RuntimeGlobals.js +5 -0
  29. package/lib/RuntimePlugin.js +2 -1
  30. package/lib/RuntimeTemplate.js +20 -2
  31. package/lib/WebpackError.js +6 -5
  32. package/lib/WebpackOptionsApply.js +4 -2
  33. package/lib/WebpackOptionsDefaulter.js +10 -3
  34. package/lib/config/defaults.js +31 -2
  35. package/lib/css/CssExportsGenerator.js +9 -0
  36. package/lib/css/CssGenerator.js +1 -1
  37. package/lib/css/CssLoadingRuntimeModule.js +13 -6
  38. package/lib/css/CssModulesPlugin.js +42 -14
  39. package/lib/css/CssParser.js +12 -0
  40. package/lib/dependencies/ConstDependency.js +2 -2
  41. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +19 -0
  42. package/lib/dependencies/ImportParserPlugin.js +25 -1
  43. package/lib/dependencies/JsonExportsDependency.js +1 -1
  44. package/lib/ids/SyncModuleIdsPlugin.js +1 -0
  45. package/lib/javascript/BasicEvaluatedExpression.js +23 -15
  46. package/lib/javascript/CommonJsChunkFormatPlugin.js +6 -2
  47. package/lib/javascript/JavascriptModulesPlugin.js +1 -0
  48. package/lib/javascript/JavascriptParser.js +118 -58
  49. package/lib/javascript/JavascriptParserHelpers.js +37 -15
  50. package/lib/json/JsonData.js +2 -2
  51. package/lib/json/JsonParser.js +25 -12
  52. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +2 -1
  53. package/lib/optimize/AggressiveMergingPlugin.js +8 -0
  54. package/lib/optimize/AggressiveSplittingPlugin.js +9 -2
  55. package/lib/optimize/EnsureChunkConditionsPlugin.js +3 -0
  56. package/lib/optimize/FlagIncludedChunksPlugin.js +11 -5
  57. package/lib/optimize/InnerGraph.js +4 -4
  58. package/lib/optimize/LimitChunkCountPlugin.js +29 -4
  59. package/lib/optimize/MangleExportsPlugin.js +1 -1
  60. package/lib/optimize/MinMaxSizeWarning.js +5 -0
  61. package/lib/optimize/ModuleConcatenationPlugin.js +59 -2
  62. package/lib/optimize/RealContentHashPlugin.js +80 -30
  63. package/lib/optimize/RemoveParentModulesPlugin.js +6 -0
  64. package/lib/optimize/RuntimeChunkPlugin.js +9 -1
  65. package/lib/optimize/SideEffectsFlagPlugin.js +10 -1
  66. package/lib/optimize/SplitChunksPlugin.js +71 -31
  67. package/lib/performance/SizeLimitsPlugin.js +7 -4
  68. package/lib/prefetch/ChunkPrefetchFunctionRuntimeModule.js +3 -1
  69. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +3 -1
  70. package/lib/prefetch/ChunkPrefetchStartupRuntimeModule.js +5 -2
  71. package/lib/prefetch/ChunkPrefetchTriggerRuntimeModule.js +3 -1
  72. package/lib/prefetch/ChunkPreloadTriggerRuntimeModule.js +3 -1
  73. package/lib/rules/BasicEffectRulePlugin.js +4 -0
  74. package/lib/rules/BasicMatcherRulePlugin.js +5 -0
  75. package/lib/rules/RuleSetCompiler.js +3 -3
  76. package/lib/rules/UseEffectRulePlugin.js +6 -4
  77. package/lib/runtime/AsyncModuleRuntimeModule.js +4 -1
  78. package/lib/runtime/AutoPublicPathRuntimeModule.js +3 -1
  79. package/lib/runtime/BaseUriRuntimeModule.js +2 -2
  80. package/lib/runtime/CompatGetDefaultExportRuntimeModule.js +4 -1
  81. package/lib/runtime/CompatRuntimeModule.js +6 -1
  82. package/lib/runtime/CreateFakeNamespaceObjectRuntimeModule.js +4 -1
  83. package/lib/runtime/CreateScriptRuntimeModule.js +3 -1
  84. package/lib/runtime/CreateScriptUrlRuntimeModule.js +3 -1
  85. package/lib/runtime/DefinePropertyGettersRuntimeModule.js +4 -1
  86. package/lib/runtime/EnsureChunkRuntimeModule.js +14 -3
  87. package/lib/runtime/GetChunkFilenameRuntimeModule.js +5 -9
  88. package/lib/runtime/GetFullHashRuntimeModule.js +3 -2
  89. package/lib/runtime/GetMainFilenameRuntimeModule.js +4 -1
  90. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +3 -1
  91. package/lib/runtime/HasOwnPropertyRuntimeModule.js +4 -1
  92. package/lib/runtime/LoadScriptRuntimeModule.js +63 -47
  93. package/lib/runtime/MakeNamespaceObjectRuntimeModule.js +4 -1
  94. package/lib/runtime/OnChunksLoadedRuntimeModule.js +3 -1
  95. package/lib/runtime/PublicPathRuntimeModule.js +3 -1
  96. package/lib/runtime/RelativeUrlRuntimeModule.js +4 -1
  97. package/lib/runtime/RuntimeIdRuntimeModule.js +5 -1
  98. package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +8 -2
  99. package/lib/runtime/StartupEntrypointRuntimeModule.js +2 -1
  100. package/lib/serialization/BinaryMiddleware.js +143 -1
  101. package/lib/serialization/ErrorObjectSerializer.js +3 -0
  102. package/lib/serialization/ObjectMiddleware.js +9 -3
  103. package/lib/serialization/types.js +1 -1
  104. package/lib/sharing/ConsumeSharedFallbackDependency.js +3 -0
  105. package/lib/sharing/ConsumeSharedRuntimeModule.js +13 -4
  106. package/lib/sharing/ProvideSharedDependency.js +11 -0
  107. package/lib/sharing/ProvideSharedModule.js +4 -0
  108. package/lib/sharing/ProvideSharedPlugin.js +22 -21
  109. package/lib/sharing/ShareRuntimeModule.js +11 -4
  110. package/lib/sharing/resolveMatchedConfigs.js +1 -1
  111. package/lib/sharing/utils.js +32 -5
  112. package/lib/util/AsyncQueue.js +4 -2
  113. package/lib/util/ParallelismFactorCalculator.js +10 -0
  114. package/lib/util/Semaphore.js +1 -1
  115. package/lib/util/createHash.js +30 -9
  116. package/lib/util/deprecation.js +10 -3
  117. package/lib/util/deterministicGrouping.js +50 -11
  118. package/lib/util/findGraphRoots.js +4 -2
  119. package/lib/util/memoize.js +3 -3
  120. package/lib/util/processAsyncTree.js +7 -1
  121. package/lib/util/registerExternalSerializer.js +1 -1
  122. package/lib/util/runtime.js +14 -1
  123. package/lib/util/smartGrouping.js +1 -1
  124. package/lib/validateSchema.js +6 -2
  125. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +5 -1
  126. package/lib/wasm-async/AsyncWebAssemblyParser.js +7 -3
  127. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +13 -6
  128. package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +3 -1
  129. package/lib/wasm-sync/WebAssemblyGenerator.js +1 -0
  130. package/lib/wasm-sync/WebAssemblyParser.js +6 -2
  131. package/lib/web/JsonpChunkLoadingRuntimeModule.js +13 -3
  132. package/lib/webpack.js +11 -2
  133. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +14 -15
  134. package/module.d.ts +1 -0
  135. package/package.json +4 -4
  136. package/schemas/WebpackOptions.check.js +1 -1
  137. package/schemas/WebpackOptions.json +51 -5
  138. package/schemas/plugins/css/CssGeneratorOptions.json +1 -1
  139. package/schemas/plugins/css/CssParserOptions.json +1 -1
  140. package/types.d.ts +476 -218
package/README.md CHANGED
@@ -232,6 +232,7 @@ or are automatically applied via regex from your webpack configuration.
232
232
  | <a href="https://github.com/webpack-contrib/polymer-webpack-loader"><img width="48" height="48" src="https://worldvectorlogo.com/logos/polymer.svg"></a> | ![polymer-npm] | ![polymer-size] | Process HTML & CSS with preprocessor of choice and `require()` Web Components like first-class modules |
233
233
  | <a href="https://github.com/TheLarkInn/angular2-template-loader"><img width="48" height="48" src="https://worldvectorlogo.com/logos/angular-icon-1.svg"></a> | ![angular-npm] | ![angular-size] | Loads and compiles Angular 2 Components |
234
234
  | <a href="https://github.com/riot/webpack-loader"><img width="48" height="48" src="https://worldvectorlogo.com/logos/riot.svg"></a> | ![riot-npm] | ![riot-size] | Riot official webpack loader |
235
+ | <a href="https://github.com/sveltejs/svelte-loader"><img width="48" height="48" src="https://worldvectorlogo.com/logos/svelte-1.svg"></a> | ![svelte-npm] | ![svelte-size] | Official Svelte loader |
235
236
 
236
237
  [vue-npm]: https://img.shields.io/npm/v/vue-loader.svg
237
238
  [vue-size]: https://packagephobia.com/badge?p=vue-loader
@@ -241,6 +242,8 @@ or are automatically applied via regex from your webpack configuration.
241
242
  [angular-size]: https://packagephobia.com/badge?p=angular2-template-loader
242
243
  [riot-npm]: https://img.shields.io/npm/v/riot-tag-loader.svg
243
244
  [riot-size]: https://packagephobia.com/badge?p=riot-tag-loader
245
+ [svelte-npm]: https://img.shields.io/npm/v/svelte-loader.svg
246
+ [svelte-size]: https://packagephobia.com/badge?p=svelte-loader
244
247
 
245
248
  ### Performance
246
249
 
@@ -714,5 +717,5 @@ src="https://static.monei.net/monei-logo.svg" height="30" alt="MONEI"></a>
714
717
  [builds2-url]: https://dev.azure.com/webpack/webpack/_build/latest?definitionId=3
715
718
  [licenses-url]: https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fwebpack%2Fwebpack?ref=badge_shield
716
719
  [licenses]: https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fwebpack%2Fwebpack.svg?type=shield
717
- [cover]: https://img.shields.io/coveralls/webpack/webpack.svg
718
- [cover-url]: https://coveralls.io/r/webpack/webpack/
720
+ [cover]: https://codecov.io/gh/webpack/webpack/branch/master/graph/badge.svg?token=mDP3mQJNnn
721
+ [cover-url]: https://codecov.io/gh/webpack/webpack
package/lib/APIPlugin.js CHANGED
@@ -5,6 +5,7 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const InitFragment = require("./InitFragment");
8
9
  const {
9
10
  JAVASCRIPT_MODULE_TYPE_AUTO,
10
11
  JAVASCRIPT_MODULE_TYPE_DYNAMIC,
@@ -14,6 +15,7 @@ const RuntimeGlobals = require("./RuntimeGlobals");
14
15
  const WebpackError = require("./WebpackError");
15
16
  const ConstDependency = require("./dependencies/ConstDependency");
16
17
  const BasicEvaluatedExpression = require("./javascript/BasicEvaluatedExpression");
18
+ const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
17
19
  const {
18
20
  toConstantDependency,
19
21
  evaluateToString
@@ -24,103 +26,121 @@ const GetFullHashRuntimeModule = require("./runtime/GetFullHashRuntimeModule");
24
26
  /** @typedef {import("./Compiler")} Compiler */
25
27
  /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
26
28
 
27
- /* eslint-disable camelcase */
28
- const REPLACEMENTS = {
29
- __webpack_require__: {
30
- expr: RuntimeGlobals.require,
31
- req: [RuntimeGlobals.require],
32
- type: "function",
33
- assign: false
34
- },
35
- __webpack_public_path__: {
36
- expr: RuntimeGlobals.publicPath,
37
- req: [RuntimeGlobals.publicPath],
38
- type: "string",
39
- assign: true
40
- },
41
- __webpack_base_uri__: {
42
- expr: RuntimeGlobals.baseURI,
43
- req: [RuntimeGlobals.baseURI],
44
- type: "string",
45
- assign: true
46
- },
47
- __webpack_modules__: {
48
- expr: RuntimeGlobals.moduleFactories,
49
- req: [RuntimeGlobals.moduleFactories],
50
- type: "object",
51
- assign: false
52
- },
53
- __webpack_chunk_load__: {
54
- expr: RuntimeGlobals.ensureChunk,
55
- req: [RuntimeGlobals.ensureChunk],
56
- type: "function",
57
- assign: true
58
- },
59
- __non_webpack_require__: {
60
- expr: "require",
61
- req: null,
62
- type: undefined, // type is not known, depends on environment
63
- assign: true
64
- },
65
- __webpack_nonce__: {
66
- expr: RuntimeGlobals.scriptNonce,
67
- req: [RuntimeGlobals.scriptNonce],
68
- type: "string",
69
- assign: true
70
- },
71
- __webpack_hash__: {
72
- expr: `${RuntimeGlobals.getFullHash}()`,
73
- req: [RuntimeGlobals.getFullHash],
74
- type: "string",
75
- assign: false
76
- },
77
- __webpack_chunkname__: {
78
- expr: RuntimeGlobals.chunkName,
79
- req: [RuntimeGlobals.chunkName],
80
- type: "string",
81
- assign: false
82
- },
83
- __webpack_get_script_filename__: {
84
- expr: RuntimeGlobals.getChunkScriptFilename,
85
- req: [RuntimeGlobals.getChunkScriptFilename],
86
- type: "function",
87
- assign: true
88
- },
89
- __webpack_runtime_id__: {
90
- expr: RuntimeGlobals.runtimeId,
91
- req: [RuntimeGlobals.runtimeId],
92
- assign: false
93
- },
94
- "require.onError": {
95
- expr: RuntimeGlobals.uncaughtErrorHandler,
96
- req: [RuntimeGlobals.uncaughtErrorHandler],
97
- type: undefined, // type is not known, could be function or undefined
98
- assign: true // is never a pattern
99
- },
100
- __system_context__: {
101
- expr: RuntimeGlobals.systemContext,
102
- req: [RuntimeGlobals.systemContext],
103
- type: "object",
104
- assign: false
105
- },
106
- __webpack_share_scopes__: {
107
- expr: RuntimeGlobals.shareScopeMap,
108
- req: [RuntimeGlobals.shareScopeMap],
109
- type: "object",
110
- assign: false
111
- },
112
- __webpack_init_sharing__: {
113
- expr: RuntimeGlobals.initializeSharing,
114
- req: [RuntimeGlobals.initializeSharing],
115
- type: "function",
116
- assign: true
117
- }
118
- };
119
- /* eslint-enable camelcase */
29
+ /**
30
+ * @param {boolean} module true if ES module
31
+ * @param {string} importMetaName `import.meta` name
32
+ * @returns {Record<string, {expr: string, req: string[], type?: string, assign: boolean}>} replacements
33
+ */
34
+ function getReplacements(module, importMetaName) {
35
+ return {
36
+ __webpack_require__: {
37
+ expr: RuntimeGlobals.require,
38
+ req: [RuntimeGlobals.require],
39
+ type: "function",
40
+ assign: false
41
+ },
42
+ __webpack_public_path__: {
43
+ expr: RuntimeGlobals.publicPath,
44
+ req: [RuntimeGlobals.publicPath],
45
+ type: "string",
46
+ assign: true
47
+ },
48
+ __webpack_base_uri__: {
49
+ expr: RuntimeGlobals.baseURI,
50
+ req: [RuntimeGlobals.baseURI],
51
+ type: "string",
52
+ assign: true
53
+ },
54
+ __webpack_modules__: {
55
+ expr: RuntimeGlobals.moduleFactories,
56
+ req: [RuntimeGlobals.moduleFactories],
57
+ type: "object",
58
+ assign: false
59
+ },
60
+ __webpack_chunk_load__: {
61
+ expr: RuntimeGlobals.ensureChunk,
62
+ req: [RuntimeGlobals.ensureChunk],
63
+ type: "function",
64
+ assign: true
65
+ },
66
+ __non_webpack_require__: {
67
+ expr: module
68
+ ? `__WEBPACK_EXTERNAL_createRequire(${importMetaName}.url)`
69
+ : "require",
70
+ req: null,
71
+ type: undefined, // type is not known, depends on environment
72
+ assign: true
73
+ },
74
+ __webpack_nonce__: {
75
+ expr: RuntimeGlobals.scriptNonce,
76
+ req: [RuntimeGlobals.scriptNonce],
77
+ type: "string",
78
+ assign: true
79
+ },
80
+ __webpack_hash__: {
81
+ expr: `${RuntimeGlobals.getFullHash}()`,
82
+ req: [RuntimeGlobals.getFullHash],
83
+ type: "string",
84
+ assign: false
85
+ },
86
+ __webpack_chunkname__: {
87
+ expr: RuntimeGlobals.chunkName,
88
+ req: [RuntimeGlobals.chunkName],
89
+ type: "string",
90
+ assign: false
91
+ },
92
+ __webpack_get_script_filename__: {
93
+ expr: RuntimeGlobals.getChunkScriptFilename,
94
+ req: [RuntimeGlobals.getChunkScriptFilename],
95
+ type: "function",
96
+ assign: true
97
+ },
98
+ __webpack_runtime_id__: {
99
+ expr: RuntimeGlobals.runtimeId,
100
+ req: [RuntimeGlobals.runtimeId],
101
+ assign: false
102
+ },
103
+ "require.onError": {
104
+ expr: RuntimeGlobals.uncaughtErrorHandler,
105
+ req: [RuntimeGlobals.uncaughtErrorHandler],
106
+ type: undefined, // type is not known, could be function or undefined
107
+ assign: true // is never a pattern
108
+ },
109
+ __system_context__: {
110
+ expr: RuntimeGlobals.systemContext,
111
+ req: [RuntimeGlobals.systemContext],
112
+ type: "object",
113
+ assign: false
114
+ },
115
+ __webpack_share_scopes__: {
116
+ expr: RuntimeGlobals.shareScopeMap,
117
+ req: [RuntimeGlobals.shareScopeMap],
118
+ type: "object",
119
+ assign: false
120
+ },
121
+ __webpack_init_sharing__: {
122
+ expr: RuntimeGlobals.initializeSharing,
123
+ req: [RuntimeGlobals.initializeSharing],
124
+ type: "function",
125
+ assign: true
126
+ }
127
+ };
128
+ }
120
129
 
121
130
  const PLUGIN_NAME = "APIPlugin";
122
131
 
132
+ /**
133
+ * @typedef {Object} APIPluginOptions
134
+ * @property {boolean} [module] the output filename
135
+ */
136
+
123
137
  class APIPlugin {
138
+ /**
139
+ * @param {APIPluginOptions} [options] options
140
+ */
141
+ constructor(options = {}) {
142
+ this.options = options;
143
+ }
124
144
  /**
125
145
  * Apply the plugin
126
146
  * @param {Compiler} compiler the compiler instance
@@ -130,6 +150,12 @@ class APIPlugin {
130
150
  compiler.hooks.compilation.tap(
131
151
  PLUGIN_NAME,
132
152
  (compilation, { normalModuleFactory }) => {
153
+ const { importMetaName } = compilation.outputOptions;
154
+ const REPLACEMENTS = getReplacements(
155
+ this.options.module,
156
+ importMetaName
157
+ );
158
+
133
159
  compilation.dependencyTemplates.set(
134
160
  ConstDependency,
135
161
  new ConstDependency.Template()
@@ -152,18 +178,43 @@ class APIPlugin {
152
178
  return true;
153
179
  });
154
180
 
181
+ const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
182
+
183
+ hooks.renderModuleContent.tap(
184
+ PLUGIN_NAME,
185
+ (source, module, renderContext) => {
186
+ if (module.buildInfo.needCreateRequire) {
187
+ const chunkInitFragments = [
188
+ new InitFragment(
189
+ 'import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "module";\n',
190
+ InitFragment.STAGE_HARMONY_IMPORTS,
191
+ 0,
192
+ "external module node-commonjs"
193
+ )
194
+ ];
195
+
196
+ renderContext.chunkInitFragments.push(...chunkInitFragments);
197
+ }
198
+
199
+ return source;
200
+ }
201
+ );
202
+
155
203
  /**
156
204
  * @param {JavascriptParser} parser the parser
157
205
  */
158
206
  const handler = parser => {
159
207
  Object.keys(REPLACEMENTS).forEach(key => {
160
208
  const info = REPLACEMENTS[key];
161
- parser.hooks.expression
162
- .for(key)
163
- .tap(
164
- PLUGIN_NAME,
165
- toConstantDependency(parser, info.expr, info.req)
166
- );
209
+ parser.hooks.expression.for(key).tap(PLUGIN_NAME, expression => {
210
+ const dep = toConstantDependency(parser, info.expr, info.req);
211
+
212
+ if (key === "__non_webpack_require__" && this.options.module) {
213
+ parser.state.module.buildInfo.needCreateRequire = true;
214
+ }
215
+
216
+ return dep(expression);
217
+ });
167
218
  if (info.assign === false) {
168
219
  parser.hooks.assign.for(key).tap(PLUGIN_NAME, expr => {
169
220
  const err = new WebpackError(`${key} must not be assigned`);
@@ -42,7 +42,9 @@ const createModulesListMessage = (modules, moduleGraph) => {
42
42
 
43
43
  if (validReasons.length > 0) {
44
44
  message += `\n Used by ${validReasons.length} module(s), i. e.`;
45
- message += `\n ${validReasons[0].identifier()}`;
45
+ message += `\n ${
46
+ /** @type {Module[]} */ (validReasons)[0].identifier()
47
+ }`;
46
48
  }
47
49
  return message;
48
50
  })
package/lib/Chunk.js CHANGED
@@ -23,6 +23,7 @@ const { mergeRuntime } = require("./util/runtime");
23
23
  /** @typedef {import("./ChunkGraph").ChunkSizeOptions} ChunkSizeOptions */
24
24
  /** @typedef {import("./ChunkGraph").ModuleFilterPredicate} ModuleFilterPredicate */
25
25
  /** @typedef {import("./ChunkGroup")} ChunkGroup */
26
+ /** @typedef {import("./ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */
26
27
  /** @typedef {import("./Compilation")} Compilation */
27
28
  /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
28
29
  /** @typedef {import("./Compilation").PathData} PathData */
@@ -32,6 +33,8 @@ const { mergeRuntime } = require("./util/runtime");
32
33
  /** @typedef {import("./util/Hash")} Hash */
33
34
  /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
34
35
 
36
+ /** @typedef {number | string} ChunkId */
37
+
35
38
  const ChunkFilesSet = createArrayToSetDeprecationSet("chunk.files");
36
39
 
37
40
  /**
@@ -66,21 +69,21 @@ class Chunk {
66
69
  * @param {boolean} backCompat enable backward-compatibility
67
70
  */
68
71
  constructor(name, backCompat = true) {
69
- /** @type {number | string | null} */
72
+ /** @type {ChunkId | null} */
70
73
  this.id = null;
71
- /** @type {(number|string)[] | null} */
74
+ /** @type {ChunkId[] | null} */
72
75
  this.ids = null;
73
76
  /** @type {number} */
74
77
  this.debugId = debugId++;
75
- /** @type {string} */
78
+ /** @type {string | undefined} */
76
79
  this.name = name;
77
80
  /** @type {SortableSet<string>} */
78
81
  this.idNameHints = new SortableSet();
79
82
  /** @type {boolean} */
80
83
  this.preventIntegration = false;
81
- /** @type {(string | function(PathData, AssetInfo=): string)?} */
84
+ /** @type {(string | function(PathData, AssetInfo=): string) | undefined} */
82
85
  this.filenameTemplate = undefined;
83
- /** @type {(string | function(PathData, AssetInfo=): string)?} */
86
+ /** @type {(string | function(PathData, AssetInfo=): string) | undefined} */
84
87
  this.cssFilenameTemplate = undefined;
85
88
  /** @private @type {SortableSet<ChunkGroup>} */
86
89
  this._groups = new SortableSet(undefined, compareChunkGroupsByIndex);
@@ -350,7 +353,7 @@ class Chunk {
350
353
  const chunkModuleHashMap = Object.create(null);
351
354
 
352
355
  for (const asyncChunk of this.getAllAsyncChunks()) {
353
- /** @type {(string|number)[]} */
356
+ /** @type {ChunkId[] | undefined} */
354
357
  let array;
355
358
  for (const module of chunkGraph.getOrderedChunkModulesIterable(
356
359
  asyncChunk,
@@ -359,7 +362,7 @@ class Chunk {
359
362
  if (filterFn(module)) {
360
363
  if (array === undefined) {
361
364
  array = [];
362
- chunkModuleIdMap[asyncChunk.id] = array;
365
+ chunkModuleIdMap[/** @type {ChunkId} */ (asyncChunk.id)] = array;
363
366
  }
364
367
  const moduleId = chunkGraph.getModuleId(module);
365
368
  array.push(moduleId);
@@ -405,15 +408,18 @@ class Chunk {
405
408
  const chunkNameMap = Object.create(null);
406
409
 
407
410
  for (const chunk of this.getAllAsyncChunks()) {
408
- chunkHashMap[chunk.id] = realHash ? chunk.hash : chunk.renderedHash;
411
+ const id = /** @type {ChunkId} */ (chunk.id);
412
+ chunkHashMap[id] =
413
+ /** @type {string} */
414
+ (realHash ? chunk.hash : chunk.renderedHash);
409
415
  for (const key of Object.keys(chunk.contentHash)) {
410
416
  if (!chunkContentHashMap[key]) {
411
417
  chunkContentHashMap[key] = Object.create(null);
412
418
  }
413
- chunkContentHashMap[key][chunk.id] = chunk.contentHash[key];
419
+ chunkContentHashMap[key][id] = chunk.contentHash[key];
414
420
  }
415
421
  if (chunk.name) {
416
- chunkNameMap[chunk.id] = chunk.name;
422
+ chunkNameMap[id] = chunk.name;
417
423
  }
418
424
  }
419
425
 
@@ -553,7 +559,11 @@ class Chunk {
553
559
  const entryModules =
554
560
  chunkGraph.getChunkEntryModulesWithChunkGroupIterable(this);
555
561
  for (const [m, chunkGroup] of entryModules) {
556
- hash.update(`entry${chunkGraph.getModuleId(m)}${chunkGroup.id}`);
562
+ hash.update(
563
+ `entry${chunkGraph.getModuleId(m)}${
564
+ /** @type {ChunkGroup} */ (chunkGroup).id
565
+ }`
566
+ );
557
567
  }
558
568
  }
559
569
 
@@ -697,7 +707,13 @@ class Chunk {
697
707
  lists.set(name, list);
698
708
  }
699
709
  list.push({
700
- order: childGroup.options[key],
710
+ order:
711
+ /** @type {number} */
712
+ (
713
+ childGroup.options[
714
+ /** @type {keyof ChunkGroupOptions} */ (key)
715
+ ]
716
+ ),
701
717
  group: childGroup
702
718
  });
703
719
  }
@@ -718,7 +734,7 @@ class Chunk {
718
734
  for (const item of list) {
719
735
  for (const chunk of item.group.chunks) {
720
736
  if (filterFn && !filterFn(chunk, chunkGraph)) continue;
721
- chunkIdSet.add(chunk.id);
737
+ chunkIdSet.add(/** @type {ChunkId} */ (chunk.id));
722
738
  }
723
739
  }
724
740
  if (chunkIdSet.size > 0) {
@@ -731,13 +747,14 @@ class Chunk {
731
747
  /**
732
748
  * @param {ChunkGraph} chunkGraph the chunk graph
733
749
  * @param {string} type option name
734
- * @returns {{ onChunks: Chunk[], chunks: Set<Chunk> }[]} referenced chunks for a specific type
750
+ * @returns {{ onChunks: Chunk[], chunks: Set<Chunk> }[] | undefined} referenced chunks for a specific type
735
751
  */
736
752
  getChildrenOfTypeInOrder(chunkGraph, type) {
737
753
  const list = [];
738
754
  for (const group of this.groupsIterable) {
739
755
  for (const childGroup of group.childrenIterable) {
740
- const order = childGroup.options[type];
756
+ const order =
757
+ childGroup.options[/** @type {keyof ChunkGroupOptions} */ (type)];
741
758
  if (order === undefined) continue;
742
759
  list.push({
743
760
  order,
@@ -748,7 +765,8 @@ class Chunk {
748
765
  }
749
766
  if (list.length === 0) return undefined;
750
767
  list.sort((a, b) => {
751
- const cmp = b.order - a.order;
768
+ const cmp =
769
+ /** @type {number} */ (b.order) - /** @type {number} */ (a.order);
752
770
  if (cmp !== 0) return cmp;
753
771
  return a.group.compareTo(chunkGraph, b.group);
754
772
  });
@@ -792,7 +810,7 @@ class Chunk {
792
810
  if (chunkMap === undefined) {
793
811
  chunkMaps[key] = chunkMap = Object.create(null);
794
812
  }
795
- chunkMap[chunk.id] = data[key];
813
+ chunkMap[/** @type {ChunkId} */ (chunk.id)] = data[key];
796
814
  }
797
815
  };
798
816
 
package/lib/ChunkGroup.js CHANGED
@@ -28,6 +28,7 @@ const {
28
28
  * @typedef {Object} RawChunkGroupOptions
29
29
  * @property {number=} preloadOrder
30
30
  * @property {number=} prefetchOrder
31
+ * @property {("low" | "high" | "auto")=} fetchPriority
31
32
  */
32
33
 
33
34
  /** @typedef {RawChunkGroupOptions & { name?: string }} ChunkGroupOptions */
@@ -97,7 +98,7 @@ class ChunkGroup {
97
98
  /** Indices in bottom-up order */
98
99
  /** @private @type {Map<Module, number>} */
99
100
  this._modulePostOrderIndices = new Map();
100
- /** @type {number} */
101
+ /** @type {number | undefined} */
101
102
  this.index = undefined;
102
103
  }
103
104
 
@@ -124,7 +125,7 @@ class ChunkGroup {
124
125
 
125
126
  /**
126
127
  * returns the name of current ChunkGroup
127
- * @returns {string|undefined} returns the ChunkGroup name
128
+ * @returns {string | undefined} returns the ChunkGroup name
128
129
  */
129
130
  get name() {
130
131
  return this.options.name;
@@ -132,7 +133,7 @@ class ChunkGroup {
132
133
 
133
134
  /**
134
135
  * sets a new name for current ChunkGroup
135
- * @param {string} value the new name for ChunkGroup
136
+ * @param {string | undefined} value the new name for ChunkGroup
136
137
  * @returns {void}
137
138
  */
138
139
  set name(value) {
@@ -496,7 +497,11 @@ class ChunkGroup {
496
497
  lists.set(name, (list = []));
497
498
  }
498
499
  list.push({
499
- order: childGroup.options[key],
500
+ order:
501
+ /** @type {number} */
502
+ (
503
+ childGroup.options[/** @type {keyof ChunkGroupOptions} */ (key)]
504
+ ),
500
505
  group: childGroup
501
506
  });
502
507
  }
@@ -528,7 +533,7 @@ class ChunkGroup {
528
533
  /**
529
534
  * Gets the top-down index of a module in this ChunkGroup
530
535
  * @param {Module} module the module
531
- * @returns {number} index
536
+ * @returns {number | undefined} index
532
537
  */
533
538
  getModulePreOrderIndex(module) {
534
539
  return this._modulePreOrderIndices.get(module);
@@ -547,7 +552,7 @@ class ChunkGroup {
547
552
  /**
548
553
  * Gets the bottom-up index of a module in this ChunkGroup
549
554
  * @param {Module} module the module
550
- * @returns {number} index
555
+ * @returns {number | undefined} index
551
556
  */
552
557
  getModulePostOrderIndex(module) {
553
558
  return this._modulePostOrderIndices.get(module);
package/lib/Compiler.js CHANGED
@@ -1011,8 +1011,7 @@ ${other}`);
1011
1011
  try {
1012
1012
  this.records = parseJson(content.toString("utf-8"));
1013
1013
  } catch (e) {
1014
- e.message = "Cannot parse records: " + e.message;
1015
- return callback(e);
1014
+ return callback(new Error(`Cannot parse records: ${e.message}`));
1016
1015
  }
1017
1016
 
1018
1017
  return callback();
@@ -1074,7 +1073,9 @@ ${other}`);
1074
1073
  childCompiler.root = this.root;
1075
1074
  if (Array.isArray(plugins)) {
1076
1075
  for (const plugin of plugins) {
1077
- plugin.apply(childCompiler);
1076
+ if (plugin) {
1077
+ plugin.apply(childCompiler);
1078
+ }
1078
1079
  }
1079
1080
  }
1080
1081
  for (const name in this.hooks) {
@@ -108,7 +108,7 @@ class ConcatenationScope {
108
108
  module,
109
109
  { ids = undefined, call = false, directImport = false, asiSafe = false }
110
110
  ) {
111
- const info = this._modulesMap.get(module);
111
+ const info = /** @type {ModuleInfo} */ (this._modulesMap.get(module));
112
112
  const callFlag = call ? "_call" : "";
113
113
  const directImportFlag = directImport ? "_directImport" : "";
114
114
  const asiSafeFlag = asiSafe
@@ -133,7 +133,7 @@ class ConcatenationScope {
133
133
 
134
134
  /**
135
135
  * @param {string} name the identifier
136
- * @returns {ModuleReferenceOptions & { index: number }} parsed options and index
136
+ * @returns {ModuleReferenceOptions & { index: number } | null} parsed options and index
137
137
  */
138
138
  static matchModuleReference(name) {
139
139
  const match = MODULE_REFERENCE_REGEXP.exec(name);
@@ -14,6 +14,11 @@ const { mergeRuntime } = require("./util/runtime");
14
14
  /** @typedef {import("./Generator").GenerateContext} GenerateContext */
15
15
  /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
16
16
 
17
+ /**
18
+ * @param {string} condition condition
19
+ * @param {string | Source} source source
20
+ * @returns {string | Source} wrapped source
21
+ */
17
22
  const wrapInCondition = (condition, source) => {
18
23
  if (typeof source === "string") {
19
24
  return Template.asString([
@@ -33,13 +38,14 @@ const wrapInCondition = (condition, source) => {
33
38
 
34
39
  /**
35
40
  * @typedef {GenerateContext} Context
41
+ * @extends {InitFragment<Context>}
36
42
  */
37
43
  class ConditionalInitFragment extends InitFragment {
38
44
  /**
39
45
  * @param {string|Source} content the source code that will be included as initialization code
40
46
  * @param {number} stage category of initialization code (contribute to order)
41
47
  * @param {number} position position in the category (contribute to order)
42
- * @param {string} key unique key to avoid emitting the same initialization code twice
48
+ * @param {string | undefined} key unique key to avoid emitting the same initialization code twice
43
49
  * @param {RuntimeSpec | boolean} runtimeCondition in which runtime this fragment should be executed
44
50
  * @param {string|Source=} endContent the source code that will be included at the end of the module
45
51
  */
@@ -89,6 +95,10 @@ class ConditionalInitFragment extends InitFragment {
89
95
  return wrapInCondition(expr, this.endContent);
90
96
  }
91
97
 
98
+ /**
99
+ * @param {ConditionalInitFragment} other fragment to merge with
100
+ * @returns {ConditionalInitFragment} merged fragment
101
+ */
92
102
  merge(other) {
93
103
  if (this.runtimeCondition === true) return this;
94
104
  if (other.runtimeCondition === true) return other;