GetMainFilenameRuntimeModule.js 1.3 KB

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