@wasmedia/wenblend 0.2.0 → 0.2.1
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/dist/loader.d.ts.map +1 -1
- package/dist/loader.js +47 -9
- package/dist/loader.js.map +1 -1
- package/dist/wenblend.wasm +0 -0
- package/dist/wenfuse.wasm +0 -0
- package/package.json +2 -1
- package/src/loader.ts +51 -10
package/dist/loader.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE5D,MAAM,MAAM,IAAI,GAAG,SAAS,GAAG,QAAQ,CAAC;AAExC;;;;;;;;;GASG;AACH,MAAM,MAAM,aAAa,GAAG,IAAI,CAAC,UAAU,EAAE,OAAO,GAAG,UAAU,CAAC,CAAC;AAEnE,gFAAgF;AAChF,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC;AACtF,KAAK,aAAa,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,aAAa,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE5D,MAAM,MAAM,IAAI,GAAG,SAAS,GAAG,QAAQ,CAAC;AAExC;;;;;;;;;GASG;AACH,MAAM,MAAM,aAAa,GAAG,IAAI,CAAC,UAAU,EAAE,OAAO,GAAG,UAAU,CAAC,CAAC;AAEnE,gFAAgF;AAChF,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC;AACtF,KAAK,aAAa,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,aAAa,CAAC,CAAC;AAyKrF,6CAA6C;AAC7C,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,aAAa,GAAG,IAAI,CAE1D;AAED,0DAA0D;AAC1D,wBAAgB,iBAAiB,CAC/B,EAAE,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,GACvE,IAAI,CAEN;AAED,6DAA6D;AAC7D,wBAAgB,iBAAiB,IAAI,IAAI,CAKxC;AAED;;;;;;;;;GASG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAW7F;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,cAAc,CAClC,IAAI,EAAE,IAAI,EACV,OAAO,CAAC,EAAE,aAAa,EACvB,KAAK,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,GAC7B,OAAO,CAAC,UAAU,CAAC,CAkBrB"}
|
package/dist/loader.js
CHANGED
|
@@ -20,10 +20,6 @@
|
|
|
20
20
|
* Without the hook, each factory call runs the glue's own createWasm,
|
|
21
21
|
* which fetches and compiles the .wasm from scratch every time.
|
|
22
22
|
*/
|
|
23
|
-
const MODULE_FILE = {
|
|
24
|
-
enblend: "wenblend.js",
|
|
25
|
-
enfuse: "wenfuse.js",
|
|
26
|
-
};
|
|
27
23
|
const WASM_FILE = {
|
|
28
24
|
enblend: "wenblend.wasm",
|
|
29
25
|
enfuse: "wenfuse.wasm",
|
|
@@ -34,10 +30,34 @@ const WASM_FILE = {
|
|
|
34
30
|
// to the same key.
|
|
35
31
|
const factories = new Map();
|
|
36
32
|
const compiled = new Map();
|
|
33
|
+
/*
|
|
34
|
+
* The default URLs are spelled out, one `new URL()` with a string literal
|
|
35
|
+
* per file, never built from a template: bundlers (Vite, webpack, Parcel)
|
|
36
|
+
* recognise the literal form and emit exactly that file, with a hashed name,
|
|
37
|
+
* while Vite turns a template literal into a glob over the whole directory
|
|
38
|
+
* — every glue, wasm, source map and .d.ts in dist/ — and under its dev
|
|
39
|
+
* server's dependency pre-bundling into a lookup that yields `undefined`
|
|
40
|
+
* (#24). `../dist/` resolves to dist/ both from the compiled dist/loader.js
|
|
41
|
+
* and from src/loader.ts under the test runner.
|
|
42
|
+
*
|
|
43
|
+
* The wasm gets its own literal for the same reason: a bundler that emits
|
|
44
|
+
* the glue as `wenblend-<hash>.js` does not put a `wenblend.wasm` beside it,
|
|
45
|
+
* so with no `wasmPath` the wasm is not looked up beside the glue.
|
|
46
|
+
*/
|
|
47
|
+
function defaultGlueUrl(tool) {
|
|
48
|
+
return tool === "enblend"
|
|
49
|
+
? new URL("../dist/wenblend.js", import.meta.url).href
|
|
50
|
+
: new URL("../dist/wenfuse.js", import.meta.url).href;
|
|
51
|
+
}
|
|
52
|
+
function defaultWasmUrl(tool) {
|
|
53
|
+
return tool === "enblend"
|
|
54
|
+
? new URL("../dist/wenblend.wasm", import.meta.url)
|
|
55
|
+
: new URL("../dist/wenfuse.wasm", import.meta.url);
|
|
56
|
+
}
|
|
37
57
|
function glueUrl(tool, options) {
|
|
38
58
|
const path = options?.wasmPath;
|
|
39
59
|
if (path === undefined)
|
|
40
|
-
return
|
|
60
|
+
return defaultGlueUrl(tool);
|
|
41
61
|
return isBareSpecifier(path) ? resolveBareSpecifier(path) : path;
|
|
42
62
|
}
|
|
43
63
|
/**
|
|
@@ -74,22 +94,40 @@ function resolvedGlueUrl(tool, options) {
|
|
|
74
94
|
function cacheKey(tool, options) {
|
|
75
95
|
return `${tool} ${resolvedGlueUrl(tool, options).href}`;
|
|
76
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Import a tool's glue. When the default URL fails, the usual cause is a
|
|
99
|
+
* bundler that moved this module away from dist/ without rewriting the URL
|
|
100
|
+
* — Vite's dev server pre-bundles dependencies into node_modules/.vite/deps/,
|
|
101
|
+
* from where no relative URL reaches dist/ — so the error says what to do.
|
|
102
|
+
*/
|
|
77
103
|
async function importFactory(tool, options) {
|
|
78
|
-
const
|
|
104
|
+
const url = glueUrl(tool, options);
|
|
105
|
+
let mod;
|
|
106
|
+
try {
|
|
107
|
+
mod = await import(/* webpackIgnore: true */ /* @vite-ignore */ url);
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
if (options?.wasmPath !== undefined)
|
|
111
|
+
throw err;
|
|
112
|
+
throw new Error(`could not import the ${tool} glue from its default URL ${url}. If a bundler moved wenblend away from ` +
|
|
113
|
+
`its dist/ directory (Vite's dev server does when it pre-bundles dependencies), add "@wasmedia/wenblend" ` +
|
|
114
|
+
`to Vite's optimizeDeps.exclude, or pass wasmPath. Cause: ${err instanceof Error ? err.message : String(err)}`, { cause: err });
|
|
115
|
+
}
|
|
79
116
|
return (mod.default ?? mod);
|
|
80
117
|
}
|
|
81
118
|
/**
|
|
82
119
|
* Fetch and compile a tool's .wasm.
|
|
83
120
|
*
|
|
84
|
-
*
|
|
85
|
-
* resolves the .wasm name against the glue's own
|
|
121
|
+
* With `wasmPath`, the URL is the one the glue itself would use: its
|
|
122
|
+
* `locateFile` default resolves the .wasm name against the glue's own
|
|
123
|
+
* directory. Without it, the default wasm URL (see `defaultWasmUrl`). `file:` URLs
|
|
86
124
|
* (Node) are read from disk, because Node's fetch does not do `file:`;
|
|
87
125
|
* everything else is fetched, streaming-compiled where the server's MIME
|
|
88
126
|
* type allows and compiled from bytes where it does not — the same
|
|
89
127
|
* fallback the glue performs.
|
|
90
128
|
*/
|
|
91
129
|
async function compileWasm(tool, options) {
|
|
92
|
-
const url = new URL(WASM_FILE[tool], resolvedGlueUrl(tool, options));
|
|
130
|
+
const url = options?.wasmPath === undefined ? defaultWasmUrl(tool) : new URL(WASM_FILE[tool], resolvedGlueUrl(tool, options));
|
|
93
131
|
if (url.protocol === "file:") {
|
|
94
132
|
// A variable specifier keeps bundlers from trying to resolve a Node
|
|
95
133
|
// builtin into browser code; this branch only runs under Node.
|
package/dist/loader.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAsBH,MAAM,
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAsBH,MAAM,SAAS,GAAyB;IACtC,OAAO,EAAE,eAAe;IACxB,MAAM,EAAE,cAAc;CACvB,CAAC;AAEF,2EAA2E;AAC3E,yEAAyE;AACzE,6EAA6E;AAC7E,mBAAmB;AACnB,MAAM,SAAS,GAAG,IAAI,GAAG,EAAkC,CAAC;AAC5D,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAuC,CAAC;AAEhE;;;;;;;;;;;;;GAaG;AACH,SAAS,cAAc,CAAC,IAAU;IAChC,OAAO,IAAI,KAAK,SAAS;QACvB,CAAC,CAAC,IAAI,GAAG,CAAC,qBAAqB,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI;QACtD,CAAC,CAAC,IAAI,GAAG,CAAC,oBAAoB,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;AAC1D,CAAC;AAED,SAAS,cAAc,CAAC,IAAU;IAChC,OAAO,IAAI,KAAK,SAAS;QACvB,CAAC,CAAC,IAAI,GAAG,CAAC,uBAAuB,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;QACnD,CAA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IACjC,MAAM,OAAO,GAAG,MAAM,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACjD,OAAO,EAAE,UAAU,EAAE,CAAC,cAAc,CAAC,CAAC;IACtC,IAAI,IAA6B,CAAC;IAClC,MAAM,MAAM,GAAG,IAAI,OAAO,CAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,GAAG,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IACrE,MAAM,eAAe,GAAG,CACtB,OAA4B,EAC5B,eAAqF,EAC9D,EAAE;QACzB,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC;aACxB,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,CAAC,eAAe,CAAC,MAAM,WAAW,CAAC,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,CAAC;aAC/F,KAAK,CAAC,IAAI,CAAC,CAAC;QACf,OAAO,EAAE,CAAC;IACZ,CAAC,CAAC;IACF,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,EAAE,GAAG,KAAK,EAAE,eAAe,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;IACtF,OAAO,EAAE,UAAU,EAAE,CAAC,OAAO,CAAC,CAAC;IAC/B,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
package/dist/wenblend.wasm
CHANGED
|
Binary file
|
package/dist/wenfuse.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wasmedia/wenblend",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Enblend and Enfuse compiled to WebAssembly for browsers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
"@playwright/test": "1.63.0",
|
|
60
60
|
"@types/node": "^22.20.4",
|
|
61
61
|
"typescript": "^5.7.0",
|
|
62
|
+
"vite": "7.3.6",
|
|
62
63
|
"vitest": "^3.0.0"
|
|
63
64
|
},
|
|
64
65
|
"dependencies": {
|
package/src/loader.ts
CHANGED
|
@@ -41,11 +41,6 @@ export type InstanceHooks = Pick<WasmModule, "print" | "printErr">;
|
|
|
41
41
|
export type ModuleFactory = (config?: Record<string, unknown>) => Promise<WasmModule>;
|
|
42
42
|
type FactoryLoader = (tool: Tool, options?: LoaderOptions) => Promise<ModuleFactory>;
|
|
43
43
|
|
|
44
|
-
const MODULE_FILE: Record<Tool, string> = {
|
|
45
|
-
enblend: "wenblend.js",
|
|
46
|
-
enfuse: "wenfuse.js",
|
|
47
|
-
};
|
|
48
|
-
|
|
49
44
|
const WASM_FILE: Record<Tool, string> = {
|
|
50
45
|
enblend: "wenblend.wasm",
|
|
51
46
|
enfuse: "wenfuse.wasm",
|
|
@@ -58,9 +53,35 @@ const WASM_FILE: Record<Tool, string> = {
|
|
|
58
53
|
const factories = new Map<string, Promise<ModuleFactory>>();
|
|
59
54
|
const compiled = new Map<string, Promise<WebAssembly.Module>>();
|
|
60
55
|
|
|
56
|
+
/*
|
|
57
|
+
* The default URLs are spelled out, one `new URL()` with a string literal
|
|
58
|
+
* per file, never built from a template: bundlers (Vite, webpack, Parcel)
|
|
59
|
+
* recognise the literal form and emit exactly that file, with a hashed name,
|
|
60
|
+
* while Vite turns a template literal into a glob over the whole directory
|
|
61
|
+
* — every glue, wasm, source map and .d.ts in dist/ — and under its dev
|
|
62
|
+
* server's dependency pre-bundling into a lookup that yields `undefined`
|
|
63
|
+
* (#24). `../dist/` resolves to dist/ both from the compiled dist/loader.js
|
|
64
|
+
* and from src/loader.ts under the test runner.
|
|
65
|
+
*
|
|
66
|
+
* The wasm gets its own literal for the same reason: a bundler that emits
|
|
67
|
+
* the glue as `wenblend-<hash>.js` does not put a `wenblend.wasm` beside it,
|
|
68
|
+
* so with no `wasmPath` the wasm is not looked up beside the glue.
|
|
69
|
+
*/
|
|
70
|
+
function defaultGlueUrl(tool: Tool): string {
|
|
71
|
+
return tool === "enblend"
|
|
72
|
+
? new URL("../dist/wenblend.js", import.meta.url).href
|
|
73
|
+
: new URL("../dist/wenfuse.js", import.meta.url).href;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function defaultWasmUrl(tool: Tool): URL {
|
|
77
|
+
return tool === "enblend"
|
|
78
|
+
? new URL("../dist/wenblend.wasm", import.meta.url)
|
|
79
|
+
: new URL("../dist/wenfuse.wasm", import.meta.url);
|
|
80
|
+
}
|
|
81
|
+
|
|
61
82
|
function glueUrl(tool: Tool, options?: LoaderOptions): string {
|
|
62
83
|
const path = options?.wasmPath;
|
|
63
|
-
if (path === undefined) return
|
|
84
|
+
if (path === undefined) return defaultGlueUrl(tool);
|
|
64
85
|
return isBareSpecifier(path) ? resolveBareSpecifier(path) : path;
|
|
65
86
|
}
|
|
66
87
|
|
|
@@ -104,23 +125,43 @@ function cacheKey(tool: Tool, options?: LoaderOptions): string {
|
|
|
104
125
|
return `${tool} ${resolvedGlueUrl(tool, options).href}`;
|
|
105
126
|
}
|
|
106
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Import a tool's glue. When the default URL fails, the usual cause is a
|
|
130
|
+
* bundler that moved this module away from dist/ without rewriting the URL
|
|
131
|
+
* — Vite's dev server pre-bundles dependencies into node_modules/.vite/deps/,
|
|
132
|
+
* from where no relative URL reaches dist/ — so the error says what to do.
|
|
133
|
+
*/
|
|
107
134
|
async function importFactory(tool: Tool, options?: LoaderOptions): Promise<ModuleFactory> {
|
|
108
|
-
const
|
|
135
|
+
const url = glueUrl(tool, options);
|
|
136
|
+
let mod;
|
|
137
|
+
try {
|
|
138
|
+
mod = await import(/* webpackIgnore: true */ /* @vite-ignore */ url);
|
|
139
|
+
} catch (err) {
|
|
140
|
+
if (options?.wasmPath !== undefined) throw err;
|
|
141
|
+
throw new Error(
|
|
142
|
+
`could not import the ${tool} glue from its default URL ${url}. If a bundler moved wenblend away from ` +
|
|
143
|
+
`its dist/ directory (Vite's dev server does when it pre-bundles dependencies), add "@wasmedia/wenblend" ` +
|
|
144
|
+
`to Vite's optimizeDeps.exclude, or pass wasmPath. Cause: ${err instanceof Error ? err.message : String(err)}`,
|
|
145
|
+
{ cause: err },
|
|
146
|
+
);
|
|
147
|
+
}
|
|
109
148
|
return (mod.default ?? mod) as ModuleFactory;
|
|
110
149
|
}
|
|
111
150
|
|
|
112
151
|
/**
|
|
113
152
|
* Fetch and compile a tool's .wasm.
|
|
114
153
|
*
|
|
115
|
-
*
|
|
116
|
-
* resolves the .wasm name against the glue's own
|
|
154
|
+
* With `wasmPath`, the URL is the one the glue itself would use: its
|
|
155
|
+
* `locateFile` default resolves the .wasm name against the glue's own
|
|
156
|
+
* directory. Without it, the default wasm URL (see `defaultWasmUrl`). `file:` URLs
|
|
117
157
|
* (Node) are read from disk, because Node's fetch does not do `file:`;
|
|
118
158
|
* everything else is fetched, streaming-compiled where the server's MIME
|
|
119
159
|
* type allows and compiled from bytes where it does not — the same
|
|
120
160
|
* fallback the glue performs.
|
|
121
161
|
*/
|
|
122
162
|
async function compileWasm(tool: Tool, options?: LoaderOptions): Promise<WebAssembly.Module> {
|
|
123
|
-
const url =
|
|
163
|
+
const url =
|
|
164
|
+
options?.wasmPath === undefined ? defaultWasmUrl(tool) : new URL(WASM_FILE[tool], resolvedGlueUrl(tool, options));
|
|
124
165
|
if (url.protocol === "file:") {
|
|
125
166
|
// A variable specifier keeps bundlers from trying to resolve a Node
|
|
126
167
|
// builtin into browser code; this branch only runs under Node.
|