From 3e3768430368d8b724e3b63640b9b790f791da48 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Mon, 29 Jun 2020 10:42:24 +0200
Subject: [PATCH] getAssetURL: Explicitely pass a package name

This way we can change the full path/namespacing at a later point.
---
 packages/common/src/icon.js  |  2 +-
 packages/common/test/unit.js |  3 +++
 packages/common/utils.js     | 14 ++++++++++++--
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/packages/common/src/icon.js b/packages/common/src/icon.js
index 1a4f9dd6..35c047a6 100644
--- a/packages/common/src/icon.js
+++ b/packages/common/src/icon.js
@@ -18,7 +18,7 @@ const errorIcon = `
 `;
 
 export function getIconSVGURL(name) {
-    return commonUtils.getAssetURL('local/vpu-common/icons/' + encodeURI(name) + '.svg');
+    return commonUtils.getAssetURL('vpu-common', 'icons/' + encodeURI(name) + '.svg');
 }
 
 export function getIconCSS(name) {
diff --git a/packages/common/test/unit.js b/packages/common/test/unit.js
index 17d7f7f1..abca165b 100644
--- a/packages/common/test/unit.js
+++ b/packages/common/test/unit.js
@@ -39,7 +39,10 @@ suite('utils', () => {
     });
 
     test('getAssetURL', () => {
+        // Backwards compat
         assert.equal(new URL(utils.getAssetURL("foo/bar")).pathname, "/foo/bar");
+        // Normal usage
+        assert.equal(new URL(utils.getAssetURL('foobar', 'bar/quux')).pathname, "/local/foobar/bar/quux");
     });
 
     test('getThemeCSS', () => {
diff --git a/packages/common/utils.js b/packages/common/utils.js
index 695ebbeb..af8d2fd4 100644
--- a/packages/common/utils.js
+++ b/packages/common/utils.js
@@ -199,12 +199,22 @@ export const dateToInputTimeString = (date) => {
 /**
  * Get an absolute path for assets given a relative path to the js bundle.
  *
+ * @param {string} pkg The package which provides the asset
  * @param {string} path The relative path based on the js bundle path
  */
-export const getAssetURL = (path) => {
-    return new URL(path, new URL('..', import.meta.url).href).href;
+export const getAssetURL = (pkg, path) => {
+    let fullPath = '';
+    if (path === undefined) {
+        // backwards compat: in case only one parameter is passed
+        // assume it is a full path
+        fullPath = pkg;
+    } else {
+        fullPath = 'local/' + pkg + '/' + path
+    }
+    return new URL(fullPath, new URL('..', import.meta.url).href).href;
 };
 
+
 /**
  * Poll <fn> every <interval> ms until <timeout> ms
  *
-- 
GitLab