GetChunkFilenameRuntimeModule.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const RuntimeGlobals = require("../RuntimeGlobals");
  6. const RuntimeModule = require("../RuntimeModule");
  7. const Template = require("../Template");
  8. const { reEncodeDigest } = require("../TemplatedPathPlugin");
  9. const { first } = require("../util/SetHelpers");
  10. /** @import Chunk, { ChunkId, ChunkFilenameTemplate } from "../Chunk" */
  11. /** @import ChunkGraph from "../ChunkGraph" */
  12. /**
  13. * @import Compilation, {
  14. * HashWithLengthFunction,
  15. * HashWithDigestFunction
  16. * } from "../Compilation"
  17. */
  18. class GetChunkFilenameRuntimeModule extends RuntimeModule {
  19. /**
  20. * Returns true, if the runtime module should get it's own scope.
  21. * When false, `generate()` must emit complete statements ending with `;`
  22. * so a following runtime IIFE is not parsed as a call (ASI).
  23. * @returns {boolean} true, if the runtime module should get it's own scope
  24. */
  25. shouldIsolate() {
  26. return false;
  27. }
  28. /**
  29. * @param {string} contentType the contentType to use the content hash for
  30. * @param {string} name kind of filename
  31. * @param {string} global function name to be assigned
  32. * @param {(chunk: Chunk) => ChunkFilenameTemplate | false} getFilenameForChunk functor to get the filename or function
  33. * @param {boolean} allChunks when false, only async chunks are included
  34. * @param {boolean=} usesFullHashDigest the filename uses `[fullhash:<digest>]`/`[hash:<digest>]`, so the re-encoded full hash must be inlined (post-hash) instead of read from the runtime `getFullHash()` expression
  35. */
  36. constructor(
  37. contentType,
  38. name,
  39. global,
  40. getFilenameForChunk,
  41. allChunks,
  42. usesFullHashDigest
  43. ) {
  44. super(`get ${name} chunk filename`);
  45. /** @type {string} */
  46. this.contentType = contentType;
  47. /** @type {string} */
  48. this.global = global;
  49. /** @type {(chunk: Chunk) => ChunkFilenameTemplate | false} */
  50. this.getFilenameForChunk = getFilenameForChunk;
  51. /** @type {boolean} */
  52. this.allChunks = allChunks;
  53. // An inline digest on `[fullhash]` needs the final hash inlined, so this
  54. // module must re-render after hashing (`fullHash`); otherwise it only needs
  55. // the referenced chunk hashes, available before the full hash (`dependentHash`).
  56. if (usesFullHashDigest) {
  57. /** @type {boolean} */
  58. this.fullHash = true;
  59. } else {
  60. /** @type {boolean} */
  61. this.dependentHash = true;
  62. }
  63. }
  64. /**
  65. * Generates runtime code for this runtime module.
  66. * @returns {string | null} runtime code
  67. */
  68. generate() {
  69. const { global, contentType, getFilenameForChunk, allChunks } = this;
  70. const compilation = /** @type {Compilation} */ (this.compilation);
  71. const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
  72. const chunk = /** @type {Chunk} */ (this.chunk);
  73. const { runtimeTemplate } = compilation;
  74. // Digest the stored hashes are encoded in, for re-encoding `[<hash>:<digest>]`.
  75. const sourceDigest = compilation.outputOptions.hashDigest;
  76. /**
  77. * Re-encodes a full hash digest into the requested digest, optionally truncated.
  78. * @param {string} value full hash digest
  79. * @param {string} digest requested digest
  80. * @param {number=} length requested length
  81. * @returns {string} re-encoded hash
  82. */
  83. const reEncode = (value, digest, length) => {
  84. const hash = reEncodeDigest(
  85. value,
  86. /** @type {string} */ (sourceDigest),
  87. digest
  88. );
  89. return length ? hash.slice(0, length) : hash;
  90. };
  91. // `[fullhash:<digest>]`/`[hash:<digest>]` would resolve to a runtime
  92. // `getFullHash()` expression that can't be re-encoded; instead this module is
  93. // flagged `fullHash` and re-rendered after hashing, so we inline the
  94. // re-encoded full hash here — byte-identical to the statically emitted file.
  95. /** @type {HashWithDigestFunction} */
  96. const fullHashWithDigest = (digest, length) => {
  97. const fullHash = compilation.fullHash;
  98. // Pre-hash pass (hash not computed yet): a placeholder, replaced on the
  99. // post-hash re-render. Matches `GetFullHashRuntimeModule`'s `|| "XXXX"`.
  100. if (!fullHash) return length ? "x".repeat(length) : "x";
  101. return reEncode(fullHash, digest, length);
  102. };
  103. /** @type {Map<ChunkFilenameTemplate, Set<Chunk>>} */
  104. const chunkFilenames = new Map();
  105. let maxChunks = 0;
  106. /** @type {string | undefined} */
  107. let dynamicFilename;
  108. /**
  109. * @param {Chunk} c the chunk
  110. * @returns {void}
  111. */
  112. const addChunk = (c) => {
  113. const chunkFilename = getFilenameForChunk(c);
  114. if (chunkFilename) {
  115. let set = chunkFilenames.get(chunkFilename);
  116. if (set === undefined) {
  117. chunkFilenames.set(chunkFilename, (set = new Set()));
  118. }
  119. set.add(c);
  120. if (typeof chunkFilename === "string") {
  121. if (set.size < maxChunks) return;
  122. if (set.size === maxChunks) {
  123. if (
  124. chunkFilename.length <
  125. /** @type {string} */ (dynamicFilename).length
  126. ) {
  127. return;
  128. }
  129. if (
  130. chunkFilename.length ===
  131. /** @type {string} */ (dynamicFilename).length &&
  132. chunkFilename < /** @type {string} */ (dynamicFilename)
  133. ) {
  134. return;
  135. }
  136. }
  137. maxChunks = set.size;
  138. dynamicFilename = chunkFilename;
  139. }
  140. }
  141. };
  142. /** @type {string[]} */
  143. const includedChunksMessages = [];
  144. if (allChunks) {
  145. includedChunksMessages.push("all chunks");
  146. for (const c of chunk.getAllReferencedChunks()) {
  147. addChunk(c);
  148. }
  149. } else {
  150. includedChunksMessages.push("async chunks");
  151. for (const c of chunk.getAllAsyncChunks()) {
  152. addChunk(c);
  153. }
  154. const includeEntries = chunkGraph
  155. .getTreeRuntimeRequirements(chunk)
  156. .has(RuntimeGlobals.ensureChunkIncludeEntries);
  157. if (includeEntries) {
  158. includedChunksMessages.push("chunks that the entrypoint depends on");
  159. for (const c of chunkGraph.getRuntimeChunkDependentChunksIterable(
  160. chunk
  161. )) {
  162. addChunk(c);
  163. }
  164. }
  165. }
  166. for (const entrypoint of chunk.getAllReferencedAsyncEntrypoints()) {
  167. addChunk(entrypoint.chunks[entrypoint.chunks.length - 1]);
  168. }
  169. /** @type {Map<string, Set<string | number | null>>} */
  170. const staticUrls = new Map();
  171. /** @type {Set<Chunk>} */
  172. const dynamicUrlChunks = new Set();
  173. /**
  174. * Drops the dead empty string a leading placeholder leaves, only when a
  175. * quoted operand ends the expression so the `+` chain stays a string.
  176. * @param {string} expr concatenation expression
  177. * @returns {string} the expression without its dead empty string
  178. */
  179. const dropDeadConcatOperand = (expr) =>
  180. expr.startsWith('"" + ') && expr.endsWith('"') ? expr.slice(5) : expr;
  181. /**
  182. * @param {Chunk} c the chunk
  183. * @param {ChunkFilenameTemplate} chunkFilename the filename template for the chunk
  184. * @returns {void}
  185. */
  186. const addStaticUrl = (c, chunkFilename) => {
  187. /**
  188. * @param {ChunkId} value a value
  189. * @returns {string} string to put in quotes
  190. */
  191. const unquotedStringify = (value) => {
  192. const str = `${value}`;
  193. if (str.length >= 5 && str === `${c.id}`) {
  194. // This is shorter and generates the same result
  195. return '" + chunkId + "';
  196. }
  197. const s = JSON.stringify(str);
  198. return s.slice(1, -1);
  199. };
  200. /**
  201. * @param {string} value string
  202. * @returns {HashWithLengthFunction} string to put in quotes with length
  203. */
  204. const unquotedStringifyWithLength = (value) => (length) =>
  205. unquotedStringify(`${value}`.slice(0, length));
  206. const chunkFilenameValue =
  207. typeof chunkFilename === "function"
  208. ? JSON.stringify(
  209. chunkFilename({
  210. chunk: c,
  211. contentHashType: contentType
  212. })
  213. )
  214. : JSON.stringify(chunkFilename);
  215. const staticChunkFilename = compilation.getPath(chunkFilenameValue, {
  216. hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
  217. hashWithLength: (length) =>
  218. `" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
  219. hashWithDigest: fullHashWithDigest,
  220. chunk: {
  221. id: unquotedStringify(/** @type {ChunkId} */ (c.id)),
  222. hash: unquotedStringify(/** @type {string} */ (c.renderedHash)),
  223. hashWithLength: unquotedStringifyWithLength(
  224. /** @type {string} */ (c.renderedHash)
  225. ),
  226. hashWithDigest: (digest, length) =>
  227. unquotedStringify(
  228. reEncode(/** @type {string} */ (c.hash), digest, length)
  229. ),
  230. name: unquotedStringify(c.name || /** @type {ChunkId} */ (c.id)),
  231. contentHash: {
  232. [contentType]: unquotedStringify(c.contentHash[contentType])
  233. },
  234. contentHashWithLength: {
  235. [contentType]: unquotedStringifyWithLength(
  236. c.contentHash[contentType]
  237. )
  238. },
  239. contentHashWithDigest: {
  240. [contentType]: (digest, length) =>
  241. unquotedStringify(
  242. reEncode(
  243. c.contentHashFull[contentType] || c.contentHash[contentType],
  244. digest,
  245. length
  246. )
  247. )
  248. }
  249. },
  250. contentHashType: contentType
  251. });
  252. const url = dropDeadConcatOperand(staticChunkFilename);
  253. let set = staticUrls.get(url);
  254. if (set === undefined) {
  255. staticUrls.set(url, (set = new Set()));
  256. }
  257. set.add(c.id);
  258. };
  259. for (const [filename, chunks] of chunkFilenames) {
  260. if (filename !== dynamicFilename) {
  261. for (const c of chunks) addStaticUrl(c, filename);
  262. } else {
  263. for (const c of chunks) dynamicUrlChunks.add(c);
  264. }
  265. }
  266. /**
  267. * @param {(chunk: Chunk) => string | number} fn function from chunk to value
  268. * @returns {string} code with static mapping of results of fn
  269. */
  270. const createMap = (fn) => {
  271. /** @type {Record<ChunkId, ChunkId>} */
  272. const obj = {};
  273. let useId = false;
  274. /** @type {ChunkId | undefined} */
  275. let lastKey;
  276. let entries = 0;
  277. for (const c of dynamicUrlChunks) {
  278. const value = fn(c);
  279. if (value === c.id) {
  280. useId = true;
  281. } else {
  282. obj[/** @type {ChunkId} */ (c.id)] = value;
  283. lastKey = /** @type {ChunkId} */ (c.id);
  284. entries++;
  285. }
  286. }
  287. if (entries === 0) return "chunkId";
  288. if (entries === 1) {
  289. return useId
  290. ? `(chunkId === ${JSON.stringify(lastKey)} ? ${JSON.stringify(
  291. obj[/** @type {ChunkId} */ (lastKey)]
  292. )} : chunkId)`
  293. : JSON.stringify(obj[/** @type {ChunkId} */ (lastKey)]);
  294. }
  295. return useId
  296. ? `(${JSON.stringify(obj)}[chunkId] || chunkId)`
  297. : `${JSON.stringify(obj)}[chunkId]`;
  298. };
  299. /**
  300. * @param {(chunk: Chunk) => string | number} fn function from chunk to value
  301. * @returns {string} code with static mapping of results of fn for including in quoted string
  302. */
  303. const mapExpr = (fn) => `" + ${createMap(fn)} + "`;
  304. /**
  305. * @param {(chunk: Chunk) => string | number} fn function from chunk to value
  306. * @returns {HashWithLengthFunction} function which generates code with static mapping of results of fn for including in quoted string for specific length
  307. */
  308. const mapExprWithLength = (fn) => (length) =>
  309. `" + ${createMap((c) => `${fn(c)}`.slice(0, length))} + "`;
  310. const dynamicUrl =
  311. dynamicFilename &&
  312. compilation.getPath(JSON.stringify(dynamicFilename), {
  313. hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
  314. hashWithLength: (length) =>
  315. `" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
  316. hashWithDigest: fullHashWithDigest,
  317. chunk: {
  318. id: '" + chunkId + "',
  319. hash: mapExpr((c) => /** @type {string} */ (c.renderedHash)),
  320. hashWithLength: mapExprWithLength(
  321. (c) => /** @type {string} */ (c.renderedHash)
  322. ),
  323. hashWithDigest: (digest, length) =>
  324. mapExpr((c) =>
  325. reEncode(/** @type {string} */ (c.hash), digest, length)
  326. ),
  327. name: mapExpr((c) => c.name || /** @type {ChunkId} */ (c.id)),
  328. contentHash: {
  329. [contentType]: mapExpr((c) => c.contentHash[contentType])
  330. },
  331. contentHashWithLength: {
  332. [contentType]: mapExprWithLength((c) => c.contentHash[contentType])
  333. },
  334. contentHashWithDigest: {
  335. [contentType]: (digest, length) =>
  336. mapExpr((c) =>
  337. reEncode(
  338. c.contentHashFull[contentType] || c.contentHash[contentType],
  339. digest,
  340. length
  341. )
  342. )
  343. }
  344. },
  345. contentHashType: contentType
  346. });
  347. const url = dynamicUrl && dropDeadConcatOperand(dynamicUrl);
  348. const comment = `// This function allow to reference ${includedChunksMessages.join(
  349. " and "
  350. )}`;
  351. // Nothing to branch on, so the whole function is the template expression.
  352. if (staticUrls.size === 0) {
  353. return Template.asString([
  354. comment,
  355. // `url` is `undefined` when no chunk contributes a filename; the
  356. // function then returns `undefined`, as it did before.
  357. `${global} = ${runtimeTemplate.returningFunction(`${url}`, "chunkId")};`
  358. ]);
  359. }
  360. return Template.asString([
  361. comment,
  362. `${global} = ${runtimeTemplate.basicFunction("chunkId", [
  363. "// return url for filenames not based on template",
  364. // it minimizes to `x===1?"...":x===2?"...":"..."`
  365. Template.asString(
  366. Array.from(staticUrls, ([url, ids]) => {
  367. const condition =
  368. ids.size === 1
  369. ? `chunkId === ${JSON.stringify(first(ids))}`
  370. : `{${Array.from(ids, (id) => `${JSON.stringify(id)}:1`).join(
  371. ","
  372. )}}[chunkId]`;
  373. return `if (${condition}) return ${url};`;
  374. })
  375. ),
  376. "// return url for filenames based on template",
  377. `return ${url};`
  378. ])};`
  379. ]);
  380. }
  381. }
  382. module.exports = GetChunkFilenameRuntimeModule;