2023-03-14 16:16:02 +00:00
|
|
|
import type { DataLoadOptions, URLLoadOptions } from "./load-options";
|
2020-11-12 22:32:53 +00:00
|
|
|
|
2021-08-07 15:57:06 +00:00
|
|
|
// This must be in global scope because `document.currentScript`
|
|
|
|
// works only while the script is initially being processed.
|
|
|
|
let currentScriptURL = "";
|
|
|
|
try {
|
|
|
|
if (
|
|
|
|
document.currentScript !== undefined &&
|
|
|
|
document.currentScript !== null &&
|
|
|
|
"src" in document.currentScript &&
|
|
|
|
document.currentScript.src !== ""
|
|
|
|
) {
|
2022-01-15 22:03:01 +00:00
|
|
|
let src = document.currentScript.src;
|
|
|
|
|
|
|
|
// CDNs allow omitting the filename. If it's omitted, append a slash to
|
|
|
|
// prevent the last component from being dropped.
|
|
|
|
if (!src.endsWith(".js") && !src.endsWith("/")) {
|
|
|
|
src += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
currentScriptURL = new URL(".", src).href;
|
2021-08-07 15:57:06 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.warn("Unable to get currentScript URL");
|
|
|
|
}
|
|
|
|
|
2019-10-15 02:52:53 +00:00
|
|
|
/**
|
|
|
|
* Attempt to discover the public path of the current Ruffle source. This can
|
|
|
|
* be used to configure Webpack.
|
2020-05-12 22:24:41 +00:00
|
|
|
*
|
2021-08-06 14:51:10 +00:00
|
|
|
* A global public path can be specified for all sources using the RufflePlayer
|
|
|
|
* config:
|
2020-05-12 22:24:41 +00:00
|
|
|
*
|
2021-08-06 14:51:10 +00:00
|
|
|
* ```js
|
|
|
|
* window.RufflePlayer.config.publicPath = "/dist/";
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* If no such config is specified, then the parent path of where this script is
|
|
|
|
* hosted is assumed, which should be the correct default in most cases.
|
2020-05-12 22:24:41 +00:00
|
|
|
*
|
2020-11-17 19:59:42 +00:00
|
|
|
* @param config The `window.RufflePlayer.config` object.
|
|
|
|
* @returns The public path for the given source.
|
2019-10-15 02:52:53 +00:00
|
|
|
*/
|
2023-03-14 16:16:02 +00:00
|
|
|
export function publicPath(
|
|
|
|
config: URLLoadOptions | DataLoadOptions | object
|
|
|
|
): string {
|
2021-08-07 15:57:06 +00:00
|
|
|
// Default to the directory where this script resides.
|
|
|
|
let path = currentScriptURL;
|
2023-03-14 16:16:02 +00:00
|
|
|
if (
|
|
|
|
"publicPath" in config &&
|
|
|
|
config.publicPath !== null &&
|
|
|
|
config.publicPath !== undefined
|
|
|
|
) {
|
2020-11-20 22:26:22 +00:00
|
|
|
path = config.publicPath;
|
2019-10-15 02:52:53 +00:00
|
|
|
}
|
2020-05-12 22:24:41 +00:00
|
|
|
|
2021-08-06 14:51:10 +00:00
|
|
|
// Webpack expects the paths to end with a slash.
|
2020-11-17 22:16:35 +00:00
|
|
|
if (path !== "" && !path.endsWith("/")) {
|
|
|
|
path += "/";
|
2020-03-25 07:54:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 22:16:35 +00:00
|
|
|
return path;
|
2020-11-12 22:32:53 +00:00
|
|
|
}
|