webpack 5.112.0 → 5.112.1

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 +152 -60
  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 -37
  28. package/lib/css/CssModule.js +10 -0
  29. package/lib/css/CssModulesPlugin.js +2 -2
  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 +1159 -298
  34. package/lib/css/syntax-printer.js +90 -35
  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 +2 -6
  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 +98 -15
  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 +16 -18
  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
@@ -23,7 +23,7 @@ const ModuleDependency = require("../core/ModuleDependency");
23
23
  * JavascriptParserOptions
24
24
  * } from "../../../declarations/WebpackOptions"
25
25
  */
26
- /** @import { ReferencedExports, DependencyKinds, LazyRole } from "../../graph/Dependency" */
26
+ /** @import { ReferencedExports, DependencyKinds, LazyRole, UpdateHashContext } from "../../graph/Dependency" */
27
27
  /** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
28
28
  /** @import ExportsInfo from "../../graph/ExportsInfo" */
29
29
  /** @import Module, { BuildMeta } from "../../module/Module" */
@@ -37,6 +37,7 @@ const ModuleDependency = require("../core/ModuleDependency");
37
37
  * ObjectSerializerContext
38
38
  * } from "../../serialization/ObjectMiddleware"
39
39
  */
40
+ /** @import Hash from "../../util/Hash" */
40
41
  /** @import { RuntimeSpec } from "../../util/runtime" */
41
42
 
42
43
  /** @typedef {0 | 1 | 2 | 3} ExportPresenceMode */
@@ -146,6 +147,20 @@ class ESMImportDependency extends ModuleDependency {
146
147
  return "esm";
147
148
  }
148
149
 
150
+ /**
151
+ * Updates the hash with the data contributed by this instance.
152
+ * @param {Hash} hash hash to be updated
153
+ * @param {UpdateHashContext} context context
154
+ * @returns {void}
155
+ */
156
+ updateHash(hash, context) {
157
+ // neither a deferred nor a missing target has a connection, yet only a
158
+ // missing one renders a throw
159
+ if (LazyBarrels.isLazy(context.chunkGraph.moduleGraph, this)) {
160
+ hash.update("lazy");
161
+ }
162
+ }
163
+
149
164
  /**
150
165
  * Returns how this dependency may be deferred when its parent module is side-effect-free (lazy barrel optimization).
151
166
  * @returns {LazyRole | null} lazy classification, null when it must be processed eagerly
@@ -522,7 +537,7 @@ ESMImportDependency.Template = class ESMImportDependencyTemplate extends (
522
537
  // When the consuming module is emitted as a generator (target without
523
538
  // `async`/`await`), the dependency `await` must become `yield`.
524
539
  const generatorLowered = isGeneratorLowered(
525
- /** @type {Module} */ (module),
540
+ module,
526
541
  moduleGraph,
527
542
  templateContext.runtimeTemplate
528
543
  );
@@ -50,7 +50,6 @@ const ESMImportSpecifierDependency = require("./ESMImportSpecifierDependency");
50
50
  * } from "../../../declarations/WebpackOptions"
51
51
  */
52
52
  /** @import Module from "../../module/Module" */
53
- /** @import { JavascriptModuleBuildInfo } from "../../javascript/JavascriptModule" */
54
53
  /**
55
54
  * @import JavascriptParser, {
56
55
  * ImportAttributes,
@@ -155,14 +154,11 @@ const findImportSpecifier = (parser, node) => {
155
154
  (parser.getTagData(node.name, harmonySpecifierTag))
156
155
  );
157
156
  case "UnaryExpression":
158
- return findImportSpecifier(
159
- parser,
160
- /** @type {Expression} */ (node.argument)
161
- );
157
+ return findImportSpecifier(parser, node.argument);
162
158
  case "LogicalExpression":
163
159
  return (
164
- findImportSpecifier(parser, /** @type {Expression} */ (node.left)) ||
165
- findImportSpecifier(parser, /** @type {Expression} */ (node.right))
160
+ findImportSpecifier(parser, node.left) ||
161
+ findImportSpecifier(parser, node.right)
166
162
  );
167
163
  case "MemberExpression":
168
164
  return findImportSpecifier(
@@ -171,10 +167,7 @@ const findImportSpecifier = (parser, node) => {
171
167
  );
172
168
  case "BinaryExpression":
173
169
  // `"x" in ns` presence guard
174
- return (
175
- node.operator === "in" &&
176
- findImportSpecifier(parser, /** @type {Expression} */ (node.right))
177
- );
170
+ return node.operator === "in" && findImportSpecifier(parser, node.right);
178
171
  default:
179
172
  return false;
180
173
  }
@@ -444,8 +437,7 @@ class ESMImportDependencyParserPlugin {
444
437
  * @returns {void}
445
438
  */
446
439
  const bailOutOfConcatenation = () => {
447
- /** @type {JavascriptModuleBuildInfo} */
448
- (parser.state.module.buildInfo).moduleConcatenationBailout =
440
+ parser.state.module.buildInfo.moduleConcatenationBailout =
449
441
  "assignment to an imported binding";
450
442
  };
451
443
 
@@ -483,10 +475,7 @@ class ESMImportDependencyParserPlugin {
483
475
  });
484
476
  parser.hooks.write.for(harmonySpecifierTag).tap(PLUGIN_NAME, (node) => {
485
477
  const written = writeTargetOf(node);
486
- const target =
487
- written.type === "Identifier"
488
- ? /** @type {Identifier} */ (written)
489
- : undefined;
478
+ const target = written.type === "Identifier" ? written : undefined;
490
479
  markImportedBindingWrite(node, target);
491
480
  // A loop's binding walks nothing on its own, so the reference the
492
481
  // write renders as has to be walked here to exist at all.
@@ -74,7 +74,7 @@ const buildLivenessFormula = (test, depByRangeStart) => {
74
74
  */
75
75
  const build = (node) => {
76
76
  if (node.type === "UnaryExpression" && node.operator === "!") {
77
- return ["!", build(/** @type {Expression} */ (node.argument))];
77
+ return ["!", build(node.argument)];
78
78
  }
79
79
  if (
80
80
  node.type === "LogicalExpression" &&
@@ -82,14 +82,9 @@ const buildLivenessFormula = (test, depByRangeStart) => {
82
82
  node.operator === "||" ||
83
83
  node.operator === "??")
84
84
  ) {
85
- return [
86
- node.operator,
87
- build(/** @type {Expression} */ (node.left)),
88
- build(/** @type {Expression} */ (node.right))
89
- ];
85
+ return [node.operator, build(node.left), build(node.right)];
90
86
  }
91
- const dep =
92
- node.range && depByRangeStart.get(/** @type {number} */ (node.range[0]));
87
+ const dep = node.range && depByRangeStart.get(node.range[0]);
93
88
  return dep ? ["v", dep] : ["?"];
94
89
  };
95
90
 
@@ -243,23 +238,19 @@ const buildPresenceFormula = (parser, test, depByRangeStart) => {
243
238
  */
244
239
  const build = (node) => {
245
240
  if (node.type === "UnaryExpression" && node.operator === "!") {
246
- return ["!", build(/** @type {Expression} */ (node.argument))];
241
+ return ["!", build(node.argument)];
247
242
  }
248
243
  if (node.type === "LogicalExpression") {
249
244
  if (node.operator === "&&") {
250
- return [
251
- "&&",
252
- build(/** @type {Expression} */ (node.left)),
253
- build(/** @type {Expression} */ (node.right))
254
- ];
245
+ return ["&&", build(node.left), build(node.right)];
255
246
  }
256
247
 
257
248
  if (node.operator === "||") {
258
249
  if (parser.evaluateExpression(node.left).asBool() === false) {
259
- return build(/** @type {Expression} */ (node.right));
250
+ return build(node.right);
260
251
  }
261
252
  if (parser.evaluateExpression(node.right).asBool() === false) {
262
- return build(/** @type {Expression} */ (node.left));
253
+ return build(node.left);
263
254
  }
264
255
  return ["?"];
265
256
  }
@@ -267,16 +258,15 @@ const buildPresenceFormula = (parser, test, depByRangeStart) => {
267
258
  if (node.operator === "??") {
268
259
  const nullish = parser.evaluateExpression(node.left).asNullish();
269
260
  if (nullish === true) {
270
- return build(/** @type {Expression} */ (node.right));
261
+ return build(node.right);
271
262
  }
272
263
  if (nullish === false) {
273
- return build(/** @type {Expression} */ (node.left));
264
+ return build(node.left);
274
265
  }
275
266
  return ["?"];
276
267
  }
277
268
  }
278
- const dep =
279
- node.range && depByRangeStart.get(/** @type {number} */ (node.range[0]));
269
+ const dep = node.range && depByRangeStart.get(node.range[0]);
280
270
  return dep ? ["p", dep] : ["?"];
281
271
  };
282
272
 
@@ -298,9 +288,7 @@ const buildPresenceFormula = (parser, test, depByRangeStart) => {
298
288
  const evalPresenceFormula = (formula, name, member, needTruthy) => {
299
289
  switch (formula[0]) {
300
290
  case "p": {
301
- const dep = /** @type {ESMEvaluatedImportSpecifierDependency} */ (
302
- formula[1]
303
- );
291
+ const dep = formula[1];
304
292
  return (
305
293
  needTruthy &&
306
294
  dep.directImport === true &&
@@ -279,7 +279,7 @@ ESMImportSideEffectDependency.Template = class ESMImportSideEffectDependencyTemp
279
279
  const exportsType = importedModule.getExportsType(
280
280
  moduleGraph,
281
281
  /** @type {JavascriptModuleBuildMeta} */
282
- (/** @type {Module} */ (module).buildMeta).strictHarmonyModule
282
+ (module.buildMeta).strictHarmonyModule
283
283
  );
284
284
  if (exportsType !== "namespace") return;
285
285
  const importVar = dep.getImportVar(moduleGraph);
@@ -169,7 +169,10 @@ class ESMImportSpecifierDependency extends ESMImportDependency {
169
169
  * @returns {string | true | null} export name, true for all exports, null for none
170
170
  */
171
171
  get forwardId() {
172
- return this.ids.length > 0 ? this.ids[0] : true;
172
+ // SideEffectsFlagPlugin keeps a call with the namespace as `this` on the
173
+ // barrel, so the barrel runs whole
174
+ if (this.ids.length === 0 || this.namespaceObjectAsContext) return true;
175
+ return this.ids[0];
173
176
  }
174
177
 
175
178
  /**
@@ -0,0 +1,311 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Alexander Akait @alexander-akait
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const RuntimeGlobals = require("../../runtime/RuntimeGlobals");
9
+ const makeSerializable = require("../../util/makeSerializable");
10
+ const { propertyAccess } = require("../../util/property");
11
+ const { getRuntimeKey } = require("../../util/runtime");
12
+ const NullDependency = require("../core/NullDependency");
13
+
14
+ // ReplaceSource sorts fractional positions before flooring them to source offsets.
15
+ // Value wrappers open before sync calls and close after them; statement inserts
16
+ // and body closures follow both.
17
+ const SYNC_START_OFFSET = 0.5;
18
+ const VALUE_END_OFFSET = 0.5;
19
+ const AFTER_VALUE_OFFSET = 0.75;
20
+
21
+ /** @import { ReplaceSource } from "webpack-sources" */
22
+ /** @import Dependency, { DependencyKinds, UpdateHashContext } from "../../graph/Dependency" */
23
+ /** @import ModuleGraph, { LiveBinding } from "../../graph/ModuleGraph" */
24
+ /** @import Module from "../../module/Module" */
25
+ /** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
26
+ /** @import Hash from "../../util/Hash" */
27
+ /** @import { RuntimeSpec } from "../../util/runtime" */
28
+ /** @typedef {import("../../serialization/ObjectMiddleware").ObjectDeserializerContext<[string[], number[], number[], boolean, boolean, boolean]>} ObjectDeserializerContext */
29
+ /** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<[string[], number[], number[], boolean, boolean, boolean]>} ObjectSerializerContext */
30
+
31
+ /**
32
+ * The names the chunk's scope declares for a declaration, and how the module keeps them.
33
+ * @typedef {object} HoistedNames
34
+ * @property {string[]} names the declared name of each name
35
+ * @property {boolean} mirrored whether the module keeps its declaration and mirrors each write
36
+ * @property {boolean} registered whether it mirrors them through the setters a module library registers
37
+ */
38
+
39
+ /**
40
+ * A module-level binding a module library may declare in the chunk's scope under a
41
+ * reserved name, so the library exports the binding itself; it holds every reference,
42
+ * and every write a module keeping its declaration mirrors there instead.
43
+ */
44
+ class ESMLiveExportDependency extends NullDependency {
45
+ /**
46
+ * Creates an instance of ESMLiveExportDependency.
47
+ * @param {string[]} names every name the declaration declares
48
+ * @param {number[]} occurrences flat triples of start, end and `name index * KINDS_PER_NAME + kind` renaming takes, a kind from {@link ESMLiveExportDependency.REFERENCE}
49
+ * @param {number[]} mirrors the same triples for where a module keeping its declaration mirrors a write, a kind from {@link ESMLiveExportDependency.SYNC}
50
+ * @param {boolean} readByEval whether the module calls `eval`, which reads the names as written
51
+ * @param {boolean} hot whether a hot update replaces the module, which then only mirrors, as renaming cannot reach it
52
+ * @param {boolean} earlyUse whether the module uses a `let` or class above its declaration, where only its own declaration throws
53
+ */
54
+ constructor(names, occurrences, mirrors, readByEval, hot, earlyUse) {
55
+ super();
56
+ this.names = names;
57
+ this.occurrences = occurrences;
58
+ this.mirrors = mirrors;
59
+ this.readByEval = readByEval;
60
+ this.hot = hot;
61
+ this.earlyUse = earlyUse;
62
+ }
63
+
64
+ get type() {
65
+ return "harmony live export";
66
+ }
67
+
68
+ /**
69
+ * Returns the names the chunk's scope declares for this runtime, if it does.
70
+ * @param {ModuleGraph} moduleGraph the module graph
71
+ * @param {RuntimeSpec} runtime the runtime the module is generated for
72
+ * @returns {HoistedNames | undefined} the names and how the module keeps them
73
+ */
74
+ getHoistedNames(moduleGraph, runtime) {
75
+ const meta = moduleGraph.getMetaIfExisting(
76
+ /** @type {Module} */ (moduleGraph.getParentModule(this))
77
+ );
78
+ if (meta === undefined) return;
79
+ const runtimeKey = getRuntimeKey(runtime);
80
+ for (const bindings of [meta.liveSetters, meta.liveBindings]) {
81
+ if (bindings === undefined) continue;
82
+ const first = bindings.get(this.names[0]);
83
+ if (first === undefined || !first.runtimes.has(runtimeKey)) continue;
84
+ return {
85
+ names: this.names.map(
86
+ (name) => /** @type {LiveBinding} */ (bindings.get(name)).name
87
+ ),
88
+ mirrored: bindings === meta.liveSetters,
89
+ registered: first.registered.has(runtimeKey)
90
+ };
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Updates the hash with the data contributed by this instance.
96
+ * @param {Hash} hash hash to be updated
97
+ * @param {UpdateHashContext} context context
98
+ * @returns {void}
99
+ */
100
+ updateHash(hash, context) {
101
+ const hoisted = this.getHoistedNames(
102
+ context.chunkGraph.moduleGraph,
103
+ context.runtime
104
+ );
105
+ if (hoisted === undefined) return;
106
+ hash.update(
107
+ `${hoisted.names.join()}|${hoisted.mirrored}|${hoisted.registered}`
108
+ );
109
+ }
110
+
111
+ /**
112
+ * Serializes this instance into the provided serializer context.
113
+ * @param {ObjectSerializerContext} context context
114
+ */
115
+ serialize(context) {
116
+ context
117
+ .write(this.names)
118
+ .write(this.occurrences)
119
+ .write(this.mirrors)
120
+ .write(this.readByEval)
121
+ .write(this.hot)
122
+ .write(this.earlyUse);
123
+ super.serialize(context);
124
+ }
125
+
126
+ /**
127
+ * Restores this instance from the provided deserializer context.
128
+ * @param {ObjectDeserializerContext} context context
129
+ */
130
+ deserialize(context) {
131
+ this.names = context.read();
132
+ const c1 = context.rest;
133
+ this.occurrences = c1.read();
134
+ const c2 = c1.rest;
135
+ this.mirrors = c2.read();
136
+ const c3 = c2.rest;
137
+ this.readByEval = c3.read();
138
+ const c4 = c3.rest;
139
+ this.hot = c4.read();
140
+ const c5 = c4.rest;
141
+ this.earlyUse = c5.read();
142
+ super.deserialize(c5.rest);
143
+ }
144
+ }
145
+
146
+ makeSerializable(
147
+ ESMLiveExportDependency,
148
+ "webpack/lib/dependencies/esm/ESMLiveExportDependency"
149
+ );
150
+
151
+ ESMLiveExportDependency.Template = class ESMLiveExportDependencyTemplate extends (
152
+ NullDependency.Template
153
+ ) {
154
+ /**
155
+ * Applies the plugin by registering its hooks on the compiler.
156
+ * @param {Dependency} dependency the dependency for which the template should be applied
157
+ * @param {ReplaceSource} source the current replace source which can be modified
158
+ * @param {DependencyTemplateContext} templateContext the context object
159
+ * @returns {void}
160
+ */
161
+ apply(
162
+ dependency,
163
+ source,
164
+ { moduleGraph, chunkGraph, module, runtime, runtimeRequirements }
165
+ ) {
166
+ const dep = /** @type {ESMLiveExportDependency} */ (dependency);
167
+ const hoisted = dep.getHoistedNames(moduleGraph, runtime);
168
+ if (hoisted === undefined) return;
169
+ const { occurrences, mirrors, names } = dep;
170
+ const kinds = ESMLiveExportDependency.KINDS_PER_NAME;
171
+ if (hoisted.mirrored) {
172
+ const id = hoisted.registered
173
+ ? JSON.stringify(chunkGraph.getModuleId(module))
174
+ : "";
175
+ if (hoisted.registered) runtimeRequirements.add(RuntimeGlobals.require);
176
+ /** @type {string[]} */
177
+ const helpers = [];
178
+ for (let index = 0; index < names.length; index++) {
179
+ const name = names[index];
180
+ const helper = `__webpack_live_sync_${name}__`;
181
+ helpers.push(helper);
182
+ // Hoisted, so any write may call it; a value passed with `v` set is what
183
+ // the binding is about to hold, read before the binding is initialized.
184
+ const sync = hoisted.registered
185
+ ? `\tvar s = ${RuntimeGlobals.liveExportSetters};\n` +
186
+ `\ts = s && s[${id}];\n` +
187
+ `\ts = s && s${propertyAccess([name])};\n` +
188
+ `\tif (s) for (var i = 0; i < s.length; i++) s[i](v ? r : ${name});\n`
189
+ : `\t${hoisted.names[index]} = v ? r : ${name};\n`;
190
+ source.insert(
191
+ 0,
192
+ `function ${helper}(r, v) {\n${
193
+ // an eval() call may come before the binding is initialized
194
+ dep.readByEval
195
+ ? `\ttry {\n\t${sync.replace(/\n\t/g, "\n\t\t")}\t} catch (e) {}\n`
196
+ : sync
197
+ }\treturn r;\n}\n`
198
+ );
199
+ }
200
+ for (let i = 0; i < mirrors.length; i += 3) {
201
+ const start = mirrors[i];
202
+ const end = mirrors[i + 1];
203
+ const helper = helpers[Math.floor(mirrors[i + 2] / kinds)];
204
+ switch (mirrors[i + 2] % kinds) {
205
+ case ESMLiveExportDependency.SYNC:
206
+ source.insert(start + SYNC_START_OFFSET, `${helper}(`);
207
+ source.insert(end, ")");
208
+ break;
209
+ case ESMLiveExportDependency.VALUE:
210
+ source.insert(start, `${helper}((`);
211
+ source.insert(end + VALUE_END_OFFSET, "), 1)");
212
+ break;
213
+ case ESMLiveExportDependency.NAMED_VALUE: {
214
+ // A property key gives the function or class the name the binding did
215
+ const name = names[Math.floor(mirrors[i + 2] / kinds)];
216
+ source.insert(start, `${helper}({ ${name}: `);
217
+ source.insert(end + VALUE_END_OFFSET, ` }.${name}, 1)`);
218
+ break;
219
+ }
220
+ case ESMLiveExportDependency.UNDEFINED:
221
+ source.insert(start, ` = ${helper}(void 0, 1)`);
222
+ break;
223
+ case ESMLiveExportDependency.STATEMENT:
224
+ source.insert(start + AFTER_VALUE_OFFSET, `;${helper}();\n`);
225
+ break;
226
+ case ESMLiveExportDependency.DECLARATOR:
227
+ source.insert(
228
+ start + AFTER_VALUE_OFFSET,
229
+ `, __webpack_live_synced__ = ${helper}()`
230
+ );
231
+ break;
232
+ default:
233
+ source.insert(start, `{;${helper}();`);
234
+ source.insert(end + AFTER_VALUE_OFFSET, "}");
235
+ }
236
+ }
237
+ return;
238
+ }
239
+ for (let i = 0; i < occurrences.length; i += 3) {
240
+ const start = occurrences[i];
241
+ const end = occurrences[i + 1];
242
+ const index = Math.floor(occurrences[i + 2] / kinds);
243
+ switch (occurrences[i + 2] % kinds) {
244
+ case ESMLiveExportDependency.KEYWORD:
245
+ source.replace(start, end - 1, "");
246
+ break;
247
+ case ESMLiveExportDependency.REFERENCE:
248
+ source.replace(start, end - 1, hoisted.names[index]);
249
+ break;
250
+ case ESMLiveExportDependency.SHORTHAND:
251
+ source.replace(
252
+ start,
253
+ end - 1,
254
+ `${names[index]}: ${hoisted.names[index]}`
255
+ );
256
+ break;
257
+ case ESMLiveExportDependency.NAMED:
258
+ // A property key gives the function or class the name the binding did
259
+ source.insert(start, `{ ${names[index]}: `);
260
+ source.insert(end, ` }.${names[index]}`);
261
+ break;
262
+ case ESMLiveExportDependency.PARENTHESIZED:
263
+ source.insert(start, "(");
264
+ source.insert(end, ")");
265
+ break;
266
+ default:
267
+ source.insert(
268
+ start,
269
+ `\n${hoisted.names[index]} = ${names[index]};\n`
270
+ );
271
+ }
272
+ }
273
+ }
274
+ };
275
+
276
+ /** An occurrence the hoisted name replaces */
277
+ ESMLiveExportDependency.REFERENCE = 0;
278
+ /** A shorthand property, which keeps its key */
279
+ ESMLiveExportDependency.SHORTHAND = 1;
280
+ /** An anonymous function or class the binding names */
281
+ ESMLiveExportDependency.NAMED = 2;
282
+ /** A declaration's `let` or `var`, dropped with the space up to its first name */
283
+ ESMLiveExportDependency.KEYWORD = 3;
284
+ /** A destructuring declarator that, without its keyword, needs parentheses */
285
+ ESMLiveExportDependency.PARENTHESIZED = 4;
286
+ /** Where a function or class declaration, which stays, sets the binding */
287
+ ESMLiveExportDependency.INITIALIZE = 5;
288
+ /** A write a hot module mirrors once it ran, wrapped in the name's sync call */
289
+ ESMLiveExportDependency.SYNC = 6;
290
+ /** A declarator's initializer, mirrored as the value the binding takes */
291
+ ESMLiveExportDependency.VALUE = 7;
292
+ /** An anonymous function or class initializer, mirrored under the name it takes */
293
+ ESMLiveExportDependency.NAMED_VALUE = 8;
294
+ /** A declarator without an initializer, mirrored as `undefined` */
295
+ ESMLiveExportDependency.UNDEFINED = 9;
296
+ /** Where a statement mirrors the binding, after a declaration or a loop's next value */
297
+ ESMLiveExportDependency.STATEMENT = 10;
298
+ /** A loop body that is not a block, wrapped in one mirroring the loop's next value */
299
+ ESMLiveExportDependency.BODY = 11;
300
+ /** The end of a loop's `var` head, where a declarator mirrors what a pattern took */
301
+ ESMLiveExportDependency.DECLARATOR = 12;
302
+ /** How many kinds an occurrence's third number tells apart per name */
303
+ ESMLiveExportDependency.KINDS_PER_NAME = 13;
304
+
305
+ /** @type {Set<keyof DependencyKinds>} */
306
+ ESMLiveExportDependency.KINDS = new Set([
307
+ ...NullDependency.KINDS,
308
+ "esm-live-export"
309
+ ]);
310
+
311
+ module.exports = ESMLiveExportDependency;