PublicPathRuntimeModule.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. /** @import { PublicPath } from "../../declarations/WebpackOptions" */
  8. /** @import Compilation from "../Compilation" */
  9. class PublicPathRuntimeModule extends RuntimeModule {
  10. /**
  11. * Returns true, if the runtime module should get it's own scope.
  12. * When false, `generate()` must emit complete statements ending with `;`
  13. * so a following runtime IIFE is not parsed as a call (ASI).
  14. * @returns {boolean} true, if the runtime module should get it's own scope
  15. */
  16. shouldIsolate() {
  17. return false;
  18. }
  19. /**
  20. * @param {PublicPath} publicPath public path
  21. */
  22. constructor(publicPath) {
  23. super("publicPath", RuntimeModule.STAGE_BASIC);
  24. /** @type {PublicPath} */
  25. this.publicPath = publicPath;
  26. }
  27. /**
  28. * Generates runtime code for this runtime module.
  29. * @returns {string | null} runtime code
  30. */
  31. generate() {
  32. const { publicPath } = this;
  33. const compilation = /** @type {Compilation} */ (this.compilation);
  34. return `${RuntimeGlobals.publicPath} = ${JSON.stringify(
  35. compilation.getPath(publicPath || "", {
  36. hash: compilation.hash || "XXXX"
  37. })
  38. )};`;
  39. }
  40. }
  41. module.exports = PublicPathRuntimeModule;