webpack 5.59.0 → 5.71.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.

Potentially problematic release.


This version of webpack might be problematic. Click here for more details.

Files changed (209) hide show
  1. package/README.md +1 -7
  2. package/hot/lazy-compilation-node.js +3 -1
  3. package/hot/poll.js +1 -1
  4. package/hot/signal.js +1 -1
  5. package/lib/APIPlugin.js +33 -0
  6. package/lib/BannerPlugin.js +10 -4
  7. package/lib/Cache.js +1 -1
  8. package/lib/CacheFacade.js +4 -11
  9. package/lib/Chunk.js +6 -3
  10. package/lib/ChunkGraph.js +1 -2
  11. package/lib/ChunkGroup.js +1 -1
  12. package/lib/CleanPlugin.js +81 -20
  13. package/lib/Compilation.js +179 -91
  14. package/lib/Compiler.js +86 -17
  15. package/lib/ConstPlugin.js +2 -2
  16. package/lib/ContextModule.js +142 -51
  17. package/lib/ContextModuleFactory.js +65 -25
  18. package/lib/DelegatedModule.js +1 -1
  19. package/lib/DelegatedModuleFactoryPlugin.js +1 -1
  20. package/lib/Dependency.js +17 -0
  21. package/lib/DependencyTemplate.js +9 -0
  22. package/lib/DependencyTemplates.js +1 -1
  23. package/lib/DllModule.js +1 -1
  24. package/lib/EntryOptionPlugin.js +2 -0
  25. package/lib/ErrorHelpers.js +2 -2
  26. package/lib/EvalDevToolModulePlugin.js +16 -1
  27. package/lib/EvalSourceMapDevToolPlugin.js +18 -1
  28. package/lib/ExportsInfo.js +4 -4
  29. package/lib/ExternalModule.js +94 -54
  30. package/lib/ExternalModuleFactoryPlugin.js +5 -5
  31. package/lib/FileSystemInfo.js +89 -44
  32. package/lib/Generator.js +3 -0
  33. package/lib/HookWebpackError.js +1 -1
  34. package/lib/HotModuleReplacementPlugin.js +3 -1
  35. package/lib/LoaderOptionsPlugin.js +1 -1
  36. package/lib/Module.js +27 -4
  37. package/lib/ModuleFilenameHelpers.js +8 -4
  38. package/lib/ModuleHashingError.js +29 -0
  39. package/lib/MultiCompiler.js +1 -1
  40. package/lib/MultiWatching.js +1 -1
  41. package/lib/NodeStuffPlugin.js +10 -0
  42. package/lib/NormalModule.js +41 -26
  43. package/lib/NormalModuleFactory.js +42 -37
  44. package/lib/ProgressPlugin.js +4 -5
  45. package/lib/RawModule.js +1 -1
  46. package/lib/RuntimeGlobals.js +29 -1
  47. package/lib/RuntimeModule.js +1 -1
  48. package/lib/RuntimePlugin.js +77 -1
  49. package/lib/RuntimeTemplate.js +114 -2
  50. package/lib/Template.js +2 -1
  51. package/lib/TemplatedPathPlugin.js +48 -23
  52. package/lib/WatchIgnorePlugin.js +19 -7
  53. package/lib/Watching.js +33 -19
  54. package/lib/WebpackOptionsApply.js +57 -11
  55. package/lib/asset/AssetGenerator.js +193 -63
  56. package/lib/asset/AssetModulesPlugin.js +3 -0
  57. package/lib/asset/RawDataUrlModule.js +148 -0
  58. package/lib/async-modules/AwaitDependenciesInitFragment.js +4 -4
  59. package/lib/buildChunkGraph.js +36 -6
  60. package/lib/cache/PackFileCacheStrategy.js +7 -4
  61. package/lib/cache/ResolverCachePlugin.js +90 -29
  62. package/lib/cli.js +44 -3
  63. package/lib/config/browserslistTargetHandler.js +41 -6
  64. package/lib/config/defaults.js +115 -19
  65. package/lib/config/normalization.js +9 -0
  66. package/lib/config/target.js +10 -0
  67. package/lib/container/ContainerEntryModule.js +8 -5
  68. package/lib/container/FallbackModule.js +4 -4
  69. package/lib/container/RemoteModule.js +4 -2
  70. package/lib/css/CssExportsGenerator.js +139 -0
  71. package/lib/css/CssGenerator.js +109 -0
  72. package/lib/css/CssLoadingRuntimeModule.js +440 -0
  73. package/lib/css/CssModulesPlugin.js +462 -0
  74. package/lib/css/CssParser.js +618 -0
  75. package/lib/css/walkCssTokens.js +659 -0
  76. package/lib/debug/ProfilingPlugin.js +24 -21
  77. package/lib/dependencies/AMDRequireDependency.js +6 -6
  78. package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -2
  79. package/lib/dependencies/CommonJsFullRequireDependency.js +5 -1
  80. package/lib/dependencies/CommonJsImportsParserPlugin.js +5 -3
  81. package/lib/dependencies/CommonJsRequireContextDependency.js +5 -1
  82. package/lib/dependencies/ContextDependency.js +1 -0
  83. package/lib/dependencies/ContextDependencyHelpers.js +3 -3
  84. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +4 -1
  85. package/lib/dependencies/ContextElementDependency.js +41 -3
  86. package/lib/dependencies/CreateScriptUrlDependency.js +12 -0
  87. package/lib/dependencies/CssExportDependency.js +85 -0
  88. package/lib/dependencies/CssImportDependency.js +75 -0
  89. package/lib/dependencies/CssLocalIdentifierDependency.js +119 -0
  90. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +101 -0
  91. package/lib/dependencies/CssUrlDependency.js +132 -0
  92. package/lib/dependencies/ExportsInfoDependency.js +6 -0
  93. package/lib/dependencies/HarmonyAcceptImportDependency.js +5 -3
  94. package/lib/dependencies/HarmonyCompatibilityDependency.js +5 -5
  95. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +95 -0
  96. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +12 -3
  97. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +25 -17
  98. package/lib/dependencies/HarmonyExportInitFragment.js +4 -1
  99. package/lib/dependencies/HarmonyImportDependency.js +23 -0
  100. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +142 -45
  101. package/lib/dependencies/HarmonyImportSpecifierDependency.js +46 -22
  102. package/lib/dependencies/HarmonyModulesPlugin.js +10 -0
  103. package/lib/dependencies/ImportContextDependency.js +0 -2
  104. package/lib/dependencies/ImportMetaContextDependency.js +35 -0
  105. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +252 -0
  106. package/lib/dependencies/ImportMetaContextPlugin.js +59 -0
  107. package/lib/dependencies/ImportMetaPlugin.js +22 -3
  108. package/lib/dependencies/LoaderPlugin.js +4 -2
  109. package/lib/dependencies/RequireContextDependency.js +0 -16
  110. package/lib/dependencies/RequireEnsureDependency.js +2 -2
  111. package/lib/dependencies/URLDependency.js +3 -8
  112. package/lib/dependencies/URLPlugin.js +1 -1
  113. package/lib/esm/ModuleChunkFormatPlugin.js +74 -49
  114. package/lib/esm/ModuleChunkLoadingPlugin.js +3 -1
  115. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +25 -9
  116. package/lib/hmr/HotModuleReplacement.runtime.js +29 -14
  117. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +4 -3
  118. package/lib/hmr/LazyCompilationPlugin.js +54 -26
  119. package/lib/hmr/lazyCompilationBackend.js +51 -12
  120. package/lib/ids/DeterministicModuleIdsPlugin.js +55 -35
  121. package/lib/ids/HashedModuleIdsPlugin.js +11 -14
  122. package/lib/ids/IdHelpers.js +25 -11
  123. package/lib/ids/NamedModuleIdsPlugin.js +6 -9
  124. package/lib/ids/NaturalModuleIdsPlugin.js +10 -13
  125. package/lib/ids/OccurrenceModuleIdsPlugin.js +13 -10
  126. package/lib/ids/SyncModuleIdsPlugin.js +140 -0
  127. package/lib/index.js +13 -0
  128. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +2 -2
  129. package/lib/javascript/BasicEvaluatedExpression.js +5 -2
  130. package/lib/javascript/ChunkHelpers.js +33 -0
  131. package/lib/javascript/JavascriptGenerator.js +1 -0
  132. package/lib/javascript/JavascriptModulesPlugin.js +27 -2
  133. package/lib/javascript/JavascriptParser.js +82 -48
  134. package/lib/javascript/StartupHelpers.js +7 -30
  135. package/lib/library/AssignLibraryPlugin.js +39 -15
  136. package/lib/library/EnableLibraryPlugin.js +11 -0
  137. package/lib/library/UmdLibraryPlugin.js +5 -3
  138. package/lib/node/NodeTargetPlugin.js +3 -0
  139. package/lib/node/NodeWatchFileSystem.js +85 -31
  140. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +23 -8
  141. package/lib/node/RequireChunkLoadingRuntimeModule.js +24 -9
  142. package/lib/optimize/ConcatenatedModule.js +21 -9
  143. package/lib/optimize/ModuleConcatenationPlugin.js +5 -2
  144. package/lib/optimize/SplitChunksPlugin.js +8 -1
  145. package/lib/runtime/AsyncModuleRuntimeModule.js +27 -17
  146. package/lib/runtime/BaseUriRuntimeModule.js +31 -0
  147. package/lib/runtime/CreateScriptRuntimeModule.js +36 -0
  148. package/lib/runtime/CreateScriptUrlRuntimeModule.js +9 -34
  149. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +76 -0
  150. package/lib/schemes/HttpUriPlugin.js +77 -14
  151. package/lib/serialization/FileMiddleware.js +44 -9
  152. package/lib/sharing/ConsumeSharedModule.js +8 -2
  153. package/lib/sharing/ConsumeSharedRuntimeModule.js +26 -5
  154. package/lib/sharing/ProvideSharedModule.js +4 -2
  155. package/lib/sharing/ShareRuntimeModule.js +1 -1
  156. package/lib/sharing/utils.js +1 -1
  157. package/lib/stats/DefaultStatsFactoryPlugin.js +113 -68
  158. package/lib/stats/DefaultStatsPrinterPlugin.js +89 -24
  159. package/lib/util/ArrayHelpers.js +30 -0
  160. package/lib/util/AsyncQueue.js +1 -1
  161. package/lib/util/compileBooleanMatcher.js +1 -1
  162. package/lib/util/create-schema-validation.js +9 -2
  163. package/lib/util/createHash.js +12 -0
  164. package/lib/util/deprecation.js +10 -2
  165. package/lib/util/deterministicGrouping.js +1 -1
  166. package/lib/util/extractUrlAndGlobal.js +3 -0
  167. package/lib/util/fs.js +11 -0
  168. package/lib/util/hash/BatchedHash.js +7 -4
  169. package/lib/util/hash/md4.js +20 -0
  170. package/lib/util/hash/wasm-hash.js +163 -0
  171. package/lib/util/hash/xxhash64.js +5 -139
  172. package/lib/util/identifier.js +65 -44
  173. package/lib/util/internalSerializables.js +15 -0
  174. package/lib/util/nonNumericOnlyHash.js +22 -0
  175. package/lib/util/semver.js +17 -10
  176. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +9 -3
  177. package/lib/web/JsonpChunkLoadingRuntimeModule.js +20 -9
  178. package/lib/webpack.js +10 -3
  179. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -11
  180. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +33 -22
  181. package/module.d.ts +215 -0
  182. package/package.json +23 -28
  183. package/schemas/WebpackOptions.check.js +1 -1
  184. package/schemas/WebpackOptions.json +254 -29
  185. package/schemas/plugins/DllReferencePlugin.check.js +1 -1
  186. package/schemas/plugins/HashedModuleIdsPlugin.check.js +1 -1
  187. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  188. package/schemas/plugins/asset/AssetGeneratorOptions.check.js +1 -1
  189. package/schemas/plugins/asset/AssetParserOptions.check.js +1 -1
  190. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +1 -1
  191. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  192. package/schemas/plugins/container/ContainerPlugin.json +2 -1
  193. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  194. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  195. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  196. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  197. package/schemas/plugins/container/ModuleFederationPlugin.json +3 -1
  198. package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +7 -0
  199. package/schemas/plugins/css/CssGeneratorOptions.check.js +6 -0
  200. package/schemas/plugins/css/CssGeneratorOptions.json +3 -0
  201. package/schemas/plugins/css/CssParserOptions.check.d.ts +7 -0
  202. package/schemas/plugins/css/CssParserOptions.check.js +6 -0
  203. package/schemas/plugins/css/CssParserOptions.json +3 -0
  204. package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.js +1 -1
  205. package/schemas/plugins/optimize/LimitChunkCountPlugin.check.js +1 -1
  206. package/schemas/plugins/optimize/MinChunkSizePlugin.check.js +1 -1
  207. package/schemas/plugins/schemes/HttpUriPlugin.check.js +1 -1
  208. package/schemas/plugins/schemes/HttpUriPlugin.json +4 -0
  209. package/types.d.ts +628 -179
package/lib/Compiler.js CHANGED
@@ -58,13 +58,13 @@ const { isSourceEqual } = require("./util/source");
58
58
  /**
59
59
  * @template T
60
60
  * @callback Callback
61
- * @param {Error=} err
61
+ * @param {(Error | null)=} err
62
62
  * @param {T=} result
63
63
  */
64
64
 
65
65
  /**
66
66
  * @callback RunAsChildCallback
67
- * @param {Error=} err
67
+ * @param {(Error | null)=} err
68
68
  * @param {Chunk[]=} entries
69
69
  * @param {Compilation=} compilation
70
70
  */
@@ -119,8 +119,9 @@ const includesHash = (filename, hashes) => {
119
119
  class Compiler {
120
120
  /**
121
121
  * @param {string} context the compilation path
122
+ * @param {WebpackOptions} options options
122
123
  */
123
- constructor(context) {
124
+ constructor(context, options = /** @type {WebpackOptions} */ ({})) {
124
125
  this.hooks = Object.freeze({
125
126
  /** @type {SyncHook<[]>} */
126
127
  initialize: new SyncHook([]),
@@ -164,6 +165,11 @@ class Compiler {
164
165
  /** @type {AsyncSeriesHook<[Compilation]>} */
165
166
  afterCompile: new AsyncSeriesHook(["compilation"]),
166
167
 
168
+ /** @type {AsyncSeriesHook<[]>} */
169
+ readRecords: new AsyncSeriesHook([]),
170
+ /** @type {AsyncSeriesHook<[]>} */
171
+ emitRecords: new AsyncSeriesHook([]),
172
+
167
173
  /** @type {AsyncSeriesHook<[Compiler]>} */
168
174
  watchRun: new AsyncSeriesHook(["compiler"]),
169
175
  /** @type {SyncHook<[Error]>} */
@@ -240,8 +246,7 @@ class Compiler {
240
246
 
241
247
  this.infrastructureLogger = undefined;
242
248
 
243
- /** @type {WebpackOptions} */
244
- this.options = /** @type {WebpackOptions} */ ({});
249
+ this.options = options;
245
250
 
246
251
  this.context = context;
247
252
 
@@ -263,6 +268,8 @@ class Compiler {
263
268
  /** @type {boolean} */
264
269
  this.watchMode = false;
265
270
 
271
+ this._backCompat = this.options.experiments.backCompat !== false;
272
+
266
273
  /** @type {Compilation} */
267
274
  this._lastCompilation = undefined;
268
275
  /** @type {NormalModuleFactory} */
@@ -538,8 +545,21 @@ class Compiler {
538
545
  */
539
546
  runAsChild(callback) {
540
547
  const startTime = Date.now();
548
+
549
+ const finalCallback = (err, entries, compilation) => {
550
+ try {
551
+ callback(err, entries, compilation);
552
+ } catch (e) {
553
+ const err = new WebpackError(
554
+ `compiler.runAsChild callback error: ${e}`
555
+ );
556
+ err.details = e.stack;
557
+ this.parentCompilation.errors.push(err);
558
+ }
559
+ };
560
+
541
561
  this.compile((err, compilation) => {
542
- if (err) return callback(err);
562
+ if (err) return finalCallback(err);
543
563
 
544
564
  this.parentCompilation.children.push(compilation);
545
565
  for (const { name, source, info } of compilation.getAssets()) {
@@ -554,7 +574,7 @@ class Compiler {
554
574
  compilation.startTime = startTime;
555
575
  compilation.endTime = Date.now();
556
576
 
557
- return callback(null, entries, compilation);
577
+ return finalCallback(null, entries, compilation);
558
578
  });
559
579
  }
560
580
 
@@ -589,7 +609,7 @@ class Compiler {
589
609
  let immutable = info.immutable;
590
610
  const queryStringIdx = targetFile.indexOf("?");
591
611
  if (queryStringIdx >= 0) {
592
- targetFile = targetFile.substr(0, queryStringIdx);
612
+ targetFile = targetFile.slice(0, queryStringIdx);
593
613
  // We may remove the hash, which is in the query string
594
614
  // So we recheck if the file is immutable
595
615
  // This doesn't cover all cases, but immutable is only a performance optimization anyway
@@ -880,8 +900,32 @@ ${other}`);
880
900
  * @returns {void}
881
901
  */
882
902
  emitRecords(callback) {
883
- if (!this.recordsOutputPath) return callback();
903
+ if (this.hooks.emitRecords.isUsed()) {
904
+ if (this.recordsOutputPath) {
905
+ asyncLib.parallel(
906
+ [
907
+ cb => this.hooks.emitRecords.callAsync(cb),
908
+ this._emitRecords.bind(this)
909
+ ],
910
+ err => callback(err)
911
+ );
912
+ } else {
913
+ this.hooks.emitRecords.callAsync(callback);
914
+ }
915
+ } else {
916
+ if (this.recordsOutputPath) {
917
+ this._emitRecords(callback);
918
+ } else {
919
+ callback();
920
+ }
921
+ }
922
+ }
884
923
 
924
+ /**
925
+ * @param {Callback<void>} callback signals when the call finishes
926
+ * @returns {void}
927
+ */
928
+ _emitRecords(callback) {
885
929
  const writeFile = () => {
886
930
  this.outputFileSystem.writeFile(
887
931
  this.recordsOutputPath,
@@ -924,6 +968,31 @@ ${other}`);
924
968
  * @returns {void}
925
969
  */
926
970
  readRecords(callback) {
971
+ if (this.hooks.readRecords.isUsed()) {
972
+ if (this.recordsInputPath) {
973
+ asyncLib.parallel([
974
+ cb => this.hooks.readRecords.callAsync(cb),
975
+ this._readRecords.bind(this)
976
+ ]);
977
+ } else {
978
+ this.records = {};
979
+ this.hooks.readRecords.callAsync(callback);
980
+ }
981
+ } else {
982
+ if (this.recordsInputPath) {
983
+ this._readRecords(callback);
984
+ } else {
985
+ this.records = {};
986
+ callback();
987
+ }
988
+ }
989
+ }
990
+
991
+ /**
992
+ * @param {Callback<void>} callback signals when the call finishes
993
+ * @returns {void}
994
+ */
995
+ _readRecords(callback) {
927
996
  if (!this.recordsInputPath) {
928
997
  this.records = {};
929
998
  return callback();
@@ -963,7 +1032,13 @@ ${other}`);
963
1032
  outputOptions,
964
1033
  plugins
965
1034
  ) {
966
- const childCompiler = new Compiler(this.context);
1035
+ const childCompiler = new Compiler(this.context, {
1036
+ ...this.options,
1037
+ output: {
1038
+ ...this.options.output,
1039
+ ...outputOptions
1040
+ }
1041
+ });
967
1042
  childCompiler.name = compilerName;
968
1043
  childCompiler.outputPath = this.outputPath;
969
1044
  childCompiler.inputFileSystem = this.inputFileSystem;
@@ -976,6 +1051,7 @@ ${other}`);
976
1051
  childCompiler.fsStartTime = this.fsStartTime;
977
1052
  childCompiler.cache = this.cache;
978
1053
  childCompiler.compilerPath = `${this.compilerPath}${compilerName}|${compilerIndex}|`;
1054
+ childCompiler._backCompat = this._backCompat;
979
1055
 
980
1056
  const relativeCompilerName = makePathsRelative(
981
1057
  this.context,
@@ -991,13 +1067,6 @@ ${other}`);
991
1067
  this.records[relativeCompilerName].push((childCompiler.records = {}));
992
1068
  }
993
1069
 
994
- childCompiler.options = {
995
- ...this.options,
996
- output: {
997
- ...this.options.output,
998
- ...outputOptions
999
- }
1000
- };
1001
1070
  childCompiler.parentCompilation = compilation;
1002
1071
  childCompiler.root = this.root;
1003
1072
  if (Array.isArray(plugins)) {
@@ -324,7 +324,7 @@ class ConstPlugin {
324
324
  }
325
325
  } else if (expression.operator === "??") {
326
326
  const param = parser.evaluateExpression(expression.left);
327
- const keepRight = param && param.asNullish();
327
+ const keepRight = param.asNullish();
328
328
  if (typeof keepRight === "boolean") {
329
329
  // ------------------------------------------
330
330
  //
@@ -407,7 +407,7 @@ class ConstPlugin {
407
407
  const expression = optionalExpressionsStack.pop();
408
408
  const evaluated = parser.evaluateExpression(expression);
409
409
 
410
- if (evaluated && evaluated.asNullish()) {
410
+ if (evaluated.asNullish()) {
411
411
  // ------------------------------------------
412
412
  //
413
413
  // Given the following code:
@@ -19,7 +19,11 @@ const {
19
19
  keepOriginalOrder,
20
20
  compareModulesById
21
21
  } = require("./util/comparators");
22
- const { contextify, parseResource } = require("./util/identifier");
22
+ const {
23
+ contextify,
24
+ parseResource,
25
+ makePathsRelative
26
+ } = require("./util/identifier");
23
27
  const makeSerializable = require("./util/makeSerializable");
24
28
 
25
29
  /** @typedef {import("webpack-sources").Source} Source */
@@ -61,7 +65,7 @@ const makeSerializable = require("./util/makeSerializable");
61
65
 
62
66
  /**
63
67
  * @typedef {Object} ContextModuleOptionsExtras
64
- * @property {string} resource
68
+ * @property {false|string|string[]} resource
65
69
  * @property {string=} resourceQuery
66
70
  * @property {string=} resourceFragment
67
71
  * @property {TODO} resolveOptions
@@ -71,7 +75,7 @@ const makeSerializable = require("./util/makeSerializable");
71
75
 
72
76
  /**
73
77
  * @callback ResolveDependenciesCallback
74
- * @param {Error=} err
78
+ * @param {(Error | null)=} err
75
79
  * @param {ContextElementDependency[]=} dependencies
76
80
  */
77
81
 
@@ -92,23 +96,36 @@ class ContextModule extends Module {
92
96
  * @param {ContextModuleOptions} options options object
93
97
  */
94
98
  constructor(resolveDependencies, options) {
95
- const parsed = parseResource(options ? options.resource : "");
96
- const resource = parsed.path;
97
- const resourceQuery = (options && options.resourceQuery) || parsed.query;
98
- const resourceFragment =
99
- (options && options.resourceFragment) || parsed.fragment;
100
-
101
- super("javascript/dynamic", resource);
99
+ if (!options || typeof options.resource === "string") {
100
+ const parsed = parseResource(
101
+ options ? /** @type {string} */ (options.resource) : ""
102
+ );
103
+ const resource = parsed.path;
104
+ const resourceQuery = (options && options.resourceQuery) || parsed.query;
105
+ const resourceFragment =
106
+ (options && options.resourceFragment) || parsed.fragment;
107
+
108
+ super("javascript/dynamic", resource);
109
+ /** @type {ContextModuleOptions} */
110
+ this.options = {
111
+ ...options,
112
+ resource,
113
+ resourceQuery,
114
+ resourceFragment
115
+ };
116
+ } else {
117
+ super("javascript/dynamic");
118
+ /** @type {ContextModuleOptions} */
119
+ this.options = {
120
+ ...options,
121
+ resource: options.resource,
122
+ resourceQuery: options.resourceQuery || "",
123
+ resourceFragment: options.resourceFragment || ""
124
+ };
125
+ }
102
126
 
103
127
  // Info from Factory
104
128
  this.resolveDependencies = resolveDependencies;
105
- /** @type {ContextModuleOptions} */
106
- this.options = {
107
- ...options,
108
- resource,
109
- resourceQuery,
110
- resourceFragment
111
- };
112
129
  if (options && options.resolveOptions !== undefined) {
113
130
  this.resolveOptions = options.resolveOptions;
114
131
  }
@@ -149,16 +166,18 @@ class ContextModule extends Module {
149
166
  this.resolveDependencies = undefined;
150
167
  }
151
168
 
152
- prettyRegExp(regexString) {
153
- // remove the "/" at the front and the beginning
154
- // "/foo/" -> "foo"
155
- return regexString
156
- .substring(1, regexString.length - 1)
157
- .replace(/!/g, "%21");
169
+ _prettyRegExp(regexString, stripSlash = true) {
170
+ const str = (regexString + "").replace(/!/g, "%21").replace(/\|/g, "%7C");
171
+ return stripSlash ? str.substring(1, str.length - 1) : str;
158
172
  }
159
173
 
160
174
  _createIdentifier() {
161
- let identifier = this.context;
175
+ let identifier =
176
+ this.context ||
177
+ (typeof this.options.resource === "string" ||
178
+ this.options.resource === false
179
+ ? `${this.options.resource}`
180
+ : this.options.resource.join("|"));
162
181
  if (this.options.resourceQuery) {
163
182
  identifier += `|${this.options.resourceQuery}`;
164
183
  }
@@ -175,13 +194,19 @@ class ContextModule extends Module {
175
194
  identifier += `|${this.options.addon}`;
176
195
  }
177
196
  if (this.options.regExp) {
178
- identifier += `|${this.options.regExp}`;
197
+ identifier += `|${this._prettyRegExp(this.options.regExp, false)}`;
179
198
  }
180
199
  if (this.options.include) {
181
- identifier += `|include: ${this.options.include}`;
200
+ identifier += `|include: ${this._prettyRegExp(
201
+ this.options.include,
202
+ false
203
+ )}`;
182
204
  }
183
205
  if (this.options.exclude) {
184
- identifier += `|exclude: ${this.options.exclude}`;
206
+ identifier += `|exclude: ${this._prettyRegExp(
207
+ this.options.exclude,
208
+ false
209
+ )}`;
185
210
  }
186
211
  if (this.options.referencedExports) {
187
212
  identifier += `|referencedExports: ${JSON.stringify(
@@ -217,7 +242,19 @@ class ContextModule extends Module {
217
242
  * @returns {string} a user readable identifier of the module
218
243
  */
219
244
  readableIdentifier(requestShortener) {
220
- let identifier = requestShortener.shorten(this.context) + "/";
245
+ let identifier;
246
+ if (this.context) {
247
+ identifier = requestShortener.shorten(this.context) + "/";
248
+ } else if (
249
+ typeof this.options.resource === "string" ||
250
+ this.options.resource === false
251
+ ) {
252
+ identifier = requestShortener.shorten(`${this.options.resource}`) + "/";
253
+ } else {
254
+ identifier = this.options.resource
255
+ .map(r => requestShortener.shorten(r) + "/")
256
+ .join(" ");
257
+ }
221
258
  if (this.options.resourceQuery) {
222
259
  identifier += ` ${this.options.resourceQuery}`;
223
260
  }
@@ -231,13 +268,13 @@ class ContextModule extends Module {
231
268
  identifier += ` ${requestShortener.shorten(this.options.addon)}`;
232
269
  }
233
270
  if (this.options.regExp) {
234
- identifier += ` ${this.prettyRegExp(this.options.regExp + "")}`;
271
+ identifier += ` ${this._prettyRegExp(this.options.regExp)}`;
235
272
  }
236
273
  if (this.options.include) {
237
- identifier += ` include: ${this.prettyRegExp(this.options.include + "")}`;
274
+ identifier += ` include: ${this._prettyRegExp(this.options.include)}`;
238
275
  }
239
276
  if (this.options.exclude) {
240
- identifier += ` exclude: ${this.prettyRegExp(this.options.exclude + "")}`;
277
+ identifier += ` exclude: ${this._prettyRegExp(this.options.exclude)}`;
241
278
  }
242
279
  if (this.options.referencedExports) {
243
280
  identifier += ` referencedExports: ${this.options.referencedExports
@@ -267,11 +304,31 @@ class ContextModule extends Module {
267
304
  * @returns {string | null} an identifier for library inclusion
268
305
  */
269
306
  libIdent(options) {
270
- let identifier = contextify(
271
- options.context,
272
- this.context,
273
- options.associatedObjectForCache
274
- );
307
+ let identifier;
308
+
309
+ if (this.context) {
310
+ identifier = contextify(
311
+ options.context,
312
+ this.context,
313
+ options.associatedObjectForCache
314
+ );
315
+ } else if (typeof this.options.resource === "string") {
316
+ identifier = contextify(
317
+ options.context,
318
+ this.options.resource,
319
+ options.associatedObjectForCache
320
+ );
321
+ } else if (this.options.resource === false) {
322
+ identifier = "false";
323
+ } else {
324
+ identifier = this.options.resource
325
+ .map(res =>
326
+ contextify(options.context, res, options.associatedObjectForCache)
327
+ )
328
+ .join(" ");
329
+ }
330
+
331
+ if (this.layer) identifier = `(${this.layer})/${identifier}`;
275
332
  if (this.options.mode) {
276
333
  identifier += ` ${this.options.mode}`;
277
334
  }
@@ -286,13 +343,13 @@ class ContextModule extends Module {
286
343
  )}`;
287
344
  }
288
345
  if (this.options.regExp) {
289
- identifier += ` ${this.prettyRegExp(this.options.regExp + "")}`;
346
+ identifier += ` ${this._prettyRegExp(this.options.regExp)}`;
290
347
  }
291
348
  if (this.options.include) {
292
- identifier += ` include: ${this.prettyRegExp(this.options.include + "")}`;
349
+ identifier += ` include: ${this._prettyRegExp(this.options.include)}`;
293
350
  }
294
351
  if (this.options.exclude) {
295
- identifier += ` exclude: ${this.prettyRegExp(this.options.exclude + "")}`;
352
+ identifier += ` exclude: ${this._prettyRegExp(this.options.exclude)}`;
296
353
  }
297
354
  if (this.options.referencedExports) {
298
355
  identifier += ` referencedExports: ${this.options.referencedExports
@@ -312,15 +369,16 @@ class ContextModule extends Module {
312
369
 
313
370
  /**
314
371
  * @param {NeedBuildContext} context context info
315
- * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
372
+ * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
316
373
  * @returns {void}
317
374
  */
318
375
  needBuild({ fileSystemInfo }, callback) {
319
376
  // build if enforced
320
377
  if (this._forceBuild) return callback(null, true);
321
378
 
322
- // always build when we have no snapshot
323
- if (!this.buildInfo.snapshot) return callback(null, true);
379
+ // always build when we have no snapshot and context
380
+ if (!this.buildInfo.snapshot)
381
+ return callback(null, Boolean(this.context || this.options.resource));
324
382
 
325
383
  fileSystemInfo.checkSnapshotValid(this.buildInfo.snapshot, (err, valid) => {
326
384
  callback(err, !valid);
@@ -435,10 +493,16 @@ class ContextModule extends Module {
435
493
  );
436
494
  return;
437
495
  }
496
+ if (!this.context && !this.options.resource) return callback();
497
+
438
498
  compilation.fileSystemInfo.createSnapshot(
439
499
  startTime,
440
500
  null,
441
- [this.context],
501
+ this.context
502
+ ? [this.context]
503
+ : typeof this.options.resource === "string"
504
+ ? [this.options.resource]
505
+ : /** @type {string[]} */ (this.options.resource),
442
506
  null,
443
507
  SNAPSHOT_OPTIONS,
444
508
  (err, snapshot) => {
@@ -462,7 +526,15 @@ class ContextModule extends Module {
462
526
  missingDependencies,
463
527
  buildDependencies
464
528
  ) {
465
- contextDependencies.add(this.context);
529
+ if (this.context) {
530
+ contextDependencies.add(this.context);
531
+ } else if (typeof this.options.resource === "string") {
532
+ contextDependencies.add(this.options.resource);
533
+ } else if (this.options.resource === false) {
534
+ return;
535
+ } else {
536
+ for (const res of this.options.resource) contextDependencies.add(res);
537
+ }
466
538
  }
467
539
 
468
540
  /**
@@ -998,9 +1070,21 @@ module.exports = webpackEmptyAsyncContext;`;
998
1070
  return this.getSourceForEmptyContext(id, runtimeTemplate);
999
1071
  }
1000
1072
 
1001
- getSource(sourceString) {
1073
+ /**
1074
+ * @param {string} sourceString source content
1075
+ * @param {Compilation=} compilation the compilation
1076
+ * @returns {Source} generated source
1077
+ */
1078
+ getSource(sourceString, compilation) {
1002
1079
  if (this.useSourceMap || this.useSimpleSourceMap) {
1003
- return new OriginalSource(sourceString, this.identifier());
1080
+ return new OriginalSource(
1081
+ sourceString,
1082
+ `webpack://${makePathsRelative(
1083
+ (compilation && compilation.compiler.context) || "",
1084
+ this.identifier(),
1085
+ compilation && compilation.compiler.root
1086
+ )}`
1087
+ );
1004
1088
  }
1005
1089
  return new RawSource(sourceString);
1006
1090
  }
@@ -1010,16 +1094,23 @@ module.exports = webpackEmptyAsyncContext;`;
1010
1094
  * @returns {CodeGenerationResult} result
1011
1095
  */
1012
1096
  codeGeneration(context) {
1013
- const { chunkGraph } = context;
1097
+ const { chunkGraph, compilation } = context;
1014
1098
  const sources = new Map();
1015
1099
  sources.set(
1016
1100
  "javascript",
1017
- this.getSource(this.getSourceString(this.options.mode, context))
1101
+ this.getSource(
1102
+ this.getSourceString(this.options.mode, context),
1103
+ compilation
1104
+ )
1018
1105
  );
1019
1106
  const set = new Set();
1020
- const allDeps = /** @type {ContextElementDependency[]} */ (
1021
- this.dependencies.concat(this.blocks.map(b => b.dependencies[0]))
1022
- );
1107
+ const allDeps =
1108
+ this.dependencies.length > 0
1109
+ ? /** @type {ContextElementDependency[]} */ (this.dependencies).slice()
1110
+ : [];
1111
+ for (const block of this.blocks)
1112
+ for (const dep of block.dependencies)
1113
+ allDeps.push(/** @type {ContextElementDependency} */ (dep));
1023
1114
  set.add(RuntimeGlobals.module);
1024
1115
  set.add(RuntimeGlobals.hasOwnProperty);
1025
1116
  if (allDeps.length > 0) {