Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): wasi fallback package load logic #1887

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"inquirer": "^9.2.12",
"js-yaml": "^4.1.0",
"lodash-es": "^4.17.21",
"semver": "^7.5.4",
"toml": "^3.0.0",
"typanion": "^3.14.0"
},
Expand Down
20 changes: 20 additions & 0 deletions cli/src/api/create-npm-dirs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { join, resolve } from 'node:path'

import { parse } from 'semver'

import {
applyDefaultCreateNpmDirsOptions,
CreateNpmDirsOptions,
Expand Down Expand Up @@ -88,6 +90,24 @@ export async function createNpmDirs(userOptions: CreateNpmDirsOptions) {
const entry = `${binaryName}.wasi.cjs`
scopedPackageJson.files.push(entry, `wasi-worker.mjs`)
scopedPackageJson.main = entry
let needRestrictNodeVersion = true
if (scopedPackageJson.engines?.node) {
try {
const { major } = parse(scopedPackageJson.engines.node) ?? {
major: 0,
}
if (major >= 14) {
needRestrictNodeVersion = false
}
} catch {
// ignore
}
}
if (needRestrictNodeVersion) {
scopedPackageJson.engines = {
node: '>=14.0.0',
}
}
const emnapiCore = await fetch(
`https://registry.npmjs.org/@emnapi/core`,
).then((res) => res.json() as Promise<PackageMeta>)
Expand Down
7 changes: 7 additions & 0 deletions cli/src/api/templates/js-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
} catch {
// ignore
}
if (!nativeBinding) {
try {
nativeBinding = require('${pkgName}-wasm32-wasi')
} catch (err) {
console.error(err)
}
}
}

if (!nativeBinding) {
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ __metadata:
js-yaml: "npm:^4.1.0"
lodash-es: "npm:^4.17.21"
prettier: "npm:^3.1.0"
semver: "npm:^7.5.4"
toml: "npm:^3.0.0"
ts-node: "npm:^10.9.1"
tslib: "npm:^2.6.2"
Expand Down
Loading