fix(cli): add browser entry (#1899)

This commit is contained in:
LongYinan 2024-01-10 11:18:13 +08:00 committed by GitHub
parent 0a70d6ffd7
commit fc3d5cbcff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 96 additions and 29 deletions

View file

@ -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(

View file

@ -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 []

View file

@ -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: {

View file

@ -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,

View file

@ -89,6 +89,7 @@ export interface CommonPackageJsonFields {
main?: string
module?: string
types?: string
browser?: string
exports?: any
dependencies?: Record<string, string>

1
examples/napi/browser.js Normal file
View file

@ -0,0 +1 @@
export * from '@examples/napi-wasm32-wasi'

View file

@ -1,15 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" href="https://napi.rs/img/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NAPI-RS examples playground</title>
</head>
</head>
<body>
<body>
<div id="app" />
</body>
</body>
</html>

View file

@ -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']?.()

View file

@ -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']?.()

View file

@ -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",

View file

@ -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)
}

View file

@ -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"