2020-11-17 21:01:56 +00:00
|
|
|
import { Config } from "./config";
|
2020-11-12 22:32:53 +00:00
|
|
|
|
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
|
|
|
*
|
2019-10-15 02:52:53 +00:00
|
|
|
* We have several points of configuration for how the Ruffle public path can
|
|
|
|
* be determined:
|
2020-05-12 22:24:41 +00:00
|
|
|
*
|
2019-10-15 02:52:53 +00:00
|
|
|
* 1. The public path can be specified on a per-source basis using the
|
|
|
|
* RufflePlayer config, for example:
|
2020-03-25 07:54:21 +00:00
|
|
|
* `window.RufflePlayer.config.public_paths.local = "/dist/";`
|
2019-10-15 02:52:53 +00:00
|
|
|
* 2. A global public path can be specified for all sources, also in config.
|
2020-11-20 22:26:22 +00:00
|
|
|
* `window.RufflePlayer.config.publicPath = "/dist/";`
|
2019-10-15 02:52:53 +00:00
|
|
|
* 3. If there is absolutely no configuration that yields a public path then we
|
2020-03-25 07:54:21 +00:00
|
|
|
* return the parent path of where this script is hosted, 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.
|
|
|
|
* @param source_name The name of the source.
|
|
|
|
* @returns The public path for the given source.
|
2019-10-15 02:52:53 +00:00
|
|
|
*/
|
2020-11-17 22:16:55 +00:00
|
|
|
export function publicPath(config: Config, source_name: string): string {
|
2020-11-17 22:16:35 +00:00
|
|
|
let path = "";
|
2020-05-12 22:24:41 +00:00
|
|
|
if (
|
|
|
|
config !== undefined &&
|
|
|
|
config.public_paths !== undefined &&
|
|
|
|
config.public_paths[source_name] !== undefined
|
|
|
|
) {
|
2020-11-17 22:16:35 +00:00
|
|
|
path = config.public_paths[source_name];
|
2020-11-20 22:26:22 +00:00
|
|
|
} else if (config !== undefined && config.publicPath !== undefined) {
|
|
|
|
path = config.publicPath;
|
2020-11-12 22:32:53 +00:00
|
|
|
} else if (
|
|
|
|
document.currentScript !== undefined &&
|
|
|
|
document.currentScript !== null &&
|
|
|
|
"src" in document.currentScript
|
|
|
|
) {
|
2020-03-25 07:54:21 +00:00
|
|
|
// Default to the directory where this script resides.
|
|
|
|
try {
|
2020-11-17 22:16:35 +00:00
|
|
|
path = new URL(".", document.currentScript.src).href;
|
2020-03-25 07:54:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.warn("Unable to get currentScript URL");
|
|
|
|
}
|
2019-10-15 02:52:53 +00:00
|
|
|
}
|
2020-05-12 22:24:41 +00:00
|
|
|
|
2020-03-25 07:54:21 +00:00
|
|
|
// Webpack expects the paths to end with a /.
|
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
|
|
|
}
|