Skip to content

Commit

Permalink
feat(cli): change wasm binding output format to cjs (#1831)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Nov 30, 2023
1 parent a888425 commit d5ecf90
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 219 deletions.
12 changes: 7 additions & 5 deletions cli/src/api/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) +
Expand Down
22 changes: 9 additions & 13 deletions cli/src/api/load-wasi-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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,
Expand All @@ -50,6 +47,7 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
...importObject.emnapi,
memory: __sharedMemory,
}
return importObject
},
beforeInit({ instance }) {
__napi_rs_initialize_modules(instance)
Expand All @@ -61,6 +59,4 @@ ${wasiRegisterFunctions
.map((name) => ` __napiInstance.exports['${name}']()`)
.join('\n')}
}
const binding = __napiModule.exports
`
2 changes: 1 addition & 1 deletion examples/napi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const require = createRequire(import.meta.url)
let exports

if (process.env.WASI_TEST) {
exports = await import('./index.wasi.mjs')
exports = require('./index.wasi.cjs')
} else {
exports = require('./index.node')
}
Expand Down

0 comments on commit d5ecf90

Please sign in to comment.