diff --git a/cli/src/api/artifacts.ts b/cli/src/api/artifacts.ts index 0700e95c..2c3ba453 100644 --- a/cli/src/api/artifacts.ts +++ b/cli/src/api/artifacts.ts @@ -137,7 +137,7 @@ export async function collectArtifacts(userOptions: ArtifactsOptions) { )}] to [${colors.yellowBright(wasiDir)}]`, ) await writeFileAsync( - join(wasiDir, `wasi-worker-browser.js`), + join(wasiDir, `wasi-worker-browser.mjs`), await readFileAsync(browserWorkerFile), ) } diff --git a/cli/src/api/create-npm-dirs.ts b/cli/src/api/create-npm-dirs.ts index 570ab356..c4208f83 100644 --- a/cli/src/api/create-npm-dirs.ts +++ b/cli/src/api/create-npm-dirs.ts @@ -88,10 +88,16 @@ export async function createNpmDirs(userOptions: CreateNpmDirsOptions) { scopedPackageJson.os = [target.platform] } else { const entry = `${binaryName}.wasi.cjs` - scopedPackageJson.files.push(entry, `wasi-worker.mjs`) scopedPackageJson.main = entry // @ts-expect-error scopedPackageJson.browser = `${binaryName}.wasi-browser.js` + scopedPackageJson.files.push( + entry, + // @ts-expect-error + scopedPackageJson.browser, + `wasi-worker.mjs`, + `wasi-worker-browser.mjs`, + ) let needRestrictNodeVersion = true if (scopedPackageJson.engines?.node) { try { diff --git a/cli/src/api/templates/load-wasi-template.ts b/cli/src/api/templates/load-wasi-template.ts index e2a596ff..829f555e 100644 --- a/cli/src/api/templates/load-wasi-template.ts +++ b/cli/src/api/templates/load-wasi-template.ts @@ -27,7 +27,7 @@ const __sharedMemory = new WebAssembly.Memory({ shared: true, }) -let __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer()) +const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer()) const { instance: __napiInstance, diff --git a/cli/src/api/templates/wasi-worker-template.ts b/cli/src/api/templates/wasi-worker-template.ts index 73d916a7..b8855a8e 100644 --- a/cli/src/api/templates/wasi-worker-template.ts +++ b/cli/src/api/templates/wasi-worker-template.ts @@ -55,15 +55,20 @@ export const WASI_WORKER_BROWSER_TEMPLATE = `import { instantiateNapiModuleSync, import { WASI } from '@tybys/wasm-util' import { Volume, createFsFromVolume } from 'memfs-browser' -const fs = createFsFromVolume(Volume.fromJSON({ - '/': null -})) +const fs = createFsFromVolume( + Volume.fromJSON({ + '/': null, + }), +) const handler = new MessageHandler({ onLoad({ wasmModule, wasmMemory }) { const wasi = new WASI({ fs, - print: function() { console.log.apply(console, arguments) } + print: function () { + // eslint-disable-next-line no-console + console.log.apply(console, arguments) + }, }) return instantiateNapiModuleSync(wasmModule, { childThread: true, @@ -73,14 +78,14 @@ const handler = new MessageHandler({ ...importObject.env, ...importObject.napi, ...importObject.emnapi, - memory: wasmMemory + memory: wasmMemory, } - } + }, }) - } + }, }) -globalThis.onmessage = function(e) { +globalThis.onmessage = function (e) { handler.handle(e) } ` diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index aeaafcc3..c173a245 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -498,10 +498,6 @@ export function runScript(script: string): unknown export function setSymbolInObj(symbol: symbol): object -export interface Shared { - value: number -} - export const enum Status { Pristine = 'Pristine', Loading = 'Loading', diff --git a/examples/napi/index.wasi-browser.js b/examples/napi/index.wasi-browser.js index 7817b0b2..5cb9c065 100644 --- a/examples/napi/index.wasi-browser.js +++ b/examples/napi/index.wasi-browser.js @@ -27,7 +27,7 @@ const __sharedMemory = new WebAssembly.Memory({ shared: true, }) -let __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer()) +const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer()) const { instance: __napiInstance, @@ -57,7 +57,6 @@ const { }) function __napi_rs_initialize_modules(__napiInstance) { - __napiInstance.exports['__napi_register__Shared_struct_0']?.() __napiInstance.exports['__napi_register__DEFAULT_COST_0']?.() __napiInstance.exports['__napi_register__TYPE_SKIPPED_CONST_1']?.() __napiInstance.exports['__napi_register__get_words_2']?.() diff --git a/examples/napi/index.wasi.cjs b/examples/napi/index.wasi.cjs index aea8a283..9d5cb137 100644 --- a/examples/napi/index.wasi.cjs +++ b/examples/napi/index.wasi.cjs @@ -70,7 +70,6 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule }) function __napi_rs_initialize_modules(__napiInstance) { - __napiInstance.exports['__napi_register__Shared_struct_0']?.() __napiInstance.exports['__napi_register__DEFAULT_COST_0']?.() __napiInstance.exports['__napi_register__TYPE_SKIPPED_CONST_1']?.() __napiInstance.exports['__napi_register__get_words_2']?.() diff --git a/examples/napi/wasi-worker-browser.js b/examples/napi/wasi-worker-browser.js deleted file mode 100644 index b6725dde..00000000 --- a/examples/napi/wasi-worker-browser.js +++ /dev/null @@ -1,36 +0,0 @@ -import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core' -import { WASI } from '@tybys/wasm-util' -import { Volume, createFsFromVolume } from 'memfs-browser' - -const fs = createFsFromVolume( - Volume.fromJSON({ - '/': null, - }), -) - -const handler = new MessageHandler({ - onLoad({ wasmModule, wasmMemory }) { - const wasi = new WASI({ - fs, - print: function () { - console.log.apply(console, arguments) - }, - }) - return instantiateNapiModuleSync(wasmModule, { - childThread: true, - wasi, - overwriteImports(importObject) { - importObject.env = { - ...importObject.env, - ...importObject.napi, - ...importObject.emnapi, - memory: wasmMemory, - } - }, - }) - }, -}) - -globalThis.onmessage = function (e) { - handler.handle(e) -} diff --git a/examples/napi/wasi-worker-browser.mjs b/examples/napi/wasi-worker-browser.mjs index 778564bb..d6e930bf 100644 --- a/examples/napi/wasi-worker-browser.mjs +++ b/examples/napi/wasi-worker-browser.mjs @@ -2,15 +2,20 @@ import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core' import { WASI } from '@tybys/wasm-util' import { Volume, createFsFromVolume } from 'memfs-browser' -const fs = createFsFromVolume(Volume.fromJSON({ - '/': null -})) +const fs = createFsFromVolume( + Volume.fromJSON({ + '/': null, + }), +) const handler = new MessageHandler({ onLoad({ wasmModule, wasmMemory }) { const wasi = new WASI({ fs, - print: function() { console.log.apply(console, arguments) } + print: function() { + // eslint-disable-next-line no-console + console.log.apply(console, arguments) + }, }) return instantiateNapiModuleSync(wasmModule, { childThread: true, @@ -20,11 +25,11 @@ const handler = new MessageHandler({ ...importObject.env, ...importObject.napi, ...importObject.emnapi, - memory: wasmMemory + memory: wasmMemory, } - } + }, }) - } + }, }) globalThis.onmessage = function(e) {