Skip to content

Commit 9873fb3

Browse files
authored
feat(cli): add wasm.browser.asyncInit for large wasm file (#2514)
1 parent 69d1c41 commit 9873fb3

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

cli/src/api/build.ts

+1
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,7 @@ class Builder {
963963
this.config.wasm?.initialMemory,
964964
this.config.wasm?.maximumMemory,
965965
this.config.wasm?.browser?.fs,
966+
this.config.wasm?.browser?.asyncInit,
966967
) +
967968
idents
968969
.map(

cli/src/api/templates/load-wasi-template.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const createWasiBrowserBinding = (
33
initialMemory = 4000,
44
maximumMemory = 65536,
55
fs = false,
6+
asyncInit = false,
67
) => {
78
const fsImport = fs ? `import { memfs } from '@napi-rs/wasm-runtime/fs'` : ''
89
const wasiCreation = fs
@@ -25,8 +26,15 @@ const __wasi = new __WASI({
2526
? ` worker.addEventListener('message', __wasmCreateOnMessageForFsProxy(__fs))\n`
2627
: ''
2728

29+
const emnapiInstantiateImport = asyncInit
30+
? `instantiateNapiModule as __emnapiInstantiateNapiModule`
31+
: `instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync`
32+
const emnapiInstantiateCall = asyncInit
33+
? `await __emnapiInstantiateNapiModule`
34+
: `__emnapiInstantiateNapiModuleSync`
35+
2836
return `import {
29-
instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
37+
${emnapiInstantiateImport},
3038
getDefaultContext as __emnapiGetDefaultContext,
3139
WASI as __WASI,
3240
createOnMessage as __wasmCreateOnMessageForFsProxy,
@@ -49,7 +57,7 @@ const {
4957
instance: __napiInstance,
5058
module: __wasiModule,
5159
napiModule: __napiModule,
52-
} = __emnapiInstantiateNapiModuleSync(__wasmFile, {
60+
} = ${emnapiInstantiateCall}(__wasmFile, {
5361
context: __emnapiContext,
5462
asyncWorkPoolSize: 4,
5563
wasi: __wasi,

cli/src/utils/config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ export interface UserNapiConfig {
7575
* Whether to use fs module in browser
7676
*/
7777
fs?: boolean
78+
/**
79+
* Whether to initialize wasm asynchronously
80+
*/
81+
asyncInit?: boolean
7882
}
7983
}
8084

0 commit comments

Comments
 (0)