webpack 5.100.2 → 5.101.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 (304) hide show
  1. package/README.md +12 -10
  2. package/bin/webpack.js +7 -7
  3. package/lib/APIPlugin.js +12 -14
  4. package/lib/AutomaticPrefetchPlugin.js +2 -2
  5. package/lib/BannerPlugin.js +4 -4
  6. package/lib/Cache.js +1 -1
  7. package/lib/CacheFacade.js +7 -7
  8. package/lib/CaseSensitiveModulesWarning.js +2 -2
  9. package/lib/Chunk.js +3 -3
  10. package/lib/ChunkGraph.js +21 -18
  11. package/lib/ChunkGroup.js +4 -4
  12. package/lib/CleanPlugin.js +19 -53
  13. package/lib/CodeGenerationResults.js +3 -3
  14. package/lib/CompatibilityPlugin.js +6 -6
  15. package/lib/Compilation.js +126 -113
  16. package/lib/Compiler.js +37 -37
  17. package/lib/ConcatenationScope.js +39 -1
  18. package/lib/ConstPlugin.js +11 -11
  19. package/lib/ContextExclusionPlugin.js +3 -3
  20. package/lib/ContextModule.js +12 -11
  21. package/lib/ContextModuleFactory.js +11 -11
  22. package/lib/ContextReplacementPlugin.js +5 -5
  23. package/lib/DefinePlugin.js +24 -22
  24. package/lib/DelegatedModuleFactoryPlugin.js +1 -1
  25. package/lib/DllEntryPlugin.js +1 -1
  26. package/lib/DllReferencePlugin.js +1 -1
  27. package/lib/DynamicEntryPlugin.js +3 -3
  28. package/lib/EntryPlugin.js +1 -1
  29. package/lib/EnvironmentPlugin.js +1 -1
  30. package/lib/ErrorHelpers.js +2 -2
  31. package/lib/EvalDevToolModulePlugin.js +2 -2
  32. package/lib/EvalSourceMapDevToolPlugin.js +5 -5
  33. package/lib/ExportsInfo.js +9 -9
  34. package/lib/ExportsInfoApiPlugin.js +2 -2
  35. package/lib/ExternalModule.js +163 -48
  36. package/lib/ExternalModuleFactoryPlugin.js +2 -2
  37. package/lib/ExternalsPlugin.js +45 -0
  38. package/lib/FileSystemInfo.js +64 -61
  39. package/lib/FlagAllModulesAsUsedPlugin.js +2 -2
  40. package/lib/FlagDependencyExportsPlugin.js +7 -7
  41. package/lib/FlagDependencyUsagePlugin.js +4 -4
  42. package/lib/FlagEntryExportAsUsedPlugin.js +1 -1
  43. package/lib/HotModuleReplacementPlugin.js +27 -27
  44. package/lib/IgnorePlugin.js +3 -3
  45. package/lib/IgnoreWarningsPlugin.js +4 -4
  46. package/lib/InitFragment.js +6 -0
  47. package/lib/InvalidDependenciesModuleWarning.js +1 -1
  48. package/lib/JavascriptMetaInfoPlugin.js +1 -1
  49. package/lib/LibManifestPlugin.js +2 -2
  50. package/lib/LoaderOptionsPlugin.js +1 -1
  51. package/lib/LoaderTargetPlugin.js +2 -2
  52. package/lib/MainTemplate.js +1 -1
  53. package/lib/Module.js +1 -1
  54. package/lib/ModuleFilenameHelpers.js +3 -3
  55. package/lib/ModuleGraph.js +9 -5
  56. package/lib/ModuleInfoHeaderPlugin.js +3 -3
  57. package/lib/ModuleParseError.js +5 -3
  58. package/lib/MultiCompiler.js +29 -24
  59. package/lib/MultiStats.js +18 -13
  60. package/lib/MultiWatching.js +1 -1
  61. package/lib/NoEmitOnErrorsPlugin.js +2 -2
  62. package/lib/NodeStuffPlugin.js +27 -15
  63. package/lib/NormalModule.js +23 -23
  64. package/lib/NormalModuleFactory.js +23 -21
  65. package/lib/NormalModuleReplacementPlugin.js +3 -3
  66. package/lib/PrefetchPlugin.js +1 -1
  67. package/lib/ProgressPlugin.js +11 -11
  68. package/lib/ProvidePlugin.js +2 -2
  69. package/lib/RecordIdsPlugin.js +3 -3
  70. package/lib/ResolverFactory.js +3 -3
  71. package/lib/RuntimeGlobals.js +20 -0
  72. package/lib/RuntimePlugin.js +16 -16
  73. package/lib/RuntimeTemplate.js +35 -26
  74. package/lib/SizeFormatHelpers.js +1 -1
  75. package/lib/SourceMapDevToolModuleOptionsPlugin.js +4 -4
  76. package/lib/SourceMapDevToolPlugin.js +9 -9
  77. package/lib/Stats.js +2 -2
  78. package/lib/Template.js +1 -1
  79. package/lib/TemplatedPathPlugin.js +2 -2
  80. package/lib/UseStrictPlugin.js +1 -1
  81. package/lib/WarnCaseSensitiveModulesPlugin.js +1 -1
  82. package/lib/WarnDeprecatedOptionPlugin.js +1 -1
  83. package/lib/WarnNoModeSetPlugin.js +1 -1
  84. package/lib/WatchIgnorePlugin.js +2 -2
  85. package/lib/Watching.js +12 -12
  86. package/lib/WebpackIsIncludedPlugin.js +2 -2
  87. package/lib/WebpackOptionsApply.js +3 -3
  88. package/lib/asset/AssetGenerator.js +1 -1
  89. package/lib/asset/AssetModulesPlugin.js +6 -6
  90. package/lib/async-modules/AsyncModuleHelpers.js +1 -1
  91. package/lib/async-modules/AwaitDependenciesInitFragment.js +36 -24
  92. package/lib/async-modules/InferAsyncModulesPlugin.js +3 -3
  93. package/lib/buildChunkGraph.js +5 -5
  94. package/lib/cache/AddBuildDependenciesPlugin.js +1 -1
  95. package/lib/cache/IdleFileCachePlugin.js +5 -5
  96. package/lib/cache/PackFileCacheStrategy.js +20 -20
  97. package/lib/cache/ResolverCachePlugin.js +4 -4
  98. package/lib/cli.js +196 -14
  99. package/lib/config/browserslistTargetHandler.js +6 -6
  100. package/lib/config/defaults.js +23 -14
  101. package/lib/config/normalization.js +41 -38
  102. package/lib/config/target.js +4 -4
  103. package/lib/container/ContainerEntryModule.js +3 -3
  104. package/lib/container/ContainerPlugin.js +3 -3
  105. package/lib/container/ContainerReferencePlugin.js +3 -3
  106. package/lib/container/FallbackModule.js +1 -1
  107. package/lib/container/HoistContainerReferencesPlugin.js +7 -7
  108. package/lib/container/options.js +4 -4
  109. package/lib/css/CssGenerator.js +5 -3
  110. package/lib/css/CssLoadingRuntimeModule.js +1 -1
  111. package/lib/css/CssModulesPlugin.js +25 -22
  112. package/lib/css/CssParser.js +5 -5
  113. package/lib/css/walkCssTokens.js +9 -9
  114. package/lib/debug/ProfilingPlugin.js +5 -5
  115. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +4 -4
  116. package/lib/dependencies/AMDPlugin.js +3 -3
  117. package/lib/dependencies/AMDRequireArrayDependency.js +1 -1
  118. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +3 -3
  119. package/lib/dependencies/CommonJsExportRequireDependency.js +2 -2
  120. package/lib/dependencies/CommonJsExportsParserPlugin.js +12 -10
  121. package/lib/dependencies/CommonJsImportsParserPlugin.js +21 -19
  122. package/lib/dependencies/CommonJsPlugin.js +2 -2
  123. package/lib/dependencies/ContextDependency.js +1 -1
  124. package/lib/dependencies/ContextDependencyHelpers.js +3 -3
  125. package/lib/dependencies/CssIcssExportDependency.js +2 -2
  126. package/lib/dependencies/CssLocalIdentifierDependency.js +2 -2
  127. package/lib/dependencies/CssUrlDependency.js +4 -4
  128. package/lib/dependencies/DynamicExports.js +5 -5
  129. package/lib/dependencies/HarmonyAcceptDependency.js +39 -5
  130. package/lib/dependencies/HarmonyDetectionParserPlugin.js +2 -2
  131. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +17 -13
  132. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +7 -8
  133. package/lib/dependencies/HarmonyExportInitFragment.js +1 -1
  134. package/lib/dependencies/HarmonyExports.js +1 -1
  135. package/lib/dependencies/HarmonyImportDependency.js +28 -9
  136. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +56 -48
  137. package/lib/dependencies/HarmonyImportSideEffectDependency.js +4 -5
  138. package/lib/dependencies/HarmonyImportSpecifierDependency.js +3 -4
  139. package/lib/dependencies/HarmonyModulesPlugin.js +2 -8
  140. package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +1 -1
  141. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +4 -4
  142. package/lib/dependencies/ImportMetaPlugin.js +7 -7
  143. package/lib/dependencies/ImportParserPlugin.js +4 -4
  144. package/lib/dependencies/JsonExportsDependency.js +1 -1
  145. package/lib/dependencies/LoaderPlugin.js +4 -4
  146. package/lib/dependencies/ProvidedDependency.js +2 -2
  147. package/lib/dependencies/PureExpressionDependency.js +1 -1
  148. package/lib/dependencies/RequireContextDependencyParserPlugin.js +1 -1
  149. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +1 -1
  150. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +3 -3
  151. package/lib/dependencies/SystemPlugin.js +3 -3
  152. package/lib/dependencies/WorkerPlugin.js +5 -5
  153. package/lib/dependencies/getFunctionExpression.js +1 -1
  154. package/lib/esm/ModuleChunkFormatPlugin.js +5 -5
  155. package/lib/esm/ModuleChunkLoadingPlugin.js +2 -2
  156. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +15 -11
  157. package/lib/formatLocation.js +2 -2
  158. package/lib/hmr/HotModuleReplacement.runtime.js +39 -27
  159. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +43 -32
  160. package/lib/hmr/JavascriptHotModuleReplacementHelper.js +1 -1
  161. package/lib/hmr/LazyCompilationPlugin.js +6 -6
  162. package/lib/hmr/lazyCompilationBackend.js +6 -6
  163. package/lib/ids/ChunkModuleIdRangePlugin.js +4 -4
  164. package/lib/ids/DeterministicChunkIdsPlugin.js +5 -4
  165. package/lib/ids/DeterministicModuleIdsPlugin.js +2 -2
  166. package/lib/ids/HashedModuleIdsPlugin.js +1 -1
  167. package/lib/ids/IdHelpers.js +9 -9
  168. package/lib/ids/NamedChunkIdsPlugin.js +5 -5
  169. package/lib/ids/NamedModuleIdsPlugin.js +2 -2
  170. package/lib/ids/NaturalChunkIdsPlugin.js +2 -2
  171. package/lib/ids/NaturalModuleIdsPlugin.js +1 -1
  172. package/lib/ids/OccurrenceChunkIdsPlugin.js +2 -2
  173. package/lib/ids/OccurrenceModuleIdsPlugin.js +4 -4
  174. package/lib/ids/SyncModuleIdsPlugin.js +4 -4
  175. package/lib/index.js +10 -6
  176. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +1 -1
  177. package/lib/javascript/BasicEvaluatedExpression.js +3 -3
  178. package/lib/javascript/CommonJsChunkFormatPlugin.js +1 -1
  179. package/lib/javascript/EnableChunkLoadingPlugin.js +1 -1
  180. package/lib/javascript/JavascriptGenerator.js +1 -1
  181. package/lib/javascript/JavascriptModulesPlugin.js +16 -16
  182. package/lib/javascript/JavascriptParser.js +80 -78
  183. package/lib/javascript/JavascriptParserHelpers.js +3 -3
  184. package/lib/javascript/StartupHelpers.js +2 -2
  185. package/lib/json/JsonGenerator.js +2 -2
  186. package/lib/json/JsonModulesPlugin.js +2 -2
  187. package/lib/library/AbstractLibraryPlugin.js +3 -3
  188. package/lib/library/AmdLibraryPlugin.js +3 -3
  189. package/lib/library/AssignLibraryPlugin.js +2 -2
  190. package/lib/library/EnableLibraryPlugin.js +2 -2
  191. package/lib/library/ModuleLibraryPlugin.js +7 -2
  192. package/lib/library/SystemLibraryPlugin.js +6 -4
  193. package/lib/library/UmdLibraryPlugin.js +14 -14
  194. package/lib/logging/createConsoleLogger.js +4 -4
  195. package/lib/logging/runtime.js +3 -3
  196. package/lib/logging/truncateArgs.js +4 -4
  197. package/lib/node/CommonJsChunkLoadingPlugin.js +2 -2
  198. package/lib/node/NodeEnvironmentPlugin.js +1 -1
  199. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +16 -14
  200. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +7 -7
  201. package/lib/node/ReadFileCompileWasmPlugin.js +7 -7
  202. package/lib/node/RequireChunkLoadingRuntimeModule.js +6 -4
  203. package/lib/node/nodeConsole.js +2 -2
  204. package/lib/optimize/AggressiveMergingPlugin.js +2 -2
  205. package/lib/optimize/AggressiveSplittingPlugin.js +10 -10
  206. package/lib/optimize/ConcatenatedModule.js +161 -52
  207. package/lib/optimize/EnsureChunkConditionsPlugin.js +2 -2
  208. package/lib/optimize/FlagIncludedChunksPlugin.js +2 -2
  209. package/lib/optimize/InnerGraph.js +5 -5
  210. package/lib/optimize/InnerGraphPlugin.js +14 -12
  211. package/lib/optimize/LimitChunkCountPlugin.js +5 -5
  212. package/lib/optimize/MangleExportsPlugin.js +5 -5
  213. package/lib/optimize/MergeDuplicateChunksPlugin.js +2 -2
  214. package/lib/optimize/MinChunkSizePlugin.js +3 -3
  215. package/lib/optimize/ModuleConcatenationPlugin.js +45 -38
  216. package/lib/optimize/RealContentHashPlugin.js +23 -23
  217. package/lib/optimize/RemoveEmptyChunksPlugin.js +2 -2
  218. package/lib/optimize/RemoveParentModulesPlugin.js +3 -3
  219. package/lib/optimize/RuntimeChunkPlugin.js +2 -2
  220. package/lib/optimize/SideEffectsFlagPlugin.js +5 -5
  221. package/lib/optimize/SplitChunksPlugin.js +20 -20
  222. package/lib/performance/AssetsOverSizeLimitWarning.js +1 -1
  223. package/lib/performance/EntrypointsOverSizeLimitWarning.js +2 -2
  224. package/lib/performance/SizeLimitsPlugin.js +4 -4
  225. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +1 -1
  226. package/lib/prefetch/ChunkPrefetchStartupRuntimeModule.js +3 -3
  227. package/lib/rules/BasicMatcherRulePlugin.js +1 -1
  228. package/lib/rules/RuleSetCompiler.js +10 -10
  229. package/lib/rules/UseEffectRulePlugin.js +2 -2
  230. package/lib/runtime/GetChunkFilenameRuntimeModule.js +15 -15
  231. package/lib/runtime/GetMainFilenameRuntimeModule.js +1 -1
  232. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +1 -1
  233. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +1 -1
  234. package/lib/runtime/StartupChunkDependenciesPlugin.js +2 -2
  235. package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +3 -3
  236. package/lib/schemes/DataUriPlugin.js +3 -3
  237. package/lib/schemes/FileUriPlugin.js +1 -1
  238. package/lib/schemes/HttpUriPlugin.js +40 -36
  239. package/lib/schemes/VirtualUrlPlugin.js +1 -1
  240. package/lib/serialization/BinaryMiddleware.js +11 -11
  241. package/lib/serialization/FileMiddleware.js +17 -17
  242. package/lib/serialization/ObjectMiddleware.js +8 -8
  243. package/lib/serialization/Serializer.js +2 -2
  244. package/lib/serialization/SerializerMiddleware.js +2 -2
  245. package/lib/sharing/ConsumeSharedPlugin.js +4 -4
  246. package/lib/sharing/ProvideSharedPlugin.js +4 -4
  247. package/lib/sharing/SharePlugin.js +1 -1
  248. package/lib/sharing/resolveMatchedConfigs.js +1 -1
  249. package/lib/stats/DefaultStatsFactoryPlugin.js +85 -80
  250. package/lib/stats/DefaultStatsPresetPlugin.js +15 -15
  251. package/lib/stats/DefaultStatsPrinterPlugin.js +87 -87
  252. package/lib/stats/StatsFactory.js +7 -7
  253. package/lib/stats/StatsPrinter.js +9 -9
  254. package/lib/url/URLParserPlugin.js +5 -5
  255. package/lib/util/AsyncQueue.js +4 -4
  256. package/lib/util/IterableHelpers.js +2 -2
  257. package/lib/util/LazyBucketSortedSet.js +3 -3
  258. package/lib/util/SetHelpers.js +2 -2
  259. package/lib/util/StackedCacheMap.js +1 -1
  260. package/lib/util/StackedMap.js +1 -1
  261. package/lib/util/TupleSet.js +1 -1
  262. package/lib/util/WeakTupleMap.js +1 -1
  263. package/lib/util/cleverMerge.js +8 -8
  264. package/lib/util/comparators.js +10 -10
  265. package/lib/util/compileBooleanMatcher.js +20 -20
  266. package/lib/util/concatenate.js +3 -3
  267. package/lib/util/conventions.js +4 -4
  268. package/lib/util/create-schema-validation.js +1 -1
  269. package/lib/util/createHash.js +1 -1
  270. package/lib/util/deprecation.js +4 -4
  271. package/lib/util/deterministicGrouping.js +7 -7
  272. package/lib/util/findGraphRoots.js +1 -1
  273. package/lib/util/fs.js +3 -3
  274. package/lib/util/identifier.js +14 -14
  275. package/lib/util/magicComment.js +5 -1
  276. package/lib/util/memoize.js +1 -1
  277. package/lib/util/processAsyncTree.js +2 -2
  278. package/lib/util/propertyName.js +1 -1
  279. package/lib/util/removeBOM.js +1 -1
  280. package/lib/util/runtime.js +6 -6
  281. package/lib/util/semver.js +16 -16
  282. package/lib/util/serialization.js +5 -5
  283. package/lib/util/smartGrouping.js +1 -1
  284. package/lib/validateSchema.js +2 -2
  285. package/lib/wasm/EnableWasmLoadingPlugin.js +1 -1
  286. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +1 -1
  287. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +1 -1
  288. package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +4 -4
  289. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +13 -9
  290. package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +2 -2
  291. package/lib/wasm-sync/WebAssemblyGenerator.js +14 -14
  292. package/lib/wasm-sync/WebAssemblyInInitialChunkError.js +4 -2
  293. package/lib/wasm-sync/WebAssemblyParser.js +2 -2
  294. package/lib/web/FetchCompileAsyncWasmPlugin.js +4 -4
  295. package/lib/web/FetchCompileWasmPlugin.js +4 -4
  296. package/lib/web/JsonpChunkLoadingPlugin.js +2 -2
  297. package/lib/web/JsonpChunkLoadingRuntimeModule.js +1 -1
  298. package/lib/webpack.js +17 -12
  299. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +2 -2
  300. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +1 -1
  301. package/package.json +13 -13
  302. package/schemas/WebpackOptions.check.js +1 -1
  303. package/schemas/WebpackOptions.json +4 -0
  304. package/types.d.ts +151 -44
@@ -75,8 +75,11 @@ const { register } = require("./util/serialization");
75
75
  * @property {string} expression
76
76
  * @property {InitFragment<ChunkRenderContext>[]=} chunkInitFragments
77
77
  * @property {ReadOnlyRuntimeRequirements=} runtimeRequirements
78
+ * @property {[string, string][]=} specifiers
78
79
  */
79
80
 
81
+ /** @typedef {true | [string, string][]} Imported */
82
+
80
83
  const RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]);
81
84
  const RUNTIME_REQUIREMENTS_FOR_SCRIPT = new Set([RuntimeGlobals.loadScript]);
82
85
  const RUNTIME_REQUIREMENTS_FOR_MODULE = new Set([
@@ -96,7 +99,9 @@ const getSourceForGlobalVariableExternal = (variableName, type) => {
96
99
  }
97
100
 
98
101
  // needed for e.g. window["some"]["thing"]
99
- const objectLookup = variableName.map(r => `[${JSON.stringify(r)}]`).join("");
102
+ const objectLookup = variableName
103
+ .map((r) => `[${JSON.stringify(r)}]`)
104
+ .join("");
100
105
  return {
101
106
  iife: type === "this",
102
107
  expression: `${type}${objectLookup}`
@@ -107,7 +112,7 @@ const getSourceForGlobalVariableExternal = (variableName, type) => {
107
112
  * @param {string|string[]} moduleAndSpecifiers the module request
108
113
  * @returns {SourceData} the generated source
109
114
  */
110
- const getSourceForCommonJsExternal = moduleAndSpecifiers => {
115
+ const getSourceForCommonJsExternal = (moduleAndSpecifiers) => {
111
116
  if (!Array.isArray(moduleAndSpecifiers)) {
112
117
  return {
113
118
  expression: `require(${JSON.stringify(moduleAndSpecifiers)})`
@@ -123,21 +128,24 @@ const getSourceForCommonJsExternal = moduleAndSpecifiers => {
123
128
  };
124
129
 
125
130
  /**
126
- * @param {string|string[]} moduleAndSpecifiers the module request
127
- * @param {string} importMetaName import.meta name
128
- * @param {boolean} needPrefix need to use `node:` prefix for `module` import
131
+ * @param {string | string[]} moduleAndSpecifiers the module request
132
+ * @param {RuntimeTemplate} runtimeTemplate the runtime template
129
133
  * @returns {SourceData} the generated source
130
134
  */
131
135
  const getSourceForCommonJsExternalInNodeModule = (
132
136
  moduleAndSpecifiers,
133
- importMetaName,
134
- needPrefix
137
+ runtimeTemplate
135
138
  ) => {
139
+ const importMetaName =
140
+ /** @type {string} */
141
+ (runtimeTemplate.outputOptions.importMetaName);
142
+
143
+ // /** @type {boolean} */
144
+ // (runtimeTemplate.supportNodePrefixForCoreModules())
145
+
136
146
  const chunkInitFragments = [
137
147
  new InitFragment(
138
- `import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "${
139
- needPrefix ? "node:" : ""
140
- }module";\n`,
148
+ `import { createRequire as __WEBPACK_EXTERNAL_createRequire } from ${runtimeTemplate.renderNodePrefixForCoreModule("module")};\n`,
141
149
  InitFragment.STAGE_HARMONY_IMPORTS,
142
150
  0,
143
151
  "external module node-commonjs"
@@ -215,10 +223,9 @@ const getSourceForImportExternal = (
215
223
  };
216
224
 
217
225
  /**
218
- * @template {{ [key: string]: string }} T
219
- * @param {keyof T} key key
220
- * @param {T[keyof T]} value value
221
- * @returns {undefined | T[keyof T]} replaced value
226
+ * @param {string} key key
227
+ * @param {ImportAttributes | string | boolean | undefined} value value
228
+ * @returns {ImportAttributes | string | boolean | undefined} replaced value
222
229
  */
223
230
  const importAssertionReplacer = (key, value) => {
224
231
  if (key === "_isLegacyAssert") {
@@ -229,17 +236,19 @@ const importAssertionReplacer = (key, value) => {
229
236
  };
230
237
 
231
238
  /**
232
- * @extends {InitFragment<ChunkRenderContext>}
239
+ * @extends {InitFragment<GenerateContext>}
233
240
  */
234
241
  class ModuleExternalInitFragment extends InitFragment {
235
242
  /**
236
243
  * @param {string} request import source
244
+ * @param {Imported} imported the imported specifiers
237
245
  * @param {string=} ident recomputed ident
238
246
  * @param {ImportDependencyMeta=} dependencyMeta the dependency meta
239
247
  * @param {HashFunction=} hashFunction the hash function to use
240
248
  */
241
249
  constructor(
242
250
  request,
251
+ imported,
243
252
  ident,
244
253
  dependencyMeta,
245
254
  hashFunction = DEFAULTS.HASH_FUNCTION
@@ -253,28 +262,78 @@ class ModuleExternalInitFragment extends InitFragment {
253
262
  .slice(0, 8)}`;
254
263
  }
255
264
  }
265
+
256
266
  const identifier = `__WEBPACK_EXTERNAL_MODULE_${ident}__`;
257
267
  super(
258
- `import * as ${identifier} from ${JSON.stringify(request)}${
259
- dependencyMeta && dependencyMeta.attributes
260
- ? dependencyMeta.attributes._isLegacyAssert
261
- ? ` assert ${JSON.stringify(
262
- dependencyMeta.attributes,
263
- importAssertionReplacer
264
- )}`
265
- : ` with ${JSON.stringify(dependencyMeta.attributes)}`
266
- : ""
267
- };\n`,
268
+ "",
268
269
  InitFragment.STAGE_HARMONY_IMPORTS,
269
270
  0,
270
- `external module import ${ident}`
271
+ `external module import ${ident} ${imported === true ? imported : imported.join(" ")}`
271
272
  );
272
273
  this._ident = ident;
273
274
  this._request = request;
274
- this._dependencyMeta = request;
275
+ this._dependencyMeta = dependencyMeta;
276
+ this._imported = imported;
275
277
  this._identifier = identifier;
276
278
  }
277
279
 
280
+ /**
281
+ * @returns {Imported} imported
282
+ */
283
+ getImported() {
284
+ return this._imported;
285
+ }
286
+
287
+ /**
288
+ * @param {Imported} imported imported
289
+ */
290
+ setImported(imported) {
291
+ this._imported = imported;
292
+ }
293
+
294
+ /**
295
+ * @param {GenerateContext} context context
296
+ * @returns {string | Source | undefined} the source code that will be included as initialization code
297
+ */
298
+ getContent(context) {
299
+ const {
300
+ _dependencyMeta: dependencyMeta,
301
+ _imported: imported,
302
+ _request: request,
303
+ _identifier: identifier
304
+ } = this;
305
+ const attributes =
306
+ dependencyMeta && dependencyMeta.attributes
307
+ ? dependencyMeta.attributes._isLegacyAssert &&
308
+ dependencyMeta.attributes._isLegacyAssert
309
+ ? ` assert ${JSON.stringify(
310
+ dependencyMeta.attributes,
311
+ importAssertionReplacer
312
+ )}`
313
+ : ` with ${JSON.stringify(dependencyMeta.attributes)}`
314
+ : "";
315
+ let content = "";
316
+ if (imported === true) {
317
+ // namespace
318
+ content = `import * as ${identifier} from ${JSON.stringify(request)}${
319
+ attributes
320
+ };\n`;
321
+ } else if (imported.length === 0) {
322
+ // just import, no use
323
+ content = `import ${JSON.stringify(request)}${attributes};\n`;
324
+ } else {
325
+ content = `import { ${imported
326
+ .map(([name, finalName]) => {
327
+ if (name !== finalName) {
328
+ return `${name} as ${finalName}`;
329
+ }
330
+ return name;
331
+ })
332
+ .join(", ")} } from ${JSON.stringify(request)}${attributes};\n`;
333
+ }
334
+ return content;
335
+ }
336
+
278
337
  getNamespaceIdentifier() {
279
338
  return this._identifier;
280
339
  }
@@ -287,11 +346,12 @@ register(
287
346
  {
288
347
  serialize(obj, { write }) {
289
348
  write(obj._request);
349
+ write(obj._imported);
290
350
  write(obj._ident);
291
351
  write(obj._dependencyMeta);
292
352
  },
293
353
  deserialize({ read }) {
294
- return new ModuleExternalInitFragment(read(), read(), read());
354
+ return new ModuleExternalInitFragment(read(), read(), read(), read());
295
355
  }
296
356
  }
297
357
  );
@@ -343,6 +403,7 @@ const generateModuleRemapping = (
343
403
  * @param {RuntimeSpec} runtime the runtime
344
404
  * @param {RuntimeTemplate} runtimeTemplate the runtime template
345
405
  * @param {ImportDependencyMeta} dependencyMeta the dependency meta
406
+ * @param {ConcatenationScope=} concatenationScope concatenationScope
346
407
  * @returns {SourceData} the generated source
347
408
  */
348
409
  const getSourceForModuleExternal = (
@@ -350,28 +411,71 @@ const getSourceForModuleExternal = (
350
411
  exportsInfo,
351
412
  runtime,
352
413
  runtimeTemplate,
353
- dependencyMeta
414
+ dependencyMeta,
415
+ concatenationScope
354
416
  ) => {
355
417
  if (!Array.isArray(moduleAndSpecifiers)) {
356
418
  moduleAndSpecifiers = [moduleAndSpecifiers];
357
419
  }
420
+
421
+ /** @type {Imported} */
422
+ let imported = true;
423
+ if (concatenationScope) {
424
+ const usedExports = exportsInfo.getUsedExports(runtime);
425
+ switch (usedExports) {
426
+ case true:
427
+ case null:
428
+ // unknown exports
429
+ imported = true;
430
+ break;
431
+ case false:
432
+ // no used exports
433
+ imported = [];
434
+ break;
435
+ default:
436
+ imported = [];
437
+ if (exportsInfo.isUsed(runtime) === false) {
438
+ // no used, only
439
+ }
440
+ for (const [name] of usedExports.entries()) {
441
+ let counter = 0;
442
+ let finalName = name;
443
+
444
+ if (concatenationScope) {
445
+ while (!concatenationScope.registerUsedName(finalName)) {
446
+ finalName = `${name}_${counter++}`;
447
+ }
448
+ }
449
+ imported.push([name, finalName]);
450
+ }
451
+ }
452
+ }
453
+
358
454
  const initFragment = new ModuleExternalInitFragment(
359
455
  moduleAndSpecifiers[0],
456
+ imported,
360
457
  undefined,
361
458
  dependencyMeta,
362
459
  runtimeTemplate.outputOptions.hashFunction
363
460
  );
461
+ const specifiers = imported === true ? undefined : imported;
364
462
  const baseAccess = `${initFragment.getNamespaceIdentifier()}${propertyAccess(
365
463
  moduleAndSpecifiers,
366
464
  1
367
465
  )}`;
368
- const moduleRemapping = generateModuleRemapping(
369
- baseAccess,
370
- exportsInfo,
371
- runtime,
372
- runtimeTemplate
373
- );
374
- const expression = moduleRemapping || baseAccess;
466
+ let expression = baseAccess;
467
+
468
+ const useNamespace = imported === true;
469
+ let moduleRemapping;
470
+ if (useNamespace) {
471
+ moduleRemapping = generateModuleRemapping(
472
+ baseAccess,
473
+ exportsInfo,
474
+ runtime,
475
+ runtimeTemplate
476
+ );
477
+ expression = moduleRemapping || baseAccess;
478
+ }
375
479
  return {
376
480
  expression,
377
481
  init: moduleRemapping
@@ -383,10 +487,13 @@ const getSourceForModuleExternal = (
383
487
  "x"
384
488
  )}`
385
489
  : undefined,
490
+ specifiers,
386
491
  runtimeRequirements: moduleRemapping
387
492
  ? RUNTIME_REQUIREMENTS_FOR_MODULE
388
493
  : undefined,
389
- chunkInitFragments: [initFragment]
494
+ chunkInitFragments: [
495
+ /** @type {InitFragment<EXPECTED_ANY>} */ (initFragment)
496
+ ]
390
497
  };
391
498
  };
392
499
 
@@ -733,6 +840,7 @@ class ExternalModule extends Module {
733
840
  * @param {ChunkGraph} chunkGraph the chunk graph
734
841
  * @param {RuntimeSpec} runtime the runtime
735
842
  * @param {DependencyMeta | undefined} dependencyMeta the dependency meta
843
+ * @param {ConcatenationScope=} concatenationScope concatenationScope
736
844
  * @returns {SourceData} the source data
737
845
  */
738
846
  _getSourceData(
@@ -742,7 +850,8 @@ class ExternalModule extends Module {
742
850
  moduleGraph,
743
851
  chunkGraph,
744
852
  runtime,
745
- dependencyMeta
853
+ dependencyMeta,
854
+ concatenationScope
746
855
  ) {
747
856
  switch (externalType) {
748
857
  case "this":
@@ -761,13 +870,7 @@ class ExternalModule extends Module {
761
870
  return getSourceForCommonJsExternal(request);
762
871
  case "node-commonjs":
763
872
  return /** @type {BuildInfo} */ (this.buildInfo).javascriptModule
764
- ? getSourceForCommonJsExternalInNodeModule(
765
- request,
766
- /** @type {string} */
767
- (runtimeTemplate.outputOptions.importMetaName),
768
- /** @type {boolean} */
769
- (runtimeTemplate.supportNodePrefixForCoreModules())
770
- )
873
+ ? getSourceForCommonJsExternalInNodeModule(request, runtimeTemplate)
771
874
  : getSourceForCommonJsExternal(request);
772
875
  case "amd":
773
876
  case "amd-require":
@@ -818,7 +921,8 @@ class ExternalModule extends Module {
818
921
  moduleGraph.getExportsInfo(this),
819
922
  runtime,
820
923
  runtimeTemplate,
821
- /** @type {ImportDependencyMeta} */ (dependencyMeta)
924
+ /** @type {ImportDependencyMeta} */ (dependencyMeta),
925
+ concatenationScope
822
926
  );
823
927
  }
824
928
  case "var":
@@ -898,14 +1002,24 @@ class ExternalModule extends Module {
898
1002
  moduleGraph,
899
1003
  chunkGraph,
900
1004
  runtime,
901
- this.dependencyMeta
1005
+ this.dependencyMeta,
1006
+ concatenationScope
902
1007
  );
903
1008
 
1009
+ // sourceString can be empty str only when there is concatenationScope
904
1010
  let sourceString = sourceData.expression;
905
1011
  if (sourceData.iife) {
906
1012
  sourceString = `(function() { return ${sourceString}; }())`;
907
1013
  }
908
- if (concatenationScope) {
1014
+
1015
+ const specifiers = sourceData.specifiers;
1016
+ if (specifiers) {
1017
+ sourceString = "";
1018
+ const scope = /** @type {ConcatenationScope} */ (concatenationScope);
1019
+ for (const [specifier, finalName] of specifiers) {
1020
+ scope.registerRawExport(specifier, finalName);
1021
+ }
1022
+ } else if (concatenationScope) {
909
1023
  sourceString = `${
910
1024
  runtimeTemplate.supportsConst() ? "const" : "var"
911
1025
  } ${ConcatenationScope.NAMESPACE_OBJECT_EXPORT} = ${sourceString};`;
@@ -1011,3 +1125,4 @@ class ExternalModule extends Module {
1011
1125
  makeSerializable(ExternalModule, "webpack/lib/ExternalModule");
1012
1126
 
1013
1127
  module.exports = ExternalModule;
1128
+ module.exports.ModuleExternalInitFragment = ModuleExternalInitFragment;
@@ -264,7 +264,7 @@ class ExternalModuleFactoryPlugin {
264
264
  request: dependency.request,
265
265
  dependencyType,
266
266
  contextInfo,
267
- getResolve: options => (context, request, callback) => {
267
+ getResolve: (options) => (context, request, callback) => {
268
268
  const resolveContext = {
269
269
  fileDependencies: data.fileDependencies,
270
270
  missingDependencies: data.missingDependencies,
@@ -307,7 +307,7 @@ class ExternalModuleFactoryPlugin {
307
307
  },
308
308
  cb
309
309
  );
310
- if (promise && promise.then) promise.then(r => cb(null, r), cb);
310
+ if (promise && promise.then) promise.then((r) => cb(null, r), cb);
311
311
  }
312
312
  return;
313
313
  } else if (typeof externals === "object") {
@@ -5,10 +5,13 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const { ModuleExternalInitFragment } = require("./ExternalModule");
8
9
  const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin");
10
+ const ConcatenatedModule = require("./optimize/ConcatenatedModule");
9
11
 
10
12
  /** @typedef {import("../declarations/WebpackOptions").Externals} Externals */
11
13
  /** @typedef {import("./Compiler")} Compiler */
14
+ /** @typedef {import("./optimize/ConcatenatedModule").ConcatenatedModuleInfo} ConcatenatedModuleInfo */
12
15
 
13
16
  const PLUGIN_NAME = "ExternalsPlugin";
14
17
 
@@ -33,6 +36,48 @@ class ExternalsPlugin {
33
36
  normalModuleFactory
34
37
  );
35
38
  });
39
+
40
+ compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
41
+ const { concatenatedModuleInfo } =
42
+ ConcatenatedModule.getCompilationHooks(compilation);
43
+ concatenatedModuleInfo.tap(PLUGIN_NAME, (updatedInfo, moduleInfo) => {
44
+ const rawExportMap =
45
+ /** @type {ConcatenatedModuleInfo} */ updatedInfo.rawExportMap;
46
+
47
+ if (!rawExportMap) {
48
+ return;
49
+ }
50
+
51
+ const chunkInitFragments =
52
+ /** @type {ConcatenatedModuleInfo} */ moduleInfo.chunkInitFragments;
53
+ const moduleExternalInitFragments = chunkInitFragments
54
+ ? chunkInitFragments.filter(
55
+ (fragment) => fragment instanceof ModuleExternalInitFragment
56
+ )
57
+ : [];
58
+
59
+ let initFragmentChanged = false;
60
+
61
+ for (const fragment of moduleExternalInitFragments) {
62
+ const imported = fragment.getImported();
63
+
64
+ if (Array.isArray(imported)) {
65
+ const newImported = imported.map(([specifier, finalName]) => [
66
+ specifier,
67
+ rawExportMap.has(specifier)
68
+ ? rawExportMap.get(specifier)
69
+ : finalName
70
+ ]);
71
+ fragment.setImported(newImported);
72
+ initFragmentChanged = true;
73
+ }
74
+ }
75
+
76
+ if (initFragmentChanged) {
77
+ return true;
78
+ }
79
+ });
80
+ });
36
81
  }
37
82
  }
38
83