2018-11-20 19:05:12 +08:00
/ *
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 */
2018-11-20 19:05:12 +08:00
class PublicPathRuntimeModule extends RuntimeModule {
2019-08-27 02:21:07 +08:00
constructor ( ) {
2018-11-20 19:05:12 +08:00
super ( "publicPath" ) ;
}
/ * *
* @ returns { string } runtime code
* /
generate ( ) {
2020-07-28 11:18:05 +08:00
const publicPath = this . compilation . outputOptions . publicPath || "auto" ;
if ( publicPath === "auto" ) {
return ` ${ RuntimeGlobals . publicPath } = (() => {
2020-08-06 01:58:17 +08:00
if ( window . document && "currentScript" in window . document ) {
2020-08-03 11:49:48 +08:00
return window . document . currentScript . src . replace ( /[^\\/]+$/ , "" ) ;
2020-07-28 11:18:05 +08:00
} else {
2020-08-03 11:54:17 +08:00
throw new Error ( "Webpack: Auto public path is not supported in modules or when 'document.currentScript' is unavailable. Use a polyfill or set 'publicPath' config explicitly." ) ;
2018-11-20 19:05:12 +08:00
}
2020-07-28 11:18:05 +08:00
} ) ( ) ; ` ;
} else {
return ` ${ RuntimeGlobals . publicPath } = ${ JSON . stringify (
this . compilation . getPath ( publicPath || "" , {
hash : this . compilation . hash || "XXXX"
} )
) } ; ` ;
}
2018-11-20 19:05:12 +08:00
}
}
module . exports = PublicPathRuntimeModule ;