wasmModuleFilename.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author sheo13666q @sheo13666q
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const {
  8. getPresentKinds,
  9. reEncodeDigest,
  10. usesFullHashDigest
  11. } = require("../TemplatedPathPlugin");
  12. /** @import Compilation from "../Compilation" */
  13. /**
  14. * Whether the runtime needs `getFullHash` to build a binary's name. A `[hash]` next to
  15. * a module resolves to that module's hash, not the compilation's, so only the explicit
  16. * `[fullhash]` spelling asks for it — and a digest is inlined rather than read.
  17. * @param {string} filename `output.webassemblyModuleFilename`
  18. * @returns {boolean} true when the runtime reads the compilation hash
  19. */
  20. const needsRuntimeFullHash = (filename) =>
  21. getPresentKinds(filename).has("fullhash") && !usesFullHashDigest(filename);
  22. /**
  23. * The compilation-hash half of the `getPath` data for interpolating
  24. * `output.webassemblyModuleFilename` into runtime code.
  25. * @param {Compilation} compilation the compilation
  26. * @returns {{ hash: string, hashWithLength: (length: number) => string, hashWithDigest: (digest: string, length?: number) => string }} the fields
  27. */
  28. const fullHashPathData = (compilation) => ({
  29. hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
  30. hashWithLength: (length) =>
  31. `" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
  32. // A digest re-encodes the hash, which the runtime expression above cannot carry, so
  33. // the settled value is inlined instead — byte-identical to the emitted filename.
  34. hashWithDigest: (digest, length) => {
  35. const { fullHash, outputOptions } = compilation;
  36. // Pre-hash pass: a stand-in, replaced on the post-hash re-render that
  37. // `needsInlinedFullHash` asks for. Matches `GetFullHashRuntimeModule`.
  38. if (!fullHash) return length ? "x".repeat(length) : "x";
  39. const hash = reEncodeDigest(
  40. fullHash,
  41. outputOptions.hashDigest || "hex",
  42. digest
  43. );
  44. return length ? hash.slice(0, length) : hash;
  45. }
  46. });
  47. module.exports.fullHashPathData = fullHashPathData;
  48. module.exports.needsRuntimeFullHash = needsRuntimeFullHash;