PublicPathRuntimeModule.js 949 B

12345678910111213141516171819202122232425262728293031323334353637
  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. /** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} OutputOptions */
  8. /** @typedef {import("../Compilation")} Compilation */
  9. class PublicPathRuntimeModule extends RuntimeModule {
  10. /**
  11. * @param {OutputOptions["publicPath"]} publicPath public path
  12. */
  13. constructor(publicPath) {
  14. super("publicPath", RuntimeModule.STAGE_BASIC);
  15. this.publicPath = publicPath;
  16. }
  17. /**
  18. * @returns {string | null} runtime code
  19. */
  20. generate() {
  21. const { publicPath } = this;
  22. const compilation = /** @type {Compilation} */ (this.compilation);
  23. return `${RuntimeGlobals.publicPath} = ${JSON.stringify(
  24. compilation.getPath(publicPath || "", {
  25. hash: compilation.hash || "XXXX"
  26. })
  27. )};`;
  28. }
  29. }
  30. module.exports = PublicPathRuntimeModule;