Skip to content
Snippets Groups Projects
Commit 39352135 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Drop initAssetBaseURL()

We no longer need to support old edge, so we can just use
import.meta.url everywhere which doesn't any initialization.
parent feb455f7
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8">
<script type="module" id="vpu-common-demo-src" src="demo.js"></script>
<script type="module" src="demo.js"></script>
<style>
body {
font-family: sans;
......
......@@ -43,7 +43,6 @@ suite('utils', () => {
});
test('getAssetURL', () => {
utils.initAssetBaseURL();
assert.equal(new URL(utils.getAssetURL("foo/bar")).pathname, "/foo/bar");
});
......
......@@ -189,60 +189,17 @@ export const dateToInputTimeString = (date) => {
return `${pad10(date.getHours())}:${pad10(date.getMinutes())}`;
};
let _assetBaseURL = null;
/**
* Set the base url for future calls to getAssetURL()
*
* @param {string} [id] An optional id of the script tag to figure out the
* base bundle URL.
*/
export const initAssetBaseURL = (id) => {
// We don't want future calls to change things
if (_assetBaseURL)
return;
if (id) {
// Find the id added to the script tag
const elm = document.getElementById(id);
if (elm && elm.src) {
_assetBaseURL = elm.src;
}
}
// In the (unlikely) event that we are bundled as a non-module
// we can use the old currentScript API
if (document.currentScript && document.currentScript.src) {
_assetBaseURL = document.currentScript.src;
}
// XXX: Can't be parsed in (old) edge, but makes everything so much easier...
if (!_assetBaseURL)
_assetBaseURL = new URL('..', import.meta.url).href;
// No longer needed, remove your call
};
/**
* Get an absolute path for assets given a relative path to the js bundle.
* Call initAssetBaseURL() before this.
*
* @param {string} path The relative path based on the js bundle path
*/
export const getAssetURL = (path) => {
// Maybe initScriptURL() can do something without the id
if (!_assetBaseURL) {
initAssetBaseURL();
}
// We already found the path before, just go with it
if (_assetBaseURL) {
return new URL(path, _assetBaseURL).href;
} else {
console.error('Using getAssetURL(), but initAssetBaseURL() wasn\'t called yet');
}
// If all fails we just fall back to relative paths and hope the
// html is on the same path as the bundle
return path;
return new URL(path, new URL('..', import.meta.url).href).href;
};
/**
......
......@@ -92,7 +92,6 @@ class VpuCommonDemo extends ScopedElementsMixin(LitElement) {
}
render() {
commonUtils.initAssetBaseURL('vpu-common-demo-src');
return html`
<style>
a:after {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment