GetMainFilenameRuntimeModule.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. /** @typedef {import("../Chunk")} Chunk */
  9. /** @typedef {import("../Compilation")} Compilation */
  10. class GetMainFilenameRuntimeModule extends RuntimeModule {
  11. /**
  12. * @param {string} name readable name
  13. * @param {string} global global object binding
  14. * @param {string} filename main file name
  15. */
  16. constructor(name, global, filename) {
  17. super(`get ${name} filename`);
  18. /** @type {string} */
  19. this.global = global;
  20. /** @type {string} */
  21. this.filename = filename;
  22. }
  23. /**
  24. * @returns {string | null} runtime code
  25. */
  26. generate() {
  27. const { global, filename } = this;
  28. const compilation = /** @type {Compilation} */ (this.compilation);
  29. const chunk = /** @type {Chunk} */ (this.chunk);
  30. const { runtimeTemplate } = compilation;
  31. const url = compilation.getPath(JSON.stringify(filename), {
  32. hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
  33. hashWithLength: (length) =>
  34. `" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
  35. chunk,
  36. runtime: chunk.runtime
  37. });
  38. return Template.asString([
  39. `${global} = ${runtimeTemplate.returningFunction(url)};`
  40. ]);
  41. }
  42. }
  43. module.exports = GetMainFilenameRuntimeModule;