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
@@ -6,6 +6,7 @@
6
6
  "use strict";
7
7
 
8
8
  const { create: createResolver } = require("enhanced-resolve");
9
+ const nodeModule = require("module");
9
10
  const asyncLib = require("neo-async");
10
11
  const AsyncQueue = require("./util/AsyncQueue");
11
12
  const StackedCacheMap = require("./util/StackedCacheMap");
@@ -22,6 +23,8 @@ const processAsyncTree = require("./util/processAsyncTree");
22
23
 
23
24
  const supportsEsm = +process.versions.modules >= 83;
24
25
 
26
+ const builtinModules = new Set(nodeModule.builtinModules);
27
+
25
28
  let FS_ACCURACY = 2000;
26
29
 
27
30
  const EMPTY_SET = new Set();
@@ -692,6 +695,11 @@ class SnapshotOptimization {
692
695
  }
693
696
  }
694
697
 
698
+ const parseString = str => {
699
+ if (str[0] === "'") str = `"${str.slice(1, -1).replace(/"/g, '\\"')}"`;
700
+ return JSON.parse(str);
701
+ };
702
+
695
703
  /* istanbul ignore next */
696
704
  /**
697
705
  * @param {number} mtime mtime
@@ -1180,7 +1188,7 @@ class FileSystemInfo {
1180
1188
 
1181
1189
  /**
1182
1190
  * @param {string} path file path
1183
- * @param {function(WebpackError=, (FileSystemInfoEntry | "ignore" | null)=): void} callback callback function
1191
+ * @param {function((WebpackError | null)=, (FileSystemInfoEntry | "ignore" | null)=): void} callback callback function
1184
1192
  * @returns {void}
1185
1193
  */
1186
1194
  getFileTimestamp(path, callback) {
@@ -1191,7 +1199,7 @@ class FileSystemInfo {
1191
1199
 
1192
1200
  /**
1193
1201
  * @param {string} path context path
1194
- * @param {function(WebpackError=, (ResolvedContextFileSystemInfoEntry | "ignore" | null)=): void} callback callback function
1202
+ * @param {function((WebpackError | null)=, (ResolvedContextFileSystemInfoEntry | "ignore" | null)=): void} callback callback function
1195
1203
  * @returns {void}
1196
1204
  */
1197
1205
  getContextTimestamp(path, callback) {
@@ -1212,7 +1220,7 @@ class FileSystemInfo {
1212
1220
 
1213
1221
  /**
1214
1222
  * @param {string} path context path
1215
- * @param {function(WebpackError=, (ContextFileSystemInfoEntry | "ignore" | null)=): void} callback callback function
1223
+ * @param {function((WebpackError | null)=, (ContextFileSystemInfoEntry | "ignore" | null)=): void} callback callback function
1216
1224
  * @returns {void}
1217
1225
  */
1218
1226
  _getUnresolvedContextTimestamp(path, callback) {
@@ -1223,7 +1231,7 @@ class FileSystemInfo {
1223
1231
 
1224
1232
  /**
1225
1233
  * @param {string} path file path
1226
- * @param {function(WebpackError=, string=): void} callback callback function
1234
+ * @param {function((WebpackError | null)=, string=): void} callback callback function
1227
1235
  * @returns {void}
1228
1236
  */
1229
1237
  getFileHash(path, callback) {
@@ -1234,7 +1242,7 @@ class FileSystemInfo {
1234
1242
 
1235
1243
  /**
1236
1244
  * @param {string} path context path
1237
- * @param {function(WebpackError=, string=): void} callback callback function
1245
+ * @param {function((WebpackError | null)=, string=): void} callback callback function
1238
1246
  * @returns {void}
1239
1247
  */
1240
1248
  getContextHash(path, callback) {
@@ -1254,7 +1262,7 @@ class FileSystemInfo {
1254
1262
 
1255
1263
  /**
1256
1264
  * @param {string} path context path
1257
- * @param {function(WebpackError=, ContextHash=): void} callback callback function
1265
+ * @param {function((WebpackError | null)=, ContextHash=): void} callback callback function
1258
1266
  * @returns {void}
1259
1267
  */
1260
1268
  _getUnresolvedContextHash(path, callback) {
@@ -1265,7 +1273,7 @@ class FileSystemInfo {
1265
1273
 
1266
1274
  /**
1267
1275
  * @param {string} path context path
1268
- * @param {function(WebpackError=, ResolvedContextTimestampAndHash=): void} callback callback function
1276
+ * @param {function((WebpackError | null)=, ResolvedContextTimestampAndHash=): void} callback callback function
1269
1277
  * @returns {void}
1270
1278
  */
1271
1279
  getContextTsh(path, callback) {
@@ -1285,7 +1293,7 @@ class FileSystemInfo {
1285
1293
 
1286
1294
  /**
1287
1295
  * @param {string} path context path
1288
- * @param {function(WebpackError=, ContextTimestampAndHash=): void} callback callback function
1296
+ * @param {function((WebpackError | null)=, ContextTimestampAndHash=): void} callback callback function
1289
1297
  * @returns {void}
1290
1298
  */
1291
1299
  _getUnresolvedContextTsh(path, callback) {
@@ -1325,7 +1333,7 @@ class FileSystemInfo {
1325
1333
  /**
1326
1334
  * @param {string} context context directory
1327
1335
  * @param {Iterable<string>} deps dependencies
1328
- * @param {function(Error=, ResolveBuildDependenciesResult=): void} callback callback function
1336
+ * @param {function((Error | null)=, ResolveBuildDependenciesResult=): void} callback callback function
1329
1337
  * @returns {void}
1330
1338
  */
1331
1339
  resolveBuildDependencies(context, deps, callback) {
@@ -1657,21 +1665,22 @@ class FileSystemInfo {
1657
1665
  let dependency;
1658
1666
  if (imp.d === -1) {
1659
1667
  // import ... from "..."
1660
- dependency = JSON.parse(
1668
+ dependency = parseString(
1661
1669
  source.substring(imp.s - 1, imp.e + 1)
1662
1670
  );
1663
1671
  } else if (imp.d > -1) {
1664
1672
  // import()
1665
1673
  let expr = source.substring(imp.s, imp.e).trim();
1666
- if (expr[0] === "'")
1667
- expr = `"${expr
1668
- .slice(1, -1)
1669
- .replace(/"/g, '\\"')}"`;
1670
- dependency = JSON.parse(expr);
1674
+ dependency = parseString(expr);
1671
1675
  } else {
1672
1676
  // e.g. import.meta
1673
1677
  continue;
1674
1678
  }
1679
+
1680
+ // we should not track Node.js build dependencies
1681
+ if (dependency.startsWith("node:")) continue;
1682
+ if (builtinModules.has(dependency)) continue;
1683
+
1675
1684
  push({
1676
1685
  type: RBDT_RESOLVE_ESM_FILE,
1677
1686
  context,
@@ -1797,7 +1806,7 @@ class FileSystemInfo {
1797
1806
 
1798
1807
  /**
1799
1808
  * @param {Map<string, string | false>} resolveResults results from resolving
1800
- * @param {function(Error=, boolean=): void} callback callback with true when resolveResults resolve the same way
1809
+ * @param {function((Error | null)=, boolean=): void} callback callback with true when resolveResults resolve the same way
1801
1810
  * @returns {void}
1802
1811
  */
1803
1812
  checkResolveResultsValid(resolveResults, callback) {
@@ -1879,7 +1888,7 @@ class FileSystemInfo {
1879
1888
  * @param {Object} options options object (for future extensions)
1880
1889
  * @param {boolean=} options.hash should use hash to snapshot
1881
1890
  * @param {boolean=} options.timestamp should use timestamp to snapshot
1882
- * @param {function(WebpackError=, Snapshot=): void} callback callback function
1891
+ * @param {function((WebpackError | null)=, (Snapshot | null)=): void} callback callback function
1883
1892
  * @returns {void}
1884
1893
  */
1885
1894
  createSnapshot(startTime, files, directories, missing, options, callback) {
@@ -1988,7 +1997,7 @@ class FileSystemInfo {
1988
1997
  for (const managedPath of this.managedPathsRegExps) {
1989
1998
  const match = managedPath.exec(path);
1990
1999
  if (match) {
1991
- const managedItem = getManagedItem(managedPath[1], path);
2000
+ const managedItem = getManagedItem(match[1], path);
1992
2001
  if (managedItem) {
1993
2002
  managedItems.add(managedItem);
1994
2003
  managedSet.add(path);
@@ -2015,8 +2024,7 @@ class FileSystemInfo {
2015
2024
  }
2016
2025
  return capturedItems;
2017
2026
  };
2018
- if (files) {
2019
- const capturedFiles = captureNonManaged(files, managedFiles);
2027
+ const processCapturedFiles = capturedFiles => {
2020
2028
  switch (mode) {
2021
2029
  case 3:
2022
2030
  this._fileTshsOptimization.optimize(snapshot, capturedFiles);
@@ -2093,12 +2101,11 @@ class FileSystemInfo {
2093
2101
  }
2094
2102
  break;
2095
2103
  }
2104
+ };
2105
+ if (files) {
2106
+ processCapturedFiles(captureNonManaged(files, managedFiles));
2096
2107
  }
2097
- if (directories) {
2098
- const capturedDirectories = captureNonManaged(
2099
- directories,
2100
- managedContexts
2101
- );
2108
+ const processCapturedDirectories = capturedDirectories => {
2102
2109
  switch (mode) {
2103
2110
  case 3:
2104
2111
  this._contextTshsOptimization.optimize(snapshot, capturedDirectories);
@@ -2218,9 +2225,13 @@ class FileSystemInfo {
2218
2225
  }
2219
2226
  break;
2220
2227
  }
2228
+ };
2229
+ if (directories) {
2230
+ processCapturedDirectories(
2231
+ captureNonManaged(directories, managedContexts)
2232
+ );
2221
2233
  }
2222
- if (missing) {
2223
- const capturedMissing = captureNonManaged(missing, managedMissing);
2234
+ const processCapturedMissing = capturedMissing => {
2224
2235
  this._missingExistenceOptimization.optimize(snapshot, capturedMissing);
2225
2236
  for (const path of capturedMissing) {
2226
2237
  const cache = this._fileTimestamps.get(path);
@@ -2245,11 +2256,19 @@ class FileSystemInfo {
2245
2256
  });
2246
2257
  }
2247
2258
  }
2259
+ };
2260
+ if (missing) {
2261
+ processCapturedMissing(captureNonManaged(missing, managedMissing));
2248
2262
  }
2249
2263
  this._managedItemInfoOptimization.optimize(snapshot, managedItems);
2250
2264
  for (const path of managedItems) {
2251
2265
  const cache = this._managedItems.get(path);
2252
2266
  if (cache !== undefined) {
2267
+ if (!cache.startsWith("*")) {
2268
+ managedFiles.add(join(this.fs, path, "package.json"));
2269
+ } else if (cache === "*nested") {
2270
+ managedMissing.add(join(this.fs, path, "package.json"));
2271
+ }
2253
2272
  managedItemInfo.set(path, cache);
2254
2273
  } else {
2255
2274
  jobs++;
@@ -2261,9 +2280,28 @@ class FileSystemInfo {
2261
2280
  );
2262
2281
  }
2263
2282
  jobError();
2264
- } else {
2283
+ } else if (entry) {
2284
+ if (!entry.startsWith("*")) {
2285
+ managedFiles.add(join(this.fs, path, "package.json"));
2286
+ } else if (cache === "*nested") {
2287
+ managedMissing.add(join(this.fs, path, "package.json"));
2288
+ }
2265
2289
  managedItemInfo.set(path, entry);
2266
2290
  jobDone();
2291
+ } else {
2292
+ // Fallback to normal snapshotting
2293
+ const process = (set, fn) => {
2294
+ if (set.size === 0) return;
2295
+ const captured = new Set();
2296
+ for (const file of set) {
2297
+ if (file.startsWith(path)) captured.add(file);
2298
+ }
2299
+ if (captured.size > 0) fn(captured);
2300
+ };
2301
+ process(managedFiles, processCapturedFiles);
2302
+ process(managedContexts, processCapturedDirectories);
2303
+ process(managedMissing, processCapturedMissing);
2304
+ jobDone();
2267
2305
  }
2268
2306
  });
2269
2307
  }
@@ -2349,7 +2387,7 @@ class FileSystemInfo {
2349
2387
 
2350
2388
  /**
2351
2389
  * @param {Snapshot} snapshot the snapshot made
2352
- * @param {function(WebpackError=, boolean=): void} callback callback function
2390
+ * @param {function((WebpackError | null)=, boolean=): void} callback callback function
2353
2391
  * @returns {void}
2354
2392
  */
2355
2393
  checkSnapshotValid(snapshot, callback) {
@@ -2369,7 +2407,7 @@ class FileSystemInfo {
2369
2407
 
2370
2408
  /**
2371
2409
  * @param {Snapshot} snapshot the snapshot made
2372
- * @param {function(WebpackError=, boolean=): void} callback callback function
2410
+ * @param {function((WebpackError | null)=, boolean=): void} callback callback function
2373
2411
  * @returns {void}
2374
2412
  */
2375
2413
  _checkSnapshotValidNoCache(snapshot, callback) {
@@ -2918,7 +2956,7 @@ class FileSystemInfo {
2918
2956
  * @param {function(string, IStats, function(Error=, ItemType=): void): void} options.fromFile called when context item is a file
2919
2957
  * @param {function(string, IStats, function(Error=, ItemType=): void): void} options.fromDirectory called when context item is a directory
2920
2958
  * @param {function(string[], ItemType[]): T} options.reduce called from all context items
2921
- * @param {function(Error=, (T)=): void} callback callback
2959
+ * @param {function((Error | null)=, (T)=): void} callback callback
2922
2960
  */
2923
2961
  _readContext(
2924
2962
  {
@@ -2962,7 +3000,7 @@ class FileSystemInfo {
2962
3000
  for (const managedPath of this.managedPathsRegExps) {
2963
3001
  const match = managedPath.exec(path);
2964
3002
  if (match) {
2965
- const managedItem = getManagedItem(managedPath[1], path);
3003
+ const managedItem = getManagedItem(match[1], path);
2966
3004
  if (managedItem) {
2967
3005
  // construct timestampHash from managed info
2968
3006
  return this.managedItemQueue.add(managedItem, (err, info) => {
@@ -3101,7 +3139,7 @@ class FileSystemInfo {
3101
3139
 
3102
3140
  /**
3103
3141
  * @param {ContextFileSystemInfoEntry} entry entry
3104
- * @param {function(Error=, ResolvedContextFileSystemInfoEntry=): void} callback callback
3142
+ * @param {function((Error | null)=, ResolvedContextFileSystemInfoEntry=): void} callback callback
3105
3143
  * @returns {void}
3106
3144
  */
3107
3145
  _resolveContextTimestamp(entry, callback) {
@@ -3209,7 +3247,7 @@ class FileSystemInfo {
3209
3247
 
3210
3248
  /**
3211
3249
  * @param {ContextHash} entry context hash
3212
- * @param {function(Error=, string=): void} callback callback
3250
+ * @param {function((Error | null)=, string=): void} callback callback
3213
3251
  * @returns {void}
3214
3252
  */
3215
3253
  _resolveContextHash(entry, callback) {
@@ -3365,7 +3403,7 @@ class FileSystemInfo {
3365
3403
 
3366
3404
  /**
3367
3405
  * @param {ContextTimestampAndHash} entry entry
3368
- * @param {function(Error=, ResolvedContextTimestampAndHash=): void} callback callback
3406
+ * @param {function((Error | null)=, ResolvedContextTimestampAndHash=): void} callback callback
3369
3407
  * @returns {void}
3370
3408
  */
3371
3409
  _resolveContextTsh(entry, callback) {
@@ -3445,8 +3483,8 @@ class FileSystemInfo {
3445
3483
  }
3446
3484
  if (!elements.has(path)) {
3447
3485
  // file or directory doesn't exist
3448
- this._managedItems.set(path, "missing");
3449
- return callback(null, "missing");
3486
+ this._managedItems.set(path, "*missing");
3487
+ return callback(null, "*missing");
3450
3488
  }
3451
3489
  // something exists
3452
3490
  // it may be a file or directory
@@ -3455,8 +3493,8 @@ class FileSystemInfo {
3455
3493
  (path.endsWith("/node_modules") || path.endsWith("\\node_modules"))
3456
3494
  ) {
3457
3495
  // we are only interested in existence of this special directory
3458
- this._managedItems.set(path, "exists");
3459
- return callback(null, "exists");
3496
+ this._managedItems.set(path, "*node_modules");
3497
+ return callback(null, "*node_modules");
3460
3498
  }
3461
3499
 
3462
3500
  // we assume it's a directory, as files shouldn't occur in managed paths
@@ -3473,12 +3511,13 @@ class FileSystemInfo {
3473
3511
  ) {
3474
3512
  // This is only a grouping folder e. g. used by yarn
3475
3513
  // we are only interested in existence of this special directory
3476
- this._managedItems.set(path, "nested");
3477
- return callback(null, "nested");
3514
+ this._managedItems.set(path, "*nested");
3515
+ return callback(null, "*nested");
3478
3516
  }
3479
- const problem = `Managed item ${path} isn't a directory or doesn't contain a package.json`;
3480
- this.logger.warn(problem);
3481
- return callback(new Error(problem));
3517
+ this.logger.warn(
3518
+ `Managed item ${path} isn't a directory or doesn't contain a package.json (see snapshot.managedPaths option)`
3519
+ );
3520
+ return callback();
3482
3521
  });
3483
3522
  return;
3484
3523
  }
@@ -3490,6 +3529,12 @@ class FileSystemInfo {
3490
3529
  } catch (e) {
3491
3530
  return callback(e);
3492
3531
  }
3532
+ if (!data.name) {
3533
+ this.logger.warn(
3534
+ `${packageJsonPath} doesn't contain a "name" property (see snapshot.managedPaths option)`
3535
+ );
3536
+ return callback();
3537
+ }
3493
3538
  const info = `${data.name || ""}@${data.version || ""}`;
3494
3539
  this._managedItems.set(path, info);
3495
3540
  callback(null, info);
package/lib/Generator.js CHANGED
@@ -7,6 +7,7 @@
7
7
 
8
8
  /** @typedef {import("webpack-sources").Source} Source */
9
9
  /** @typedef {import("./ChunkGraph")} ChunkGraph */
10
+ /** @typedef {import("./CodeGenerationResults")} CodeGenerationResults */
10
11
  /** @typedef {import("./Compilation")} Compilation */
11
12
  /** @typedef {import("./ConcatenationScope")} ConcatenationScope */
12
13
  /** @typedef {import("./DependencyTemplate")} DependencyTemplate */
@@ -27,6 +28,7 @@
27
28
  * @property {Set<string>} runtimeRequirements the requirements for runtime
28
29
  * @property {RuntimeSpec} runtime the runtime
29
30
  * @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
31
+ * @property {CodeGenerationResults=} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
30
32
  * @property {string} type which kind of code should be generated
31
33
  * @property {function(): Map<string, any>=} getData get access to the code generation data
32
34
  */
@@ -36,6 +38,7 @@
36
38
  * @property {NormalModule} module the module
37
39
  * @property {ChunkGraph} chunkGraph
38
40
  * @property {RuntimeSpec} runtime
41
+ * @property {RuntimeTemplate=} runtimeTemplate
39
42
  */
40
43
 
41
44
  /**
@@ -51,7 +51,7 @@ module.exports.makeWebpackError = makeWebpackError;
51
51
 
52
52
  /**
53
53
  * @template T
54
- * @param {function(WebpackError=, T=): void} callback webpack error callback
54
+ * @param {function((WebpackError | null)=, T=): void} callback webpack error callback
55
55
  * @param {string} hook name of hook
56
56
  * @returns {Callback<T>} generic callback
57
57
  */
@@ -83,6 +83,7 @@ class HotModuleReplacementPlugin {
83
83
  * @returns {void}
84
84
  */
85
85
  apply(compiler) {
86
+ const { _backCompat: backCompat } = compiler;
86
87
  if (compiler.options.output.strictModuleErrorHandling === undefined)
87
88
  compiler.options.output.strictModuleErrorHandling = true;
88
89
  const runtimeRequirements = [RuntimeGlobals.module];
@@ -597,7 +598,8 @@ class HotModuleReplacementPlugin {
597
598
  (newRuntimeModules && newRuntimeModules.length > 0)
598
599
  ) {
599
600
  const hotUpdateChunk = new HotUpdateChunk();
600
- ChunkGraph.setChunkGraphForChunk(hotUpdateChunk, chunkGraph);
601
+ if (backCompat)
602
+ ChunkGraph.setChunkGraphForChunk(hotUpdateChunk, chunkGraph);
601
603
  hotUpdateChunk.id = chunkId;
602
604
  hotUpdateChunk.runtime = newRuntime;
603
605
  if (currentChunk) {
@@ -52,7 +52,7 @@ class LoaderOptionsPlugin {
52
52
  if (
53
53
  ModuleFilenameHelpers.matchObject(
54
54
  options,
55
- i < 0 ? resource : resource.substr(0, i)
55
+ i < 0 ? resource : resource.slice(0, i)
56
56
  )
57
57
  ) {
58
58
  for (const key of Object.keys(options)) {
package/lib/Module.js CHANGED
@@ -19,6 +19,7 @@ const makeSerializable = require("./util/makeSerializable");
19
19
  /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
20
20
  /** @typedef {import("./Chunk")} Chunk */
21
21
  /** @typedef {import("./ChunkGroup")} ChunkGroup */
22
+ /** @typedef {import("./CodeGenerationResults")} CodeGenerationResults */
22
23
  /** @typedef {import("./Compilation")} Compilation */
23
24
  /** @typedef {import("./ConcatenationScope")} ConcatenationScope */
24
25
  /** @typedef {import("./Dependency")} Dependency */
@@ -48,6 +49,7 @@ const makeSerializable = require("./util/makeSerializable");
48
49
  * @property {string=} type the type of source that should be generated
49
50
  */
50
51
 
52
+ // TODO webpack 6: compilation will be required in CodeGenerationContext
51
53
  /**
52
54
  * @typedef {Object} CodeGenerationContext
53
55
  * @property {DependencyTemplates} dependencyTemplates the dependency templates
@@ -56,6 +58,8 @@ const makeSerializable = require("./util/makeSerializable");
56
58
  * @property {ChunkGraph} chunkGraph the chunk graph
57
59
  * @property {RuntimeSpec} runtime the runtimes code should be generated for
58
60
  * @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
61
+ * @property {CodeGenerationResults} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
62
+ * @property {Compilation=} compilation the compilation
59
63
  */
60
64
 
61
65
  /**
@@ -165,6 +169,8 @@ class Module extends DependenciesBlock {
165
169
  this.buildInfo = undefined;
166
170
  /** @type {Dependency[] | undefined} */
167
171
  this.presentationalDependencies = undefined;
172
+ /** @type {Dependency[] | undefined} */
173
+ this.codeGenerationDependencies = undefined;
168
174
  }
169
175
 
170
176
  // TODO remove in webpack 6
@@ -396,7 +402,6 @@ class Module extends DependenciesBlock {
396
402
  // BACKWARD-COMPAT END
397
403
 
398
404
  /**
399
- * @deprecated moved to .buildInfo.exportsArgument
400
405
  * @returns {string} name of the exports argument
401
406
  */
402
407
  get exportsArgument() {
@@ -404,7 +409,6 @@ class Module extends DependenciesBlock {
404
409
  }
405
410
 
406
411
  /**
407
- * @deprecated moved to .buildInfo.moduleArgument
408
412
  * @returns {string} name of the module argument
409
413
  */
410
414
  get moduleArgument() {
@@ -493,6 +497,19 @@ class Module extends DependenciesBlock {
493
497
  this.presentationalDependencies.push(presentationalDependency);
494
498
  }
495
499
 
500
+ /**
501
+ * @param {Dependency} codeGenerationDependency dependency being tied to module.
502
+ * This is a Dependency where the code generation result of the referenced module is needed during code generation.
503
+ * The Dependency should also be added to normal dependencies via addDependency.
504
+ * @returns {void}
505
+ */
506
+ addCodeGenerationDependency(codeGenerationDependency) {
507
+ if (this.codeGenerationDependencies === undefined) {
508
+ this.codeGenerationDependencies = [];
509
+ }
510
+ this.codeGenerationDependencies.push(codeGenerationDependency);
511
+ }
512
+
496
513
  /**
497
514
  * Removes all dependencies and blocks
498
515
  * @returns {void}
@@ -501,6 +518,9 @@ class Module extends DependenciesBlock {
501
518
  if (this.presentationalDependencies !== undefined) {
502
519
  this.presentationalDependencies.length = 0;
503
520
  }
521
+ if (this.codeGenerationDependencies !== undefined) {
522
+ this.codeGenerationDependencies.length = 0;
523
+ }
504
524
  super.clearDependenciesAndBlocks();
505
525
  }
506
526
 
@@ -671,7 +691,7 @@ class Module extends DependenciesBlock {
671
691
 
672
692
  /**
673
693
  * @param {NeedBuildContext} context context info
674
- * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
694
+ * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
675
695
  * @returns {void}
676
696
  */
677
697
  needBuild(context, callback) {
@@ -799,7 +819,8 @@ class Module extends DependenciesBlock {
799
819
  runtimeTemplate,
800
820
  moduleGraph: chunkGraph.moduleGraph,
801
821
  chunkGraph,
802
- runtime: undefined
822
+ runtime: undefined,
823
+ codeGenerationResults: undefined
803
824
  };
804
825
  const sources = this.codeGeneration(codeGenContext).sources;
805
826
  return type ? sources.get(type) : sources.get(first(this.getSourceTypes()));
@@ -976,6 +997,7 @@ class Module extends DependenciesBlock {
976
997
  write(this.buildMeta);
977
998
  write(this.buildInfo);
978
999
  write(this.presentationalDependencies);
1000
+ write(this.codeGenerationDependencies);
979
1001
  super.serialize(context);
980
1002
  }
981
1003
 
@@ -993,6 +1015,7 @@ class Module extends DependenciesBlock {
993
1015
  this.buildMeta = read();
994
1016
  this.buildInfo = read();
995
1017
  this.presentationalDependencies = read();
1018
+ this.codeGenerationDependencies = read();
996
1019
  super.deserialize(context);
997
1020
  }
998
1021
  }
@@ -5,6 +5,7 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const NormalModule = require("./NormalModule");
8
9
  const createHash = require("./util/createHash");
9
10
  const memoize = require("./util/memoize");
10
11
 
@@ -46,7 +47,7 @@ const getAfter = (strFn, token) => {
46
47
  return () => {
47
48
  const str = strFn();
48
49
  const idx = str.indexOf(token);
49
- return idx < 0 ? "" : str.substr(idx);
50
+ return idx < 0 ? "" : str.slice(idx);
50
51
  };
51
52
  };
52
53
 
@@ -54,7 +55,7 @@ const getBefore = (strFn, token) => {
54
55
  return () => {
55
56
  const str = strFn();
56
57
  const idx = str.lastIndexOf(token);
57
- return idx < 0 ? "" : str.substr(0, idx);
58
+ return idx < 0 ? "" : str.slice(0, idx);
58
59
  };
59
60
  };
60
61
 
@@ -63,7 +64,7 @@ const getHash = (strFn, hashFunction) => {
63
64
  const hash = createHash(hashFunction);
64
65
  hash.update(strFn());
65
66
  const digest = /** @type {string} */ (hash.digest("hex"));
66
- return digest.substr(0, 4);
67
+ return digest.slice(0, 4);
67
68
  };
68
69
  };
69
70
 
@@ -138,7 +139,10 @@ ModuleFilenameHelpers.createFilename = (
138
139
  );
139
140
  identifier = memoize(() => requestShortener.shorten(module.identifier()));
140
141
  moduleId = () => chunkGraph.getModuleId(module);
141
- absoluteResourcePath = () => module.identifier().split("!").pop();
142
+ absoluteResourcePath = () =>
143
+ module instanceof NormalModule
144
+ ? module.resource
145
+ : module.identifier().split("!").pop();
142
146
  hash = getHash(identifier, hashFunction);
143
147
  }
144
148
  const resource = memoize(() => shortIdentifier().split("!").pop());
@@ -0,0 +1,29 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const WebpackError = require("./WebpackError");
9
+
10
+ /** @typedef {import("./Module")} Module */
11
+
12
+ class ModuleHashingError extends WebpackError {
13
+ /**
14
+ * Create a new ModuleHashingError
15
+ * @param {Module} module related module
16
+ * @param {Error} error Original error
17
+ */
18
+ constructor(module, error) {
19
+ super();
20
+
21
+ this.name = "ModuleHashingError";
22
+ this.error = error;
23
+ this.message = error.message;
24
+ this.details = error.stack;
25
+ this.module = module;
26
+ }
27
+ }
28
+
29
+ module.exports = ModuleHashingError;
@@ -27,7 +27,7 @@ const ArrayQueue = require("./util/ArrayQueue");
27
27
  /**
28
28
  * @template T
29
29
  * @callback Callback
30
- * @param {Error=} err
30
+ * @param {(Error | null)=} err
31
31
  * @param {T=} result
32
32
  */
33
33
 
@@ -13,7 +13,7 @@ const asyncLib = require("neo-async");
13
13
  /**
14
14
  * @template T
15
15
  * @callback Callback
16
- * @param {Error=} err
16
+ * @param {(Error | null)=} err
17
17
  * @param {T=} result
18
18
  */
19
19
 
@@ -69,6 +69,16 @@ class NodeStuffPlugin {
69
69
  );
70
70
  }
71
71
  });
72
+ parser.hooks.rename.for("global").tap("NodeStuffPlugin", expr => {
73
+ const dep = new ConstDependency(
74
+ RuntimeGlobals.global,
75
+ expr.range,
76
+ [RuntimeGlobals.global]
77
+ );
78
+ dep.loc = expr.loc;
79
+ parser.state.module.addPresentationalDependency(dep);
80
+ return false;
81
+ });
72
82
  }
73
83
 
74
84
  const setModuleConstant = (expressionName, fn, warning) => {