GetMainFilenameRuntimeModule.js 1.1 KB

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