webpack 5.102.0 → 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 (146) hide show
  1. package/README.md +121 -134
  2. package/lib/ChunkGraph.js +2 -2
  3. package/lib/CodeGenerationResults.js +1 -1
  4. package/lib/CompatibilityPlugin.js +25 -2
  5. package/lib/Compilation.js +30 -13
  6. package/lib/ConcatenationScope.js +0 -15
  7. package/lib/ContextModule.js +3 -1
  8. package/lib/CssModule.js +6 -1
  9. package/lib/DefinePlugin.js +12 -12
  10. package/lib/Dependency.js +8 -1
  11. package/lib/DependencyTemplate.js +1 -0
  12. package/lib/DependencyTemplates.js +1 -1
  13. package/lib/DotenvPlugin.js +457 -0
  14. package/lib/EnvironmentPlugin.js +19 -16
  15. package/lib/EvalSourceMapDevToolPlugin.js +16 -0
  16. package/lib/ExportsInfo.js +6 -2
  17. package/lib/ExternalModule.js +20 -28
  18. package/lib/ExternalModuleFactoryPlugin.js +10 -8
  19. package/lib/ExternalsPlugin.js +2 -1
  20. package/lib/FileSystemInfo.js +9 -12
  21. package/lib/ManifestPlugin.js +235 -0
  22. package/lib/Module.js +3 -0
  23. package/lib/ModuleFilenameHelpers.js +1 -1
  24. package/lib/ModuleGraph.js +2 -1
  25. package/lib/ModuleSourceTypesConstants.js +0 -6
  26. package/lib/MultiCompiler.js +1 -1
  27. package/lib/NodeStuffPlugin.js +419 -121
  28. package/lib/NormalModule.js +18 -17
  29. package/lib/NormalModuleFactory.js +75 -4
  30. package/lib/RuntimeGlobals.js +22 -4
  31. package/lib/RuntimePlugin.js +27 -6
  32. package/lib/RuntimeTemplate.js +125 -57
  33. package/lib/SourceMapDevToolPlugin.js +26 -8
  34. package/lib/WebpackOptionsApply.js +33 -9
  35. package/lib/asset/AssetBytesGenerator.js +2 -1
  36. package/lib/asset/AssetGenerator.js +3 -5
  37. package/lib/asset/AssetSourceGenerator.js +1 -1
  38. package/lib/cache/getLazyHashedEtag.js +1 -1
  39. package/lib/config/browserslistTargetHandler.js +82 -76
  40. package/lib/config/defaults.js +105 -20
  41. package/lib/config/normalization.js +2 -1
  42. package/lib/config/target.js +7 -1
  43. package/lib/css/CssGenerator.js +283 -57
  44. package/lib/css/CssLoadingRuntimeModule.js +2 -0
  45. package/lib/css/CssMergeStyleSheetsRuntimeModule.js +56 -0
  46. package/lib/css/CssModulesPlugin.js +86 -40
  47. package/lib/css/CssParser.js +1174 -667
  48. package/lib/css/walkCssTokens.js +98 -1
  49. package/lib/dependencies/CommonJsImportsParserPlugin.js +0 -9
  50. package/lib/dependencies/CommonJsPlugin.js +12 -0
  51. package/lib/dependencies/ContextElementDependency.js +2 -2
  52. package/lib/dependencies/CssIcssExportDependency.js +247 -8
  53. package/lib/dependencies/CssIcssFromIdentifierDependency.js +124 -0
  54. package/lib/dependencies/CssIcssGlobalIdentifierDependency.js +48 -0
  55. package/lib/dependencies/CssIcssImportDependency.js +60 -54
  56. package/lib/dependencies/CssIcssLocalIdentifierDependency.js +61 -0
  57. package/lib/dependencies/{CssSelfLocalIdentifierDependency.js → CssIcssSelfLocalIdentifierDependency.js} +88 -10
  58. package/lib/dependencies/CssIcssSymbolDependency.js +31 -29
  59. package/lib/dependencies/CssImportDependency.js +15 -5
  60. package/lib/dependencies/ExternalModuleInitFragment.js +1 -1
  61. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +95 -0
  62. package/lib/dependencies/HarmonyAcceptDependency.js +6 -1
  63. package/lib/dependencies/HarmonyAcceptImportDependency.js +2 -1
  64. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +12 -1
  65. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +32 -21
  66. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +12 -8
  67. package/lib/dependencies/HarmonyImportDependency.js +27 -28
  68. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +28 -69
  69. package/lib/dependencies/HarmonyImportSideEffectDependency.js +4 -3
  70. package/lib/dependencies/HarmonyImportSpecifierDependency.js +10 -8
  71. package/lib/dependencies/ImportContextDependency.js +13 -0
  72. package/lib/dependencies/ImportDependency.js +10 -4
  73. package/lib/dependencies/ImportEagerDependency.js +6 -3
  74. package/lib/dependencies/ImportMetaPlugin.js +98 -10
  75. package/lib/dependencies/ImportParserPlugin.js +19 -21
  76. package/lib/dependencies/ImportPhase.js +121 -0
  77. package/lib/dependencies/ImportWeakDependency.js +6 -3
  78. package/lib/dependencies/ModuleDependency.js +5 -1
  79. package/lib/dependencies/ModuleHotAcceptDependency.js +1 -1
  80. package/lib/dependencies/WorkerPlugin.js +1 -3
  81. package/lib/esm/ExportWebpackRequireRuntimeModule.js +1 -8
  82. package/lib/hmr/LazyCompilationPlugin.js +1 -0
  83. package/lib/ids/HashedModuleIdsPlugin.js +5 -7
  84. package/lib/ids/IdHelpers.js +5 -2
  85. package/lib/index.js +6 -0
  86. package/lib/javascript/ChunkHelpers.js +16 -5
  87. package/lib/javascript/JavascriptGenerator.js +101 -101
  88. package/lib/javascript/JavascriptModulesPlugin.js +25 -16
  89. package/lib/javascript/JavascriptParser.js +143 -39
  90. package/lib/json/JsonParser.js +7 -1
  91. package/lib/library/ModuleLibraryPlugin.js +0 -10
  92. package/lib/library/SystemLibraryPlugin.js +19 -5
  93. package/lib/library/UmdLibraryPlugin.js +1 -1
  94. package/lib/node/NodeTargetPlugin.js +9 -1
  95. package/lib/node/ReadFileCompileWasmPlugin.js +0 -2
  96. package/lib/optimize/ConcatenatedModule.js +161 -135
  97. package/lib/optimize/RealContentHashPlugin.js +5 -3
  98. package/lib/runtime/AsyncModuleRuntimeModule.js +28 -18
  99. package/lib/runtime/AutoPublicPathRuntimeModule.js +8 -3
  100. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +89 -55
  101. package/lib/serialization/FileMiddleware.js +1 -1
  102. package/lib/serialization/ObjectMiddleware.js +1 -1
  103. package/lib/stats/DefaultStatsFactoryPlugin.js +1 -1
  104. package/lib/util/Hash.js +35 -5
  105. package/lib/util/comparators.js +4 -3
  106. package/lib/util/create-schema-validation.js +1 -1
  107. package/lib/util/createHash.js +85 -15
  108. package/lib/util/hash/BatchedHash.js +47 -8
  109. package/lib/util/hash/wasm-hash.js +53 -13
  110. package/lib/util/internalSerializables.js +4 -4
  111. package/lib/util/jsonParseEvenBetterErrors.js +10 -0
  112. package/lib/wasm/EnableWasmLoadingPlugin.js +10 -4
  113. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +8 -5
  114. package/lib/wasm-async/AsyncWebAssemblyParser.js +0 -9
  115. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +8 -4
  116. package/lib/wasm-sync/WebAssemblyParser.js +0 -9
  117. package/lib/web/FetchCompileWasmPlugin.js +0 -2
  118. package/lib/web/JsonpChunkLoadingRuntimeModule.js +1 -1
  119. package/lib/webpack.js +85 -82
  120. package/module.d.ts +5 -0
  121. package/package.json +28 -26
  122. package/schemas/WebpackOptions.check.js +1 -1
  123. package/schemas/WebpackOptions.json +110 -15
  124. package/schemas/plugins/{HashedModuleIdsPlugin.check.d.ts → ManifestPlugin.check.d.ts} +1 -1
  125. package/schemas/plugins/ManifestPlugin.check.js +6 -0
  126. package/schemas/plugins/ManifestPlugin.json +98 -0
  127. package/schemas/plugins/SourceMapDevToolPlugin.check.js +1 -1
  128. package/schemas/plugins/SourceMapDevToolPlugin.json +16 -3
  129. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  130. package/schemas/plugins/container/ContainerReferencePlugin.json +4 -1
  131. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  132. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  133. package/schemas/plugins/container/ModuleFederationPlugin.json +4 -1
  134. package/schemas/plugins/css/CssAutoParserOptions.check.js +1 -1
  135. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.js +1 -1
  136. package/schemas/plugins/css/CssGlobalParserOptions.check.js +1 -1
  137. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +1 -1
  138. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  139. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  140. package/schemas/plugins/ids/HashedModuleIdsPlugin.check.d.ts +7 -0
  141. package/schemas/plugins/ids/HashedModuleIdsPlugin.check.js +6 -0
  142. package/schemas/plugins/{HashedModuleIdsPlugin.json → ids/HashedModuleIdsPlugin.json} +15 -2
  143. package/schemas/plugins/json/JsonModulesPluginParser.check.js +1 -1
  144. package/types.d.ts +788 -127
  145. package/lib/dependencies/CssLocalIdentifierDependency.js +0 -252
  146. package/schemas/plugins/HashedModuleIdsPlugin.check.js +0 -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 */
@@ -106,7 +112,7 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
106
112
  /** @typedef {Set<DestructuringAssignmentProperty>} DestructuringAssignmentProperties */
107
113
 
108
114
  // TODO remove cast when @types/estree has been updated to import assertions
109
- /** @typedef {import("estree").ImportExpression & { options?: Expression | null, phase?: "defer" }} ImportExpression */
115
+ /** @typedef {import("estree").ImportExpression & { phase?: "defer" }} ImportExpression */
110
116
 
111
117
  /** @type {string[]} */
112
118
  const EMPTY_ARRAY = [];
@@ -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
@@ -134,11 +134,21 @@ class SystemLibraryPlugin extends AbstractLibraryPlugin {
134
134
  );
135
135
  if (used) {
136
136
  if (otherUnused || used !== exportInfo.name) {
137
- instructions.push(
138
- `${external}${propertyAccess([
139
- used
140
- ])} = module${propertyAccess([exportInfo.name])};`
141
- );
137
+ if (exportInfo.name === "default") {
138
+ // Ideally we should use `module && module.__esModule ? module['default'] : module`
139
+ // But we need to keep compatibility with SystemJS format libraries (they are using `default`) and bundled SystemJS libraries from commonjs format
140
+ instructions.push(
141
+ `${external}${propertyAccess([
142
+ used
143
+ ])} = module["default"] || module;`
144
+ );
145
+ } else {
146
+ instructions.push(
147
+ `${external}${propertyAccess([
148
+ used
149
+ ])} = module${propertyAccess([exportInfo.name])};`
150
+ );
151
+ }
142
152
  handledNames.push(exportInfo.name);
143
153
  }
144
154
  } else {
@@ -154,6 +164,10 @@ class SystemLibraryPlugin extends AbstractLibraryPlugin {
154
164
  `Object.defineProperty(${external}, "__esModule", { value: true });`
155
165
  );
156
166
  }
167
+ // See comment above
168
+ instructions.push(
169
+ `${external}["default"] = module["default"] || module;`
170
+ );
157
171
  if (handledNames.length > 0) {
158
172
  const name = `${external}handledNames`;
159
173
  externalVarInitialization.push(
@@ -338,7 +338,7 @@ class UmdLibraryPlugin extends AbstractLibraryPlugin {
338
338
  : " var a = factory();\n"
339
339
  } for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n` +
340
340
  " }\n"
341
- }})(${runtimeTemplate.outputOptions.globalObject}, ${
341
+ }})(${runtimeTemplate.globalObject}, ${
342
342
  runtimeTemplate.supportsArrowFunction()
343
343
  ? `(${externalsArguments(externals)}) =>`
344
344
  : `function(${externalsArguments(externals)})`
@@ -7,6 +7,7 @@
7
7
 
8
8
  const ExternalsPlugin = require("../ExternalsPlugin");
9
9
 
10
+ /** @typedef {import("../../declarations/WebpackOptions").ExternalsType} ExternalsType */
10
11
  /** @typedef {import("../Compiler")} Compiler */
11
12
 
12
13
  const builtins = [
@@ -72,13 +73,20 @@ const builtins = [
72
73
  ];
73
74
 
74
75
  class NodeTargetPlugin {
76
+ /**
77
+ * @param {ExternalsType} type default external type
78
+ */
79
+ constructor(type = "node-commonjs") {
80
+ this.type = type;
81
+ }
82
+
75
83
  /**
76
84
  * Apply the plugin
77
85
  * @param {Compiler} compiler the compiler instance
78
86
  * @returns {void}
79
87
  */
80
88
  apply(compiler) {
81
- new ExternalsPlugin("node-commonjs", builtins).apply(compiler);
89
+ new ExternalsPlugin(this.type, builtins).apply(compiler);
82
90
  }
83
91
  }
84
92
 
@@ -19,8 +19,6 @@ const WasmChunkLoadingRuntimeModule = require("../wasm-sync/WasmChunkLoadingRunt
19
19
  * @property {boolean=} import use import?
20
20
  */
21
21
 
22
- // TODO webpack 6 remove
23
-
24
22
  const PLUGIN_NAME = "ReadFileCompileWasmPlugin";
25
23
 
26
24
  class ReadFileCompileWasmPlugin {