@abi-software/simulationvuer 4.0.1 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abi-software/simulationvuer",
3
- "version": "4.0.1",
3
+ "version": "4.0.2",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "serve": "vite serve --host --force",
package/vite.config.js CHANGED
@@ -9,6 +9,10 @@ import { defineConfig } from "vite";
9
9
 
10
10
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
11
11
  const pathSrc = path.resolve(__dirname, "./src");
12
+ const libopencorDir = path.resolve(
13
+ __dirname,
14
+ "node_modules/@opencor/opencor/dist/libopencor",
15
+ );
12
16
 
13
17
  export default defineConfig(({ command, mode }) => {
14
18
  const config = {
@@ -27,42 +31,101 @@ export default defineConfig(({ command, mode }) => {
27
31
  dts: "src/components.d.ts",
28
32
  }),
29
33
 
30
- // Serve the local libopencor/ directory from @opencor/opencor so that the dynamic import in opencor.es.js (new
31
- // URL("./libopencor/wasm/...", window.location.href)) resolves same-origin. libopencor.js itself is bundled in
32
- // the package while the threaded WASM is loaded from opencor.ws via locateFile.
34
+ // Resolve the libopencor.js import URL in @opencor/opencor's opencor.es.js so that it works for all consumers
35
+ // (not just Vite projects) and this without server configuration.
36
+ //
37
+ // In @opencor/opencor, the code uses:
38
+ // new URL("./libopencor/wasm/<version>/libopencor.js", window.location.href).href
39
+ //
40
+ // This resolves relative to the page URL, requiring the server to host libopencor.js at a page-relative path
41
+ // (which doesn't work for library consumers or static builds).
42
+ //
43
+ // We fix this by:
44
+ // - Dev (serve): rewriting to "/libopencor/libopencor.js", served by the middleware below.
45
+ // - Build (lib): rewriting to use import.meta.url, so the import resolves relative to simulationvuer.js's URL
46
+ // (same directory, libopencor.js is copied alongside it via closeBundle).
33
47
 
34
48
  {
35
- name: "opencor-serve-local-libopencor",
49
+ name: "opencor-libopencor",
50
+ transform(code, id) {
51
+ if (id.includes("@opencor/opencor") && id.endsWith("opencor.es.js")) {
52
+ const importRegex =
53
+ /(\/\*\s*@vite-ignore\s*\*\/[\s\n]*)(new URL\("(\.\/libopencor\/wasm\/[^/]+\/libopencor\.js)",\s*window\.location\.href\)\.href)/g;
54
+ const res = code.replace(
55
+ importRegex,
56
+ (_match, prefix, _urlExpr, urlPath) => {
57
+ if (command === "serve") {
58
+ // Dev mode: absolute path served by the middleware below.
59
+
60
+ return `${prefix}"/libopencor/libopencor.js"`;
61
+ }
62
+
63
+ // Build mode: resolve relative to the bundle's own URL.
64
+ // Note: we use replace() instead of new URL(..., import.meta.url) to avoid Vite's asset handler, which
65
+ // would inline the file as a data: URL (which breaks Emscripten's pthread Worker creation). At
66
+ // runtime, import.meta.url points to simulationvuer.js and the derived path correctly reaches
67
+ // libopencor in the same directory (copied alongside by closeBundle).
68
+
69
+ return `${prefix}import.meta.url.replace(/[^/]*$/, "") + ${JSON.stringify(urlPath.substring(2))}`;
70
+ },
71
+ );
72
+
73
+ if (res !== code) {
74
+ return {
75
+ code: res,
76
+ map: null,
77
+ };
78
+ }
79
+ }
80
+ },
36
81
  configureServer(server) {
37
- const libopencorDir = path.resolve(
38
- __dirname,
39
- "node_modules/@opencor/opencor/dist/libopencor",
40
- );
82
+ // Serve libopencor files from @opencor/opencor during development so the dynamic import in opencor.es.js
83
+ // resolves same-origin (required for pthread Workers).
41
84
 
42
85
  server.middlewares.use((req, res, next) => {
43
86
  const urlPath = req.url;
44
87
 
45
- if (urlPath.startsWith("/libopencor/")) {
46
- const filePath = path.join(
47
- libopencorDir,
48
- urlPath.replace("/libopencor/", ""),
49
- );
50
-
51
- if (fs.existsSync(filePath)) {
52
- res.writeHead(200, {
53
- "Content-Type": "application/javascript",
54
- "Cross-Origin-Embedder-Policy": "require-corp",
55
- "Cross-Origin-Resource-Policy": "same-origin",
56
- });
57
- res.end(fs.readFileSync(filePath));
58
-
59
- return;
60
- }
88
+ if (!urlPath.startsWith("/libopencor/")) {
89
+ next();
90
+
91
+ return;
61
92
  }
62
93
 
63
- next();
94
+ const filePath = path.join(
95
+ libopencorDir,
96
+ urlPath.replace("/libopencor/", ""),
97
+ );
98
+
99
+ if (!fs.existsSync(filePath)) {
100
+ next();
101
+
102
+ return;
103
+ }
104
+
105
+ res.writeHead(200, {
106
+ "Content-Type": "application/javascript",
107
+ "Cross-Origin-Embedder-Policy": "require-corp",
108
+ "Cross-Origin-Resource-Policy": "same-origin",
109
+ });
110
+ res.end(fs.readFileSync(filePath));
64
111
  });
65
112
  },
113
+ closeBundle() {
114
+ // Copy libopencor alongside the built JavaScript file so that the dynamic import (using import.meta.url as
115
+ // base) resolves correctly. App mode puts the built JavaScript file in dist/assets while lib mode puts it in
116
+ // dist.
117
+
118
+ if (!fs.existsSync(libopencorDir)) {
119
+ return;
120
+ }
121
+
122
+ const distDir = path.resolve(__dirname, "dist");
123
+ const targetDir = fs.existsSync(path.join(distDir, "assets"))
124
+ ? path.join(distDir, "assets", "libopencor")
125
+ : path.join(distDir, "libopencor");
126
+
127
+ fs.cpSync(libopencorDir, targetDir, { recursive: true, force: true });
128
+ },
66
129
  },
67
130
  ],
68
131
  base: mode === "app" ? "./" : "/",