feat(cli): change wasm binding output format to cjs (#1831)
This commit is contained in:
parent
a888425871
commit
d5ecf90d29
4 changed files with 211 additions and 219 deletions
cli/src/api
|
@ -658,12 +658,14 @@ class Builder {
|
|||
) {
|
||||
if (distFileName && wasiRegisterFunctions.length) {
|
||||
const { name, dir } = parse(distFileName)
|
||||
const newPath = join(dir, `${this.config.binaryName}.wasi.mjs`)
|
||||
const newPath = join(dir, `${this.config.binaryName}.wasi.cjs`)
|
||||
const workerPath = join(dir, 'wasi-worker.mjs')
|
||||
const declareCodes = `const { ${idents.join(', ')} } = binding\n`
|
||||
const exportsCode = `export {\n${idents
|
||||
.map((ident) => ` ${ident}`)
|
||||
.join(',\n')}\n}`
|
||||
const declareCodes = `const { ${idents.join(
|
||||
', ',
|
||||
)} } = __napiModule.exports\n`
|
||||
const exportsCode = idents
|
||||
.map((ident) => `module.exports.${ident} = ${ident}`)
|
||||
.join(',\n')
|
||||
await writeFileAsync(
|
||||
newPath,
|
||||
createWasiBinding(name, wasiRegisterFunctions) +
|
||||
|
|
|
@ -6,25 +6,22 @@ export const createWasiBinding = (
|
|||
|
||||
/* auto-generated by NAPI-RS */
|
||||
|
||||
import * as __nodeFsPromises from 'node:fs/promises'
|
||||
import * as __nodePath from 'node:path'
|
||||
import { WASI as __nodeWASI } from 'node:wasi'
|
||||
import { Worker } from 'node:worker_threads'
|
||||
import * as __nodeURL from 'node:url'
|
||||
const __nodeFs= require('node:fs')
|
||||
const __nodePath = require('node:path')
|
||||
const { WASI: __nodeWASI } = require('node:wasi')
|
||||
const { Worker } = require('node:worker_threads')
|
||||
|
||||
import { instantiateNapiModule as __emnapiInstantiateNapiModule } from '@emnapi/core'
|
||||
import { getDefaultContext as __emnapiGetDefaultContext } from '@emnapi/runtime'
|
||||
const { instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync } = require('@emnapi/core')
|
||||
const { getDefaultContext: __emnapiGetDefaultContext } = require('@emnapi/runtime')
|
||||
|
||||
const __wasi = new __nodeWASI({
|
||||
version: 'preview1',
|
||||
env: process.env,
|
||||
preopens: {
|
||||
'/': __nodePath.join(__nodeURL.fileURLToPath(import.meta.url), '..'),
|
||||
'/': __dirname,
|
||||
}
|
||||
})
|
||||
|
||||
const __dirname = __nodePath.join(__nodeURL.fileURLToPath(import.meta.url), '..')
|
||||
|
||||
const __emnapiContext = __emnapiGetDefaultContext()
|
||||
|
||||
const __sharedMemory = new WebAssembly.Memory({
|
||||
|
@ -33,7 +30,7 @@ const __sharedMemory = new WebAssembly.Memory({
|
|||
shared: true,
|
||||
})
|
||||
|
||||
const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = await __emnapiInstantiateNapiModule(__nodeFsPromises.readFile(__nodePath.join(__dirname, '${wasmFileName}.wasm')), {
|
||||
const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, '${wasmFileName}.wasm')), {
|
||||
context: __emnapiContext,
|
||||
asyncWorkPoolSize: 4,
|
||||
wasi: __wasi,
|
||||
|
@ -50,6 +47,7 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
|
|||
...importObject.emnapi,
|
||||
memory: __sharedMemory,
|
||||
}
|
||||
return importObject
|
||||
},
|
||||
beforeInit({ instance }) {
|
||||
__napi_rs_initialize_modules(instance)
|
||||
|
@ -61,6 +59,4 @@ ${wasiRegisterFunctions
|
|||
.map((name) => ` __napiInstance.exports['${name}']()`)
|
||||
.join('\n')}
|
||||
}
|
||||
|
||||
const binding = __napiModule.exports
|
||||
`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue