| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- */
- "use strict";
- const RuntimeGlobals = require("../RuntimeGlobals");
- const RuntimeModule = require("../RuntimeModule");
- const Template = require("../Template");
- const { reEncodeDigest } = require("../TemplatedPathPlugin");
- const { first } = require("../util/SetHelpers");
- /** @import Chunk, { ChunkId, ChunkFilenameTemplate } from "../Chunk" */
- /** @import ChunkGraph from "../ChunkGraph" */
- /**
- * @import Compilation, {
- * HashWithLengthFunction,
- * HashWithDigestFunction
- * } from "../Compilation"
- */
- class GetChunkFilenameRuntimeModule extends RuntimeModule {
- /**
- * Returns true, if the runtime module should get it's own scope.
- * When false, `generate()` must emit complete statements ending with `;`
- * so a following runtime IIFE is not parsed as a call (ASI).
- * @returns {boolean} true, if the runtime module should get it's own scope
- */
- shouldIsolate() {
- return false;
- }
- /**
- * @param {string} contentType the contentType to use the content hash for
- * @param {string} name kind of filename
- * @param {string} global function name to be assigned
- * @param {(chunk: Chunk) => ChunkFilenameTemplate | false} getFilenameForChunk functor to get the filename or function
- * @param {boolean} allChunks when false, only async chunks are included
- * @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
- */
- constructor(
- contentType,
- name,
- global,
- getFilenameForChunk,
- allChunks,
- usesFullHashDigest
- ) {
- super(`get ${name} chunk filename`);
- /** @type {string} */
- this.contentType = contentType;
- /** @type {string} */
- this.global = global;
- /** @type {(chunk: Chunk) => ChunkFilenameTemplate | false} */
- this.getFilenameForChunk = getFilenameForChunk;
- /** @type {boolean} */
- this.allChunks = allChunks;
- // An inline digest on `[fullhash]` needs the final hash inlined, so this
- // module must re-render after hashing (`fullHash`); otherwise it only needs
- // the referenced chunk hashes, available before the full hash (`dependentHash`).
- if (usesFullHashDigest) {
- /** @type {boolean} */
- this.fullHash = true;
- } else {
- /** @type {boolean} */
- this.dependentHash = true;
- }
- }
- /**
- * Generates runtime code for this runtime module.
- * @returns {string | null} runtime code
- */
- generate() {
- const { global, contentType, getFilenameForChunk, allChunks } = this;
- const compilation = /** @type {Compilation} */ (this.compilation);
- const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
- const chunk = /** @type {Chunk} */ (this.chunk);
- const { runtimeTemplate } = compilation;
- // Digest the stored hashes are encoded in, for re-encoding `[<hash>:<digest>]`.
- const sourceDigest = compilation.outputOptions.hashDigest;
- /**
- * Re-encodes a full hash digest into the requested digest, optionally truncated.
- * @param {string} value full hash digest
- * @param {string} digest requested digest
- * @param {number=} length requested length
- * @returns {string} re-encoded hash
- */
- const reEncode = (value, digest, length) => {
- const hash = reEncodeDigest(
- value,
- /** @type {string} */ (sourceDigest),
- digest
- );
- return length ? hash.slice(0, length) : hash;
- };
- // `[fullhash:<digest>]`/`[hash:<digest>]` would resolve to a runtime
- // `getFullHash()` expression that can't be re-encoded; instead this module is
- // flagged `fullHash` and re-rendered after hashing, so we inline the
- // re-encoded full hash here — byte-identical to the statically emitted file.
- /** @type {HashWithDigestFunction} */
- const fullHashWithDigest = (digest, length) => {
- const fullHash = compilation.fullHash;
- // Pre-hash pass (hash not computed yet): a placeholder, replaced on the
- // post-hash re-render. Matches `GetFullHashRuntimeModule`'s `|| "XXXX"`.
- if (!fullHash) return length ? "x".repeat(length) : "x";
- return reEncode(fullHash, digest, length);
- };
- /** @type {Map<ChunkFilenameTemplate, Set<Chunk>>} */
- const chunkFilenames = new Map();
- let maxChunks = 0;
- /** @type {string | undefined} */
- let dynamicFilename;
- /**
- * @param {Chunk} c the chunk
- * @returns {void}
- */
- const addChunk = (c) => {
- const chunkFilename = getFilenameForChunk(c);
- if (chunkFilename) {
- let set = chunkFilenames.get(chunkFilename);
- if (set === undefined) {
- chunkFilenames.set(chunkFilename, (set = new Set()));
- }
- set.add(c);
- if (typeof chunkFilename === "string") {
- if (set.size < maxChunks) return;
- if (set.size === maxChunks) {
- if (
- chunkFilename.length <
- /** @type {string} */ (dynamicFilename).length
- ) {
- return;
- }
- if (
- chunkFilename.length ===
- /** @type {string} */ (dynamicFilename).length &&
- chunkFilename < /** @type {string} */ (dynamicFilename)
- ) {
- return;
- }
- }
- maxChunks = set.size;
- dynamicFilename = chunkFilename;
- }
- }
- };
- /** @type {string[]} */
- const includedChunksMessages = [];
- if (allChunks) {
- includedChunksMessages.push("all chunks");
- for (const c of chunk.getAllReferencedChunks()) {
- addChunk(c);
- }
- } else {
- includedChunksMessages.push("async chunks");
- for (const c of chunk.getAllAsyncChunks()) {
- addChunk(c);
- }
- const includeEntries = chunkGraph
- .getTreeRuntimeRequirements(chunk)
- .has(RuntimeGlobals.ensureChunkIncludeEntries);
- if (includeEntries) {
- includedChunksMessages.push("chunks that the entrypoint depends on");
- for (const c of chunkGraph.getRuntimeChunkDependentChunksIterable(
- chunk
- )) {
- addChunk(c);
- }
- }
- }
- for (const entrypoint of chunk.getAllReferencedAsyncEntrypoints()) {
- addChunk(entrypoint.chunks[entrypoint.chunks.length - 1]);
- }
- /** @type {Map<string, Set<string | number | null>>} */
- const staticUrls = new Map();
- /** @type {Set<Chunk>} */
- const dynamicUrlChunks = new Set();
- /**
- * Drops the dead empty string a leading placeholder leaves, only when a
- * quoted operand ends the expression so the `+` chain stays a string.
- * @param {string} expr concatenation expression
- * @returns {string} the expression without its dead empty string
- */
- const dropDeadConcatOperand = (expr) =>
- expr.startsWith('"" + ') && expr.endsWith('"') ? expr.slice(5) : expr;
- /**
- * @param {Chunk} c the chunk
- * @param {ChunkFilenameTemplate} chunkFilename the filename template for the chunk
- * @returns {void}
- */
- const addStaticUrl = (c, chunkFilename) => {
- /**
- * @param {ChunkId} value a value
- * @returns {string} string to put in quotes
- */
- const unquotedStringify = (value) => {
- const str = `${value}`;
- if (str.length >= 5 && str === `${c.id}`) {
- // This is shorter and generates the same result
- return '" + chunkId + "';
- }
- const s = JSON.stringify(str);
- return s.slice(1, -1);
- };
- /**
- * @param {string} value string
- * @returns {HashWithLengthFunction} string to put in quotes with length
- */
- const unquotedStringifyWithLength = (value) => (length) =>
- unquotedStringify(`${value}`.slice(0, length));
- const chunkFilenameValue =
- typeof chunkFilename === "function"
- ? JSON.stringify(
- chunkFilename({
- chunk: c,
- contentHashType: contentType
- })
- )
- : JSON.stringify(chunkFilename);
- const staticChunkFilename = compilation.getPath(chunkFilenameValue, {
- hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
- hashWithLength: (length) =>
- `" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
- hashWithDigest: fullHashWithDigest,
- chunk: {
- id: unquotedStringify(/** @type {ChunkId} */ (c.id)),
- hash: unquotedStringify(/** @type {string} */ (c.renderedHash)),
- hashWithLength: unquotedStringifyWithLength(
- /** @type {string} */ (c.renderedHash)
- ),
- hashWithDigest: (digest, length) =>
- unquotedStringify(
- reEncode(/** @type {string} */ (c.hash), digest, length)
- ),
- name: unquotedStringify(c.name || /** @type {ChunkId} */ (c.id)),
- contentHash: {
- [contentType]: unquotedStringify(c.contentHash[contentType])
- },
- contentHashWithLength: {
- [contentType]: unquotedStringifyWithLength(
- c.contentHash[contentType]
- )
- },
- contentHashWithDigest: {
- [contentType]: (digest, length) =>
- unquotedStringify(
- reEncode(
- c.contentHashFull[contentType] || c.contentHash[contentType],
- digest,
- length
- )
- )
- }
- },
- contentHashType: contentType
- });
- const url = dropDeadConcatOperand(staticChunkFilename);
- let set = staticUrls.get(url);
- if (set === undefined) {
- staticUrls.set(url, (set = new Set()));
- }
- set.add(c.id);
- };
- for (const [filename, chunks] of chunkFilenames) {
- if (filename !== dynamicFilename) {
- for (const c of chunks) addStaticUrl(c, filename);
- } else {
- for (const c of chunks) dynamicUrlChunks.add(c);
- }
- }
- /**
- * @param {(chunk: Chunk) => string | number} fn function from chunk to value
- * @returns {string} code with static mapping of results of fn
- */
- const createMap = (fn) => {
- /** @type {Record<ChunkId, ChunkId>} */
- const obj = {};
- let useId = false;
- /** @type {ChunkId | undefined} */
- let lastKey;
- let entries = 0;
- for (const c of dynamicUrlChunks) {
- const value = fn(c);
- if (value === c.id) {
- useId = true;
- } else {
- obj[/** @type {ChunkId} */ (c.id)] = value;
- lastKey = /** @type {ChunkId} */ (c.id);
- entries++;
- }
- }
- if (entries === 0) return "chunkId";
- if (entries === 1) {
- return useId
- ? `(chunkId === ${JSON.stringify(lastKey)} ? ${JSON.stringify(
- obj[/** @type {ChunkId} */ (lastKey)]
- )} : chunkId)`
- : JSON.stringify(obj[/** @type {ChunkId} */ (lastKey)]);
- }
- return useId
- ? `(${JSON.stringify(obj)}[chunkId] || chunkId)`
- : `${JSON.stringify(obj)}[chunkId]`;
- };
- /**
- * @param {(chunk: Chunk) => string | number} fn function from chunk to value
- * @returns {string} code with static mapping of results of fn for including in quoted string
- */
- const mapExpr = (fn) => `" + ${createMap(fn)} + "`;
- /**
- * @param {(chunk: Chunk) => string | number} fn function from chunk to value
- * @returns {HashWithLengthFunction} function which generates code with static mapping of results of fn for including in quoted string for specific length
- */
- const mapExprWithLength = (fn) => (length) =>
- `" + ${createMap((c) => `${fn(c)}`.slice(0, length))} + "`;
- const dynamicUrl =
- dynamicFilename &&
- compilation.getPath(JSON.stringify(dynamicFilename), {
- hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
- hashWithLength: (length) =>
- `" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
- hashWithDigest: fullHashWithDigest,
- chunk: {
- id: '" + chunkId + "',
- hash: mapExpr((c) => /** @type {string} */ (c.renderedHash)),
- hashWithLength: mapExprWithLength(
- (c) => /** @type {string} */ (c.renderedHash)
- ),
- hashWithDigest: (digest, length) =>
- mapExpr((c) =>
- reEncode(/** @type {string} */ (c.hash), digest, length)
- ),
- name: mapExpr((c) => c.name || /** @type {ChunkId} */ (c.id)),
- contentHash: {
- [contentType]: mapExpr((c) => c.contentHash[contentType])
- },
- contentHashWithLength: {
- [contentType]: mapExprWithLength((c) => c.contentHash[contentType])
- },
- contentHashWithDigest: {
- [contentType]: (digest, length) =>
- mapExpr((c) =>
- reEncode(
- c.contentHashFull[contentType] || c.contentHash[contentType],
- digest,
- length
- )
- )
- }
- },
- contentHashType: contentType
- });
- const url = dynamicUrl && dropDeadConcatOperand(dynamicUrl);
- const comment = `// This function allow to reference ${includedChunksMessages.join(
- " and "
- )}`;
- // Nothing to branch on, so the whole function is the template expression.
- if (staticUrls.size === 0) {
- return Template.asString([
- comment,
- // `url` is `undefined` when no chunk contributes a filename; the
- // function then returns `undefined`, as it did before.
- `${global} = ${runtimeTemplate.returningFunction(`${url}`, "chunkId")};`
- ]);
- }
- return Template.asString([
- comment,
- `${global} = ${runtimeTemplate.basicFunction("chunkId", [
- "// return url for filenames not based on template",
- // it minimizes to `x===1?"...":x===2?"...":"..."`
- Template.asString(
- Array.from(staticUrls, ([url, ids]) => {
- const condition =
- ids.size === 1
- ? `chunkId === ${JSON.stringify(first(ids))}`
- : `{${Array.from(ids, (id) => `${JSON.stringify(id)}:1`).join(
- ","
- )}}[chunkId]`;
- return `if (${condition}) return ${url};`;
- })
- ),
- "// return url for filenames based on template",
- `return ${url};`
- ])};`
- ]);
- }
- }
- module.exports = GetChunkFilenameRuntimeModule;
|