webpack 5.107.2 → 5.108.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.
Files changed (357) hide show
  1. package/hot/dev-server.js +2 -0
  2. package/lib/APIPlugin.js +8 -4
  3. package/lib/CacheFacade.js +7 -0
  4. package/lib/Chunk.js +4 -0
  5. package/lib/ChunkGraph.js +30 -9
  6. package/lib/CircularModulesPlugin.js +190 -0
  7. package/lib/CleanPlugin.js +2 -1
  8. package/lib/Compilation.js +396 -65
  9. package/lib/Compiler.js +25 -11
  10. package/lib/ConcatenationScope.js +10 -3
  11. package/lib/ContextExclusionPlugin.js +1 -0
  12. package/lib/ContextModule.js +92 -47
  13. package/lib/ContextReplacementPlugin.js +4 -0
  14. package/lib/DefinePlugin.js +89 -15
  15. package/lib/Dependency.js +111 -7
  16. package/lib/EntryOptionPlugin.js +39 -2
  17. package/lib/EntryPlugin.js +2 -0
  18. package/lib/ExportsInfo.js +314 -73
  19. package/lib/ExternalModule.js +82 -34
  20. package/lib/ExternalModuleFactoryPlugin.js +1 -0
  21. package/lib/ExternalsPlugin.js +1 -0
  22. package/lib/FileSystemInfo.js +173 -23
  23. package/lib/FlagAllModulesAsUsedPlugin.js +1 -5
  24. package/lib/FlagDependencyExportsPlugin.js +23 -0
  25. package/lib/FlagDependencyUsagePlugin.js +87 -5
  26. package/lib/FlagEntryExportAsUsedPlugin.js +2 -0
  27. package/lib/Generator.js +8 -0
  28. package/lib/HotModuleReplacementPlugin.js +65 -28
  29. package/lib/IgnorePlugin.js +1 -0
  30. package/lib/InitFragment.js +5 -0
  31. package/lib/JavascriptMetaInfoPlugin.js +7 -5
  32. package/lib/LazyBarrel.js +361 -0
  33. package/lib/LoaderTargetPlugin.js +1 -0
  34. package/lib/Module.js +21 -42
  35. package/lib/ModuleGraph.js +3 -2
  36. package/lib/ModuleProfile.js +27 -1
  37. package/lib/ModuleTemplate.js +2 -0
  38. package/lib/MultiCompiler.js +2 -0
  39. package/lib/MultiStats.js +1 -0
  40. package/lib/MultiWatching.js +2 -0
  41. package/lib/NodeStuffPlugin.js +22 -17
  42. package/lib/NormalModule.js +142 -80
  43. package/lib/NormalModuleReplacementPlugin.js +2 -0
  44. package/lib/PrefetchPlugin.js +2 -0
  45. package/lib/ProgressPlugin.js +8 -0
  46. package/lib/ProvidePlugin.js +1 -0
  47. package/lib/RawModule.js +20 -16
  48. package/lib/RecordIdsPlugin.js +1 -0
  49. package/lib/RuntimeGlobals.js +5 -0
  50. package/lib/RuntimePlugin.js +58 -26
  51. package/lib/RuntimeTemplate.js +278 -21
  52. package/lib/SelfModuleFactory.js +1 -0
  53. package/lib/SourceMapDevToolPlugin.js +3 -5
  54. package/lib/Stats.js +1 -0
  55. package/lib/Template.js +8 -2
  56. package/lib/TemplatedPathPlugin.js +473 -131
  57. package/lib/WarnCaseSensitiveModulesPlugin.js +1 -0
  58. package/lib/WarnDeprecatedOptionPlugin.js +4 -0
  59. package/lib/WarnNoModeSetPlugin.js +1 -0
  60. package/lib/WatchIgnorePlugin.js +1 -0
  61. package/lib/Watching.js +9 -0
  62. package/lib/WebpackOptionsApply.js +50 -5
  63. package/lib/asset/AssetBytesGenerator.js +11 -3
  64. package/lib/asset/AssetGenerator.js +47 -22
  65. package/lib/asset/AssetModule.js +47 -0
  66. package/lib/asset/AssetModulesPlugin.js +14 -14
  67. package/lib/asset/AssetParser.js +2 -2
  68. package/lib/asset/AssetSourceGenerator.js +8 -0
  69. package/lib/asset/RawDataUrlModule.js +12 -13
  70. package/lib/buildChunkGraph.js +64 -7
  71. package/lib/bun/BunTargetPlugin.js +48 -0
  72. package/lib/cache/AddBuildDependenciesPlugin.js +1 -0
  73. package/lib/cache/AddManagedPathsPlugin.js +3 -0
  74. package/lib/cache/IdleFileCachePlugin.js +4 -0
  75. package/lib/cache/MemoryWithGcCachePlugin.js +1 -0
  76. package/lib/cache/PackFileCacheStrategy.js +1 -0
  77. package/lib/cache/ResolverCachePlugin.js +2 -0
  78. package/lib/cache/mergeEtags.js +2 -0
  79. package/lib/cli.js +109 -19
  80. package/lib/config/browserslistTargetHandler.js +99 -0
  81. package/lib/config/defaults.js +147 -7
  82. package/lib/config/defineConfig.js +31 -0
  83. package/lib/config/normalization.js +5 -0
  84. package/lib/config/target.js +181 -2
  85. package/lib/container/ContainerEntryModule.js +15 -14
  86. package/lib/container/ContainerExposedDependency.js +2 -2
  87. package/lib/container/ContainerReferencePlugin.js +1 -1
  88. package/lib/container/FallbackDependency.js +5 -7
  89. package/lib/container/FallbackModule.js +10 -9
  90. package/lib/container/RemoteModule.js +20 -10
  91. package/lib/container/RemoteRuntimeModule.js +14 -12
  92. package/lib/css/CssGenerator.js +353 -327
  93. package/lib/css/CssInjectStyleRuntimeModule.js +36 -8
  94. package/lib/css/CssLoadingRuntimeModule.js +118 -47
  95. package/lib/css/CssModule.js +52 -23
  96. package/lib/css/CssModulesPlugin.js +107 -124
  97. package/lib/css/CssParser.js +2759 -2205
  98. package/lib/css/syntax.js +2859 -0
  99. package/lib/debug/ProfilingPlugin.js +2 -0
  100. package/lib/deno/DenoTargetPlugin.js +47 -0
  101. package/lib/dependencies/AMDDefineDependency.js +22 -17
  102. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +1 -0
  103. package/lib/dependencies/AMDRequireArrayDependency.js +7 -11
  104. package/lib/dependencies/AMDRequireContextDependency.js +7 -11
  105. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +1 -0
  106. package/lib/dependencies/AMDRequireDependency.js +24 -20
  107. package/lib/dependencies/CachedConstDependency.js +3 -0
  108. package/lib/dependencies/CommonJsDependencyHelpers.js +1 -0
  109. package/lib/dependencies/CommonJsExportRequireDependency.js +79 -31
  110. package/lib/dependencies/CommonJsExportsDependency.js +21 -16
  111. package/lib/dependencies/CommonJsExportsParserPlugin.js +230 -7
  112. package/lib/dependencies/CommonJsFullRequireDependency.js +27 -19
  113. package/lib/dependencies/CommonJsImportsParserPlugin.js +51 -16
  114. package/lib/dependencies/CommonJsPlugin.js +1 -1
  115. package/lib/dependencies/CommonJsRequireContextDependency.js +10 -13
  116. package/lib/dependencies/CommonJsRequireDependency.js +42 -11
  117. package/lib/dependencies/CommonJsSelfReferenceDependency.js +22 -14
  118. package/lib/dependencies/ConstDependency.js +16 -11
  119. package/lib/dependencies/ContextDependency.js +4 -0
  120. package/lib/dependencies/ContextDependencyTemplateAsId.js +1 -1
  121. package/lib/dependencies/ContextElementDependency.js +23 -14
  122. package/lib/dependencies/CreateRequireParserPlugin.js +1 -0
  123. package/lib/dependencies/CreateScriptUrlDependency.js +5 -7
  124. package/lib/dependencies/CriticalDependencyWarning.js +1 -0
  125. package/lib/dependencies/CssIcssExportDependency.js +512 -574
  126. package/lib/dependencies/CssIcssImportDependency.js +9 -9
  127. package/lib/dependencies/CssIcssSymbolDependency.js +22 -15
  128. package/lib/dependencies/CssImportDependency.js +25 -1
  129. package/lib/dependencies/CssUrlDependency.js +23 -14
  130. package/lib/dependencies/DllEntryDependency.js +9 -13
  131. package/lib/dependencies/ExportBindingInitFragment.js +164 -0
  132. package/lib/dependencies/ExportsInfoDependency.js +12 -12
  133. package/lib/dependencies/ExternalModuleDependency.js +8 -5
  134. package/lib/dependencies/ExternalModuleInitFragment.js +7 -5
  135. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +13 -10
  136. package/lib/dependencies/HarmonyAcceptDependency.js +11 -11
  137. package/lib/dependencies/HarmonyAcceptImportDependency.js +1 -0
  138. package/lib/dependencies/HarmonyDetectionParserPlugin.js +47 -22
  139. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +28 -2
  140. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +46 -22
  141. package/lib/dependencies/HarmonyExportExpressionDependency.js +107 -30
  142. package/lib/dependencies/HarmonyExportHeaderDependency.js +7 -9
  143. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +109 -31
  144. package/lib/dependencies/HarmonyExportInitFragment.js +18 -15
  145. package/lib/dependencies/HarmonyExportSpecifierDependency.js +83 -18
  146. package/lib/dependencies/HarmonyExports.js +4 -1
  147. package/lib/dependencies/HarmonyImportDependency.js +24 -0
  148. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +110 -74
  149. package/lib/dependencies/HarmonyImportGuard.js +254 -0
  150. package/lib/dependencies/HarmonyImportSideEffectDependency.js +18 -1
  151. package/lib/dependencies/HarmonyImportSpecifierDependency.js +118 -14
  152. package/lib/dependencies/HarmonyLinkingError.js +1 -0
  153. package/lib/dependencies/HarmonyModulesPlugin.js +1 -0
  154. package/lib/dependencies/{HtmlScriptSrcDependency.js → HtmlEntryDependency.js} +261 -109
  155. package/lib/dependencies/HtmlInlineHtmlDependency.js +107 -0
  156. package/lib/dependencies/HtmlInlineScriptDependency.js +17 -13
  157. package/lib/dependencies/HtmlInlineStyleDependency.js +45 -11
  158. package/lib/dependencies/ImportContextDependency.js +5 -9
  159. package/lib/dependencies/ImportDependency.js +44 -2
  160. package/lib/dependencies/ImportMetaHotAcceptDependency.js +1 -0
  161. package/lib/dependencies/ImportMetaHotDeclineDependency.js +1 -0
  162. package/lib/dependencies/ImportMetaPlugin.js +74 -30
  163. package/lib/dependencies/ImportParserPlugin.js +19 -0
  164. package/lib/dependencies/ImportWeakDependency.js +1 -0
  165. package/lib/dependencies/JsonExportsDependency.js +9 -9
  166. package/lib/dependencies/LoaderImportDependency.js +1 -0
  167. package/lib/dependencies/LocalModule.js +11 -12
  168. package/lib/dependencies/LocalModuleDependency.js +11 -13
  169. package/lib/dependencies/ModuleDecoratorDependency.js +9 -9
  170. package/lib/dependencies/ModuleDependency.js +7 -0
  171. package/lib/dependencies/ModuleHotAcceptDependency.js +1 -0
  172. package/lib/dependencies/ModuleHotDeclineDependency.js +1 -0
  173. package/lib/dependencies/ModuleInitFragmentDependency.js +15 -11
  174. package/lib/dependencies/ProvidedDependency.js +31 -27
  175. package/lib/dependencies/PureExpressionDependency.js +7 -9
  176. package/lib/dependencies/RequireEnsureDependency.js +12 -13
  177. package/lib/dependencies/RequireHeaderDependency.js +3 -4
  178. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +3 -0
  179. package/lib/dependencies/RequireResolveContextDependency.js +7 -11
  180. package/lib/dependencies/RequireResolveDependency.js +1 -0
  181. package/lib/dependencies/RequireResolveHeaderDependency.js +3 -5
  182. package/lib/dependencies/RuntimeRequirementsDependency.js +6 -7
  183. package/lib/dependencies/StaticExportsDependency.js +10 -10
  184. package/lib/dependencies/SystemPlugin.js +2 -0
  185. package/lib/dependencies/URLContextDependency.js +5 -7
  186. package/lib/dependencies/URLDependency.js +14 -16
  187. package/lib/dependencies/UnsupportedDependency.js +8 -13
  188. package/lib/dependencies/WebAssemblyExportImportedDependency.js +11 -16
  189. package/lib/dependencies/WebAssemblyImportDependency.js +14 -16
  190. package/lib/dependencies/WebpackIsIncludedDependency.js +1 -0
  191. package/lib/dependencies/WorkerDependency.js +19 -8
  192. package/lib/dependencies/WorkerPlugin.js +22 -6
  193. package/lib/dependencies/processExportInfo.js +13 -11
  194. package/lib/dll/DelegatedModule.js +29 -15
  195. package/lib/dll/DelegatedModuleFactoryPlugin.js +1 -0
  196. package/lib/dll/DelegatedPlugin.js +1 -0
  197. package/lib/dll/DllEntryPlugin.js +3 -0
  198. package/lib/dll/DllModule.js +3 -2
  199. package/lib/dll/DllPlugin.js +16 -0
  200. package/lib/dll/DllReferencePlugin.js +3 -0
  201. package/lib/dll/LibManifestPlugin.js +1 -0
  202. package/lib/electron/ElectronTargetPlugin.js +22 -4
  203. package/lib/errors/ConcurrentCompilationError.js +1 -0
  204. package/lib/errors/HookWebpackError.js +11 -13
  205. package/lib/errors/IgnoreErrorModuleFactory.js +1 -0
  206. package/lib/errors/InvalidDependenciesModuleWarning.js +2 -0
  207. package/lib/errors/JSONParseError.js +9 -8
  208. package/lib/errors/ModuleBuildError.js +16 -13
  209. package/lib/errors/ModuleDependencyError.js +8 -1
  210. package/lib/errors/ModuleDependencyWarning.js +8 -1
  211. package/lib/errors/ModuleError.js +5 -11
  212. package/lib/errors/ModuleHashingError.js +4 -0
  213. package/lib/errors/ModuleNotFoundError.js +3 -0
  214. package/lib/errors/ModuleParseError.js +5 -11
  215. package/lib/errors/ModuleRestoreError.js +2 -0
  216. package/lib/errors/ModuleStoreError.js +3 -0
  217. package/lib/errors/ModuleWarning.js +7 -11
  218. package/lib/errors/NodeStuffInWebError.js +1 -0
  219. package/lib/errors/NonErrorEmittedError.js +2 -0
  220. package/lib/errors/UnhandledSchemeError.js +1 -0
  221. package/lib/esm/ModuleChunkLoadingPlugin.js +0 -1
  222. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +28 -23
  223. package/lib/hmr/HotModuleReplacement.runtime.js +175 -30
  224. package/lib/hmr/HotModuleReplacementRuntimeModule.js +7 -0
  225. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +173 -26
  226. package/lib/hmr/LazyCompilationPlugin.js +30 -6
  227. package/lib/html/HtmlGenerator.js +217 -23
  228. package/lib/html/HtmlModule.js +40 -0
  229. package/lib/html/HtmlModulesPlugin.js +219 -55
  230. package/lib/html/HtmlParser.js +1108 -1099
  231. package/lib/html/{walkHtmlTokens.js → syntax.js} +4701 -212
  232. package/lib/ids/IdHelpers.js +4 -2
  233. package/lib/index.js +19 -0
  234. package/lib/javascript/BasicEvaluatedExpression.js +1 -0
  235. package/lib/javascript/EnableChunkLoadingPlugin.js +1 -0
  236. package/lib/javascript/JavascriptGenerator.js +6 -2
  237. package/lib/javascript/JavascriptModule.js +54 -0
  238. package/lib/javascript/JavascriptModulesPlugin.js +257 -102
  239. package/lib/javascript/JavascriptParser.js +285 -158
  240. package/lib/json/JsonModule.js +40 -0
  241. package/lib/json/JsonModulesPlugin.js +7 -0
  242. package/lib/json/JsonParser.js +4 -2
  243. package/lib/library/AssignLibraryPlugin.js +5 -3
  244. package/lib/library/ExportPropertyLibraryPlugin.js +1 -0
  245. package/lib/library/FalseIIFEUmdWarning.js +1 -0
  246. package/lib/library/ModuleLibraryPlugin.js +24 -5
  247. package/lib/library/SystemLibraryPlugin.js +3 -3
  248. package/lib/node/NodeTargetPlugin.js +1 -0
  249. package/lib/node/NodeWatchFileSystem.js +1 -0
  250. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +10 -9
  251. package/lib/node/RequireChunkLoadingRuntimeModule.js +18 -16
  252. package/lib/optimize/ConcatenatedModule.js +395 -77
  253. package/lib/optimize/ConstExportsPlugin.js +211 -0
  254. package/lib/optimize/InlineExports.js +178 -0
  255. package/lib/optimize/InnerGraph.js +372 -275
  256. package/lib/optimize/InnerGraphPlugin.js +196 -99
  257. package/lib/optimize/ModuleConcatenationPlugin.js +56 -25
  258. package/lib/optimize/RealContentHashPlugin.js +14 -1
  259. package/lib/optimize/SideEffectsFlagPlugin.js +234 -64
  260. package/lib/runtime/AsyncModuleRuntimeModule.js +32 -30
  261. package/lib/runtime/AutoPublicPathRuntimeModule.js +11 -5
  262. package/lib/runtime/CompatGetDefaultExportRuntimeModule.js +1 -1
  263. package/lib/runtime/CreateFakeNamespaceObjectRuntimeModule.js +6 -4
  264. package/lib/runtime/DefinePropertyGettersRuntimeModule.js +33 -4
  265. package/lib/runtime/GetChunkFilenameRuntimeModule.js +82 -3
  266. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +1 -1
  267. package/lib/runtime/HasOwnPropertyRuntimeModule.js +1 -1
  268. package/lib/runtime/LoadScriptRuntimeModule.js +18 -13
  269. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +39 -26
  270. package/lib/runtime/MakeNamespaceObjectRuntimeModule.js +1 -1
  271. package/lib/runtime/OnChunksLoadedRuntimeModule.js +4 -4
  272. package/lib/runtime/RelativeUrlRuntimeModule.js +3 -2
  273. package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +1 -1
  274. package/lib/runtime/StartupEntrypointRuntimeModule.js +4 -3
  275. package/lib/runtime/WorkerRuntimeModule.js +33 -0
  276. package/lib/schemes/HttpUriPlugin.js +3 -2
  277. package/lib/serialization/AggregateErrorSerializer.js +7 -6
  278. package/lib/serialization/ArraySerializer.js +4 -8
  279. package/lib/serialization/BinaryMiddleware.js +538 -468
  280. package/lib/serialization/DateObjectSerializer.js +2 -2
  281. package/lib/serialization/ErrorObjectSerializer.js +7 -6
  282. package/lib/serialization/MapObjectSerializer.js +5 -9
  283. package/lib/serialization/NullPrototypeObjectSerializer.js +7 -9
  284. package/lib/serialization/ObjectMiddleware.js +50 -13
  285. package/lib/serialization/PlainObjectSerializer.js +9 -8
  286. package/lib/serialization/RegExpObjectSerializer.js +2 -2
  287. package/lib/serialization/Serializer.js +1 -0
  288. package/lib/serialization/SetObjectSerializer.js +4 -8
  289. package/lib/sharing/ConsumeSharedModule.js +6 -7
  290. package/lib/sharing/ConsumeSharedPlugin.js +1 -0
  291. package/lib/sharing/ConsumeSharedRuntimeModule.js +46 -41
  292. package/lib/sharing/ProvideSharedDependency.js +30 -15
  293. package/lib/sharing/ProvideSharedModule.js +30 -11
  294. package/lib/sharing/ProvideSharedPlugin.js +4 -2
  295. package/lib/sharing/SharePlugin.js +3 -0
  296. package/lib/sharing/ShareRuntimeModule.js +18 -16
  297. package/lib/sharing/resolveMatchedConfigs.js +2 -1
  298. package/lib/sharing/utils.js +1 -1
  299. package/lib/stats/DefaultStatsFactoryPlugin.js +4 -6
  300. package/lib/stats/DefaultStatsPrinterPlugin.js +14 -14
  301. package/lib/stats/StatsFactory.js +11 -9
  302. package/lib/stats/StatsPrinter.js +9 -2
  303. package/lib/url/URLParserPlugin.js +5 -2
  304. package/lib/util/AsyncQueue.js +10 -0
  305. package/lib/util/LazyBucketSortedSet.js +5 -0
  306. package/lib/util/LazySet.js +6 -4
  307. package/lib/util/LocConverter.js +11 -1
  308. package/lib/util/Semaphore.js +1 -0
  309. package/lib/util/SourceProcessor.js +106 -0
  310. package/lib/util/TupleSet.js +1 -0
  311. package/lib/util/WeakTupleMap.js +27 -1
  312. package/lib/util/chainedImports.js +3 -3
  313. package/lib/util/comparators.js +2 -2
  314. package/lib/util/concatenate.js +31 -7
  315. package/lib/util/createHash.js +0 -1
  316. package/lib/util/deterministicGrouping.js +19 -12
  317. package/lib/util/extractSourceMap.js +1 -1
  318. package/lib/util/findGraphRoots.js +2 -0
  319. package/lib/util/fs.js +10 -5
  320. package/lib/util/hash/DebugHash.js +1 -0
  321. package/lib/util/hash/hash-digest.js +24 -15
  322. package/lib/util/identifier.js +7 -0
  323. package/lib/util/internalSerializables.js +11 -2
  324. package/lib/util/magicComment.js +42 -0
  325. package/lib/util/makeSerializable.js +1 -0
  326. package/lib/util/nonNumericOnlyHash.js +30 -1
  327. package/lib/util/property.js +7 -1
  328. package/lib/util/publicPathPlaceholder.js +64 -0
  329. package/lib/util/registerExternalSerializer.js +4 -4
  330. package/lib/wasm-async/AsyncWasmModule.js +77 -0
  331. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +1 -1
  332. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +1 -96
  333. package/lib/wasm-async/AsyncWebAssemblyParser.js +1 -1
  334. package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +1 -1
  335. package/lib/wasm-sync/SyncWasmModule.js +39 -0
  336. package/lib/wasm-sync/UnsupportedWebAssemblyFeatureError.js +1 -0
  337. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +6 -3
  338. package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +2 -1
  339. package/lib/wasm-sync/WebAssemblyGenerator.js +1 -0
  340. package/lib/wasm-sync/WebAssemblyInInitialChunkError.js +2 -0
  341. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +10 -0
  342. package/lib/wasm-sync/WebAssemblyParser.js +7 -3
  343. package/lib/web/JsonpChunkLoadingRuntimeModule.js +45 -39
  344. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +11 -10
  345. package/package.json +35 -27
  346. package/schemas/WebpackOptions.check.js +1 -1
  347. package/schemas/WebpackOptions.json +212 -3
  348. package/schemas/plugins/HtmlGeneratorOptions.check.js +1 -1
  349. package/schemas/plugins/HtmlParserOptions.check.d.ts +7 -0
  350. package/schemas/plugins/HtmlParserOptions.check.js +6 -0
  351. package/schemas/plugins/HtmlParserOptions.json +3 -0
  352. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +1 -1
  353. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  354. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  355. package/types.d.ts +1456 -290
  356. package/lib/css/walkCssTokens.js +0 -2020
  357. package/lib/util/AppendOnlyStackedSet.js +0 -93
@@ -19,7 +19,9 @@ const ModuleDecoratorDependency = require("./ModuleDecoratorDependency");
19
19
  /** @typedef {import("estree").AssignmentExpression} AssignmentExpression */
20
20
  /** @typedef {import("estree").CallExpression} CallExpression */
21
21
  /** @typedef {import("estree").Expression} Expression */
22
+ /** @typedef {import("estree").Node} Node */
22
23
  /** @typedef {import("estree").Super} Super */
24
+ /** @typedef {import("estree").ThisExpression} ThisExpression */
23
25
  /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
24
26
  /** @typedef {import("../ModuleGraph")} ModuleGraph */
25
27
  /** @typedef {import("../ExportsInfo").ExportInfoName} ExportInfoName */
@@ -29,7 +31,7 @@ const ModuleDecoratorDependency = require("./ModuleDecoratorDependency");
29
31
  /** @typedef {import("../javascript/JavascriptParser").Members} Members */
30
32
  /** @typedef {import("../javascript/JavascriptParser").StatementPath} StatementPath */
31
33
  /** @typedef {import("./CommonJsDependencyHelpers").CommonJSDependencyBaseKeywords} CommonJSDependencyBaseKeywords */
32
- /** @typedef {import("../Module").BuildMeta} BuildMeta */
34
+ /** @typedef {import("../javascript/JavascriptModule").JavascriptModuleBuildMeta} JavascriptModuleBuildMeta */
33
35
 
34
36
  /**
35
37
  * This function takes a generic expression and detects whether it is an ObjectExpression.
@@ -59,6 +61,55 @@ const getValueOfPropertyDescription = (expr) => {
59
61
  }
60
62
  };
61
63
 
64
+ /**
65
+ * Extracts the re-exportable expression from a property descriptor, handling
66
+ * both the eager `{ value: <expr> }` form and the lazy
67
+ * `{ get: () => <expr> }` / `{ get() { return <expr>; } }` accessor form used
68
+ * by barrel files (e.g. webpack's own `lib/index.js`). A `set` accessor cannot
69
+ * be reproduced by the rewritten descriptor, so descriptors with a setter are
70
+ * left to the generic handler (which keeps the descriptor verbatim).
71
+ * @param {Expression} expr property descriptor expression
72
+ * @returns {{ expr: Expression, getter: boolean } | undefined} the value expression and whether it is a lazy getter
73
+ */
74
+ const getReexportOfPropertyDescriptor = (expr) => {
75
+ if (expr.type !== "ObjectExpression") return;
76
+ /** @type {Expression | undefined} */
77
+ let valueExpr;
78
+ /** @type {Expression | undefined} */
79
+ let getFn;
80
+ for (const property of expr.properties) {
81
+ if (property.type === "SpreadElement" || property.computed) continue;
82
+ const key = property.key;
83
+ if (key.type !== "Identifier") continue;
84
+ // A setter would be silently dropped by the rewrite — bail out.
85
+ if (key.name === "set") return;
86
+ if (key.name === "value") {
87
+ valueExpr = /** @type {Expression} */ (property.value);
88
+ } else if (key.name === "get") {
89
+ getFn = /** @type {Expression} */ (property.value);
90
+ }
91
+ }
92
+ if (valueExpr) return { expr: valueExpr, getter: false };
93
+ if (
94
+ getFn &&
95
+ (getFn.type === "FunctionExpression" ||
96
+ getFn.type === "ArrowFunctionExpression")
97
+ ) {
98
+ // Arrow with expression body: `() => require("./x")`.
99
+ if (getFn.body.type !== "BlockStatement") {
100
+ return { expr: /** @type {Expression} */ (getFn.body), getter: true };
101
+ }
102
+ // Function body must be exactly `return <expr>;`.
103
+ if (
104
+ getFn.body.body.length === 1 &&
105
+ getFn.body.body[0].type === "ReturnStatement" &&
106
+ getFn.body.body[0].argument
107
+ ) {
108
+ return { expr: getFn.body.body[0].argument, getter: true };
109
+ }
110
+ }
111
+ };
112
+
62
113
  /**
63
114
  * The purpose of this function is to check whether an expression is a truthy literal or not. This is
64
115
  * useful when parsing CommonJS exports, because CommonJS modules can export any value, including falsy
@@ -129,6 +180,105 @@ const parseRequireCall = (parser, expr) => {
129
180
  return { argument: argValue, ids: ids.reverse() };
130
181
  };
131
182
 
183
+ /**
184
+ * Checks if a value is an AST node.
185
+ * @param {unknown} value candidate
186
+ * @returns {value is Node} true when the value is an AST node
187
+ */
188
+ const isNode = (value) =>
189
+ typeof value === "object" &&
190
+ value !== null &&
191
+ typeof (/** @type {{ type?: unknown }} */ (value).type) === "string";
192
+
193
+ /** Positional/metadata keys that never hold child AST nodes. */
194
+ const NON_CHILD_KEYS = new Set([
195
+ "type",
196
+ "start",
197
+ "end",
198
+ "loc",
199
+ "range",
200
+ "leadingComments",
201
+ "trailingComments"
202
+ ]);
203
+
204
+ /** @type {Map<string, string[]>} per node type: own keys that may hold child nodes */
205
+ const childKeysByType = new Map();
206
+
207
+ /**
208
+ * Returns the keys of a node that may hold child nodes, excluding positional
209
+ * metadata. Computed once per node type and cached: acorn initializes every
210
+ * field of a given type (to `null` when absent), so the key set is stable, and
211
+ * the cache stays allocation-free on the hot path after the first node of each
212
+ * type is seen.
213
+ * @param {Node} node node
214
+ * @returns {string[]} candidate child keys
215
+ */
216
+ const getChildKeys = (node) => {
217
+ let keys = childKeysByType.get(node.type);
218
+ if (keys === undefined) {
219
+ keys = Object.keys(node).filter((key) => !NON_CHILD_KEYS.has(key));
220
+ childKeysByType.set(node.type, keys);
221
+ }
222
+ return keys;
223
+ };
224
+
225
+ /**
226
+ * Searches for a `this` belonging to the scanned function's own `this`-scope.
227
+ * Descends into arrow functions, but not into nested functions, static blocks
228
+ * and class field values, which have their own `this`. Class method bodies are
229
+ * function expressions and are skipped by the same rule, while computed keys
230
+ * and `extends` clauses evaluate in the outer scope and are searched. The
231
+ * `this`-scope boundary is enforced when a node is popped, so the generic
232
+ * branch may enumerate any child without crossing it.
233
+ * @param {Node[]} initialNodes nodes to search
234
+ * @returns {ThisExpression | undefined} first own `this` expression
235
+ */
236
+ const findOwnThisExpression = (initialNodes) => {
237
+ const stack = [...initialNodes];
238
+ let node;
239
+ while ((node = stack.pop()) !== undefined) {
240
+ switch (node.type) {
241
+ case "ThisExpression":
242
+ return node;
243
+ case "FunctionExpression":
244
+ case "FunctionDeclaration":
245
+ case "StaticBlock":
246
+ break;
247
+ case "PropertyDefinition":
248
+ if (node.computed) stack.push(node.key);
249
+ break;
250
+ default: {
251
+ const children =
252
+ /** @type {Record<string, unknown>} */
253
+ (/** @type {unknown} */ (node));
254
+ const keys = getChildKeys(node);
255
+ for (let i = 0; i < keys.length; i++) {
256
+ const value = children[keys[i]];
257
+ if (Array.isArray(value)) {
258
+ for (const item of value) {
259
+ if (isNode(item)) stack.push(item);
260
+ }
261
+ } else if (isNode(value)) {
262
+ stack.push(value);
263
+ }
264
+ }
265
+ }
266
+ }
267
+ }
268
+ };
269
+
270
+ /**
271
+ * Returns the own `this` of a function-expression export value. When such a
272
+ * function is called as a method of the exports object, `this` is the exports
273
+ * object and may reach any sibling export (#21178).
274
+ * @param {Node} expr the exported value
275
+ * @returns {ThisExpression | undefined} first own `this` expression
276
+ */
277
+ const getThisAccessInExportedValue = (expr) =>
278
+ expr.type === "FunctionExpression"
279
+ ? findOwnThisExpression([...expr.params, expr.body])
280
+ : undefined;
281
+
132
282
  const PLUGIN_NAME = "CommonJsExportsParserPlugin";
133
283
 
134
284
  class CommonJsExportsParserPlugin {
@@ -137,6 +287,7 @@ class CommonJsExportsParserPlugin {
137
287
  * @param {ModuleGraph} moduleGraph module graph
138
288
  */
139
289
  constructor(moduleGraph) {
290
+ /** @type {ModuleGraph} */
140
291
  this.moduleGraph = moduleGraph;
141
292
  }
142
293
 
@@ -184,6 +335,36 @@ class CommonJsExportsParserPlugin {
184
335
  .getOptimizationBailout(parser.state.module)
185
336
  .push(`CommonJS bailout: ${reason}`);
186
337
  };
338
+ /** @type {unknown} */
339
+ let keptAllExportsForState;
340
+ /**
341
+ * An exported function accessing its own `this` may reach any export
342
+ * through it when called as a method of the exports object, so the
343
+ * whole exports object must be kept (#21178).
344
+ * @param {Node} valueExpr the exported value
345
+ * @param {CommonJSDependencyBaseKeywords} base commonjs base keywords
346
+ * @returns {void}
347
+ */
348
+ const keepAllExportsOnThisAccess = (valueExpr, base) => {
349
+ // One bailout per module is enough
350
+ if (keptAllExportsForState === parser.state) return;
351
+ const thisExpr = getThisAccessInExportedValue(valueExpr);
352
+ if (thisExpr === undefined) return;
353
+ keptAllExportsForState = parser.state;
354
+ bailoutHint(
355
+ `this in exported function may access any export, so all exports are kept, at ${formatLocation(
356
+ /** @type {DependencyLocation} */ (thisExpr.loc)
357
+ )}`
358
+ );
359
+ const dep = new CommonJsSelfReferenceDependency(
360
+ /** @type {Range} */ (thisExpr.range),
361
+ base,
362
+ [],
363
+ false
364
+ );
365
+ dep.loc = /** @type {DependencyLocation} */ (thisExpr.loc);
366
+ parser.state.module.addDependency(dep);
367
+ };
187
368
 
188
369
  // metadata //
189
370
  parser.hooks.evaluateTypeof
@@ -226,7 +407,7 @@ class CommonJsExportsParserPlugin {
226
407
  dep.loc = /** @type {DependencyLocation} */ (expr.loc);
227
408
  dep.optional = Boolean(parser.scope.inTry);
228
409
  parser.state.module.addDependency(dep);
229
- /** @type {BuildMeta} */ (
410
+ /** @type {JavascriptModuleBuildMeta} */ (
230
411
  parser.state.module.buildMeta
231
412
  ).treatAsCommonJs = true;
232
413
 
@@ -250,8 +431,10 @@ class CommonJsExportsParserPlugin {
250
431
  );
251
432
  dep.loc = /** @type {DependencyLocation} */ (expr.loc);
252
433
  parser.state.module.addDependency(dep);
253
- /** @type {BuildMeta} */ (parser.state.module.buildMeta).treatAsCommonJs =
254
- true;
434
+ /** @type {JavascriptModuleBuildMeta} */ (
435
+ parser.state.module.buildMeta
436
+ ).treatAsCommonJs = true;
437
+ keepAllExportsOnThisAccess(expr.right, base);
255
438
  parser.walkExpression(expr.right);
256
439
  return true;
257
440
  };
@@ -295,6 +478,33 @@ class CommonJsExportsParserPlugin {
295
478
  if (typeof property !== "string") return;
296
479
  enableStructuredExports();
297
480
  const descArg = expr.arguments[2];
481
+ // Handle reexporting: `Object.defineProperty(exports, "x", { value: require("./y")[.z] })`
482
+ // and the lazy getter form `{ get: () => require("./y")[.z] }`.
483
+ if (property !== "__esModule") {
484
+ const reexport = getReexportOfPropertyDescriptor(descArg);
485
+ const requireCall =
486
+ reexport && parseRequireCall(parser, reexport.expr);
487
+ if (reexport && requireCall && requireCall.argument.isString()) {
488
+ const dep = new CommonJsExportRequireDependency(
489
+ /** @type {Range} */ (expr.range),
490
+ /** @type {Range} */ (reexport.expr.range),
491
+ `Object.defineProperty(${exportsArg.identifier})`,
492
+ [property],
493
+ /** @type {string} */ (requireCall.argument.string),
494
+ requireCall.ids,
495
+ false
496
+ );
497
+ dep.getter = reexport.getter;
498
+ dep.loc = /** @type {DependencyLocation} */ (expr.loc);
499
+ dep.optional = Boolean(parser.scope.inTry);
500
+ parser.state.module.addDependency(dep);
501
+ /** @type {JavascriptModuleBuildMeta} */ (
502
+ parser.state.module.buildMeta
503
+ ).treatAsCommonJs = true;
504
+
505
+ return true;
506
+ }
507
+ }
298
508
  checkNamespace(
299
509
  /** @type {StatementPath} */
300
510
  (parser.statementPath).length === 1,
@@ -309,10 +519,22 @@ class CommonJsExportsParserPlugin {
309
519
  );
310
520
  dep.loc = /** @type {DependencyLocation} */ (expr.loc);
311
521
  parser.state.module.addDependency(dep);
312
- /** @type {BuildMeta} */ (
522
+ /** @type {JavascriptModuleBuildMeta} */ (
313
523
  parser.state.module.buildMeta
314
524
  ).treatAsCommonJs = true;
315
525
 
526
+ if (descArg.type === "ObjectExpression") {
527
+ // `value`, `get` and `set` descriptor functions are all called
528
+ // with the exports object as `this`
529
+ for (const descProperty of descArg.properties) {
530
+ if (descProperty.type === "SpreadElement") continue;
531
+ keepAllExportsOnThisAccess(
532
+ descProperty.value,
533
+ /** @type {CommonJSDependencyBaseKeywords} */
534
+ (exportsArg.identifier)
535
+ );
536
+ }
537
+ }
316
538
  parser.walkExpression(expr.arguments[2]);
317
539
  return true;
318
540
  });
@@ -353,8 +575,9 @@ class CommonJsExportsParserPlugin {
353
575
  );
354
576
  dep.loc = /** @type {DependencyLocation} */ (expr.loc);
355
577
  parser.state.module.addDependency(dep);
356
- /** @type {BuildMeta} */ (parser.state.module.buildMeta).treatAsCommonJs =
357
- true;
578
+ /** @type {JavascriptModuleBuildMeta} */ (
579
+ parser.state.module.buildMeta
580
+ ).treatAsCommonJs = true;
358
581
 
359
582
  if (call) {
360
583
  parser.walkExpressions(call.arguments);
@@ -24,10 +24,10 @@ const ModuleDependency = require("./ModuleDependency");
24
24
  /** @typedef {import("../ModuleGraph")} ModuleGraph */
25
25
  /** @typedef {import("../javascript/JavascriptParser").Range} Range */
26
26
  /** @typedef {import("../ExportsInfo").ExportInfoName} ExportInfoName */
27
- /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
28
- /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
29
27
  /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
30
28
  /** @typedef {import("../util/chainedImports").IdRanges} IdRanges */
29
+ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[ExportInfoName[], IdRanges | undefined, boolean, undefined | boolean]>} ObjectDeserializerContext */
30
+ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[ExportInfoName[], IdRanges | undefined, boolean, undefined | boolean]>} ObjectSerializerContext */
31
31
 
32
32
  class CommonJsFullRequireDependency extends ModuleDependency {
33
33
  /**
@@ -45,7 +45,9 @@ class CommonJsFullRequireDependency extends ModuleDependency {
45
45
  ) {
46
46
  super(request);
47
47
  this.range = range;
48
+ /** @type {string[]} */
48
49
  this.names = names;
50
+ /** @type {IdRanges | undefined} */
49
51
  this.idRanges = idRanges;
50
52
  /** @type {boolean} */
51
53
  this.call = false;
@@ -61,6 +63,7 @@ class CommonJsFullRequireDependency extends ModuleDependency {
61
63
  */
62
64
  getReferencedExports(moduleGraph, runtime) {
63
65
  const importedModule = moduleGraph.getModule(this);
66
+ // CommonJS property access is never rewritten to a literal, so it can't inline
64
67
  if (
65
68
  importedModule &&
66
69
  isRequireEsmModuleExportsModule(importedModule, moduleGraph)
@@ -68,16 +71,16 @@ class CommonJsFullRequireDependency extends ModuleDependency {
68
71
  // When `require(esm)` unwraps a `"module.exports"` named export, the
69
72
  // user's property access lands on that value (which webpack does not
70
73
  // model), so only the "module.exports" export itself is referenced.
71
- return [[ESM_MODULE_EXPORTS_NAME]];
74
+ return [{ name: [ESM_MODULE_EXPORTS_NAME], canInline: false }];
72
75
  }
73
76
  if (
74
77
  this.call &&
75
78
  (!importedModule ||
76
79
  importedModule.getExportsType(moduleGraph, false) !== "namespace")
77
80
  ) {
78
- return [this.names.slice(0, -1)];
81
+ return [{ name: this.names.slice(0, -1), canInline: false }];
79
82
  }
80
- return [this.names];
83
+ return [{ name: this.names, canInline: false }];
81
84
  }
82
85
 
83
86
  /**
@@ -85,11 +88,11 @@ class CommonJsFullRequireDependency extends ModuleDependency {
85
88
  * @param {ObjectSerializerContext} context context
86
89
  */
87
90
  serialize(context) {
88
- const { write } = context;
89
- write(this.names);
90
- write(this.idRanges);
91
- write(this.call);
92
- write(this.asiSafe);
91
+ context
92
+ .write(this.names)
93
+ .write(this.idRanges)
94
+ .write(this.call)
95
+ .write(this.asiSafe);
93
96
  super.serialize(context);
94
97
  }
95
98
 
@@ -98,12 +101,14 @@ class CommonJsFullRequireDependency extends ModuleDependency {
98
101
  * @param {ObjectDeserializerContext} context context
99
102
  */
100
103
  deserialize(context) {
101
- const { read } = context;
102
- this.names = read();
103
- this.idRanges = read();
104
- this.call = read();
105
- this.asiSafe = read();
106
- super.deserialize(context);
104
+ this.names = context.read();
105
+ const c1 = context.rest;
106
+ this.idRanges = c1.read();
107
+ const c2 = c1.rest;
108
+ this.call = c2.read();
109
+ const c3 = c2.rest;
110
+ this.asiSafe = c3.read();
111
+ super.deserialize(c3.rest);
107
112
  }
108
113
 
109
114
  get type() {
@@ -163,9 +168,12 @@ CommonJsFullRequireDependency.Template = class CommonJsFullRequireDependencyTemp
163
168
  ? `(${requireExpr}${access})`
164
169
  : `${requireExpr}${access}`;
165
170
  } else if (importedModule) {
166
- const usedImported = moduleGraph
167
- .getExportsInfo(importedModule)
168
- .getUsedName(trimmedIds, runtime);
171
+ // CJS exports are never inlined
172
+ const usedImported = /** @type {string | string[] | false} */ (
173
+ moduleGraph
174
+ .getExportsInfo(importedModule)
175
+ .getUsedName(trimmedIds, runtime)
176
+ );
169
177
  if (usedImported) {
170
178
  const comment = equals(usedImported, trimmedIds)
171
179
  ? ""
@@ -14,6 +14,7 @@ const {
14
14
  expressionIsUnsupported,
15
15
  toConstantDependency
16
16
  } = require("../javascript/JavascriptParserHelpers");
17
+ const memoize = require("../util/memoize");
17
18
  const traverseDestructuringAssignmentProperties = require("../util/traverseDestructuringAssignmentProperties");
18
19
  const CommonJsFullRequireDependency = require("./CommonJsFullRequireDependency");
19
20
  const CommonJsRequireContextDependency = require("./CommonJsRequireContextDependency");
@@ -53,12 +54,11 @@ const RequireResolveHeaderDependency = require("./RequireResolveHeaderDependency
53
54
  * member-access references on `NAME` to the `CommonJsRequireDependency`
54
55
  * created for the `require()` call.
55
56
  * @typedef {object} RequireBindingData
56
- * @property {RawReferencedExports} referencedExports mutable list shared with the dependency; pushed to as `NAME.x.y` accesses are walked
57
- * @property {InstanceType<typeof import("./CommonJsRequireDependency")> | null} dep dependency for the `require()` call (assigned during walk)
57
+ * @property {RawReferencedExports | null} referencedExports mutable list, pushed to as `NAME.x.y` accesses are walked; null means the whole namespace is used. Separate from `dep` so a deferred require (walked after accesses on `NAME`) still collects them; becomes the dep's shared list on creation.
58
+ * @property {InstanceType<typeof import("./CommonJsRequireDependency")> | null} dep dependency for the `require()` call, null until the `require()` is walked; kept so a later whole-namespace use can null its list directly
58
59
  */
59
60
 
60
- /** @type {WeakMap<CallExpression, RequireBindingData>} */
61
- const requireBindingData = new WeakMap();
61
+ const getHarmonyImportGuard = memoize(() => require("./HarmonyImportGuard"));
62
62
 
63
63
  const REQUIRE_BINDING_TAG = Symbol(
64
64
  "CommonJsImportsParserPlugin require binding"
@@ -157,9 +157,15 @@ const createRequireAsExpressionHandler =
157
157
  * @param {JavascriptParser} parser parser
158
158
  * @param {JavascriptParserOptions} options options
159
159
  * @param {() => undefined | string} getContext context accessor
160
+ * @param {WeakMap<CallExpression, RequireBindingData>=} requireBindingData per-parser `const NAME = require(LITERAL)` binding state; only the free-`require` plugin populates it
160
161
  * @returns {(callNew: boolean) => (expr: CallExpression | NewExpression) => (boolean | void)} handler factory
161
162
  */
162
- const createRequireCallHandler = (parser, options, getContext) => {
163
+ const createRequireCallHandler = (
164
+ parser,
165
+ options,
166
+ getContext,
167
+ requireBindingData
168
+ ) => {
163
169
  /**
164
170
  * Process require item.
165
171
  * @param {CallExpression | NewExpression} expr expression
@@ -172,9 +178,9 @@ const createRequireCallHandler = (parser, options, getContext) => {
172
178
  parser,
173
179
  expr
174
180
  );
175
- const binding = requireBindingData.get(
176
- /** @type {CallExpression} */ (expr)
177
- );
181
+ const binding =
182
+ requireBindingData &&
183
+ requireBindingData.get(/** @type {CallExpression} */ (expr));
178
184
  if (binding && !referencedExports) {
179
185
  // `const NAME = require(LITERAL)` — let later member-access walks
180
186
  // on `NAME` populate the dependency's referenced exports.
@@ -191,6 +197,7 @@ const createRequireCallHandler = (parser, options, getContext) => {
191
197
  dep.loc = /** @type {DependencyLocation} */ (expr.loc);
192
198
  dep.optional = Boolean(parser.scope.inTry);
193
199
  parser.state.current.addDependency(dep);
200
+ getHarmonyImportGuard().attachDependencyGuards(parser, dep);
194
201
  return true;
195
202
  }
196
203
  };
@@ -436,6 +443,7 @@ class CommonJsImportsParserPlugin {
436
443
  * @param {JavascriptParserOptions} options parser options
437
444
  */
438
445
  constructor(options) {
446
+ /** @type {JavascriptParserOptions} */
439
447
  this.options = options;
440
448
  }
441
449
 
@@ -577,10 +585,13 @@ class CommonJsImportsParserPlugin {
577
585
  * @param {boolean} callNew true, when require is called with new
578
586
  * @returns {(expr: CallExpression | NewExpression) => (boolean | void)} handler
579
587
  */
588
+ /** @type {WeakMap<CallExpression, RequireBindingData>} */
589
+ const requireBindingData = new WeakMap();
580
590
  const createRequireHandler = createRequireCallHandler(
581
591
  parser,
582
592
  options,
583
- getContext
593
+ getContext,
594
+ requireBindingData
584
595
  );
585
596
  parser.hooks.call
586
597
  .for("require")
@@ -727,11 +738,15 @@ class CommonJsImportsParserPlugin {
727
738
  const binding =
728
739
  /** @type {RequireBindingData} */
729
740
  (parser.currentTagData);
730
- if (binding && binding.dep) {
741
+ if (binding) {
731
742
  // `NAME` is read as a value (not as the object of a static member
732
743
  // chain), so we have to assume the whole exports object is used.
733
- binding.dep.referencedExports = null;
744
+ binding.referencedExports = null;
745
+ if (binding.dep) {
746
+ binding.dep.referencedExports = null;
747
+ }
734
748
  }
749
+ return true;
735
750
  });
736
751
 
737
752
  parser.hooks.expressionMemberChain
@@ -740,8 +755,8 @@ class CommonJsImportsParserPlugin {
740
755
  const binding =
741
756
  /** @type {RequireBindingData} */
742
757
  (parser.currentTagData);
743
- if (binding && binding.dep && binding.dep.referencedExports) {
744
- binding.dep.referencedExports.push(members);
758
+ if (binding && binding.referencedExports) {
759
+ binding.referencedExports.push(members);
745
760
  }
746
761
  // Returning truthy suppresses the parser's fallback chain (which
747
762
  // would otherwise walk `NAME` as a bare expression and trigger our
@@ -755,18 +770,38 @@ class CommonJsImportsParserPlugin {
755
770
  const binding =
756
771
  /** @type {RequireBindingData} */
757
772
  (parser.currentTagData);
758
- if (binding && binding.dep && binding.dep.referencedExports) {
773
+ if (binding && binding.referencedExports) {
759
774
  if (members.length === 0) {
760
775
  // `NAME(...)` — calling the require result directly; the
761
776
  // whole exports object is observable.
762
- binding.dep.referencedExports = null;
777
+ binding.referencedExports = null;
778
+ if (binding.dep) {
779
+ binding.dep.referencedExports = null;
780
+ }
763
781
  } else {
764
- binding.dep.referencedExports.push(members);
782
+ binding.referencedExports.push(members);
765
783
  }
766
784
  }
767
785
  if (expr.arguments) parser.walkExpressions(expr.arguments);
768
786
  return true;
769
787
  });
788
+
789
+ // Re-exporting the binding lets the require result escape, so the whole
790
+ // exports object is observable. Stage -10 runs before the harmony bail tap.
791
+ parser.hooks.exportSpecifier.tap(
792
+ { name: PLUGIN_NAME, stage: -10 },
793
+ (_statement, id) => {
794
+ const binding =
795
+ /** @type {RequireBindingData | undefined} */
796
+ (parser.getTagData(id, REQUIRE_BINDING_TAG));
797
+ if (binding) {
798
+ binding.referencedExports = null;
799
+ if (binding.dep) {
800
+ binding.dep.referencedExports = null;
801
+ }
802
+ }
803
+ }
804
+ );
770
805
  // #endregion
771
806
 
772
807
  // #region Require.resolve
@@ -281,7 +281,7 @@ class HarmonyModuleDecoratorRuntimeModule extends RuntimeModule {
281
281
  "Object.defineProperty(module, 'exports', {",
282
282
  Template.indent([
283
283
  "enumerable: true,",
284
- `set: ${runtimeTemplate.basicFunction("", [
284
+ `${runtimeTemplate.method("set", "", [
285
285
  "throw new Error('ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ' + module.id);"
286
286
  ])}`
287
287
  ]),
@@ -11,8 +11,8 @@ const ContextDependency = require("./ContextDependency");
11
11
  const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall");
12
12
 
13
13
  /** @typedef {import("../javascript/JavascriptParser").Range} Range */
14
- /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
15
- /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
14
+ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Range, Range | undefined, boolean | string | undefined]>} ObjectDeserializerContext */
15
+ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Range, Range | undefined, boolean | string | undefined]>} ObjectSerializerContext */
16
16
  /** @typedef {import("./ContextDependency").ContextDependencyOptions} ContextDependencyOptions */
17
17
  /** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
18
18
  /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
@@ -34,6 +34,7 @@ class CommonJsRequireContextDependency extends ContextDependency {
34
34
  this.range = range;
35
35
  this.valueRange = valueRange;
36
36
  // inShorthand must be serialized by subclasses that use it
37
+ /** @type {string | boolean | undefined} */
37
38
  this.inShorthand = inShorthand;
38
39
  }
39
40
 
@@ -62,11 +63,7 @@ class CommonJsRequireContextDependency extends ContextDependency {
62
63
  * @param {ObjectSerializerContext} context context
63
64
  */
64
65
  serialize(context) {
65
- const { write } = context;
66
-
67
- write(this.range);
68
- write(this.valueRange);
69
- write(this.inShorthand);
66
+ context.write(this.range).write(this.valueRange).write(this.inShorthand);
70
67
 
71
68
  super.serialize(context);
72
69
  }
@@ -76,13 +73,13 @@ class CommonJsRequireContextDependency extends ContextDependency {
76
73
  * @param {ObjectDeserializerContext} context context
77
74
  */
78
75
  deserialize(context) {
79
- const { read } = context;
80
-
81
- this.range = read();
82
- this.valueRange = read();
83
- this.inShorthand = read();
76
+ this.range = context.read();
77
+ const c1 = context.rest;
78
+ this.valueRange = c1.read();
79
+ const c2 = c1.rest;
80
+ this.inShorthand = c2.read();
84
81
 
85
- super.deserialize(context);
82
+ super.deserialize(c2.rest);
86
83
  }
87
84
  }
88
85