GetMainFilenameRuntimeModule.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. * Generates runtime code for this runtime module.
  25. * @returns {string | null} runtime code
  26. */
  27. generate() {
  28. const { global, filename } = this;
  29. const compilation = /** @type {Compilation} */ (this.compilation);
  30. const chunk = /** @type {Chunk} */ (this.chunk);
  31. const { runtimeTemplate } = compilation;
  32. const url = compilation.getPath(JSON.stringify(filename), {
  33. hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
  34. hashWithLength: (length) =>
  35. `" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
  36. chunk,
  37. runtime: chunk.runtime
  38. });
  39. return Template.asString([
  40. `${global} = ${runtimeTemplate.returningFunction(url)};`
  41. ]);
  42. }
  43. }
  44. module.exports = GetMainFilenameRuntimeModule;