« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/static/js/module-import-shims.js27
-rw-r--r--src/web-routes.js46
2 files changed, 55 insertions, 18 deletions
diff --git a/src/static/js/module-import-shims.js b/src/static/js/module-import-shims.js
new file mode 100644
index 00000000..e7e1e0cc
--- /dev/null
+++ b/src/static/js/module-import-shims.js
@@ -0,0 +1,27 @@
+export const loadDependency = {
+  async fromWindow(modulePath) {
+    globalThis.window = {};
+
+    await import(modulePath);
+
+    const exports = globalThis.window;
+
+    delete globalThis.window;
+
+    return exports;
+  },
+
+  async fromModuleExports(modulePath) {
+    globalThis.exports = {};
+    globalThis.module = {exports: globalThis.exports};
+
+    await import(modulePath);
+
+    const exports = globalThis.exports;
+
+    delete globalThis.module;
+    delete globalThis.exports;
+
+    return exports;
+  },
+};
diff --git a/src/web-routes.js b/src/web-routes.js
index 2ebd6f3e..8bb2fba3 100644
--- a/src/web-routes.js
+++ b/src/web-routes.js
@@ -8,10 +8,10 @@ const codeSrcPath = __dirname;
 const codeRootPath = path.resolve(codeSrcPath, '..');
 
 function getNodeDependencyRootPath(dependencyName) {
-  const packageJSON =
-    import.meta.resolve(dependencyName + '/package.json');
-
-  return path.dirname(fileURLToPath(packageJSON));
+  return (
+    path.dirname(
+      fileURLToPath(
+        import.meta.resolve(dependencyName))));
 }
 
 export const stationaryCodeRoutes = [
@@ -36,23 +36,33 @@ export const stationaryCodeRoutes = [
   },
 ];
 
-export const dependencyRoutes = [
-  {
-    from:
-      path.join(
-        getNodeDependencyRootPath('flexsearch'),
-        'dist'),
+function quickNodeDependency({
+  name,
+  path: subpath = '',
+}) {
+  const root = getNodeDependencyRootPath(name);
 
-    to: ['staticLib.path', 'flexsearch'],
-  },
+  return [
+    {
+      from:
+        (subpath
+          ? path.join(root, subpath)
+          : root),
 
-  {
-    from:
-      getNodeDependencyRootPath('chroma-js'),
+      to: ['staticLib.path', name],
+    },
+  ];
+}
 
-    to: ['staticLib.path', 'chroma-js'],
-  }
-];
+export const dependencyRoutes = [
+  quickNodeDependency({
+    name: 'chroma-js',
+  }),
+
+  quickNodeDependency({
+    name: 'flexsearch',
+  }),
+].flat();
 
 export const allStaticWebRoutes = [
   ...stationaryCodeRoutes,