webpack/lib/runtime/PublicPathRuntimeModule.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
*/
"use strict";
const RuntimeGlobals = require("../RuntimeGlobals");
const RuntimeModule = require("../RuntimeModule");
2018-12-18 05:05:08 +08:00
/** @typedef {import("../Compilation")} Compilation */
class PublicPathRuntimeModule extends RuntimeModule {
constructor() {
super("publicPath");
}
/**
* @returns {string} runtime code
*/
generate() {
const publicPath = this.compilation.outputOptions.publicPath || "auto";
if (publicPath === "auto") {
return `${RuntimeGlobals.publicPath} = (() => {
2020-08-06 02:17:33 +08:00
if (typeof window !== "undefined" && "currentScript" in window.document) {
2020-08-03 11:49:48 +08:00
return window.document.currentScript.src.replace(/[^\\/]+$/, "");
} else {
2020-08-18 08:13:17 +08:00
console.error("Webpack: Auto public path is not supported in modules or when 'document.currentScript' is unavailable. Use a polyfill or set 'publicPath' config explicitly.");
2020-08-19 00:37:22 +08:00
return ${JSON.stringify(
this.compilation.getPath(publicPath || "", {
hash: this.compilation.hash || "XXXX"
})
)};
}
})();`;
} else {
return `${RuntimeGlobals.publicPath} = ${JSON.stringify(
this.compilation.getPath(publicPath || "", {
hash: this.compilation.hash || "XXXX"
})
)};`;
}
}
}
module.exports = PublicPathRuntimeModule;