PublicPathRuntimeModule.js 951 B

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