webpack 5.112.0 → 5.112.2

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.
Files changed (263) hide show
  1. package/bin/webpack.js +13 -7
  2. package/lib/Compilation.js +198 -61
  3. package/lib/Compiler.js +12 -25
  4. package/lib/asset/AssetBytesGenerator.js +14 -6
  5. package/lib/asset/AssetBytesParser.js +10 -8
  6. package/lib/asset/AssetGenerator.js +36 -48
  7. package/lib/asset/AssetModule.js +11 -0
  8. package/lib/asset/AssetModulesPlugin.js +5 -4
  9. package/lib/asset/AssetParser.js +8 -8
  10. package/lib/asset/AssetSourceGenerator.js +16 -8
  11. package/lib/asset/AssetSourceParser.js +10 -8
  12. package/lib/asset/WebManifestGenerator.js +11 -6
  13. package/lib/asset/WebManifestParser.js +3 -3
  14. package/lib/async-modules/AsyncModuleHelpers.js +8 -2
  15. package/lib/async-modules/InferAsyncModulesPlugin.js +7 -2
  16. package/lib/cache/CacheFacade.js +1 -1
  17. package/lib/cache/PackFileCacheStrategy.js +6 -10
  18. package/lib/cache/ResolverCachePlugin.js +58 -8
  19. package/lib/config/WebpackOptionsApply.js +8 -28
  20. package/lib/config/cli.js +9 -6
  21. package/lib/config/defaults.js +24 -13
  22. package/lib/config/getClaimedAssetTypes.js +2 -2
  23. package/lib/config/target.js +1 -1
  24. package/lib/context/ContextModule.js +1 -1
  25. package/lib/context/ContextModuleFactory.js +1 -1
  26. package/lib/context/ContextReplacementPlugin.js +3 -9
  27. package/lib/css/CssGenerator.js +40 -47
  28. package/lib/css/CssModule.js +10 -0
  29. package/lib/css/CssModulesPlugin.js +13 -6
  30. package/lib/css/CssParser.js +33 -32
  31. package/lib/css/conventions.js +1 -1
  32. package/lib/css/data.js +224 -219
  33. package/lib/css/syntax-parser.js +1312 -312
  34. package/lib/css/syntax-printer.js +1142 -1025
  35. package/lib/debug/ProfilingPlugin.js +65 -43
  36. package/lib/define/DefinePlugin.js +10 -15
  37. package/lib/define/DotenvPlugin.js +10 -6
  38. package/lib/define/EnvironmentPlugin.js +4 -2
  39. package/lib/dependencies/amd/AMDDefineDependencyParserPlugin.js +1 -2
  40. package/lib/dependencies/amd/AMDRequireDependenciesBlockParserPlugin.js +4 -1
  41. package/lib/dependencies/commonjs/CommonJsExportRequireDependency.js +1 -4
  42. package/lib/dependencies/commonjs/CommonJsExportsParserPlugin.js +18 -33
  43. package/lib/dependencies/commonjs/CommonJsImportsParserPlugin.js +1 -3
  44. package/lib/dependencies/commonjs/CommonJsObjectExportDependency.js +4 -4
  45. package/lib/dependencies/commonjs/CommonJsPlugin.js +2 -5
  46. package/lib/dependencies/commonjs/RequireEnsureDependenciesBlockParserPlugin.js +4 -1
  47. package/lib/dependencies/context/ImportMetaContextDependencyParserPlugin.js +1 -2
  48. package/lib/dependencies/context/ImportMetaGlobDependencyParserPlugin.js +4 -7
  49. package/lib/dependencies/context/ImportMetaGlobHelpers.js +8 -10
  50. package/lib/dependencies/core/CachedConstDependency.js +9 -6
  51. package/lib/dependencies/css/CssIcssExportDependency.js +6 -7
  52. package/lib/dependencies/css/CssIcssImportDependency.js +1 -3
  53. package/lib/dependencies/css/CssUrlDependency.js +2 -6
  54. package/lib/dependencies/esm/ESMDetectionParserPlugin.js +2 -4
  55. package/lib/dependencies/esm/ESMExportExpressionDependency.js +2 -2
  56. package/lib/dependencies/esm/ESMExportImportedSpecifierDependency.js +16 -18
  57. package/lib/dependencies/esm/ESMExportSpecifierDependency.js +20 -3
  58. package/lib/dependencies/esm/ESMExports.js +2 -10
  59. package/lib/dependencies/esm/ESMImportDependency.js +17 -2
  60. package/lib/dependencies/esm/ESMImportDependencyParserPlugin.js +6 -17
  61. package/lib/dependencies/esm/ESMImportGuard.js +11 -23
  62. package/lib/dependencies/esm/ESMImportSideEffectDependency.js +1 -1
  63. package/lib/dependencies/esm/ESMImportSpecifierDependency.js +4 -1
  64. package/lib/dependencies/esm/ESMLiveExportDependency.js +311 -0
  65. package/lib/dependencies/esm/ESMLiveExportParserPlugin.js +862 -0
  66. package/lib/dependencies/esm/ESMModulesPlugin.js +1 -3
  67. package/lib/dependencies/esm/TopLevelAwaitDependency.js +1 -8
  68. package/lib/dependencies/helpers/DynamicExports.js +4 -6
  69. package/lib/dependencies/html/HtmlEntryDependency.js +5 -14
  70. package/lib/dependencies/html/HtmlInlineHtmlDependency.js +1 -4
  71. package/lib/dependencies/html/HtmlInlineScriptDependency.js +2 -6
  72. package/lib/dependencies/html/HtmlInlineStyleDependency.js +1 -4
  73. package/lib/dependencies/import/ImportParserPlugin.js +3 -6
  74. package/lib/dependencies/import-meta/ImportMetaPlugin.js +4 -6
  75. package/lib/dependencies/url/URLParserPlugin.js +1 -1
  76. package/lib/dependencies/worker/WorkerAndWorkletPlugin.js +51 -20
  77. package/lib/devtool/ModuleFilenameHelpers.js +1 -1
  78. package/lib/devtool/SourceMapDevToolPlugin.js +1 -1
  79. package/lib/dll/DelegatedModule.js +3 -3
  80. package/lib/dll/DllModule.js +3 -1
  81. package/lib/dll/DllReferencePlugin.js +1 -4
  82. package/lib/errors/AbstractMethodError.js +1 -1
  83. package/lib/errors/ErrorHelpers.js +2 -1
  84. package/lib/errors/HookWebpackError.js +2 -1
  85. package/lib/errors/JSONParseError.js +4 -4
  86. package/lib/errors/WebpackError.js +3 -2
  87. package/lib/esm/ModuleChunkFormatPlugin.js +36 -1
  88. package/lib/externals/ExternalModule.js +12 -9
  89. package/lib/externals/ExternalModuleFactoryPlugin.js +1 -2
  90. package/lib/externals/ExternalsPlugin.js +1 -1
  91. package/lib/fs/FileSystemInfo.js +4 -4
  92. package/lib/graph/AsyncDependenciesBlock.js +1 -1
  93. package/lib/graph/ChunkGraph.js +5 -0
  94. package/lib/graph/Dependency.js +8 -2
  95. package/lib/graph/ExportsInfo.js +1 -1
  96. package/lib/graph/ModuleGraph.js +14 -3
  97. package/lib/graph/buildChunkGraph.js +17 -19
  98. package/lib/graph/findGraphRoots.js +1 -1
  99. package/lib/hmr/HotModuleReplacement.runtime.js +10 -9
  100. package/lib/hmr/HotModuleReplacementPlugin.js +5 -11
  101. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +16 -19
  102. package/lib/hmr/LazyCompilationPlugin.js +1 -7
  103. package/lib/html/HtmlGenerator.js +25 -17
  104. package/lib/html/HtmlModule.js +11 -1
  105. package/lib/html/HtmlModulesPlugin.js +12 -11
  106. package/lib/html/HtmlParser.js +10 -9
  107. package/lib/html/builtinEmbeddedRenderer.js +31 -33
  108. package/lib/html/data.js +42 -21
  109. package/lib/html/htmlMinify.js +4 -1
  110. package/lib/html/syntax-parser.js +34 -35
  111. package/lib/html/syntax-printer.js +2 -3
  112. package/lib/html/xmlMinify.js +4 -1
  113. package/lib/ids/HashedModuleIdsPlugin.js +1 -1
  114. package/lib/index.js +13 -12
  115. package/lib/javascript/APIPlugin.js +7 -13
  116. package/lib/javascript/BasicEvaluatedExpression.js +5 -5
  117. package/lib/javascript/CommonJsChunkFormatPlugin.js +2 -7
  118. package/lib/javascript/ConstPlugin.js +1 -1
  119. package/lib/javascript/JavascriptGenerator.js +16 -11
  120. package/lib/javascript/JavascriptMetaInfoPlugin.js +3 -15
  121. package/lib/javascript/JavascriptModule.js +10 -0
  122. package/lib/javascript/JavascriptModulesPlugin.js +2 -4
  123. package/lib/javascript/JavascriptParser.js +99 -11
  124. package/lib/javascript/ScopeAnalyzer.js +183 -107
  125. package/lib/javascript/UseStrictPlugin.js +2 -5
  126. package/lib/javascript/jsMinify.js +1 -1
  127. package/lib/javascript/regexp.js +2 -2
  128. package/lib/javascript/syntax-parser.js +2114 -1506
  129. package/lib/javascript/syntax-printer-data.js +167 -1
  130. package/lib/javascript/syntax-printer.js +9554 -2782
  131. package/lib/json/JsonGenerator.js +15 -7
  132. package/lib/json/JsonModule.js +11 -0
  133. package/lib/json/JsonParser.js +7 -6
  134. package/lib/library/AssignLibraryPlugin.js +1 -1
  135. package/lib/library/JsonpLibraryPlugin.js +1 -1
  136. package/lib/library/ModuleLibraryPlugin.js +399 -23
  137. package/lib/loaders/LoaderRunner.js +76 -38
  138. package/lib/loaders/loadLoader.js +16 -10
  139. package/lib/module/Generator.js +17 -9
  140. package/lib/module/Module.js +2 -2
  141. package/lib/module/NormalModule.js +7 -5
  142. package/lib/module/NormalModuleFactory.js +11 -17
  143. package/lib/module/Parser.js +9 -2
  144. package/lib/module/extractSourceMap.js +69 -18
  145. package/lib/module/findCaseMismatch.js +2 -2
  146. package/lib/node/ExternalModuleInitFragment.js +2 -2
  147. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +4 -12
  148. package/lib/node/ReadFileCompileWasmPlugin.js +2 -6
  149. package/lib/node/nodeConsole.js +4 -3
  150. package/lib/optimize/CircularModulesPlugin.js +1 -1
  151. package/lib/optimize/ConcatenatedModule.js +4 -8
  152. package/lib/optimize/FlagDependencyExportsPlugin.js +6 -11
  153. package/lib/optimize/FlagDependencyUsagePlugin.js +28 -3
  154. package/lib/optimize/InlineExports.js +4 -7
  155. package/lib/optimize/LazyBarrels.js +41 -5
  156. package/lib/optimize/ModuleConcatenationPlugin.js +8 -10
  157. package/lib/optimize/RealContentHashPlugin.js +2 -6
  158. package/lib/optimize/SideEffectsFlagPlugin.js +7 -21
  159. package/lib/optimize/SplitChunksPlugin.js +11 -24
  160. package/lib/optimize/TupleQueue.js +1 -1
  161. package/lib/output/CleanPlugin.js +3 -1
  162. package/lib/output/CopyPlugin.js +3 -4
  163. package/lib/performance/EvalUsagePlugin.js +1 -3
  164. package/lib/performance/HotspotsPlugin.js +33 -9
  165. package/lib/performance/PureAnnotationsPlugin.js +16 -10
  166. package/lib/performance/TopLevelThisPlugin.js +17 -9
  167. package/lib/prefetch/ResourceHintPlugin.js +1 -1
  168. package/lib/provide/ProvidedDependency.js +36 -4
  169. package/lib/rules/BasicEffectRulePlugin.js +1 -1
  170. package/lib/rules/RuleSetCompiler.js +15 -5
  171. package/lib/rules/UseEffectRulePlugin.js +1 -3
  172. package/lib/runtime/AsyncModuleRuntimeModule.js +7 -1
  173. package/lib/runtime/GetChunkFilenameRuntimeModule.js +1 -5
  174. package/lib/runtime/RuntimeGlobals.js +6 -0
  175. package/lib/runtime/RuntimePlugin.js +2 -0
  176. package/lib/schemes/HttpUriPlugin.js +1 -2
  177. package/lib/schemes/VirtualUrlPlugin.js +3 -1
  178. package/lib/serialization/AggregateErrorSerializer.js +3 -3
  179. package/lib/serialization/BinaryMiddleware.js +4 -1
  180. package/lib/serialization/ErrorObjectSerializer.js +2 -5
  181. package/lib/serialization/ObjectMiddleware.js +17 -15
  182. package/lib/serialization/PlainObjectSerializer.js +3 -7
  183. package/lib/serialization/Serializer.js +21 -11
  184. package/lib/serialization/SerializerMiddleware.js +8 -6
  185. package/lib/stats/DefaultStatsFactoryPlugin.js +35 -35
  186. package/lib/stats/DefaultStatsPresetPlugin.js +2 -6
  187. package/lib/stats/DefaultStatsPrinterPlugin.js +12 -11
  188. package/lib/stats/StatsFactory.js +194 -24
  189. package/lib/stats/StatsPrinter.js +126 -13
  190. package/lib/template/DependencyTemplate.js +1 -1
  191. package/lib/template/RuntimeTemplate.js +13 -32
  192. package/lib/template/Template.js +1 -1
  193. package/lib/util/SourceProcessor.js +146 -29
  194. package/lib/util/WeakTupleMap.js +10 -4
  195. package/lib/util/cleverMerge.js +61 -33
  196. package/lib/util/comparators.js +9 -5
  197. package/lib/util/dataURL.js +2 -0
  198. package/lib/util/deprecation.js +4 -3
  199. package/lib/util/globUtils.js +1 -4
  200. package/lib/util/internalSerializables.js +2 -0
  201. package/lib/util/makeSerializable.js +6 -4
  202. package/lib/util/memoize.js +1 -1
  203. package/lib/util/parseJson.js +2 -2
  204. package/lib/util/serialization.js +31 -27
  205. package/lib/wasm/AsyncWasmModule.js +12 -2
  206. package/lib/wasm/AsyncWebAssemblyGenerator.js +12 -4
  207. package/lib/wasm/AsyncWebAssemblyJavascriptGenerator.js +13 -5
  208. package/lib/wasm/AsyncWebAssemblyParser.js +7 -5
  209. package/lib/wasm/UniversalCompileAsyncWasmPlugin.js +2 -6
  210. package/lib/wasm-sync/SyncWasmModule.js +16 -1
  211. package/lib/wasm-sync/WebAssemblyGenerator.js +15 -10
  212. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +13 -5
  213. package/lib/wasm-sync/WebAssemblyParser.js +10 -12
  214. package/lib/web/FetchCompileAsyncWasmPlugin.js +4 -12
  215. package/lib/web/FetchCompileWasmPlugin.js +2 -6
  216. package/package.json +18 -19
  217. package/schemas/WebpackOptions.check.js +3 -3
  218. package/schemas/WebpackOptions.json +2 -2
  219. package/schemas/plugins/BannerPlugin.check.js +3 -3
  220. package/schemas/plugins/HtmlGeneratorOptions.check.js +3 -3
  221. package/schemas/plugins/HtmlParserOptions.check.js +3 -3
  222. package/schemas/plugins/IgnorePlugin.check.js +3 -3
  223. package/schemas/plugins/LoaderOptionsPlugin.check.js +3 -3
  224. package/schemas/plugins/ManifestPlugin.check.js +3 -3
  225. package/schemas/plugins/ProgressPlugin.check.js +3 -3
  226. package/schemas/plugins/SSRManifestPlugin.check.js +3 -3
  227. package/schemas/plugins/SourceMapDevToolPlugin.check.js +3 -3
  228. package/schemas/plugins/WatchIgnorePlugin.check.js +3 -3
  229. package/schemas/plugins/asset/AssetGeneratorOptions.check.js +3 -3
  230. package/schemas/plugins/asset/AssetInlineGeneratorOptions.check.js +3 -3
  231. package/schemas/plugins/asset/AssetParserOptions.check.js +3 -3
  232. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +3 -3
  233. package/schemas/plugins/container/ContainerPlugin.check.js +3 -3
  234. package/schemas/plugins/container/ContainerReferencePlugin.check.js +3 -3
  235. package/schemas/plugins/container/ExternalsType.check.js +3 -3
  236. package/schemas/plugins/container/ModuleFederationPlugin.check.js +3 -3
  237. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +3 -3
  238. package/schemas/plugins/css/CssGeneratorOptions.check.js +3 -3
  239. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +3 -3
  240. package/schemas/plugins/css/CssModuleParserOptions.check.js +3 -3
  241. package/schemas/plugins/css/CssParserOptions.check.js +3 -3
  242. package/schemas/plugins/debug/ProfilingPlugin.check.js +3 -3
  243. package/schemas/plugins/define/DotenvPluginOptions.check.js +7 -0
  244. package/schemas/plugins/define/DotenvPluginOptions.json +3 -0
  245. package/schemas/plugins/dll/DllPlugin.check.js +3 -3
  246. package/schemas/plugins/dll/DllReferencePlugin.check.js +3 -3
  247. package/schemas/plugins/ids/HashedModuleIdsPlugin.check.js +3 -3
  248. package/schemas/plugins/ids/OccurrenceChunkIdsPlugin.check.js +3 -3
  249. package/schemas/plugins/ids/OccurrenceModuleIdsPlugin.check.js +3 -3
  250. package/schemas/plugins/json/JsonModulesPluginGenerator.check.js +3 -3
  251. package/schemas/plugins/json/JsonModulesPluginParser.check.js +3 -3
  252. package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.js +3 -3
  253. package/schemas/plugins/optimize/LimitChunkCountPlugin.check.js +3 -3
  254. package/schemas/plugins/optimize/MergeDuplicateChunksPlugin.check.js +3 -3
  255. package/schemas/plugins/optimize/MinChunkSizePlugin.check.js +3 -3
  256. package/schemas/plugins/output/CleanOptions.check.js +7 -0
  257. package/schemas/plugins/output/CleanOptions.json +3 -0
  258. package/schemas/plugins/schemes/HttpUriPlugin.check.js +3 -3
  259. package/schemas/plugins/schemes/VirtualUrlPlugin.check.js +3 -3
  260. package/schemas/plugins/sharing/ConsumeSharedPlugin.check.js +3 -3
  261. package/schemas/plugins/sharing/ProvideSharedPlugin.check.js +3 -3
  262. package/schemas/plugins/sharing/SharePlugin.check.js +3 -3
  263. package/types.d.ts +1482 -358
@@ -33,6 +33,7 @@ const {
33
33
  positionAt
34
34
  } = require("./syntax-parser");
35
35
 
36
+ /** @import { BuiltJavascriptModule } from "./JavascriptModule" */
36
37
  /** @import { EcmaVersion, ParserOptions, ParserPlugin } from "./syntax-parser" */
37
38
  /**
38
39
  * @import {
@@ -138,7 +139,7 @@ const {
138
139
  * @property {number=} lastHarmonyImportOrder deprecated, use `lastESMImportOrder`
139
140
  */
140
141
 
141
- /** @typedef {ParserState & KnownJavascriptParserState} JavascriptParserState */
142
+ /** @typedef {ParserState<BuiltJavascriptModule> & KnownJavascriptParserState} JavascriptParserState */
142
143
 
143
144
  /** @import Compilation from "../Compilation" */
144
145
  /** @import Module from "../module/Module" */
@@ -181,6 +182,7 @@ const EMPTY_MODULE_DECLARATIONS = [];
181
182
  // object pattern, so a tap on that type is not served from the list.
182
183
  const BLOCK_DECLARATION_TYPES = new Set([
183
184
  "VariableDeclaration",
185
+ "FunctionDeclaration",
184
186
  "ClassDeclaration",
185
187
  "ExportNamedDeclaration",
186
188
  "ExportDefaultDeclaration"
@@ -387,7 +389,8 @@ class VariableInfo {
387
389
  /** @import { TopLevelSymbol } from "../optimize/InnerGraph" */
388
390
 
389
391
  /** @typedef {ESMSettings | ImportSettings | CommonJsImportSettings | TopLevelSymbol | CompatibilitySettings} KnownTagData */
390
- /** @typedef {KnownTagData | Record<string, EXPECTED_ANY>} TagData */
392
+ // The open branch is whatever a plugin tags a variable with.
393
+ /** @typedef {KnownTagData | EXPECTED_OBJECT} TagData */
391
394
 
392
395
  /**
393
396
  * Defines the tag info type used by this module.
@@ -1120,6 +1123,35 @@ const hoistedDeclarationsOf = (node, trusted) => {
1120
1123
  return collectHoistedDeclarations(node.body, EMPTY_HOISTED_DECLARATIONS);
1121
1124
  };
1122
1125
 
1126
+ /**
1127
+ * Whether a statement is one of the list's own, or the declaration of an
1128
+ * `export` that is, rather than nested in one of them. It is found by its start
1129
+ * offset since the list is in source order.
1130
+ * @param {(Statement | ModuleDeclaration)[]} statements the list
1131
+ * @param {Statement | ModuleDeclaration | MaybeNamedFunctionDeclaration} statement the statement to find
1132
+ * @returns {boolean} whether the list holds it directly
1133
+ */
1134
+ const holdsStatement = (statements, statement) => {
1135
+ const start = /** @type {Range} */ (statement.range)[0];
1136
+ let low = 0;
1137
+ let high = statements.length - 1;
1138
+ while (low <= high) {
1139
+ const middle = (low + high) >>> 1;
1140
+ const candidate = statements[middle];
1141
+ if (candidate === statement) return true;
1142
+ if (/** @type {Range} */ (candidate.range)[0] < start) low = middle + 1;
1143
+ else high = middle - 1;
1144
+ }
1145
+ // the last statement starting before it is the one that could hold it
1146
+ const holder = high >= 0 ? statements[high] : undefined;
1147
+ return (
1148
+ holder !== undefined &&
1149
+ (holder.type === "ExportNamedDeclaration" ||
1150
+ holder.type === "ExportDefaultDeclaration") &&
1151
+ holder.declaration === statement
1152
+ );
1153
+ };
1154
+
1123
1155
  /**
1124
1156
  * Which statements of a list declare into its block scope, scanned for an AST
1125
1157
  * some other parser built. The parser's own record is read instead when there
@@ -1193,6 +1225,9 @@ const STRICT_MODE_OUTPUT_REASON =
1193
1225
  const MODULE_GOAL_OUTPUT_REASON =
1194
1226
  'The output is an ES module, where "await" is a reserved word.';
1195
1227
 
1228
+ /**
1229
+ * @extends {Parser<BuiltJavascriptModule>}
1230
+ */
1196
1231
  class JavascriptParser extends Parser {
1197
1232
  /**
1198
1233
  * Creates an instance of JavascriptParser.
@@ -1459,9 +1494,13 @@ class JavascriptParser extends Parser {
1459
1494
  this.options = options;
1460
1495
 
1461
1496
  /** @type {ScopeInfo} */
1462
- this.scope = /** @type {EXPECTED_ANY} */ (undefined);
1497
+ this.scope = /** @type {ScopeInfo} */ (
1498
+ /** @type {ScopeInfo | undefined} */ (undefined)
1499
+ );
1463
1500
  /** @type {JavascriptParserState} */
1464
- this.state = /** @type {EXPECTED_ANY} */ (undefined);
1501
+ this.state = /** @type {JavascriptParserState} */ (
1502
+ /** @type {JavascriptParserState | undefined} */ (undefined)
1503
+ );
1465
1504
  /** @type {Comment[] | undefined} */
1466
1505
  this.comments = undefined;
1467
1506
  // ASI overrides keyed by statement-end offset: true forces an ASI
@@ -1475,6 +1514,9 @@ class JavascriptParser extends Parser {
1475
1514
  this.prevStatement = undefined;
1476
1515
  /** @type {boolean} */
1477
1516
  this._hasParserRecord = false;
1517
+ // the statements of the scope last hoisted into, which its block pass reads next
1518
+ /** @type {(Statement | ModuleDeclaration)[] | undefined} */
1519
+ this._scopeStatements = undefined;
1478
1520
  // BACKWARD-COMPAT START
1479
1521
 
1480
1522
  // WHY: the hoisting pass reaches declarations alone, so a tap that
@@ -2720,6 +2762,7 @@ class JavascriptParser extends Parser {
2720
2762
  * @param {Program | BlockStatement} node the node owning the scope
2721
2763
  */
2722
2764
  _preWalkScope(node) {
2765
+ this._scopeStatements = node.body;
2723
2766
  // BACKWARD-COMPAT START
2724
2767
  if (this._needsFullPreWalk || this.hooks.preStatement.taps.length !== 0) {
2725
2768
  if (node.type === "Program") this.preWalkStatements(node.body);
@@ -2747,6 +2790,25 @@ class JavascriptParser extends Parser {
2747
2790
  }
2748
2791
  }
2749
2792
 
2793
+ /**
2794
+ * Whether a function declaration stays in its block rather than hoisting to
2795
+ * the scope last pre-walked, which only sloppy-mode Annex B lets it do.
2796
+ * @param {FunctionDeclaration | MaybeNamedFunctionDeclaration} statement function declaration
2797
+ * @returns {boolean} whether its block, not the scope, binds it
2798
+ */
2799
+ _isBlockScopedFunction(statement) {
2800
+ const statements = this._scopeStatements;
2801
+ // WHY: strictness is read here, not in the parser: `javascript/auto`
2802
+ // reads a script as a strict module first.
2803
+ return (
2804
+ statements !== undefined &&
2805
+ (this.scope.isStrict ||
2806
+ statement.generator === true ||
2807
+ statement.async === true) &&
2808
+ !holdsStatement(statements, statement)
2809
+ );
2810
+ }
2811
+
2750
2812
  // BACKWARD-COMPAT START
2751
2813
  /**
2752
2814
  * Walk every statement of a scope to declare what hoists to it. Only a tap
@@ -3032,8 +3094,18 @@ class JavascriptParser extends Parser {
3032
3094
  let bail =
3033
3095
  preStatement.taps.length !== 0 ? preStatement.call(statement) : undefined;
3034
3096
  if (!bail) {
3035
- const typed = preStatementByType.get(statement.type);
3036
- if (typed !== undefined) bail = typed.call(statement);
3097
+ // WHY: a block-scoped function is the block pass's; the full pre-walk
3098
+ // still shows it to `preStatement`, but not to the typed hooks, which
3099
+ // would declare it in the scope (`InnerGraphPlugin` tags it top-level).
3100
+ if (
3101
+ statement.type === "FunctionDeclaration" &&
3102
+ this._isBlockScopedFunction(statement)
3103
+ ) {
3104
+ bail = true;
3105
+ } else {
3106
+ const typed = preStatementByType.get(statement.type);
3107
+ if (typed !== undefined) bail = typed.call(statement);
3108
+ }
3037
3109
  }
3038
3110
  if (bail) {
3039
3111
  this.prevStatement =
@@ -3123,6 +3195,9 @@ class JavascriptParser extends Parser {
3123
3195
  case "ClassDeclaration":
3124
3196
  this.blockPreWalkClassDeclaration(statement);
3125
3197
  break;
3198
+ case "FunctionDeclaration":
3199
+ this.blockPreWalkFunctionDeclaration(statement);
3200
+ break;
3126
3201
  case "ExportNamedDeclaration":
3127
3202
  this.blockPreWalkExportNamedDeclaration(statement);
3128
3203
  break;
@@ -3569,6 +3644,17 @@ class JavascriptParser extends Parser {
3569
3644
  }
3570
3645
  }
3571
3646
 
3647
+ /**
3648
+ * Bind a function declaration in the block holding it, when it does not
3649
+ * hoist to the enclosing scope.
3650
+ * @param {FunctionDeclaration | MaybeNamedFunctionDeclaration} statement function declaration
3651
+ */
3652
+ blockPreWalkFunctionDeclaration(statement) {
3653
+ if (statement.id && this._isBlockScopedFunction(statement)) {
3654
+ this.defineVariable(statement.id.name);
3655
+ }
3656
+ }
3657
+
3572
3658
  /**
3573
3659
  * Walk function declaration.
3574
3660
  * @param {FunctionDeclaration | MaybeNamedFunctionDeclaration} statement function declaration
@@ -4409,7 +4495,7 @@ class JavascriptParser extends Parser {
4409
4495
  * @param {Literal} expression literal
4410
4496
  */
4411
4497
  _checkStrictModeLiteral(expression) {
4412
- const raw = /** @type {string | undefined} */ (expression.raw);
4498
+ const raw = expression.raw;
4413
4499
  if (raw === undefined) return;
4414
4500
  if (typeof expression.value === "number") {
4415
4501
  // Legacy octal (`0777`) or non-octal decimal (`08`) integer literal.
@@ -4666,7 +4752,7 @@ class JavascriptParser extends Parser {
4666
4752
  this._walkPatternWriteMembers(
4667
4753
  property.type === "RestElement"
4668
4754
  ? property.argument
4669
- : /** @type {Pattern} */ (property.value),
4755
+ : property.value,
4670
4756
  node
4671
4757
  );
4672
4758
  }
@@ -6316,7 +6402,7 @@ class JavascriptParser extends Parser {
6316
6402
  terminated: undefined,
6317
6403
  definitions: new StackedMap()
6318
6404
  };
6319
- this.state = state;
6405
+ this.state = /** @type {JavascriptParserState} */ (state);
6320
6406
  this.comments = comments;
6321
6407
  this.semicolons = undefined;
6322
6408
  // evaluating a defined, untagged identifier can only fall through when
@@ -6703,7 +6789,9 @@ class JavascriptParser extends Parser {
6703
6789
  }
6704
6790
  const loc = node.loc;
6705
6791
  if (!loc) {
6706
- return /** @type {DependencyLocation} */ (/** @type {unknown} */ (loc));
6792
+ return /** @type {DependencyLocation} */ (
6793
+ /** @type {DependencyLocation | null | undefined} */ (loc)
6794
+ );
6707
6795
  }
6708
6796
  // a plain copy: a foreign parser's location class must not reach the cache
6709
6797
  return {
@@ -7475,7 +7563,7 @@ class JavascriptParser extends Parser {
7475
7563
 
7476
7564
  /**
7477
7565
  * Whether a definition is a variable rather than a free name or a scope.
7478
- * @param {unknown} value a scope definition
7566
+ * @param {ExportedVariableInfo | undefined} value a scope definition
7479
7567
  * @returns {value is VariableInfo} true when the value is a variable
7480
7568
  */
7481
7569
  static isVariableInfo(value) {