@cfasim-ui/wasm 0.1.1 → 0.1.3

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": "@cfasim-ui/wasm",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "description": "WebAssembly integration for cfasim-ui",
6
6
  "license": "Apache-2.0",
@@ -13,10 +13,11 @@
13
13
  "src"
14
14
  ],
15
15
  "exports": {
16
- ".": "./src/index.ts"
16
+ ".": "./src/index.ts",
17
+ "./vite": "./src/vitePlugin.ts"
17
18
  },
18
19
  "dependencies": {
19
- "@cfasim-ui/shared": "0.1.1"
20
+ "@cfasim-ui/shared": "0.1.3"
20
21
  },
21
22
  "peerDependencies": {
22
23
  "vue": "^3.5.0"
@@ -0,0 +1,34 @@
1
+ import { execSync } from "node:child_process";
2
+ import { basename, resolve } from "node:path";
3
+ import type { Plugin } from "vite";
4
+
5
+ interface CfasimWasmOptions {
6
+ /** Path to the Rust model directory (default: "model") */
7
+ model?: string;
8
+ /** Output name used for the wasm module (default: project directory name) */
9
+ name?: string;
10
+ }
11
+
12
+ /**
13
+ * Vite plugin that builds a Rust model to WebAssembly via wasm-pack
14
+ * and outputs it to public/wasm/{name}/.
15
+ */
16
+ export function cfasimWasm(options?: CfasimWasmOptions): Plugin {
17
+ const modelDir = options?.model ?? "model";
18
+
19
+ function build(root: string) {
20
+ const name = options?.name ?? basename(root);
21
+ const outDir = resolve(root, "public", "wasm", name);
22
+ execSync(`wasm-pack build ${modelDir} --target web --out-dir ${outDir}`, {
23
+ cwd: root,
24
+ stdio: "pipe",
25
+ });
26
+ }
27
+
28
+ return {
29
+ name: "cfasim-wasm",
30
+ configResolved(config) {
31
+ build(config.root);
32
+ },
33
+ };
34
+ }
@@ -14,12 +14,14 @@ interface WorkerMessage {
14
14
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
15
  const loadedModules = new Map<string, Record<string, any>>();
16
16
 
17
+ const baseUrl = import.meta.env.BASE_URL ?? "/";
18
+
17
19
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
20
  async function ensureModule(model: string): Promise<Record<string, any>> {
19
21
  const cached = loadedModules.get(model);
20
22
  if (cached) return cached;
21
23
 
22
- const url = `${self.location.origin}/wasm/${model}/${model}.js`;
24
+ const url = `${self.location.origin}${baseUrl}wasm/${model}/${model}.js`;
23
25
  const mod = await import(/* @vite-ignore */ url);
24
26
  await mod.default();
25
27
  loadedModules.set(model, mod);