From 39352135c8c767f07d4bab650d8523769e19bdc3 Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Thu, 30 Apr 2020 15:36:44 +0200 Subject: [PATCH] Drop initAssetBaseURL() We no longer need to support old edge, so we can just use import.meta.url everywhere which doesn't any initialization. --- packages/common/assets/index.html | 2 +- packages/common/test/unit.js | 1 - packages/common/utils.js | 47 ++---------------------------- packages/common/vpu-common-demo.js | 1 - 4 files changed, 3 insertions(+), 48 deletions(-) diff --git a/packages/common/assets/index.html b/packages/common/assets/index.html index bae2d4ad..9469485c 100644 --- a/packages/common/assets/index.html +++ b/packages/common/assets/index.html @@ -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; diff --git a/packages/common/test/unit.js b/packages/common/test/unit.js index 0f0d92e1..98b40049 100644 --- a/packages/common/test/unit.js +++ b/packages/common/test/unit.js @@ -43,7 +43,6 @@ suite('utils', () => { }); test('getAssetURL', () => { - utils.initAssetBaseURL(); assert.equal(new URL(utils.getAssetURL("foo/bar")).pathname, "/foo/bar"); }); diff --git a/packages/common/utils.js b/packages/common/utils.js index a0d3c469..ed241a6d 100644 --- a/packages/common/utils.js +++ b/packages/common/utils.js @@ -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; }; /** diff --git a/packages/common/vpu-common-demo.js b/packages/common/vpu-common-demo.js index 323acc88..9b5e1825 100644 --- a/packages/common/vpu-common-demo.js +++ b/packages/common/vpu-common-demo.js @@ -92,7 +92,6 @@ class VpuCommonDemo extends ScopedElementsMixin(LitElement) { } render() { - commonUtils.initAssetBaseURL('vpu-common-demo-src'); return html` <style> a:after { -- GitLab