unwasm 0.3.9 → 0.3.11
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/README.md +6 -3
- package/dist/plugin.cjs +101 -70
- package/dist/plugin.d.cts +5 -2
- package/dist/plugin.d.mts +3 -2
- package/dist/plugin.d.ts +5 -2
- package/dist/plugin.mjs +101 -70
- package/dist/tools.cjs +392 -335
- package/dist/tools.d.cts +2 -1
- package/dist/tools.d.mts +2 -1
- package/dist/tools.d.ts +2 -1
- package/dist/tools.mjs +392 -335
- package/package.json +35 -33
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
<!-- automd:badges color=yellow codecov -->
|
|
4
4
|
|
|
5
5
|
[](https://npmjs.com/package/unwasm)
|
|
6
|
-
[](https://
|
|
6
|
+
[](https://npm.chart.dev/unwasm)
|
|
7
7
|
[](https://codecov.io/gh/unjs/unwasm)
|
|
8
8
|
|
|
9
9
|
<!-- /automd -->
|
|
@@ -100,6 +100,9 @@ pnpm install unwasm
|
|
|
100
100
|
|
|
101
101
|
# bun
|
|
102
102
|
bun install unwasm
|
|
103
|
+
|
|
104
|
+
# deno
|
|
105
|
+
deno install unwasm
|
|
103
106
|
```
|
|
104
107
|
|
|
105
108
|
<!-- /automd -->
|
|
@@ -140,7 +143,7 @@ Parses `wasm` binary format with useful information using [webassemblyjs/wasm-pa
|
|
|
140
143
|
import { readFile } from "node:fs/promises";
|
|
141
144
|
import { parseWasm } from "unwasm/tools";
|
|
142
145
|
|
|
143
|
-
const source = await readFile(new URL("
|
|
146
|
+
const source = await readFile(new URL("examples/sum.wasm", import.meta.url));
|
|
144
147
|
const parsed = parseWasm(source);
|
|
145
148
|
console.log(JSON.stringify(parsed, undefined, 2));
|
|
146
149
|
```
|
|
@@ -184,7 +187,7 @@ To hint to the bundler how to resolve imports needed by the `.wasm` file, you ne
|
|
|
184
187
|
|
|
185
188
|
**Example:**
|
|
186
189
|
|
|
187
|
-
```
|
|
190
|
+
```json
|
|
188
191
|
{
|
|
189
192
|
"exports": {
|
|
190
193
|
"./rand.wasm": "./rand.wasm"
|
package/dist/plugin.cjs
CHANGED
|
@@ -7,7 +7,6 @@ const pathe = require('pathe');
|
|
|
7
7
|
const MagicString = require('magic-string');
|
|
8
8
|
const unplugin$1 = require('unplugin');
|
|
9
9
|
const node_crypto = require('node:crypto');
|
|
10
|
-
const pkgTypes = require('pkg-types');
|
|
11
10
|
const knitwork = require('knitwork');
|
|
12
11
|
const tools = require('./tools.cjs');
|
|
13
12
|
|
|
@@ -22,30 +21,100 @@ function sha1(source) {
|
|
|
22
21
|
return node_crypto.createHash("sha1").update(source).digest("hex").slice(0, 16);
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
async function getWasmImports(asset, _opts) {
|
|
25
|
+
const importNames = Object.keys(asset.imports || {});
|
|
26
|
+
if (importNames.length === 0) {
|
|
27
|
+
return {
|
|
28
|
+
code: "const _imports = { /* no imports */ }",
|
|
29
|
+
resolved: true
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
const { readPackageJSON } = await import('pkg-types');
|
|
33
|
+
const pkgJSON = await readPackageJSON(asset.id);
|
|
34
|
+
let resolved = true;
|
|
35
|
+
const imports = [];
|
|
36
|
+
const importsObject = {};
|
|
37
|
+
for (const moduleName of importNames) {
|
|
38
|
+
const importNames2 = asset.imports[moduleName];
|
|
39
|
+
const pkgImport = pkgJSON.imports?.[moduleName] || pkgJSON.imports?.[`#${moduleName}`];
|
|
40
|
+
const importName = "_imports_" + knitwork.genSafeVariableName(moduleName);
|
|
41
|
+
if (pkgImport && typeof pkgImport === "string") {
|
|
42
|
+
imports.push(knitwork.genImport(pkgImport, { name: "*", as: importName }));
|
|
43
|
+
} else {
|
|
44
|
+
resolved = false;
|
|
45
|
+
}
|
|
46
|
+
importsObject[moduleName] = Object.fromEntries(
|
|
47
|
+
importNames2.map((name) => [
|
|
48
|
+
name,
|
|
49
|
+
pkgImport ? `${importName}[${knitwork.genString(name)}]` : `() => { throw new Error(${knitwork.genString(moduleName + "." + importName)} + " is not provided!")}`
|
|
50
|
+
])
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
const code = `${imports.join("\n")}
|
|
54
|
+
|
|
55
|
+
const _imports = ${knitwork.genObjectFromRaw(importsObject)}`;
|
|
56
|
+
return {
|
|
57
|
+
code,
|
|
58
|
+
resolved
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
26
62
|
async function getWasmESMBinding(asset, opts) {
|
|
27
63
|
const autoImports = await getWasmImports(asset);
|
|
28
|
-
const
|
|
29
|
-
|
|
64
|
+
const instantiateCode = opts.esmImport ? getESMImportInstantiate(asset, autoImports.code) : getBase64Instantiate(asset, autoImports.code);
|
|
65
|
+
return opts.lazy !== true && autoImports.resolved ? getExports(asset, instantiateCode) : getLazyExports(asset, instantiateCode);
|
|
66
|
+
}
|
|
67
|
+
function getWasmModuleBinding(asset, opts) {
|
|
68
|
+
return opts.esmImport ? (
|
|
69
|
+
/* js */
|
|
70
|
+
`
|
|
71
|
+
const _mod = ${opts.lazy === true ? "" : `await`} import("${UNWASM_EXTERNAL_PREFIX}${asset.name}").then(r => r.default || r);
|
|
72
|
+
export default _mod;
|
|
73
|
+
`
|
|
74
|
+
) : (
|
|
75
|
+
/* js */
|
|
76
|
+
`
|
|
77
|
+
import { base64ToUint8Array } from "${UMWASM_HELPERS_ID}";
|
|
78
|
+
const _data = base64ToUint8Array("${asset.source.toString("base64")}");
|
|
79
|
+
const _mod = new WebAssembly.Module(_data);
|
|
80
|
+
export default _mod;
|
|
81
|
+
`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
function getESMImportInstantiate(asset, importsCode) {
|
|
85
|
+
return (
|
|
86
|
+
/* js */
|
|
87
|
+
`
|
|
88
|
+
${importsCode}
|
|
30
89
|
|
|
31
90
|
async function _instantiate(imports = _imports) {
|
|
32
|
-
|
|
33
|
-
|
|
91
|
+
const _mod = await import("${UNWASM_EXTERNAL_PREFIX}${asset.name}").then(r => r.default || r);
|
|
92
|
+
return WebAssembly.instantiate(_mod, imports)
|
|
93
|
+
}
|
|
94
|
+
`
|
|
95
|
+
);
|
|
34
96
|
}
|
|
35
|
-
|
|
97
|
+
function getBase64Instantiate(asset, importsCode) {
|
|
98
|
+
return (
|
|
99
|
+
/* js */
|
|
100
|
+
`
|
|
36
101
|
import { base64ToUint8Array } from "${UMWASM_HELPERS_ID}";
|
|
37
|
-
|
|
102
|
+
|
|
103
|
+
${importsCode}
|
|
38
104
|
|
|
39
105
|
function _instantiate(imports = _imports) {
|
|
40
106
|
const _data = base64ToUint8Array("${asset.source.toString("base64")}")
|
|
41
|
-
return WebAssembly.instantiate(_data, imports)
|
|
107
|
+
return WebAssembly.instantiate(_data, imports) }
|
|
108
|
+
`
|
|
109
|
+
);
|
|
42
110
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
111
|
+
function getExports(asset, instantiateCode) {
|
|
112
|
+
return (
|
|
113
|
+
/* js */
|
|
114
|
+
`
|
|
47
115
|
import { getExports } from "${UMWASM_HELPERS_ID}";
|
|
48
|
-
|
|
116
|
+
|
|
117
|
+
${instantiateCode}
|
|
49
118
|
|
|
50
119
|
const $exports = getExports(await _instantiate());
|
|
51
120
|
|
|
@@ -54,35 +123,32 @@ ${asset.exports.map((name) => `export const ${name} = $exports.${name};`).join("
|
|
|
54
123
|
const defaultExport = () => $exports;
|
|
55
124
|
${asset.exports.map((name) => `defaultExport["${name}"] = $exports.${name};`).join("\n")}
|
|
56
125
|
export default defaultExport;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
126
|
+
`
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
function getLazyExports(asset, instantiateCode) {
|
|
130
|
+
return (
|
|
131
|
+
/* js */
|
|
132
|
+
`
|
|
60
133
|
import { createLazyWasmModule } from "${UMWASM_HELPERS_ID}";
|
|
61
|
-
|
|
134
|
+
|
|
135
|
+
${instantiateCode}
|
|
62
136
|
|
|
63
137
|
const _mod = createLazyWasmModule(_instantiate);
|
|
64
138
|
|
|
65
139
|
${asset.exports.map((name) => `export const ${name} = _mod.${name};`).join("\n")}
|
|
66
140
|
|
|
67
141
|
export default _mod;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
function getWasmModuleBinding(asset, opts) {
|
|
72
|
-
return opts.esmImport ? js`
|
|
73
|
-
const _mod = ${opts.lazy === true ? "" : `await`} import("${UNWASM_EXTERNAL_PREFIX}${asset.name}").then(r => r.default || r);
|
|
74
|
-
export default _mod;
|
|
75
|
-
` : js`
|
|
76
|
-
import { base64ToUint8Array } from "${UMWASM_HELPERS_ID}";
|
|
77
|
-
const _data = base64ToUint8Array("${asset.source.toString("base64")}");
|
|
78
|
-
const _mod = new WebAssembly.Module(_data);
|
|
79
|
-
export default _mod;
|
|
80
|
-
`;
|
|
142
|
+
`
|
|
143
|
+
);
|
|
81
144
|
}
|
|
145
|
+
|
|
82
146
|
function getPluginUtils() {
|
|
83
|
-
return
|
|
147
|
+
return (
|
|
148
|
+
/* js */
|
|
149
|
+
`
|
|
84
150
|
export function debug(...args) {
|
|
85
|
-
console.log('[
|
|
151
|
+
console.log('[unwasm] [debug]', ...args);
|
|
86
152
|
}
|
|
87
153
|
|
|
88
154
|
export function getExports(input) {
|
|
@@ -150,43 +216,8 @@ export function createLazyWasmModule(_instantiator) {
|
|
|
150
216
|
|
|
151
217
|
return lazyProxy;
|
|
152
218
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
async function getWasmImports(asset, _opts) {
|
|
156
|
-
const importNames = Object.keys(asset.imports || {});
|
|
157
|
-
if (importNames.length === 0) {
|
|
158
|
-
return {
|
|
159
|
-
code: "const _imports = { /* no imports */ }",
|
|
160
|
-
resolved: true
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
const pkgJSON = await pkgTypes.readPackageJSON(asset.id);
|
|
164
|
-
let resolved = true;
|
|
165
|
-
const imports = [];
|
|
166
|
-
const importsObject = {};
|
|
167
|
-
for (const moduleName of importNames) {
|
|
168
|
-
const importNames2 = asset.imports[moduleName];
|
|
169
|
-
const pkgImport = pkgJSON.imports?.[moduleName] || pkgJSON.imports?.[`#${moduleName}`];
|
|
170
|
-
const importName = "_imports_" + knitwork.genSafeVariableName(moduleName);
|
|
171
|
-
if (pkgImport) {
|
|
172
|
-
imports.push(knitwork.genImport(pkgImport, { name: "*", as: importName }));
|
|
173
|
-
} else {
|
|
174
|
-
resolved = false;
|
|
175
|
-
}
|
|
176
|
-
importsObject[moduleName] = Object.fromEntries(
|
|
177
|
-
importNames2.map((name) => [
|
|
178
|
-
name,
|
|
179
|
-
pkgImport ? `${importName}[${knitwork.genString(name)}]` : `() => { throw new Error(${knitwork.genString(moduleName + "." + importName)} + " is not provided!")}`
|
|
180
|
-
])
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
const code = `${imports.join("\n")}
|
|
184
|
-
|
|
185
|
-
const _imports = ${knitwork.genObjectFromRaw(importsObject)}`;
|
|
186
|
-
return {
|
|
187
|
-
code,
|
|
188
|
-
resolved
|
|
189
|
-
};
|
|
219
|
+
`
|
|
220
|
+
);
|
|
190
221
|
}
|
|
191
222
|
|
|
192
223
|
const WASM_ID_RE = /\.wasm\??.*$/i;
|
package/dist/plugin.d.cts
CHANGED
|
@@ -19,7 +19,10 @@ interface UnwasmPluginOptions {
|
|
|
19
19
|
|
|
20
20
|
declare const rollup: (opts: UnwasmPluginOptions) => Plugin;
|
|
21
21
|
declare const _default: {
|
|
22
|
-
rollup: (opts: UnwasmPluginOptions) => Plugin
|
|
22
|
+
rollup: (opts: UnwasmPluginOptions) => Plugin;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
export = _default;
|
|
27
|
+
export { rollup };
|
|
28
|
+
export type { UnwasmPluginOptions };
|
package/dist/plugin.d.mts
CHANGED
|
@@ -19,7 +19,8 @@ interface UnwasmPluginOptions {
|
|
|
19
19
|
|
|
20
20
|
declare const rollup: (opts: UnwasmPluginOptions) => Plugin;
|
|
21
21
|
declare const _default: {
|
|
22
|
-
rollup: (opts: UnwasmPluginOptions) => Plugin
|
|
22
|
+
rollup: (opts: UnwasmPluginOptions) => Plugin;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
export {
|
|
25
|
+
export { _default as default, rollup };
|
|
26
|
+
export type { UnwasmPluginOptions };
|
package/dist/plugin.d.ts
CHANGED
|
@@ -19,7 +19,10 @@ interface UnwasmPluginOptions {
|
|
|
19
19
|
|
|
20
20
|
declare const rollup: (opts: UnwasmPluginOptions) => Plugin;
|
|
21
21
|
declare const _default: {
|
|
22
|
-
rollup: (opts: UnwasmPluginOptions) => Plugin
|
|
22
|
+
rollup: (opts: UnwasmPluginOptions) => Plugin;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
export = _default;
|
|
27
|
+
export { rollup };
|
|
28
|
+
export type { UnwasmPluginOptions };
|
package/dist/plugin.mjs
CHANGED
|
@@ -3,7 +3,6 @@ import { basename } from 'pathe';
|
|
|
3
3
|
import MagicString from 'magic-string';
|
|
4
4
|
import { createUnplugin } from 'unplugin';
|
|
5
5
|
import { createHash } from 'node:crypto';
|
|
6
|
-
import { readPackageJSON } from 'pkg-types';
|
|
7
6
|
import { genSafeVariableName, genImport, genString, genObjectFromRaw } from 'knitwork';
|
|
8
7
|
import { parseWasm } from './tools.mjs';
|
|
9
8
|
|
|
@@ -14,30 +13,100 @@ function sha1(source) {
|
|
|
14
13
|
return createHash("sha1").update(source).digest("hex").slice(0, 16);
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
async function getWasmImports(asset, _opts) {
|
|
17
|
+
const importNames = Object.keys(asset.imports || {});
|
|
18
|
+
if (importNames.length === 0) {
|
|
19
|
+
return {
|
|
20
|
+
code: "const _imports = { /* no imports */ }",
|
|
21
|
+
resolved: true
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const { readPackageJSON } = await import('pkg-types');
|
|
25
|
+
const pkgJSON = await readPackageJSON(asset.id);
|
|
26
|
+
let resolved = true;
|
|
27
|
+
const imports = [];
|
|
28
|
+
const importsObject = {};
|
|
29
|
+
for (const moduleName of importNames) {
|
|
30
|
+
const importNames2 = asset.imports[moduleName];
|
|
31
|
+
const pkgImport = pkgJSON.imports?.[moduleName] || pkgJSON.imports?.[`#${moduleName}`];
|
|
32
|
+
const importName = "_imports_" + genSafeVariableName(moduleName);
|
|
33
|
+
if (pkgImport && typeof pkgImport === "string") {
|
|
34
|
+
imports.push(genImport(pkgImport, { name: "*", as: importName }));
|
|
35
|
+
} else {
|
|
36
|
+
resolved = false;
|
|
37
|
+
}
|
|
38
|
+
importsObject[moduleName] = Object.fromEntries(
|
|
39
|
+
importNames2.map((name) => [
|
|
40
|
+
name,
|
|
41
|
+
pkgImport ? `${importName}[${genString(name)}]` : `() => { throw new Error(${genString(moduleName + "." + importName)} + " is not provided!")}`
|
|
42
|
+
])
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
const code = `${imports.join("\n")}
|
|
46
|
+
|
|
47
|
+
const _imports = ${genObjectFromRaw(importsObject)}`;
|
|
48
|
+
return {
|
|
49
|
+
code,
|
|
50
|
+
resolved
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
18
54
|
async function getWasmESMBinding(asset, opts) {
|
|
19
55
|
const autoImports = await getWasmImports(asset);
|
|
20
|
-
const
|
|
21
|
-
|
|
56
|
+
const instantiateCode = opts.esmImport ? getESMImportInstantiate(asset, autoImports.code) : getBase64Instantiate(asset, autoImports.code);
|
|
57
|
+
return opts.lazy !== true && autoImports.resolved ? getExports(asset, instantiateCode) : getLazyExports(asset, instantiateCode);
|
|
58
|
+
}
|
|
59
|
+
function getWasmModuleBinding(asset, opts) {
|
|
60
|
+
return opts.esmImport ? (
|
|
61
|
+
/* js */
|
|
62
|
+
`
|
|
63
|
+
const _mod = ${opts.lazy === true ? "" : `await`} import("${UNWASM_EXTERNAL_PREFIX}${asset.name}").then(r => r.default || r);
|
|
64
|
+
export default _mod;
|
|
65
|
+
`
|
|
66
|
+
) : (
|
|
67
|
+
/* js */
|
|
68
|
+
`
|
|
69
|
+
import { base64ToUint8Array } from "${UMWASM_HELPERS_ID}";
|
|
70
|
+
const _data = base64ToUint8Array("${asset.source.toString("base64")}");
|
|
71
|
+
const _mod = new WebAssembly.Module(_data);
|
|
72
|
+
export default _mod;
|
|
73
|
+
`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
function getESMImportInstantiate(asset, importsCode) {
|
|
77
|
+
return (
|
|
78
|
+
/* js */
|
|
79
|
+
`
|
|
80
|
+
${importsCode}
|
|
22
81
|
|
|
23
82
|
async function _instantiate(imports = _imports) {
|
|
24
|
-
|
|
25
|
-
|
|
83
|
+
const _mod = await import("${UNWASM_EXTERNAL_PREFIX}${asset.name}").then(r => r.default || r);
|
|
84
|
+
return WebAssembly.instantiate(_mod, imports)
|
|
26
85
|
}
|
|
27
|
-
`
|
|
86
|
+
`
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
function getBase64Instantiate(asset, importsCode) {
|
|
90
|
+
return (
|
|
91
|
+
/* js */
|
|
92
|
+
`
|
|
28
93
|
import { base64ToUint8Array } from "${UMWASM_HELPERS_ID}";
|
|
29
|
-
|
|
94
|
+
|
|
95
|
+
${importsCode}
|
|
30
96
|
|
|
31
97
|
function _instantiate(imports = _imports) {
|
|
32
98
|
const _data = base64ToUint8Array("${asset.source.toString("base64")}")
|
|
33
|
-
return WebAssembly.instantiate(_data, imports)
|
|
99
|
+
return WebAssembly.instantiate(_data, imports) }
|
|
100
|
+
`
|
|
101
|
+
);
|
|
34
102
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
103
|
+
function getExports(asset, instantiateCode) {
|
|
104
|
+
return (
|
|
105
|
+
/* js */
|
|
106
|
+
`
|
|
39
107
|
import { getExports } from "${UMWASM_HELPERS_ID}";
|
|
40
|
-
|
|
108
|
+
|
|
109
|
+
${instantiateCode}
|
|
41
110
|
|
|
42
111
|
const $exports = getExports(await _instantiate());
|
|
43
112
|
|
|
@@ -46,35 +115,32 @@ ${asset.exports.map((name) => `export const ${name} = $exports.${name};`).join("
|
|
|
46
115
|
const defaultExport = () => $exports;
|
|
47
116
|
${asset.exports.map((name) => `defaultExport["${name}"] = $exports.${name};`).join("\n")}
|
|
48
117
|
export default defaultExport;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
118
|
+
`
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
function getLazyExports(asset, instantiateCode) {
|
|
122
|
+
return (
|
|
123
|
+
/* js */
|
|
124
|
+
`
|
|
52
125
|
import { createLazyWasmModule } from "${UMWASM_HELPERS_ID}";
|
|
53
|
-
|
|
126
|
+
|
|
127
|
+
${instantiateCode}
|
|
54
128
|
|
|
55
129
|
const _mod = createLazyWasmModule(_instantiate);
|
|
56
130
|
|
|
57
131
|
${asset.exports.map((name) => `export const ${name} = _mod.${name};`).join("\n")}
|
|
58
132
|
|
|
59
133
|
export default _mod;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
function getWasmModuleBinding(asset, opts) {
|
|
64
|
-
return opts.esmImport ? js`
|
|
65
|
-
const _mod = ${opts.lazy === true ? "" : `await`} import("${UNWASM_EXTERNAL_PREFIX}${asset.name}").then(r => r.default || r);
|
|
66
|
-
export default _mod;
|
|
67
|
-
` : js`
|
|
68
|
-
import { base64ToUint8Array } from "${UMWASM_HELPERS_ID}";
|
|
69
|
-
const _data = base64ToUint8Array("${asset.source.toString("base64")}");
|
|
70
|
-
const _mod = new WebAssembly.Module(_data);
|
|
71
|
-
export default _mod;
|
|
72
|
-
`;
|
|
134
|
+
`
|
|
135
|
+
);
|
|
73
136
|
}
|
|
137
|
+
|
|
74
138
|
function getPluginUtils() {
|
|
75
|
-
return
|
|
139
|
+
return (
|
|
140
|
+
/* js */
|
|
141
|
+
`
|
|
76
142
|
export function debug(...args) {
|
|
77
|
-
console.log('[
|
|
143
|
+
console.log('[unwasm] [debug]', ...args);
|
|
78
144
|
}
|
|
79
145
|
|
|
80
146
|
export function getExports(input) {
|
|
@@ -142,43 +208,8 @@ export function createLazyWasmModule(_instantiator) {
|
|
|
142
208
|
|
|
143
209
|
return lazyProxy;
|
|
144
210
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
async function getWasmImports(asset, _opts) {
|
|
148
|
-
const importNames = Object.keys(asset.imports || {});
|
|
149
|
-
if (importNames.length === 0) {
|
|
150
|
-
return {
|
|
151
|
-
code: "const _imports = { /* no imports */ }",
|
|
152
|
-
resolved: true
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
const pkgJSON = await readPackageJSON(asset.id);
|
|
156
|
-
let resolved = true;
|
|
157
|
-
const imports = [];
|
|
158
|
-
const importsObject = {};
|
|
159
|
-
for (const moduleName of importNames) {
|
|
160
|
-
const importNames2 = asset.imports[moduleName];
|
|
161
|
-
const pkgImport = pkgJSON.imports?.[moduleName] || pkgJSON.imports?.[`#${moduleName}`];
|
|
162
|
-
const importName = "_imports_" + genSafeVariableName(moduleName);
|
|
163
|
-
if (pkgImport) {
|
|
164
|
-
imports.push(genImport(pkgImport, { name: "*", as: importName }));
|
|
165
|
-
} else {
|
|
166
|
-
resolved = false;
|
|
167
|
-
}
|
|
168
|
-
importsObject[moduleName] = Object.fromEntries(
|
|
169
|
-
importNames2.map((name) => [
|
|
170
|
-
name,
|
|
171
|
-
pkgImport ? `${importName}[${genString(name)}]` : `() => { throw new Error(${genString(moduleName + "." + importName)} + " is not provided!")}`
|
|
172
|
-
])
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
const code = `${imports.join("\n")}
|
|
176
|
-
|
|
177
|
-
const _imports = ${genObjectFromRaw(importsObject)}`;
|
|
178
|
-
return {
|
|
179
|
-
code,
|
|
180
|
-
resolved
|
|
181
|
-
};
|
|
211
|
+
`
|
|
212
|
+
);
|
|
182
213
|
}
|
|
183
214
|
|
|
184
215
|
const WASM_ID_RE = /\.wasm\??.*$/i;
|