From fc3d5cbcff722ce4ffcd2911afedffe2be768046 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 10 Jan 2024 11:18:13 +0800 Subject: [PATCH] fix(cli): add browser entry (#1899) --- cli/src/api/artifacts.ts | 9 ++- cli/src/api/build.ts | 9 +++ cli/src/api/templates/package.json.ts | 1 + cli/src/api/templates/wasi-worker-template.ts | 4 + cli/src/utils/config.ts | 1 + examples/napi/browser.js | 1 + examples/napi/index.html | 10 +-- examples/napi/index.wasi-browser.js | 1 + examples/napi/index.wasi.cjs | 1 + examples/napi/package.json | 2 +- examples/napi/wasi-worker-browser.mjs | 8 +- yarn.lock | 78 ++++++++++++++----- 12 files changed, 96 insertions(+), 29 deletions(-) create mode 100644 examples/napi/browser.js diff --git a/cli/src/api/artifacts.ts b/cli/src/api/artifacts.ts index 4f4b6668..607c939a 100644 --- a/cli/src/api/artifacts.ts +++ b/cli/src/api/artifacts.ts @@ -21,7 +21,8 @@ export async function collectArtifacts(userOptions: ArtifactsOptions) { const options = applyDefaultArtifactsOptions(userOptions) const packageJsonPath = join(options.cwd, options.packageJsonPath) - const { targets, binaryName } = await readNapiConfig(packageJsonPath) + const { targets, binaryName, packageName } = + await readNapiConfig(packageJsonPath) const distDirs = targets.map((platform) => join(options.cwd, options.npmDir, platform.platformArchABI), @@ -129,7 +130,11 @@ export async function collectArtifacts(userOptions: ArtifactsOptions) { ) await writeFileAsync( join(wasiDir, `${binaryName}.wasi-browser.js`), - await readFileAsync(browserEntry), + // https://github.com/vitejs/vite/issues/8427 + (await readFileAsync(browserEntry, 'utf8')).replace( + `new URL('./wasi-worker-browser.mjs', import.meta.url)`, + `new URL('${packageName}-wasm32-wasi/wasi-worker-browser.mjs', import.meta.url)`, + ), ) debug.info( `Move wasi browser worker file [${colors.yellowBright( diff --git a/cli/src/api/build.ts b/cli/src/api/build.ts index e89f89fc..dfcd0a08 100644 --- a/cli/src/api/build.ts +++ b/cli/src/api/build.ts @@ -803,6 +803,7 @@ class Builder { ) const workerPath = join(dir, 'wasi-worker.mjs') const browserWorkerPath = join(dir, 'wasi-worker-browser.mjs') + const browserEntryPath = join(dir, 'browser.js') const exportsCode = idents .map( (ident) => `module.exports.${ident} = __napiModule.exports.${ident}`, @@ -837,6 +838,10 @@ class Builder { WASI_WORKER_BROWSER_TEMPLATE, 'utf8', ) + await writeFileAsync( + browserEntryPath, + `export * from '${this.config.packageName}-wasm32-wasi'\n`, + ) return [ { kind: 'js', @@ -854,6 +859,10 @@ class Builder { kind: 'js', path: browserWorkerPath, }, + { + kind: 'js', + path: browserEntryPath, + }, ] satisfies Output[] } return [] diff --git a/cli/src/api/templates/package.json.ts b/cli/src/api/templates/package.json.ts index 50f0570b..285006f1 100644 --- a/cli/src/api/templates/package.json.ts +++ b/cli/src/api/templates/package.json.ts @@ -48,6 +48,7 @@ export const createPackageJson = async ({ type: 'commonjs', main: 'index.js', types: 'index.d.ts', + browser: 'browser.js', module: undefined, exports: undefined, napi: { diff --git a/cli/src/api/templates/wasi-worker-template.ts b/cli/src/api/templates/wasi-worker-template.ts index b8855a8e..faa2d426 100644 --- a/cli/src/api/templates/wasi-worker-template.ts +++ b/cli/src/api/templates/wasi-worker-template.ts @@ -69,6 +69,10 @@ const handler = new MessageHandler({ // eslint-disable-next-line no-console console.log.apply(console, arguments) }, + printErr: function() { + // eslint-disable-next-line no-console + console.error.apply(console, arguments) + }, }) return instantiateNapiModuleSync(wasmModule, { childThread: true, diff --git a/cli/src/utils/config.ts b/cli/src/utils/config.ts index 8ccd6954..e7e2398e 100644 --- a/cli/src/utils/config.ts +++ b/cli/src/utils/config.ts @@ -89,6 +89,7 @@ export interface CommonPackageJsonFields { main?: string module?: string types?: string + browser?: string exports?: any dependencies?: Record diff --git a/examples/napi/browser.js b/examples/napi/browser.js new file mode 100644 index 00000000..2c78c4bb --- /dev/null +++ b/examples/napi/browser.js @@ -0,0 +1 @@ +export * from '@examples/napi-wasm32-wasi' diff --git a/examples/napi/index.html b/examples/napi/index.html index b67f01ab..8c2c1c35 100644 --- a/examples/napi/index.html +++ b/examples/napi/index.html @@ -1,15 +1,13 @@ - - + NAPI-RS examples playground - + - +
- - + diff --git a/examples/napi/index.wasi-browser.js b/examples/napi/index.wasi-browser.js index 5cb9c065..2f51ea23 100644 --- a/examples/napi/index.wasi-browser.js +++ b/examples/napi/index.wasi-browser.js @@ -57,6 +57,7 @@ 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 9d5cb137..aea8a283 100644 --- a/examples/napi/index.wasi.cjs +++ b/examples/napi/index.wasi.cjs @@ -70,6 +70,7 @@ 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/package.json b/examples/napi/package.json index 140b69ee..f5e4e58c 100644 --- a/examples/napi/package.json +++ b/examples/napi/package.json @@ -19,7 +19,7 @@ "cross-env": "7.0.3", "electron": "^28.0.0", "lodash": "^4.17.21", - "memfs-browser": "^3.4.13000", + "memfs-browser": "^4.6.0", "sinon": "^17.0.1", "vite": "^5.0.11", "vite-plugin-node-polyfills": "^0.19.0", diff --git a/examples/napi/wasi-worker-browser.mjs b/examples/napi/wasi-worker-browser.mjs index d6e930bf..53b6b81c 100644 --- a/examples/napi/wasi-worker-browser.mjs +++ b/examples/napi/wasi-worker-browser.mjs @@ -12,10 +12,14 @@ const handler = new MessageHandler({ onLoad({ wasmModule, wasmMemory }) { const wasi = new WASI({ fs, - print: function() { + print: function () { // eslint-disable-next-line no-console console.log.apply(console, arguments) }, + printErr: function() { + // eslint-disable-next-line no-console + console.error.apply(console, arguments) + }, }) return instantiateNapiModuleSync(wasmModule, { childThread: true, @@ -32,6 +36,6 @@ const handler = new MessageHandler({ }, }) -globalThis.onmessage = function(e) { +globalThis.onmessage = function (e) { handler.handle(e) } diff --git a/yarn.lock b/yarn.lock index d0951d34..8fdbfee0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -370,7 +370,7 @@ __metadata: cross-env: "npm:7.0.3" electron: "npm:^28.0.0" lodash: "npm:^4.17.21" - memfs-browser: "npm:^3.4.13000" + memfs-browser: "npm:^4.6.0" sinon: "npm:^17.0.1" vite: "npm:^5.0.11" vite-plugin-node-polyfills: "npm:^0.19.0" @@ -2806,6 +2806,13 @@ __metadata: languageName: node linkType: hard +"arg@npm:^5.0.2": + version: 5.0.2 + resolution: "arg@npm:5.0.2" + checksum: ccaf86f4e05d342af6666c569f844bec426595c567d32a8289715087825c2ca7edd8a3d204e4d2fb2aa4602e09a57d0c13ea8c9eea75aac3dbb4af5514e6800e + languageName: node + linkType: hard + "argparse@npm:^1.0.7": version: 1.0.10 resolution: "argparse@npm:1.0.10" @@ -5831,13 +5838,6 @@ __metadata: languageName: node linkType: hard -"fs-monkey@npm:^1.0.3": - version: 1.0.5 - resolution: "fs-monkey@npm:1.0.5" - checksum: 815025e75549fb1ac6c403413b82fd631eded862ae27694a515c0f666069e95874ab34e79c33d1b3b8c87d1e54350d5e4262090d0aa5bd7130143cbc627537e4 - languageName: node - linkType: hard - "fs.realpath@npm:^1.0.0": version: 1.0.0 resolution: "fs.realpath@npm:1.0.0" @@ -6626,6 +6626,13 @@ __metadata: languageName: node linkType: hard +"hyperdyperid@npm:^1.2.0": + version: 1.2.0 + resolution: "hyperdyperid@npm:1.2.0" + checksum: 885ba3177c7181d315a856ee9c0005ff8eb5dcb1ce9e9d61be70987895d934d84686c37c981cceeb53216d4c9c15c1cc25f1804e84cc6a74a16993c5d7fd0893 + languageName: node + linkType: hard + "iconv-lite@npm:^0.4.24": version: 0.4.24 resolution: "iconv-lite@npm:0.4.24" @@ -7411,6 +7418,29 @@ __metadata: languageName: node linkType: hard +"json-joy@npm:^9.2.0": + version: 9.9.1 + resolution: "json-joy@npm:9.9.1" + dependencies: + arg: "npm:^5.0.2" + hyperdyperid: "npm:^1.2.0" + peerDependencies: + quill-delta: ^5 + rxjs: 7 + tslib: 2 + bin: + jj: bin/jj.js + json-pack: bin/json-pack.js + json-pack-test: bin/json-pack-test.js + json-patch: bin/json-patch.js + json-patch-test: bin/json-patch-test.js + json-pointer: bin/json-pointer.js + json-pointer-test: bin/json-pointer-test.js + json-unpack: bin/json-unpack.js + checksum: e147eceae446a84521d60c36326c1528268aafbaaedfc0a79a25207ace9da23dc1b599be4b5a90ca84ff1d01eda1b29b037abc0f970fef7cff4ab1e9db20ba6b + languageName: node + linkType: hard + "json-parse-better-errors@npm:^1.0.1": version: 1.0.2 resolution: "json-parse-better-errors@npm:1.0.2" @@ -8129,22 +8159,25 @@ __metadata: languageName: node linkType: hard -"memfs-browser@npm:^3.4.13000": - version: 3.4.13000 - resolution: "memfs-browser@npm:3.4.13000" +"memfs-browser@npm:^4.6.0": + version: 4.6.0 + resolution: "memfs-browser@npm:4.6.0" dependencies: buffer: "npm:^6.0.3" - memfs: "npm:3.4.13" - checksum: 580fa76a69638b4f1e3e9f36db4ece7de3689427cc9519c567e5e6413e2106390892a63acf19675cc0908eafbb7f991ebf5425fede19ebd8b1fe0880e8b16cba + memfs: "npm:4.6.0" + checksum: f6b56982b8c2584c27e609bcfc18688e42ed9f0db5fbe348373639922f9bf350a24ba71a737a3485291257b52d1abbd0e62db0f413903f0a9e404c96c5eaec04 languageName: node linkType: hard -"memfs@npm:3.4.13": - version: 3.4.13 - resolution: "memfs@npm:3.4.13" +"memfs@npm:4.6.0": + version: 4.6.0 + resolution: "memfs@npm:4.6.0" dependencies: - fs-monkey: "npm:^1.0.3" - checksum: f14ab3ff938eacf688577d1b0f7bf77ca3a05d4df9c335b024ed6790e6b224b569cc4b61c1de604c0420a0fac6b3fbf3f283c72fd2be9ce395534539599ac63b + json-joy: "npm:^9.2.0" + thingies: "npm:^1.11.1" + peerDependencies: + tslib: 2 + checksum: 370a70ff9156aaf5a75bfeca265d66b08795bf736c5ad0c1a597e489864d4f031f424b320ef3f331a99f0fa36ad90688ab28859b7c647ee3b473d4ee1fdd8d95 languageName: node linkType: hard @@ -11621,6 +11654,15 @@ __metadata: languageName: node linkType: hard +"thingies@npm:^1.11.1": + version: 1.15.0 + resolution: "thingies@npm:1.15.0" + peerDependencies: + tslib: ^2 + checksum: 62d855eedcaeec748d6b99a35052ab2b476ac5ec4fb22b89b3bf2056e97e6750f234c7e2fc541c400d08fc301a6ae08077ea306a3fb99ba6002acb957b710081 + languageName: node + linkType: hard + "through2@npm:^2.0.0": version: 2.0.5 resolution: "through2@npm:2.0.5"