webpack 5.102.1 → 5.103.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 (118) hide show
  1. package/README.md +121 -134
  2. package/lib/CompatibilityPlugin.js +25 -2
  3. package/lib/Compilation.js +25 -2
  4. package/lib/ConcatenationScope.js +0 -15
  5. package/lib/CssModule.js +6 -1
  6. package/lib/DefinePlugin.js +11 -11
  7. package/lib/Dependency.js +8 -1
  8. package/lib/DependencyTemplate.js +1 -0
  9. package/lib/DotenvPlugin.js +457 -0
  10. package/lib/EnvironmentPlugin.js +19 -16
  11. package/lib/EvalSourceMapDevToolPlugin.js +16 -0
  12. package/lib/ExportsInfo.js +6 -2
  13. package/lib/ExternalModule.js +20 -28
  14. package/lib/ExternalModuleFactoryPlugin.js +10 -8
  15. package/lib/ExternalsPlugin.js +2 -1
  16. package/lib/ManifestPlugin.js +235 -0
  17. package/lib/Module.js +3 -0
  18. package/lib/ModuleGraph.js +2 -1
  19. package/lib/ModuleSourceTypesConstants.js +0 -6
  20. package/lib/MultiCompiler.js +1 -1
  21. package/lib/NodeStuffPlugin.js +419 -121
  22. package/lib/NormalModule.js +17 -16
  23. package/lib/RuntimeGlobals.js +22 -4
  24. package/lib/RuntimePlugin.js +27 -6
  25. package/lib/RuntimeTemplate.js +115 -56
  26. package/lib/SourceMapDevToolPlugin.js +20 -0
  27. package/lib/WebpackOptionsApply.js +33 -9
  28. package/lib/asset/AssetBytesGenerator.js +1 -1
  29. package/lib/asset/AssetGenerator.js +1 -2
  30. package/lib/asset/AssetSourceGenerator.js +1 -1
  31. package/lib/config/browserslistTargetHandler.js +5 -0
  32. package/lib/config/defaults.js +98 -18
  33. package/lib/config/normalization.js +2 -1
  34. package/lib/config/target.js +6 -0
  35. package/lib/css/CssGenerator.js +283 -57
  36. package/lib/css/CssLoadingRuntimeModule.js +2 -0
  37. package/lib/css/CssMergeStyleSheetsRuntimeModule.js +56 -0
  38. package/lib/css/CssModulesPlugin.js +84 -34
  39. package/lib/css/CssParser.js +1174 -667
  40. package/lib/css/walkCssTokens.js +97 -0
  41. package/lib/dependencies/CommonJsImportsParserPlugin.js +0 -9
  42. package/lib/dependencies/CommonJsPlugin.js +12 -0
  43. package/lib/dependencies/CssIcssExportDependency.js +247 -8
  44. package/lib/dependencies/CssIcssFromIdentifierDependency.js +124 -0
  45. package/lib/dependencies/CssIcssGlobalIdentifierDependency.js +48 -0
  46. package/lib/dependencies/CssIcssImportDependency.js +60 -54
  47. package/lib/dependencies/CssIcssLocalIdentifierDependency.js +61 -0
  48. package/lib/dependencies/{CssSelfLocalIdentifierDependency.js → CssIcssSelfLocalIdentifierDependency.js} +88 -10
  49. package/lib/dependencies/CssIcssSymbolDependency.js +31 -29
  50. package/lib/dependencies/CssImportDependency.js +15 -5
  51. package/lib/dependencies/ExternalModuleInitFragment.js +1 -1
  52. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +95 -0
  53. package/lib/dependencies/HarmonyAcceptDependency.js +6 -1
  54. package/lib/dependencies/HarmonyAcceptImportDependency.js +2 -1
  55. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +12 -1
  56. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +32 -21
  57. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +12 -8
  58. package/lib/dependencies/HarmonyImportDependency.js +23 -27
  59. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +28 -69
  60. package/lib/dependencies/HarmonyImportSideEffectDependency.js +4 -3
  61. package/lib/dependencies/HarmonyImportSpecifierDependency.js +10 -8
  62. package/lib/dependencies/ImportDependency.js +8 -2
  63. package/lib/dependencies/ImportEagerDependency.js +6 -3
  64. package/lib/dependencies/ImportMetaPlugin.js +97 -9
  65. package/lib/dependencies/ImportParserPlugin.js +19 -21
  66. package/lib/dependencies/ImportPhase.js +121 -0
  67. package/lib/dependencies/ImportWeakDependency.js +6 -3
  68. package/lib/dependencies/ModuleDependency.js +5 -1
  69. package/lib/dependencies/ModuleHotAcceptDependency.js +1 -1
  70. package/lib/esm/ExportWebpackRequireRuntimeModule.js +1 -8
  71. package/lib/hmr/LazyCompilationPlugin.js +1 -0
  72. package/lib/ids/IdHelpers.js +4 -1
  73. package/lib/index.js +6 -0
  74. package/lib/javascript/ChunkHelpers.js +16 -5
  75. package/lib/javascript/JavascriptGenerator.js +101 -101
  76. package/lib/javascript/JavascriptModulesPlugin.js +23 -13
  77. package/lib/javascript/JavascriptParser.js +142 -38
  78. package/lib/json/JsonParser.js +7 -1
  79. package/lib/library/ModuleLibraryPlugin.js +0 -10
  80. package/lib/library/SystemLibraryPlugin.js +4 -0
  81. package/lib/library/UmdLibraryPlugin.js +1 -1
  82. package/lib/node/NodeTargetPlugin.js +9 -1
  83. package/lib/node/ReadFileCompileWasmPlugin.js +0 -2
  84. package/lib/optimize/ConcatenatedModule.js +161 -135
  85. package/lib/runtime/AsyncModuleRuntimeModule.js +28 -18
  86. package/lib/runtime/AutoPublicPathRuntimeModule.js +8 -3
  87. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +89 -55
  88. package/lib/util/comparators.js +4 -3
  89. package/lib/util/internalSerializables.js +4 -4
  90. package/lib/util/jsonParseEvenBetterErrors.js +10 -0
  91. package/lib/wasm/EnableWasmLoadingPlugin.js +10 -4
  92. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +8 -5
  93. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +8 -4
  94. package/lib/web/FetchCompileWasmPlugin.js +0 -2
  95. package/lib/webpack.js +85 -82
  96. package/module.d.ts +5 -0
  97. package/package.json +16 -14
  98. package/schemas/WebpackOptions.check.js +1 -1
  99. package/schemas/WebpackOptions.json +109 -27
  100. package/schemas/plugins/ManifestPlugin.check.d.ts +7 -0
  101. package/schemas/plugins/ManifestPlugin.check.js +6 -0
  102. package/schemas/plugins/ManifestPlugin.json +98 -0
  103. package/schemas/plugins/SourceMapDevToolPlugin.check.js +1 -1
  104. package/schemas/plugins/SourceMapDevToolPlugin.json +16 -3
  105. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  106. package/schemas/plugins/container/ContainerReferencePlugin.json +4 -1
  107. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  108. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  109. package/schemas/plugins/container/ModuleFederationPlugin.json +4 -1
  110. package/schemas/plugins/css/CssAutoParserOptions.check.js +1 -1
  111. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.js +1 -1
  112. package/schemas/plugins/css/CssGlobalParserOptions.check.js +1 -1
  113. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +1 -1
  114. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  115. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  116. package/schemas/plugins/json/JsonModulesPluginParser.check.js +1 -1
  117. package/types.d.ts +560 -293
  118. package/lib/dependencies/CssLocalIdentifierDependency.js +0 -250
@@ -16,17 +16,28 @@ const Entrypoint = require("../Entrypoint");
16
16
  * @returns {Set<Chunk>} chunks
17
17
  */
18
18
  const getAllChunks = (entrypoint, excludedChunk1, excludedChunk2) => {
19
+ /** @type {Set<Entrypoint>} */
19
20
  const queue = new Set([entrypoint]);
21
+ /** @type {Set<Entrypoint>} */
22
+ const groups = new Set([]);
23
+ for (const group of queue) {
24
+ if (group !== entrypoint) {
25
+ groups.add(group);
26
+ }
27
+ for (const parent of group.parentsIterable) {
28
+ if (parent instanceof Entrypoint) queue.add(parent);
29
+ }
30
+ }
31
+ groups.add(entrypoint);
32
+
33
+ /** @type {Set<Chunk>} */
20
34
  const chunks = new Set();
21
- for (const entrypoint of queue) {
22
- for (const chunk of entrypoint.chunks) {
35
+ for (const group of groups) {
36
+ for (const chunk of group.chunks) {
23
37
  if (chunk === excludedChunk1) continue;
24
38
  if (chunk === excludedChunk2) continue;
25
39
  chunks.add(chunk);
26
40
  }
27
- for (const parent of entrypoint.parentsIterable) {
28
- if (parent instanceof Entrypoint) queue.add(parent);
29
- }
30
41
  }
31
42
  return chunks;
32
43
  };
@@ -101,107 +101,6 @@ class JavascriptGenerator extends Generator {
101
101
  }
102
102
  }
103
103
 
104
- /**
105
- * @param {NormalModule} module module for which the code should be generated
106
- * @param {GenerateContext} generateContext context for generate
107
- * @returns {Source | null} generated code
108
- */
109
- generate(module, generateContext) {
110
- const originalSource = module.originalSource();
111
- if (!originalSource) {
112
- return DEFAULT_SOURCE.source();
113
- }
114
-
115
- const source = new ReplaceSource(originalSource);
116
- /** @type {InitFragment<GenerateContext>[]} */
117
- const initFragments = [];
118
-
119
- this.sourceModule(module, initFragments, source, generateContext);
120
-
121
- return InitFragment.addToSource(source, initFragments, generateContext);
122
- }
123
-
124
- /**
125
- * @param {Error} error the error
126
- * @param {NormalModule} module module for which the code should be generated
127
- * @param {GenerateContext} generateContext context for generate
128
- * @returns {Source | null} generated code
129
- */
130
- generateError(error, module, generateContext) {
131
- return new RawSource(`throw new Error(${JSON.stringify(error.message)});`);
132
- }
133
-
134
- /**
135
- * @param {Module} module the module to generate
136
- * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
137
- * @param {ReplaceSource} source the current replace source which can be modified
138
- * @param {GenerateContext} generateContext the generateContext
139
- * @returns {void}
140
- */
141
- sourceModule(module, initFragments, source, generateContext) {
142
- for (const dependency of module.dependencies) {
143
- this.sourceDependency(
144
- module,
145
- dependency,
146
- initFragments,
147
- source,
148
- generateContext
149
- );
150
- }
151
-
152
- if (module.presentationalDependencies !== undefined) {
153
- for (const dependency of module.presentationalDependencies) {
154
- this.sourceDependency(
155
- module,
156
- dependency,
157
- initFragments,
158
- source,
159
- generateContext
160
- );
161
- }
162
- }
163
-
164
- for (const childBlock of module.blocks) {
165
- this.sourceBlock(
166
- module,
167
- childBlock,
168
- initFragments,
169
- source,
170
- generateContext
171
- );
172
- }
173
- }
174
-
175
- /**
176
- * @param {Module} module the module to generate
177
- * @param {DependenciesBlock} block the dependencies block which will be processed
178
- * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
179
- * @param {ReplaceSource} source the current replace source which can be modified
180
- * @param {GenerateContext} generateContext the generateContext
181
- * @returns {void}
182
- */
183
- sourceBlock(module, block, initFragments, source, generateContext) {
184
- for (const dependency of block.dependencies) {
185
- this.sourceDependency(
186
- module,
187
- dependency,
188
- initFragments,
189
- source,
190
- generateContext
191
- );
192
- }
193
-
194
- for (const childBlock of block.blocks) {
195
- this.sourceBlock(
196
- module,
197
- childBlock,
198
- initFragments,
199
- source,
200
- generateContext
201
- );
202
- }
203
- }
204
-
205
104
  /**
206
105
  * @param {Module} module the current module
207
106
  * @param {Dependency} dependency the dependency to generate
@@ -271,6 +170,107 @@ class JavascriptGenerator extends Generator {
271
170
  }
272
171
  }
273
172
  }
173
+
174
+ /**
175
+ * @param {Module} module the module to generate
176
+ * @param {DependenciesBlock} block the dependencies block which will be processed
177
+ * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
178
+ * @param {ReplaceSource} source the current replace source which can be modified
179
+ * @param {GenerateContext} generateContext the generateContext
180
+ * @returns {void}
181
+ */
182
+ sourceBlock(module, block, initFragments, source, generateContext) {
183
+ for (const dependency of block.dependencies) {
184
+ this.sourceDependency(
185
+ module,
186
+ dependency,
187
+ initFragments,
188
+ source,
189
+ generateContext
190
+ );
191
+ }
192
+
193
+ for (const childBlock of block.blocks) {
194
+ this.sourceBlock(
195
+ module,
196
+ childBlock,
197
+ initFragments,
198
+ source,
199
+ generateContext
200
+ );
201
+ }
202
+ }
203
+
204
+ /**
205
+ * @param {Module} module the module to generate
206
+ * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
207
+ * @param {ReplaceSource} source the current replace source which can be modified
208
+ * @param {GenerateContext} generateContext the generateContext
209
+ * @returns {void}
210
+ */
211
+ sourceModule(module, initFragments, source, generateContext) {
212
+ for (const dependency of module.dependencies) {
213
+ this.sourceDependency(
214
+ module,
215
+ dependency,
216
+ initFragments,
217
+ source,
218
+ generateContext
219
+ );
220
+ }
221
+
222
+ if (module.presentationalDependencies !== undefined) {
223
+ for (const dependency of module.presentationalDependencies) {
224
+ this.sourceDependency(
225
+ module,
226
+ dependency,
227
+ initFragments,
228
+ source,
229
+ generateContext
230
+ );
231
+ }
232
+ }
233
+
234
+ for (const childBlock of module.blocks) {
235
+ this.sourceBlock(
236
+ module,
237
+ childBlock,
238
+ initFragments,
239
+ source,
240
+ generateContext
241
+ );
242
+ }
243
+ }
244
+
245
+ /**
246
+ * @param {NormalModule} module module for which the code should be generated
247
+ * @param {GenerateContext} generateContext context for generate
248
+ * @returns {Source | null} generated code
249
+ */
250
+ generate(module, generateContext) {
251
+ const originalSource = module.originalSource();
252
+ if (!originalSource) {
253
+ return DEFAULT_SOURCE.source();
254
+ }
255
+
256
+ const source = new ReplaceSource(originalSource);
257
+ /** @type {InitFragment<GenerateContext>[]} */
258
+ const initFragments = [];
259
+
260
+ this.sourceModule(module, initFragments, source, generateContext);
261
+
262
+ return InitFragment.addToSource(source, initFragments, generateContext);
263
+ }
264
+
265
+ /**
266
+ * @param {Error} error the error
267
+ * @param {NormalModule} module module for which the code should be generated
268
+ * @param {GenerateContext} generateContext context for generate
269
+ * @returns {Source | null} generated code
270
+ */
271
+ generateError(error, module, generateContext) {
272
+ return new RawSource(`throw new Error(${JSON.stringify(error.message)});`);
273
+ }
274
274
  }
275
275
 
276
276
  module.exports = JavascriptGenerator;
@@ -298,16 +298,20 @@ class JavascriptModulesPlugin {
298
298
  ]) {
299
299
  normalModuleFactory.hooks.createParser
300
300
  .for(type)
301
- .tap(PLUGIN_NAME, (_options) => {
301
+ .tap(PLUGIN_NAME, (options) => {
302
302
  switch (type) {
303
303
  case JAVASCRIPT_MODULE_TYPE_AUTO: {
304
- return new JavascriptParser("auto");
304
+ return new JavascriptParser("auto", { parse: options.parse });
305
305
  }
306
306
  case JAVASCRIPT_MODULE_TYPE_DYNAMIC: {
307
- return new JavascriptParser("script");
307
+ return new JavascriptParser("script", {
308
+ parse: options.parse
309
+ });
308
310
  }
309
311
  case JAVASCRIPT_MODULE_TYPE_ESM: {
310
- return new JavascriptParser("module");
312
+ return new JavascriptParser("module", {
313
+ parse: options.parse
314
+ });
311
315
  }
312
316
  }
313
317
  });
@@ -950,7 +954,8 @@ class JavascriptModulesPlugin {
950
954
  let inlinedInIIFE = false;
951
955
 
952
956
  if (avoidEntryIife) {
953
- renamedInlinedModule = this.getRenamedInlineModule(
957
+ renamedInlinedModule = this._getRenamedInlineModule(
958
+ compilation,
954
959
  allModules,
955
960
  renderContext,
956
961
  inlinedModules,
@@ -1584,10 +1589,7 @@ class JavascriptModulesPlugin {
1584
1589
  "// Execute the module function",
1585
1590
  moduleExecution,
1586
1591
  ...(needModuleDefer
1587
- ? [
1588
- "// delete __webpack_module_deferred_exports__[module];",
1589
- "// skipped because strictModuleErrorHandling is not enabled."
1590
- ]
1592
+ ? ["delete __webpack_module_deferred_exports__[moduleId];"]
1591
1593
  : [])
1592
1594
  ]),
1593
1595
  needModuleLoaded
@@ -1608,6 +1610,7 @@ class JavascriptModulesPlugin {
1608
1610
  }
1609
1611
 
1610
1612
  /**
1613
+ * @param {Compilation} compilation compilation
1611
1614
  * @param {Module[]} allModules allModules
1612
1615
  * @param {MainRenderContext} renderContext renderContext
1613
1616
  * @param {Set<Module>} inlinedModules inlinedModules
@@ -1617,7 +1620,8 @@ class JavascriptModulesPlugin {
1617
1620
  * @param {boolean} hasChunkModules hasChunkModules
1618
1621
  * @returns {Map<Module, Source> | false} renamed inlined modules
1619
1622
  */
1620
- getRenamedInlineModule(
1623
+ _getRenamedInlineModule(
1624
+ compilation,
1621
1625
  allModules,
1622
1626
  renderContext,
1623
1627
  inlinedModules,
@@ -1664,9 +1668,15 @@ class JavascriptModulesPlugin {
1664
1668
 
1665
1669
  if (!moduleSource) continue;
1666
1670
  const code = /** @type {string} */ (moduleSource.source());
1667
- const ast = JavascriptParser._parse(code, {
1668
- sourceType: "auto"
1669
- });
1671
+
1672
+ const { ast } = JavascriptParser._parse(
1673
+ code,
1674
+ {
1675
+ sourceType: "auto",
1676
+ ranges: true
1677
+ },
1678
+ JavascriptParser._getModuleParseFunction(compilation, m)
1679
+ );
1670
1680
 
1671
1681
  const scopeManager = eslintScope.analyze(ast, {
1672
1682
  ecmaVersion: 6,
@@ -8,6 +8,7 @@
8
8
  const vm = require("vm");
9
9
  const { Parser: AcornParser, tokTypes } = require("acorn");
10
10
  const { HookMap, SyncBailHook } = require("tapable");
11
+ const NormalModule = require("../NormalModule");
11
12
  const Parser = require("../Parser");
12
13
  const StackedMap = require("../util/StackedMap");
13
14
  const binarySearchBounds = require("../util/binarySearchBounds");
@@ -19,6 +20,7 @@ const memoize = require("../util/memoize");
19
20
  const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
20
21
 
21
22
  /** @typedef {import("acorn").Options} AcornOptions */
23
+ /** @typedef {import("acorn").ecmaVersion} EcmaVersion */
22
24
  /** @typedef {import("estree").AssignmentExpression} AssignmentExpression */
23
25
  /** @typedef {import("estree").BinaryExpression} BinaryExpression */
24
26
  /** @typedef {import("estree").BlockStatement} BlockStatement */
@@ -29,7 +31,8 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
29
31
  /** @typedef {import("estree").ForStatement} ForStatement */
30
32
  /** @typedef {import("estree").SwitchStatement} SwitchStatement */
31
33
  /** @typedef {import("estree").ClassExpression} ClassExpression */
32
- /** @typedef {import("estree").Comment} Comment */
34
+ /** @typedef {import("estree").SourceLocation} SourceLocation */
35
+ /** @typedef {import("estree").Comment & { start: number, end: number, loc: SourceLocation }} Comment */
33
36
  /** @typedef {import("estree").ConditionalExpression} ConditionalExpression */
34
37
  /** @typedef {import("estree").Declaration} Declaration */
35
38
  /** @typedef {import("estree").PrivateIdentifier} PrivateIdentifier */
@@ -97,6 +100,9 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
97
100
  /** @typedef {import("../Parser").ParserState} ParserState */
98
101
  /** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
99
102
 
103
+ /** @typedef {import("../Compilation")} Compilation */
104
+ /** @typedef {import("../Module")} Module */
105
+
100
106
  /** @typedef {{ name: string | VariableInfo, rootInfo: string | VariableInfo, getMembers: () => Members, getMembersOptionals: () => MembersOptionals, getMemberRanges: () => MemberRanges }} GetInfoResult */
101
107
  /** @typedef {Statement | ModuleDeclaration | Expression | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration} StatementPathItem */
102
108
  /** @typedef {(ident: string) => void} OnIdentString */
@@ -297,7 +303,33 @@ class VariableInfo {
297
303
 
298
304
  /** @typedef {string | ScopeInfo | VariableInfo} ExportedVariableInfo */
299
305
  /** @typedef {Literal | string | null | undefined} ImportSource */
300
- /** @typedef {Omit<AcornOptions, "sourceType" | "ecmaVersion"> & { sourceType: "module" | "script" | "auto", ecmaVersion?: AcornOptions["ecmaVersion"] }} ParseOptions */
306
+
307
+ /**
308
+ * @typedef {Omit<ParseOptions, "sourceType"> & {sourceType: "module" | "script" | "auto"}} InternalParseOptions
309
+ */
310
+
311
+ /**
312
+ * @typedef {object} ParseOptions
313
+ * @property {"module" | "script"} sourceType
314
+ * @property {EcmaVersion=} ecmaVersion
315
+ * @property {boolean=} locations
316
+ * @property {boolean=} comments
317
+ * @property {boolean=} ranges
318
+ * @property {boolean=} semicolons
319
+ * @property {boolean=} allowHashBang
320
+ * @property {boolean=} allowReturnOutsideFunction
321
+ */
322
+
323
+ /**
324
+ * @typedef {object} ParseResult
325
+ * @property {Program} ast
326
+ * @property {Comment[]} comments
327
+ * @property {Set<number>} semicolons
328
+ */
329
+
330
+ /**
331
+ * @typedef {(code: string, options: ParseOptions) => ParseResult} ParseFunction
332
+ */
301
333
 
302
334
  /** @typedef {symbol} Tag */
303
335
 
@@ -343,6 +375,7 @@ const SCOPE_INFO_TERMINATED_THROW = 2;
343
375
  * @typedef {object} DestructuringAssignmentProperty
344
376
  * @property {string} id
345
377
  * @property {Range} range
378
+ * @property {SourceLocation} loc
346
379
  * @property {Set<DestructuringAssignmentProperty> | undefined=} pattern
347
380
  * @property {boolean | string} shorthand
348
381
  */
@@ -412,15 +445,15 @@ const getRootName = (expression) => {
412
445
  }
413
446
  };
414
447
 
415
- /** @type {AcornOptions} */
448
+ /** @type {ParseOptions} */
416
449
  const defaultParserOptions = {
417
- ranges: true,
418
- locations: true,
419
- ecmaVersion: "latest",
420
450
  sourceType: "module",
451
+ ecmaVersion: "latest",
452
+ ranges: false,
453
+ locations: false,
454
+ comments: false,
421
455
  // https://github.com/tc39/proposal-hashbang
422
- allowHashBang: true,
423
- onComment: undefined
456
+ allowHashBang: true
424
457
  };
425
458
 
426
459
  const EMPTY_COMMENT_OPTIONS = {
@@ -432,9 +465,10 @@ const CLASS_NAME = "JavascriptParser";
432
465
 
433
466
  class JavascriptParser extends Parser {
434
467
  /**
435
- * @param {"module" | "script" | "auto"} sourceType default source type
468
+ * @param {"module" | "script" | "auto"=} sourceType default source type
469
+ * @param {{ parse?: ParseFunction }=} options parser options
436
470
  */
437
- constructor(sourceType = "auto") {
471
+ constructor(sourceType = "auto", options = {}) {
438
472
  super();
439
473
  this.hooks = Object.freeze({
440
474
  /** @type {HookMap<SyncBailHook<[UnaryExpression], BasicEvaluatedExpression | null | undefined>>} */
@@ -628,6 +662,8 @@ class JavascriptParser extends Parser {
628
662
  unusedStatement: new SyncBailHook(["statement"])
629
663
  });
630
664
  this.sourceType = sourceType;
665
+ this.options = options;
666
+
631
667
  /** @type {ScopeInfo} */
632
668
  this.scope = /** @type {EXPECTED_ANY} */ (undefined);
633
669
  /** @type {ParserState} */
@@ -3003,6 +3039,7 @@ class JavascriptParser extends Parser {
3003
3039
  props.add({
3004
3040
  id: key.name,
3005
3041
  range: /** @type {Range} */ (key.range),
3042
+ loc: /** @type {SourceLocation} */ (key.loc),
3006
3043
  pattern,
3007
3044
  shorthand: this.scope.inShorthand
3008
3045
  });
@@ -3019,6 +3056,7 @@ class JavascriptParser extends Parser {
3019
3056
  props.add({
3020
3057
  id: str,
3021
3058
  range: /** @type {Range} */ (key.range),
3059
+ loc: /** @type {SourceLocation} */ (key.loc),
3022
3060
  pattern,
3023
3061
  shorthand: this.scope.inShorthand
3024
3062
  });
@@ -3054,6 +3092,7 @@ class JavascriptParser extends Parser {
3054
3092
  props.add({
3055
3093
  id: `${i}`,
3056
3094
  range: /** @type {Range} */ (element.range),
3095
+ loc: /** @type {SourceLocation} */ (element.loc),
3057
3096
  pattern,
3058
3097
  shorthand: false
3059
3098
  });
@@ -4626,17 +4665,23 @@ class JavascriptParser extends Parser {
4626
4665
  * @returns {ParserState} the parser state
4627
4666
  */
4628
4667
  parse(source, state) {
4629
- let ast;
4630
- /** @type {import("acorn").Comment[]} */
4631
- let comments;
4632
- const semicolons = new Set();
4633
4668
  if (source === null) {
4634
4669
  throw new Error("source must not be null");
4635
4670
  }
4671
+
4636
4672
  if (Buffer.isBuffer(source)) {
4637
4673
  source = source.toString("utf8");
4638
4674
  }
4675
+
4676
+ let ast;
4677
+ /** @type {Comment[]} */
4678
+ let comments;
4679
+ /** @type {Set<number>} */
4680
+ let semicolons;
4681
+
4639
4682
  if (typeof source === "object") {
4683
+ semicolons = new Set();
4684
+
4640
4685
  ast = /** @type {Program} */ (source);
4641
4686
  comments = source.comments;
4642
4687
  if (source.semicolons) {
@@ -4647,12 +4692,17 @@ class JavascriptParser extends Parser {
4647
4692
  }
4648
4693
  }
4649
4694
  } else {
4650
- comments = [];
4651
- ast = JavascriptParser._parse(source, {
4652
- sourceType: this.sourceType,
4653
- onComment: comments,
4654
- onInsertedSemicolon: (pos) => semicolons.add(pos)
4655
- });
4695
+ ({ ast, comments, semicolons } = JavascriptParser._parse(
4696
+ source,
4697
+ {
4698
+ sourceType: this.sourceType,
4699
+ locations: true,
4700
+ ranges: true,
4701
+ comments: true,
4702
+ semicolons: true
4703
+ },
4704
+ this.options.parse
4705
+ ));
4656
4706
  }
4657
4707
 
4658
4708
  const oldScope = this.scope;
@@ -4702,10 +4752,11 @@ class JavascriptParser extends Parser {
4702
4752
  * @returns {BasicEvaluatedExpression} evaluation result
4703
4753
  */
4704
4754
  evaluate(source) {
4705
- const ast = JavascriptParser._parse(`(${source})`, {
4706
- sourceType: this.sourceType,
4707
- locations: false
4708
- });
4755
+ const { ast } = JavascriptParser._parse(
4756
+ `(${source})`,
4757
+ { sourceType: this.sourceType },
4758
+ this.options.parse
4759
+ );
4709
4760
  if (ast.body.length !== 1 || ast.body[0].type !== "ExpressionStatement") {
4710
4761
  throw new Error("evaluate: Source is not a expression");
4711
4762
  }
@@ -5255,27 +5306,84 @@ class JavascriptParser extends Parser {
5255
5306
  );
5256
5307
  }
5257
5308
 
5309
+ /**
5310
+ * @param {Compilation} compilation compilation
5311
+ * @param {Module} module module
5312
+ * @returns {ParseFunction | undefined} parser
5313
+ */
5314
+ static _getModuleParseFunction(compilation, module) {
5315
+ // Get from module if available
5316
+ if (
5317
+ module instanceof NormalModule &&
5318
+ module.parser instanceof JavascriptParser
5319
+ ) {
5320
+ return module.parser.options.parse;
5321
+ }
5322
+
5323
+ // Fallback to the global javascript parse function
5324
+ if (typeof compilation.options.module.parser.javascript !== "undefined") {
5325
+ return compilation.options.module.parser.javascript.parse;
5326
+ }
5327
+ }
5328
+
5258
5329
  /**
5259
5330
  * @param {string} code source code
5260
- * @param {ParseOptions} options parsing options
5261
- * @returns {Program} parsed ast
5331
+ * @param {InternalParseOptions} options parsing options
5332
+ * @param {ParseFunction=} customParse custom function to parse
5333
+ * @returns {ParseResult} parse result
5262
5334
  */
5263
- static _parse(code, options) {
5335
+ static _parse(code, options, customParse) {
5264
5336
  const type = options ? options.sourceType : "module";
5265
- /** @type {AcornOptions} */
5337
+ /** @type {ParseOptions} */
5266
5338
  const parserOptions = {
5267
5339
  ...defaultParserOptions,
5268
5340
  allowReturnOutsideFunction: type === "script",
5269
5341
  ...options,
5270
5342
  sourceType: type === "auto" ? "module" : type
5271
5343
  };
5344
+ /**
5345
+ * @param {string} code source code
5346
+ * @param {ParseOptions} options parsing options
5347
+ * @returns {ParseResult} parse result
5348
+ */
5349
+ const internalParse = (code, options) => {
5350
+ if (typeof customParse === "function") {
5351
+ return customParse(code, options);
5352
+ }
5353
+
5354
+ /** @type {Comment[]} */
5355
+ const comments = [];
5356
+
5357
+ if (options.comments) {
5358
+ /** @type {AcornOptions} */
5359
+ (options).onComment = comments;
5360
+ }
5361
+
5362
+ /** @type {Set<number>} */
5363
+ const semicolons = new Set();
5364
+
5365
+ if (options.semicolons) {
5366
+ /** @type {AcornOptions} */
5367
+ (options).onInsertedSemicolon = (pos) => semicolons.add(pos);
5368
+ }
5369
+
5370
+ const ast =
5371
+ /** @type {Program} */
5372
+ (parser.parse(code, /** @type {AcornOptions} */ (options)));
5373
+
5374
+ return { ast, comments, semicolons };
5375
+ };
5272
5376
 
5273
- /** @type {import("acorn").Program | undefined} */
5377
+ /** @type {Program | undefined} */
5274
5378
  let ast;
5379
+ /** @type {Comment[] | undefined} */
5380
+ let comments;
5381
+ /** @type {Set<number> | undefined} */
5382
+ let semicolons;
5275
5383
  let error;
5276
5384
  let threw = false;
5277
5385
  try {
5278
- ast = parser.parse(code, parserOptions);
5386
+ ({ ast, comments, semicolons } = internalParse(code, parserOptions));
5279
5387
  } catch (err) {
5280
5388
  error = err;
5281
5389
  threw = true;
@@ -5283,14 +5391,10 @@ class JavascriptParser extends Parser {
5283
5391
 
5284
5392
  if (threw && type === "auto") {
5285
5393
  parserOptions.sourceType = "script";
5286
- if (!("allowReturnOutsideFunction" in options)) {
5287
- parserOptions.allowReturnOutsideFunction = true;
5288
- }
5289
- if (Array.isArray(parserOptions.onComment)) {
5290
- parserOptions.onComment.length = 0;
5291
- }
5394
+ parserOptions.allowReturnOutsideFunction = true;
5395
+
5292
5396
  try {
5293
- ast = parser.parse(code, parserOptions);
5397
+ ({ ast, comments, semicolons } = internalParse(code, parserOptions));
5294
5398
  threw = false;
5295
5399
  } catch (_err) {
5296
5400
  // we use the error from first parse try
@@ -5302,7 +5406,7 @@ class JavascriptParser extends Parser {
5302
5406
  throw error;
5303
5407
  }
5304
5408
 
5305
- return /** @type {Program} */ (ast);
5409
+ return /** @type {ParseResult} */ ({ ast, comments, semicolons });
5306
5410
  }
5307
5411
 
5308
5412
  /**
@@ -64,7 +64,13 @@ class JsonParser extends Parser {
64
64
  const buildMeta = /** @type {BuildMeta} */ (state.module.buildMeta);
65
65
  buildMeta.exportsType = "default";
66
66
  buildMeta.defaultObject =
67
- typeof data === "object" ? "redirect-warn" : false;
67
+ typeof data === "object"
68
+ ? this.options.namedExports === false
69
+ ? false
70
+ : this.options.namedExports === true
71
+ ? "redirect"
72
+ : "redirect-warn"
73
+ : false;
68
74
  state.module.addDependency(
69
75
  new JsonExportsDependency(
70
76
  jsonData,
@@ -104,16 +104,6 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
104
104
  moduleGraph.addExtraReason(module, "used as library export");
105
105
  }
106
106
 
107
- /**
108
- * @param {Chunk} chunk the chunk
109
- * @param {RuntimeRequirements} set runtime requirements
110
- * @param {LibraryContext<T>} libraryContext context
111
- * @returns {void}
112
- */
113
- runtimeRequirements(chunk, set, libraryContext) {
114
- set.add(RuntimeGlobals.exports);
115
- }
116
-
117
107
  /**
118
108
  * @param {LibraryOptions} library normalized library option
119
109
  * @returns {T | false} preprocess as needed by overriding