GetMainFilenameRuntimeModule.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. /** @import Chunk from "../Chunk" */
  9. /** @import Compilation from "../Compilation" */
  10. class GetMainFilenameRuntimeModule extends RuntimeModule {
  11. /**
  12. * Returns true, if the runtime module should get it's own scope.
  13. * When false, `generate()` must emit complete statements ending with `;`
  14. * so a following runtime IIFE is not parsed as a call (ASI).
  15. * @returns {boolean} true, if the runtime module should get it's own scope
  16. */
  17. shouldIsolate() {
  18. return false;
  19. }
  20. /**
  21. * @param {string} name readable name
  22. * @param {string} global global object binding
  23. * @param {string} filename main file name
  24. */
  25. constructor(name, global, filename) {
  26. super(`get ${name} filename`);
  27. /** @type {string} */
  28. this.global = global;
  29. /** @type {string} */
  30. this.filename = filename;
  31. }
  32. /**
  33. * Generates runtime code for this runtime module.
  34. * @returns {string | null} runtime code
  35. */
  36. generate() {
  37. const { global, filename } = this;
  38. const compilation = /** @type {Compilation} */ (this.compilation);
  39. const chunk = /** @type {Chunk} */ (this.chunk);
  40. const { runtimeTemplate } = compilation;
  41. const url = compilation.getPath(JSON.stringify(filename), {
  42. hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
  43. hashWithLength: (length) =>
  44. `" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
  45. chunk,
  46. runtime: chunk.runtime
  47. });
  48. return Template.asString([
  49. `${global} = ${runtimeTemplate.returningFunction(url)};`
  50. ]);
  51. }
  52. }
  53. module.exports = GetMainFilenameRuntimeModule;