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

This commit is contained in:
LongYinan 2024-01-03 18:53:09 +08:00 committed by GitHub
parent 5080fb28a2
commit 57463554e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 0 deletions

View file

@ -77,6 +77,7 @@
"inquirer": "^9.2.12", "inquirer": "^9.2.12",
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"semver": "^7.5.4",
"toml": "^3.0.0", "toml": "^3.0.0",
"typanion": "^3.14.0" "typanion": "^3.14.0"
}, },

View file

@ -1,5 +1,7 @@
import { join, resolve } from 'node:path' import { join, resolve } from 'node:path'
import { parse } from 'semver'
import { import {
applyDefaultCreateNpmDirsOptions, applyDefaultCreateNpmDirsOptions,
CreateNpmDirsOptions, CreateNpmDirsOptions,
@ -88,6 +90,24 @@ export async function createNpmDirs(userOptions: CreateNpmDirsOptions) {
const entry = `${binaryName}.wasi.cjs` const entry = `${binaryName}.wasi.cjs`
scopedPackageJson.files.push(entry, `wasi-worker.mjs`) scopedPackageJson.files.push(entry, `wasi-worker.mjs`)
scopedPackageJson.main = entry 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( const emnapiCore = await fetch(
`https://registry.npmjs.org/@emnapi/core`, `https://registry.npmjs.org/@emnapi/core`,
).then((res) => res.json() as Promise<PackageMeta>) ).then((res) => res.json() as Promise<PackageMeta>)

View file

@ -317,6 +317,13 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
} catch { } catch {
// ignore // ignore
} }
if (!nativeBinding) {
try {
nativeBinding = require('${pkgName}-wasm32-wasi')
} catch (err) {
console.error(err)
}
}
} }
if (!nativeBinding) { if (!nativeBinding) {

View file

@ -594,6 +594,7 @@ __metadata:
js-yaml: "npm:^4.1.0" js-yaml: "npm:^4.1.0"
lodash-es: "npm:^4.17.21" lodash-es: "npm:^4.17.21"
prettier: "npm:^3.1.0" prettier: "npm:^3.1.0"
semver: "npm:^7.5.4"
toml: "npm:^3.0.0" toml: "npm:^3.0.0"
ts-node: "npm:^10.9.1" ts-node: "npm:^10.9.1"
tslib: "npm:^2.6.2" tslib: "npm:^2.6.2"