webpack 5.59.0 → 5.76.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 (236) hide show
  1. package/README.md +22 -24
  2. package/bin/webpack.js +0 -0
  3. package/hot/dev-server.js +17 -4
  4. package/hot/lazy-compilation-node.js +3 -1
  5. package/hot/poll.js +1 -1
  6. package/hot/signal.js +1 -1
  7. package/lib/APIPlugin.js +33 -0
  8. package/lib/BannerPlugin.js +13 -5
  9. package/lib/Cache.js +1 -1
  10. package/lib/CacheFacade.js +4 -11
  11. package/lib/Chunk.js +6 -3
  12. package/lib/ChunkGraph.js +94 -8
  13. package/lib/ChunkGroup.js +1 -1
  14. package/lib/CleanPlugin.js +81 -20
  15. package/lib/Compilation.js +188 -93
  16. package/lib/Compiler.js +87 -18
  17. package/lib/ConstPlugin.js +2 -2
  18. package/lib/ContextModule.js +142 -51
  19. package/lib/ContextModuleFactory.js +65 -25
  20. package/lib/DelegatedModule.js +1 -1
  21. package/lib/DelegatedModuleFactoryPlugin.js +1 -1
  22. package/lib/Dependency.js +17 -0
  23. package/lib/DependencyTemplate.js +9 -0
  24. package/lib/DependencyTemplates.js +1 -1
  25. package/lib/DllModule.js +1 -1
  26. package/lib/DllReferencePlugin.js +1 -1
  27. package/lib/EntryOptionPlugin.js +2 -0
  28. package/lib/ErrorHelpers.js +2 -2
  29. package/lib/EvalDevToolModulePlugin.js +16 -1
  30. package/lib/EvalSourceMapDevToolPlugin.js +25 -4
  31. package/lib/ExportsInfo.js +5 -5
  32. package/lib/ExternalModule.js +94 -54
  33. package/lib/ExternalModuleFactoryPlugin.js +5 -5
  34. package/lib/FileSystemInfo.js +124 -58
  35. package/lib/Generator.js +3 -0
  36. package/lib/HookWebpackError.js +1 -1
  37. package/lib/HotModuleReplacementPlugin.js +3 -1
  38. package/lib/LoaderOptionsPlugin.js +1 -1
  39. package/lib/Module.js +28 -4
  40. package/lib/ModuleFilenameHelpers.js +8 -4
  41. package/lib/ModuleHashingError.js +29 -0
  42. package/lib/MultiCompiler.js +1 -1
  43. package/lib/MultiWatching.js +1 -1
  44. package/lib/NodeStuffPlugin.js +13 -3
  45. package/lib/NormalModule.js +51 -33
  46. package/lib/NormalModuleFactory.js +42 -37
  47. package/lib/ProgressPlugin.js +4 -5
  48. package/lib/RawModule.js +1 -1
  49. package/lib/RuntimeGlobals.js +29 -1
  50. package/lib/RuntimeModule.js +1 -1
  51. package/lib/RuntimePlugin.js +84 -1
  52. package/lib/RuntimeTemplate.js +114 -2
  53. package/lib/Template.js +3 -2
  54. package/lib/TemplatedPathPlugin.js +48 -23
  55. package/lib/WatchIgnorePlugin.js +19 -7
  56. package/lib/Watching.js +33 -19
  57. package/lib/WebpackOptionsApply.js +79 -11
  58. package/lib/asset/AssetGenerator.js +228 -71
  59. package/lib/asset/AssetModulesPlugin.js +3 -0
  60. package/lib/asset/AssetParser.js +1 -0
  61. package/lib/asset/AssetSourceGenerator.js +31 -6
  62. package/lib/asset/AssetSourceParser.js +1 -0
  63. package/lib/asset/RawDataUrlModule.js +148 -0
  64. package/lib/async-modules/AwaitDependenciesInitFragment.js +4 -4
  65. package/lib/buildChunkGraph.js +38 -7
  66. package/lib/cache/PackFileCacheStrategy.js +15 -8
  67. package/lib/cache/ResolverCachePlugin.js +90 -29
  68. package/lib/cli.js +44 -3
  69. package/lib/config/browserslistTargetHandler.js +41 -6
  70. package/lib/config/defaults.js +123 -19
  71. package/lib/config/normalization.js +10 -2
  72. package/lib/config/target.js +10 -0
  73. package/lib/container/ContainerEntryModule.js +8 -5
  74. package/lib/container/FallbackModule.js +4 -4
  75. package/lib/container/ModuleFederationPlugin.js +2 -0
  76. package/lib/container/RemoteModule.js +4 -2
  77. package/lib/container/RemoteRuntimeModule.js +8 -7
  78. package/lib/css/CssExportsGenerator.js +139 -0
  79. package/lib/css/CssGenerator.js +109 -0
  80. package/lib/css/CssLoadingRuntimeModule.js +442 -0
  81. package/lib/css/CssModulesPlugin.js +462 -0
  82. package/lib/css/CssParser.js +618 -0
  83. package/lib/css/walkCssTokens.js +659 -0
  84. package/lib/debug/ProfilingPlugin.js +24 -21
  85. package/lib/dependencies/AMDRequireDependency.js +6 -6
  86. package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -2
  87. package/lib/dependencies/CommonJsFullRequireDependency.js +5 -1
  88. package/lib/dependencies/CommonJsImportsParserPlugin.js +344 -61
  89. package/lib/dependencies/CommonJsRequireContextDependency.js +6 -2
  90. package/lib/dependencies/CommonJsRequireDependency.js +2 -1
  91. package/lib/dependencies/ContextDependency.js +16 -2
  92. package/lib/dependencies/ContextDependencyHelpers.js +21 -8
  93. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +4 -1
  94. package/lib/dependencies/ContextElementDependency.js +25 -3
  95. package/lib/dependencies/CreateScriptUrlDependency.js +12 -0
  96. package/lib/dependencies/CssExportDependency.js +85 -0
  97. package/lib/dependencies/CssImportDependency.js +75 -0
  98. package/lib/dependencies/CssLocalIdentifierDependency.js +119 -0
  99. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +101 -0
  100. package/lib/dependencies/CssUrlDependency.js +132 -0
  101. package/lib/dependencies/ExportsInfoDependency.js +6 -0
  102. package/lib/dependencies/HarmonyAcceptImportDependency.js +5 -3
  103. package/lib/dependencies/HarmonyCompatibilityDependency.js +5 -5
  104. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +127 -0
  105. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +12 -3
  106. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +25 -17
  107. package/lib/dependencies/HarmonyExportInitFragment.js +4 -1
  108. package/lib/dependencies/HarmonyImportDependency.js +23 -0
  109. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +142 -45
  110. package/lib/dependencies/HarmonyImportSpecifierDependency.js +46 -22
  111. package/lib/dependencies/HarmonyModulesPlugin.js +10 -0
  112. package/lib/dependencies/ImportContextDependency.js +0 -2
  113. package/lib/dependencies/ImportMetaContextDependency.js +35 -0
  114. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +252 -0
  115. package/lib/dependencies/ImportMetaContextPlugin.js +59 -0
  116. package/lib/dependencies/ImportMetaPlugin.js +22 -3
  117. package/lib/dependencies/ImportParserPlugin.js +35 -29
  118. package/lib/dependencies/JsonExportsDependency.js +17 -21
  119. package/lib/dependencies/LoaderDependency.js +13 -0
  120. package/lib/dependencies/LoaderImportDependency.js +13 -0
  121. package/lib/dependencies/LoaderPlugin.js +4 -2
  122. package/lib/dependencies/ModuleDependency.js +11 -1
  123. package/lib/dependencies/ProvidedDependency.js +31 -8
  124. package/lib/dependencies/RequireContextDependency.js +0 -16
  125. package/lib/dependencies/RequireEnsureDependency.js +2 -2
  126. package/lib/dependencies/RequireResolveContextDependency.js +2 -2
  127. package/lib/dependencies/RequireResolveDependency.js +2 -1
  128. package/lib/dependencies/URLDependency.js +3 -8
  129. package/lib/dependencies/URLPlugin.js +22 -1
  130. package/lib/dependencies/WorkerPlugin.js +2 -0
  131. package/lib/esm/ModuleChunkFormatPlugin.js +74 -49
  132. package/lib/esm/ModuleChunkLoadingPlugin.js +3 -1
  133. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +25 -9
  134. package/lib/hmr/HotModuleReplacement.runtime.js +29 -14
  135. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +4 -3
  136. package/lib/hmr/LazyCompilationPlugin.js +54 -26
  137. package/lib/hmr/lazyCompilationBackend.js +51 -12
  138. package/lib/ids/DeterministicModuleIdsPlugin.js +55 -35
  139. package/lib/ids/HashedModuleIdsPlugin.js +11 -14
  140. package/lib/ids/IdHelpers.js +26 -12
  141. package/lib/ids/NamedModuleIdsPlugin.js +6 -9
  142. package/lib/ids/NaturalModuleIdsPlugin.js +10 -13
  143. package/lib/ids/OccurrenceModuleIdsPlugin.js +13 -10
  144. package/lib/ids/SyncModuleIdsPlugin.js +140 -0
  145. package/lib/index.js +20 -0
  146. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +2 -2
  147. package/lib/javascript/BasicEvaluatedExpression.js +5 -2
  148. package/lib/javascript/ChunkHelpers.js +33 -0
  149. package/lib/javascript/JavascriptGenerator.js +1 -0
  150. package/lib/javascript/JavascriptModulesPlugin.js +27 -2
  151. package/lib/javascript/JavascriptParser.js +143 -73
  152. package/lib/javascript/StartupHelpers.js +7 -30
  153. package/lib/json/JsonData.js +8 -0
  154. package/lib/json/JsonParser.js +4 -6
  155. package/lib/library/AssignLibraryPlugin.js +39 -15
  156. package/lib/library/EnableLibraryPlugin.js +11 -0
  157. package/lib/library/UmdLibraryPlugin.js +5 -3
  158. package/lib/node/NodeTargetPlugin.js +3 -0
  159. package/lib/node/NodeWatchFileSystem.js +85 -31
  160. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +23 -8
  161. package/lib/node/RequireChunkLoadingRuntimeModule.js +24 -9
  162. package/lib/optimize/ConcatenatedModule.js +62 -26
  163. package/lib/optimize/ModuleConcatenationPlugin.js +26 -4
  164. package/lib/optimize/RealContentHashPlugin.js +45 -15
  165. package/lib/optimize/SplitChunksPlugin.js +8 -1
  166. package/lib/runtime/AsyncModuleRuntimeModule.js +50 -66
  167. package/lib/runtime/BaseUriRuntimeModule.js +31 -0
  168. package/lib/runtime/CreateScriptRuntimeModule.js +36 -0
  169. package/lib/runtime/CreateScriptUrlRuntimeModule.js +9 -34
  170. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +76 -0
  171. package/lib/runtime/LoadScriptRuntimeModule.js +11 -9
  172. package/lib/runtime/NonceRuntimeModule.js +24 -0
  173. package/lib/schemes/HttpUriPlugin.js +77 -14
  174. package/lib/serialization/FileMiddleware.js +44 -9
  175. package/lib/sharing/ConsumeSharedModule.js +8 -2
  176. package/lib/sharing/ConsumeSharedRuntimeModule.js +26 -5
  177. package/lib/sharing/ProvideSharedModule.js +4 -2
  178. package/lib/sharing/ProvideSharedPlugin.js +1 -2
  179. package/lib/sharing/ShareRuntimeModule.js +1 -1
  180. package/lib/sharing/utils.js +1 -1
  181. package/lib/stats/DefaultStatsFactoryPlugin.js +113 -68
  182. package/lib/stats/DefaultStatsPrinterPlugin.js +90 -25
  183. package/lib/util/ArrayHelpers.js +30 -0
  184. package/lib/util/AsyncQueue.js +1 -1
  185. package/lib/util/compileBooleanMatcher.js +1 -1
  186. package/lib/util/create-schema-validation.js +9 -2
  187. package/lib/util/createHash.js +12 -0
  188. package/lib/util/deprecation.js +10 -2
  189. package/lib/util/deterministicGrouping.js +1 -1
  190. package/lib/util/extractUrlAndGlobal.js +3 -0
  191. package/lib/util/fs.js +11 -0
  192. package/lib/util/hash/BatchedHash.js +7 -4
  193. package/lib/util/hash/md4.js +20 -0
  194. package/lib/util/hash/wasm-hash.js +163 -0
  195. package/lib/util/hash/xxhash64.js +5 -139
  196. package/lib/util/identifier.js +65 -44
  197. package/lib/util/internalSerializables.js +15 -0
  198. package/lib/util/nonNumericOnlyHash.js +22 -0
  199. package/lib/util/semver.js +17 -10
  200. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +9 -3
  201. package/lib/wasm-sync/WebAssemblyParser.js +1 -1
  202. package/lib/web/JsonpChunkLoadingRuntimeModule.js +31 -18
  203. package/lib/webpack.js +10 -3
  204. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -11
  205. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +33 -22
  206. package/module.d.ts +215 -0
  207. package/package.json +28 -32
  208. package/schemas/WebpackOptions.check.js +1 -1
  209. package/schemas/WebpackOptions.json +321 -30
  210. package/schemas/plugins/BannerPlugin.check.js +1 -1
  211. package/schemas/plugins/BannerPlugin.json +4 -0
  212. package/schemas/plugins/DllReferencePlugin.check.js +1 -1
  213. package/schemas/plugins/HashedModuleIdsPlugin.check.js +1 -1
  214. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  215. package/schemas/plugins/asset/AssetGeneratorOptions.check.js +1 -1
  216. package/schemas/plugins/asset/AssetParserOptions.check.js +1 -1
  217. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +1 -1
  218. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  219. package/schemas/plugins/container/ContainerPlugin.json +2 -1
  220. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  221. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  222. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  223. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  224. package/schemas/plugins/container/ModuleFederationPlugin.json +3 -1
  225. package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +7 -0
  226. package/schemas/plugins/css/CssGeneratorOptions.check.js +6 -0
  227. package/schemas/plugins/css/CssGeneratorOptions.json +3 -0
  228. package/schemas/plugins/css/CssParserOptions.check.d.ts +7 -0
  229. package/schemas/plugins/css/CssParserOptions.check.js +6 -0
  230. package/schemas/plugins/css/CssParserOptions.json +3 -0
  231. package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.js +1 -1
  232. package/schemas/plugins/optimize/LimitChunkCountPlugin.check.js +1 -1
  233. package/schemas/plugins/optimize/MinChunkSizePlugin.check.js +1 -1
  234. package/schemas/plugins/schemes/HttpUriPlugin.check.js +1 -1
  235. package/schemas/plugins/schemes/HttpUriPlugin.json +4 -0
  236. package/types.d.ts +869 -296
@@ -8,19 +8,23 @@
8
8
  const mimeTypes = require("mime-types");
9
9
  const path = require("path");
10
10
  const { RawSource } = require("webpack-sources");
11
+ const ConcatenationScope = require("../ConcatenationScope");
11
12
  const Generator = require("../Generator");
12
13
  const RuntimeGlobals = require("../RuntimeGlobals");
13
14
  const createHash = require("../util/createHash");
14
15
  const { makePathsRelative } = require("../util/identifier");
16
+ const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
15
17
 
16
18
  /** @typedef {import("webpack-sources").Source} Source */
17
19
  /** @typedef {import("../../declarations/WebpackOptions").AssetGeneratorOptions} AssetGeneratorOptions */
20
+ /** @typedef {import("../../declarations/WebpackOptions").AssetModuleOutputPath} AssetModuleOutputPath */
18
21
  /** @typedef {import("../../declarations/WebpackOptions").RawPublicPath} RawPublicPath */
19
22
  /** @typedef {import("../Compilation")} Compilation */
20
23
  /** @typedef {import("../Compiler")} Compiler */
21
24
  /** @typedef {import("../Generator").GenerateContext} GenerateContext */
22
25
  /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
23
26
  /** @typedef {import("../Module")} Module */
27
+ /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
24
28
  /** @typedef {import("../NormalModule")} NormalModule */
25
29
  /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
26
30
  /** @typedef {import("../util/Hash")} Hash */
@@ -49,7 +53,7 @@ const mergeAssetInfo = (a, b) => {
49
53
  case "immutable":
50
54
  case "development":
51
55
  case "hotModuleReplacement":
52
- case "javascriptModule ":
56
+ case "javascriptModule":
53
57
  result[key] = a[key] || b[key];
54
58
  break;
55
59
  case "related":
@@ -74,24 +78,130 @@ const mergeRelatedInfo = (a, b) => {
74
78
  return result;
75
79
  };
76
80
 
81
+ const encodeDataUri = (encoding, source) => {
82
+ let encodedContent;
83
+
84
+ switch (encoding) {
85
+ case "base64": {
86
+ encodedContent = source.buffer().toString("base64");
87
+ break;
88
+ }
89
+ case false: {
90
+ const content = source.source();
91
+
92
+ if (typeof content !== "string") {
93
+ encodedContent = content.toString("utf-8");
94
+ }
95
+
96
+ encodedContent = encodeURIComponent(encodedContent).replace(
97
+ /[!'()*]/g,
98
+ character => "%" + character.codePointAt(0).toString(16)
99
+ );
100
+ break;
101
+ }
102
+ default:
103
+ throw new Error(`Unsupported encoding '${encoding}'`);
104
+ }
105
+
106
+ return encodedContent;
107
+ };
108
+
109
+ const decodeDataUriContent = (encoding, content) => {
110
+ const isBase64 = encoding === "base64";
111
+ return isBase64
112
+ ? Buffer.from(content, "base64")
113
+ : Buffer.from(decodeURIComponent(content), "ascii");
114
+ };
115
+
77
116
  const JS_TYPES = new Set(["javascript"]);
78
117
  const JS_AND_ASSET_TYPES = new Set(["javascript", "asset"]);
118
+ const DEFAULT_ENCODING = "base64";
79
119
 
80
120
  class AssetGenerator extends Generator {
81
121
  /**
82
122
  * @param {AssetGeneratorOptions["dataUrl"]=} dataUrlOptions the options for the data url
83
123
  * @param {string=} filename override for output.assetModuleFilename
84
124
  * @param {RawPublicPath=} publicPath override for output.assetModulePublicPath
125
+ * @param {AssetModuleOutputPath=} outputPath the output path for the emitted file which is not included in the runtime import
85
126
  * @param {boolean=} emit generate output asset
86
127
  */
87
- constructor(dataUrlOptions, filename, publicPath, emit) {
128
+ constructor(dataUrlOptions, filename, publicPath, outputPath, emit) {
88
129
  super();
89
130
  this.dataUrlOptions = dataUrlOptions;
90
131
  this.filename = filename;
91
132
  this.publicPath = publicPath;
133
+ this.outputPath = outputPath;
92
134
  this.emit = emit;
93
135
  }
94
136
 
137
+ /**
138
+ * @param {NormalModule} module module
139
+ * @param {RuntimeTemplate} runtimeTemplate runtime template
140
+ * @returns {string} source file name
141
+ */
142
+ getSourceFileName(module, runtimeTemplate) {
143
+ return makePathsRelative(
144
+ runtimeTemplate.compilation.compiler.context,
145
+ module.matchResource || module.resource,
146
+ runtimeTemplate.compilation.compiler.root
147
+ ).replace(/^\.\//, "");
148
+ }
149
+
150
+ /**
151
+ * @param {NormalModule} module module for which the bailout reason should be determined
152
+ * @param {ConcatenationBailoutReasonContext} context context
153
+ * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
154
+ */
155
+ getConcatenationBailoutReason(module, context) {
156
+ return undefined;
157
+ }
158
+
159
+ /**
160
+ * @param {NormalModule} module module
161
+ * @returns {string} mime type
162
+ */
163
+ getMimeType(module) {
164
+ if (typeof this.dataUrlOptions === "function") {
165
+ throw new Error(
166
+ "This method must not be called when dataUrlOptions is a function"
167
+ );
168
+ }
169
+
170
+ let mimeType = this.dataUrlOptions.mimetype;
171
+ if (mimeType === undefined) {
172
+ const ext = path.extname(module.nameForCondition());
173
+ if (
174
+ module.resourceResolveData &&
175
+ module.resourceResolveData.mimetype !== undefined
176
+ ) {
177
+ mimeType =
178
+ module.resourceResolveData.mimetype +
179
+ module.resourceResolveData.parameters;
180
+ } else if (ext) {
181
+ mimeType = mimeTypes.lookup(ext);
182
+
183
+ if (typeof mimeType !== "string") {
184
+ throw new Error(
185
+ "DataUrl can't be generated automatically, " +
186
+ `because there is no mimetype for "${ext}" in mimetype database. ` +
187
+ 'Either pass a mimetype via "generator.mimetype" or ' +
188
+ 'use type: "asset/resource" to create a resource file instead of a DataUrl'
189
+ );
190
+ }
191
+ }
192
+ }
193
+
194
+ if (typeof mimeType !== "string") {
195
+ throw new Error(
196
+ "DataUrl can't be generated automatically. " +
197
+ 'Either pass a mimetype via "generator.mimetype" or ' +
198
+ 'use type: "asset/resource" to create a resource file instead of a DataUrl'
199
+ );
200
+ }
201
+
202
+ return mimeType;
203
+ }
204
+
95
205
  /**
96
206
  * @param {NormalModule} module module for which the code should be generated
97
207
  * @param {GenerateContext} generateContext context for generate
@@ -99,14 +209,21 @@ class AssetGenerator extends Generator {
99
209
  */
100
210
  generate(
101
211
  module,
102
- { runtime, chunkGraph, runtimeTemplate, runtimeRequirements, type, getData }
212
+ {
213
+ runtime,
214
+ concatenationScope,
215
+ chunkGraph,
216
+ runtimeTemplate,
217
+ runtimeRequirements,
218
+ type,
219
+ getData
220
+ }
103
221
  ) {
104
222
  switch (type) {
105
223
  case "asset":
106
224
  return module.originalSource();
107
225
  default: {
108
- runtimeRequirements.add(RuntimeGlobals.module);
109
-
226
+ let content;
110
227
  const originalSource = module.originalSource();
111
228
  if (module.buildInfo.dataUrl) {
112
229
  let encodedSource;
@@ -131,71 +248,32 @@ class AssetGenerator extends Generator {
131
248
  }
132
249
  }
133
250
  if (encoding === undefined) {
134
- encoding = "base64";
135
- }
136
- let ext;
137
- let mimeType = this.dataUrlOptions.mimetype;
138
- if (mimeType === undefined) {
139
- ext = path.extname(module.nameForCondition());
140
- if (
141
- module.resourceResolveData &&
142
- module.resourceResolveData.mimetype !== undefined
143
- ) {
144
- mimeType =
145
- module.resourceResolveData.mimetype +
146
- module.resourceResolveData.parameters;
147
- } else if (ext) {
148
- mimeType = mimeTypes.lookup(ext);
149
- }
150
- }
151
- if (typeof mimeType !== "string") {
152
- throw new Error(
153
- "DataUrl can't be generated automatically, " +
154
- `because there is no mimetype for "${ext}" in mimetype database. ` +
155
- 'Either pass a mimetype via "generator.mimetype" or ' +
156
- 'use type: "asset/resource" to create a resource file instead of a DataUrl'
157
- );
251
+ encoding = DEFAULT_ENCODING;
158
252
  }
253
+ const mimeType = this.getMimeType(module);
159
254
 
160
255
  let encodedContent;
256
+
161
257
  if (
162
258
  module.resourceResolveData &&
163
- module.resourceResolveData.encoding === encoding
259
+ module.resourceResolveData.encoding === encoding &&
260
+ decodeDataUriContent(
261
+ module.resourceResolveData.encoding,
262
+ module.resourceResolveData.encodedContent
263
+ ).equals(originalSource.buffer())
164
264
  ) {
165
265
  encodedContent = module.resourceResolveData.encodedContent;
166
266
  } else {
167
- switch (encoding) {
168
- case "base64": {
169
- encodedContent = originalSource.buffer().toString("base64");
170
- break;
171
- }
172
- case false: {
173
- const content = originalSource.source();
174
-
175
- if (typeof content !== "string") {
176
- encodedContent = content.toString("utf-8");
177
- }
178
-
179
- encodedContent = encodeURIComponent(encodedContent).replace(
180
- /[!'()*]/g,
181
- character => "%" + character.codePointAt(0).toString(16)
182
- );
183
- break;
184
- }
185
- default:
186
- throw new Error(`Unsupported encoding '${encoding}'`);
187
- }
267
+ encodedContent = encodeDataUri(encoding, originalSource);
188
268
  }
189
269
 
190
270
  encodedSource = `data:${mimeType}${
191
271
  encoding ? `;${encoding}` : ""
192
272
  },${encodedContent}`;
193
273
  }
194
- return new RawSource(
195
- `${RuntimeGlobals.module}.exports = ${JSON.stringify(
196
- encodedSource
197
- )};`
198
- );
274
+ const data = getData();
275
+ data.set("url", Buffer.from(encodedSource));
276
+ content = JSON.stringify(encodedSource);
199
277
  } else {
200
278
  const assetModuleFilename =
201
279
  this.filename || runtimeTemplate.outputOptions.assetModuleFilename;
@@ -207,16 +285,15 @@ class AssetGenerator extends Generator {
207
285
  const fullHash = /** @type {string} */ (
208
286
  hash.digest(runtimeTemplate.outputOptions.hashDigest)
209
287
  );
210
- const contentHash = fullHash.slice(
211
- 0,
288
+ const contentHash = nonNumericOnlyHash(
289
+ fullHash,
212
290
  runtimeTemplate.outputOptions.hashDigestLength
213
291
  );
214
292
  module.buildInfo.fullContentHash = fullHash;
215
- const sourceFilename = makePathsRelative(
216
- runtimeTemplate.compilation.compiler.context,
217
- module.matchResource || module.resource,
218
- runtimeTemplate.compilation.compiler.root
219
- ).replace(/^\.\//, "");
293
+ const sourceFilename = this.getSourceFileName(
294
+ module,
295
+ runtimeTemplate
296
+ );
220
297
  let { path: filename, info: assetInfo } =
221
298
  runtimeTemplate.compilation.getAssetPathWithInfo(
222
299
  assetModuleFilename,
@@ -228,7 +305,7 @@ class AssetGenerator extends Generator {
228
305
  contentHash
229
306
  }
230
307
  );
231
- let publicPath;
308
+ let assetPath;
232
309
  if (this.publicPath !== undefined) {
233
310
  const { path, info } =
234
311
  runtimeTemplate.compilation.getAssetPathWithInfo(
@@ -241,16 +318,34 @@ class AssetGenerator extends Generator {
241
318
  contentHash
242
319
  }
243
320
  );
244
- publicPath = JSON.stringify(path);
245
321
  assetInfo = mergeAssetInfo(assetInfo, info);
322
+ assetPath = JSON.stringify(path + filename);
246
323
  } else {
247
- publicPath = RuntimeGlobals.publicPath;
248
324
  runtimeRequirements.add(RuntimeGlobals.publicPath); // add __webpack_require__.p
325
+ assetPath = runtimeTemplate.concatenation(
326
+ { expr: RuntimeGlobals.publicPath },
327
+ filename
328
+ );
249
329
  }
250
330
  assetInfo = {
251
331
  sourceFilename,
252
332
  ...assetInfo
253
333
  };
334
+ if (this.outputPath) {
335
+ const { path: outputPath, info } =
336
+ runtimeTemplate.compilation.getAssetPathWithInfo(
337
+ this.outputPath,
338
+ {
339
+ module,
340
+ runtime,
341
+ filename: sourceFilename,
342
+ chunkGraph,
343
+ contentHash
344
+ }
345
+ );
346
+ assetInfo = mergeAssetInfo(assetInfo, info);
347
+ filename = path.posix.join(outputPath, filename);
348
+ }
254
349
  module.buildInfo.filename = filename;
255
350
  module.buildInfo.assetInfo = assetInfo;
256
351
  if (getData) {
@@ -262,11 +357,22 @@ class AssetGenerator extends Generator {
262
357
  data.set("filename", filename);
263
358
  data.set("assetInfo", assetInfo);
264
359
  }
360
+ content = assetPath;
361
+ }
265
362
 
363
+ if (concatenationScope) {
364
+ concatenationScope.registerNamespaceExport(
365
+ ConcatenationScope.NAMESPACE_OBJECT_EXPORT
366
+ );
367
+ return new RawSource(
368
+ `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
369
+ ConcatenationScope.NAMESPACE_OBJECT_EXPORT
370
+ } = ${content};`
371
+ );
372
+ } else {
373
+ runtimeRequirements.add(RuntimeGlobals.module);
266
374
  return new RawSource(
267
- `${
268
- RuntimeGlobals.module
269
- }.exports = ${publicPath} + ${JSON.stringify(filename)};`
375
+ `${RuntimeGlobals.module}.exports = ${content};`
270
376
  );
271
377
  }
272
378
  }
@@ -326,8 +432,59 @@ class AssetGenerator extends Generator {
326
432
  * @param {Hash} hash hash that will be modified
327
433
  * @param {UpdateHashContext} updateHashContext context for updating hash
328
434
  */
329
- updateHash(hash, { module }) {
330
- hash.update(module.buildInfo.dataUrl ? "data-url" : "resource");
435
+ updateHash(hash, { module, runtime, runtimeTemplate, chunkGraph }) {
436
+ if (module.buildInfo.dataUrl) {
437
+ hash.update("data-url");
438
+ // this.dataUrlOptions as function should be pure and only depend on input source and filename
439
+ // therefore it doesn't need to be hashed
440
+ if (typeof this.dataUrlOptions === "function") {
441
+ const ident = /** @type {{ ident?: string }} */ (this.dataUrlOptions)
442
+ .ident;
443
+ if (ident) hash.update(ident);
444
+ } else {
445
+ if (
446
+ this.dataUrlOptions.encoding &&
447
+ this.dataUrlOptions.encoding !== DEFAULT_ENCODING
448
+ ) {
449
+ hash.update(this.dataUrlOptions.encoding);
450
+ }
451
+ if (this.dataUrlOptions.mimetype)
452
+ hash.update(this.dataUrlOptions.mimetype);
453
+ // computed mimetype depends only on module filename which is already part of the hash
454
+ }
455
+ } else {
456
+ hash.update("resource");
457
+
458
+ const pathData = {
459
+ module,
460
+ runtime,
461
+ filename: this.getSourceFileName(module, runtimeTemplate),
462
+ chunkGraph,
463
+ contentHash: runtimeTemplate.contentHashReplacement
464
+ };
465
+
466
+ if (typeof this.publicPath === "function") {
467
+ hash.update("path");
468
+ const assetInfo = {};
469
+ hash.update(this.publicPath(pathData, assetInfo));
470
+ hash.update(JSON.stringify(assetInfo));
471
+ } else if (this.publicPath) {
472
+ hash.update("path");
473
+ hash.update(this.publicPath);
474
+ } else {
475
+ hash.update("no-path");
476
+ }
477
+
478
+ const assetModuleFilename =
479
+ this.filename || runtimeTemplate.outputOptions.assetModuleFilename;
480
+ const { path: filename, info } =
481
+ runtimeTemplate.compilation.getAssetPathWithInfo(
482
+ assetModuleFilename,
483
+ pathData
484
+ );
485
+ hash.update(filename);
486
+ hash.update(JSON.stringify(info));
487
+ }
331
488
  }
332
489
  }
333
490
 
@@ -137,9 +137,11 @@ class AssetModulesPlugin {
137
137
 
138
138
  let filename = undefined;
139
139
  let publicPath = undefined;
140
+ let outputPath = undefined;
140
141
  if (type !== "asset/inline") {
141
142
  filename = generatorOptions.filename;
142
143
  publicPath = generatorOptions.publicPath;
144
+ outputPath = generatorOptions.outputPath;
143
145
  }
144
146
 
145
147
  const AssetGenerator = getAssetGenerator();
@@ -148,6 +150,7 @@ class AssetModulesPlugin {
148
150
  dataUrl,
149
151
  filename,
150
152
  publicPath,
153
+ outputPath,
151
154
  generatorOptions.emit !== false
152
155
  );
153
156
  });
@@ -31,6 +31,7 @@ class AssetParser extends Parser {
31
31
  }
32
32
  state.module.buildInfo.strict = true;
33
33
  state.module.buildMeta.exportsType = "default";
34
+ state.module.buildMeta.defaultObject = false;
34
35
 
35
36
  if (typeof this.dataUrlCondition === "function") {
36
37
  state.module.buildInfo.dataUrl = this.dataUrlCondition(source, {
@@ -6,11 +6,13 @@
6
6
  "use strict";
7
7
 
8
8
  const { RawSource } = require("webpack-sources");
9
+ const ConcatenationScope = require("../ConcatenationScope");
9
10
  const Generator = require("../Generator");
10
11
  const RuntimeGlobals = require("../RuntimeGlobals");
11
12
 
12
13
  /** @typedef {import("webpack-sources").Source} Source */
13
14
  /** @typedef {import("../Generator").GenerateContext} GenerateContext */
15
+ /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
14
16
  /** @typedef {import("../NormalModule")} NormalModule */
15
17
 
16
18
  const TYPES = new Set(["javascript"]);
@@ -21,9 +23,10 @@ class AssetSourceGenerator extends Generator {
21
23
  * @param {GenerateContext} generateContext context for generate
22
24
  * @returns {Source} generated code
23
25
  */
24
- generate(module, { chunkGraph, runtimeTemplate, runtimeRequirements }) {
25
- runtimeRequirements.add(RuntimeGlobals.module);
26
-
26
+ generate(
27
+ module,
28
+ { concatenationScope, chunkGraph, runtimeTemplate, runtimeRequirements }
29
+ ) {
27
30
  const originalSource = module.originalSource();
28
31
 
29
32
  if (!originalSource) {
@@ -38,9 +41,31 @@ class AssetSourceGenerator extends Generator {
38
41
  } else {
39
42
  encodedSource = content.toString("utf-8");
40
43
  }
41
- return new RawSource(
42
- `${RuntimeGlobals.module}.exports = ${JSON.stringify(encodedSource)};`
43
- );
44
+
45
+ let sourceContent;
46
+ if (concatenationScope) {
47
+ concatenationScope.registerNamespaceExport(
48
+ ConcatenationScope.NAMESPACE_OBJECT_EXPORT
49
+ );
50
+ sourceContent = `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
51
+ ConcatenationScope.NAMESPACE_OBJECT_EXPORT
52
+ } = ${JSON.stringify(encodedSource)};`;
53
+ } else {
54
+ runtimeRequirements.add(RuntimeGlobals.module);
55
+ sourceContent = `${RuntimeGlobals.module}.exports = ${JSON.stringify(
56
+ encodedSource
57
+ )};`;
58
+ }
59
+ return new RawSource(sourceContent);
60
+ }
61
+
62
+ /**
63
+ * @param {NormalModule} module module for which the bailout reason should be determined
64
+ * @param {ConcatenationBailoutReasonContext} context context
65
+ * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
66
+ */
67
+ getConcatenationBailoutReason(module, context) {
68
+ return undefined;
44
69
  }
45
70
 
46
71
  /**
@@ -23,6 +23,7 @@ class AssetSourceParser extends Parser {
23
23
  const { module } = state;
24
24
  module.buildInfo.strict = true;
25
25
  module.buildMeta.exportsType = "default";
26
+ state.module.buildMeta.defaultObject = false;
26
27
 
27
28
  return state;
28
29
  }
@@ -0,0 +1,148 @@
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 { RawSource } = require("webpack-sources");
9
+ const Module = require("../Module");
10
+ const RuntimeGlobals = require("../RuntimeGlobals");
11
+ const makeSerializable = require("../util/makeSerializable");
12
+
13
+ /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
14
+ /** @typedef {import("../Compilation")} Compilation */
15
+ /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
16
+ /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
17
+ /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
18
+ /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
19
+ /** @typedef {import("../RequestShortener")} RequestShortener */
20
+ /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
21
+ /** @typedef {import("../WebpackError")} WebpackError */
22
+ /** @typedef {import("../util/Hash")} Hash */
23
+ /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
24
+
25
+ const TYPES = new Set(["javascript"]);
26
+
27
+ class RawDataUrlModule extends Module {
28
+ /**
29
+ * @param {string} url raw url
30
+ * @param {string} identifier unique identifier
31
+ * @param {string=} readableIdentifier readable identifier
32
+ */
33
+ constructor(url, identifier, readableIdentifier) {
34
+ super("asset/raw-data-url", null);
35
+ this.url = url;
36
+ this.urlBuffer = url ? Buffer.from(url) : undefined;
37
+ this.identifierStr = identifier || this.url;
38
+ this.readableIdentifierStr = readableIdentifier || this.identifierStr;
39
+ }
40
+
41
+ /**
42
+ * @returns {Set<string>} types available (do not mutate)
43
+ */
44
+ getSourceTypes() {
45
+ return TYPES;
46
+ }
47
+
48
+ /**
49
+ * @returns {string} a unique identifier of the module
50
+ */
51
+ identifier() {
52
+ return this.identifierStr;
53
+ }
54
+
55
+ /**
56
+ * @param {string=} type the source type for which the size should be estimated
57
+ * @returns {number} the estimated size of the module (must be non-zero)
58
+ */
59
+ size(type) {
60
+ if (this.url === undefined) this.url = this.urlBuffer.toString();
61
+ return Math.max(1, this.url.length);
62
+ }
63
+
64
+ /**
65
+ * @param {RequestShortener} requestShortener the request shortener
66
+ * @returns {string} a user readable identifier of the module
67
+ */
68
+ readableIdentifier(requestShortener) {
69
+ return requestShortener.shorten(this.readableIdentifierStr);
70
+ }
71
+
72
+ /**
73
+ * @param {NeedBuildContext} context context info
74
+ * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
75
+ * @returns {void}
76
+ */
77
+ needBuild(context, callback) {
78
+ return callback(null, !this.buildMeta);
79
+ }
80
+
81
+ /**
82
+ * @param {WebpackOptions} options webpack options
83
+ * @param {Compilation} compilation the compilation
84
+ * @param {ResolverWithOptions} resolver the resolver
85
+ * @param {InputFileSystem} fs the file system
86
+ * @param {function(WebpackError=): void} callback callback function
87
+ * @returns {void}
88
+ */
89
+ build(options, compilation, resolver, fs, callback) {
90
+ this.buildMeta = {};
91
+ this.buildInfo = {
92
+ cacheable: true
93
+ };
94
+ callback();
95
+ }
96
+
97
+ /**
98
+ * @param {CodeGenerationContext} context context for code generation
99
+ * @returns {CodeGenerationResult} result
100
+ */
101
+ codeGeneration(context) {
102
+ if (this.url === undefined) this.url = this.urlBuffer.toString();
103
+ const sources = new Map();
104
+ sources.set(
105
+ "javascript",
106
+ new RawSource(`module.exports = ${JSON.stringify(this.url)};`)
107
+ );
108
+ const data = new Map();
109
+ data.set("url", this.urlBuffer);
110
+ const runtimeRequirements = new Set();
111
+ runtimeRequirements.add(RuntimeGlobals.module);
112
+ return { sources, runtimeRequirements, data };
113
+ }
114
+
115
+ /**
116
+ * @param {Hash} hash the hash used to track dependencies
117
+ * @param {UpdateHashContext} context context
118
+ * @returns {void}
119
+ */
120
+ updateHash(hash, context) {
121
+ hash.update(this.urlBuffer);
122
+ super.updateHash(hash, context);
123
+ }
124
+
125
+ serialize(context) {
126
+ const { write } = context;
127
+
128
+ write(this.urlBuffer);
129
+ write(this.identifierStr);
130
+ write(this.readableIdentifierStr);
131
+
132
+ super.serialize(context);
133
+ }
134
+
135
+ deserialize(context) {
136
+ const { read } = context;
137
+
138
+ this.urlBuffer = read();
139
+ this.identifierStr = read();
140
+ this.readableIdentifierStr = read();
141
+
142
+ super.deserialize(context);
143
+ }
144
+ }
145
+
146
+ makeSerializable(RawDataUrlModule, "webpack/lib/asset/RawDataUrlModule");
147
+
148
+ module.exports = RawDataUrlModule;