fix(cli): missing files in created wasi package (#1894)

This commit is contained in:
LongYinan 2024-01-09 00:34:59 +08:00 committed by GitHub
parent ffbaba3a9b
commit 9b8dab6b63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 35 additions and 61 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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