Skip to content

Commit dc98667

Browse files
BrooooooklynBoshen
andauthored
chore: re-enable the wasi build (#193)
--------- Co-authored-by: Boshen <[email protected]>
1 parent 98b4ede commit dc98667

File tree

9 files changed

+70
-41
lines changed

9 files changed

+70
-41
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ jobs:
6666
cargo check --target wasm32-unknown-unknown
6767
6868
wasi:
69-
if: false # LinkError: WebAssembly.Instance(): Import #68 module="env" function="uv_default_loop" error: function import requires a callable
7069
name: Test wasi target
7170
runs-on: ubuntu-latest
7271
steps:

.github/workflows/release-napi.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ jobs:
7777
- os: macos-latest
7878
target: aarch64-apple-darwin
7979

80-
# - os: ubuntu-latest
81-
# target: wasm32-wasip1-threads
80+
- os: ubuntu-latest
81+
target: wasm32-wasip1-threads
8282

8383
name: Package ${{ matrix.target }}
8484
runs-on: ${{ matrix.os }}
@@ -93,7 +93,7 @@ jobs:
9393
- uses: goto-bus-stop/setup-zig@v2
9494
if: ${{ contains(matrix.target, 'musl') }}
9595
with:
96-
version: 0.11.0
96+
version: 0.13.0
9797

9898
- name: Build with zig cross
9999
if: ${{ contains(matrix.target, 'musl') }}

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

napi/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ doctest = false
1212

1313
[dependencies]
1414
oxc_resolver = { path = ".." }
15-
napi = { version = "2.16.6", default-features = false, features = ["napi3", "serde-json", "async"] }
16-
napi-derive = { version = "2.16.5" }
15+
napi = { version = "2.16.7", default-features = false, features = ["napi3", "serde-json", "async"] }
16+
napi-derive = { version = "2.16.6" }
1717
tokio = "1.38.0"
1818

1919
[build-dependencies]

napi/resolver.wasi-browser.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@ import {
22
instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
33
getDefaultContext as __emnapiGetDefaultContext,
44
WASI as __WASI,
5+
createOnMessage as __wasmCreateOnMessageForFsProxy,
56
} from '@napi-rs/wasm-runtime'
6-
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from '@napi-rs/wasm-runtime/fs'
7-
7+
import { memfs } from '@napi-rs/wasm-runtime/fs'
88
import __wasmUrl from './resolver.wasm32-wasi.wasm?url'
99

10-
const __fs = __createFsFromVolume(
11-
__Volume.fromJSON({
12-
'/': null,
13-
}),
14-
)
10+
export const { fs: __fs, vol: __volume } = memfs()
1511

1612
const __wasi = new __WASI({
1713
version: 'preview1',
1814
fs: __fs,
15+
preopens: {
16+
'/': '/',
17+
}
1918
})
2019

2120
const __emnapiContext = __emnapiGetDefaultContext()
2221

2322
const __sharedMemory = new WebAssembly.Memory({
24-
initial: 1024,
25-
maximum: 10240,
23+
initial: 4000,
24+
maximum: 65536,
2625
shared: true,
2726
})
2827

@@ -37,9 +36,13 @@ const {
3736
asyncWorkPoolSize: 4,
3837
wasi: __wasi,
3938
onCreateWorker() {
40-
return new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), {
39+
const worker = new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), {
4140
type: 'module',
4241
})
42+
43+
worker.addEventListener('message', __wasmCreateOnMessageForFsProxy(__fs))
44+
45+
return worker
4346
},
4447
overwriteImports(importObject) {
4548
importObject.env = {
@@ -63,7 +66,7 @@ function __napi_rs_initialize_modules(__napiInstance) {
6366
__napiInstance.exports['__napi_register__ResolveResult_struct_4']?.()
6467
__napiInstance.exports['__napi_register__sync_5']?.()
6568
__napiInstance.exports['__napi_register__ResolverFactory_struct_6']?.()
66-
__napiInstance.exports['__napi_register__ResolverFactory_impl_12']?.()
69+
__napiInstance.exports['__napi_register__ResolverFactory_impl_13']?.()
6770
}
6871
export const ResolverFactory = __napiModule.exports.ResolverFactory
6972
export const EnforceExtension = __napiModule.exports.EnforceExtension

napi/resolver.wasi.cjs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,41 @@
33

44
/* auto-generated by NAPI-RS */
55

6-
const __nodeFs= require('node:fs')
6+
const __nodeFs = require('node:fs')
77
const __nodePath = require('node:path')
88
const { WASI: __nodeWASI } = require('node:wasi')
99
const { Worker } = require('node:worker_threads')
1010

1111
const {
1212
instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync,
1313
getDefaultContext: __emnapiGetDefaultContext,
14+
createOnMessage: __wasmCreateOnMessageForFsProxy,
1415
} = require('@napi-rs/wasm-runtime')
1516

17+
const __rootDir = __nodePath.parse(process.cwd()).root
18+
1619
const __wasi = new __nodeWASI({
1720
version: 'preview1',
1821
env: process.env,
1922
preopens: {
20-
'/': '/'
23+
[__rootDir]: __rootDir,
2124
}
2225
})
2326

2427
const __emnapiContext = __emnapiGetDefaultContext()
2528

2629
const __sharedMemory = new WebAssembly.Memory({
27-
initial: 1024,
28-
maximum: 10240,
30+
initial: 4000,
31+
maximum: 65536,
2932
shared: true,
3033
})
3134

3235
let __wasmFilePath = __nodePath.join(__dirname, 'resolver.wasm32-wasi.wasm')
36+
const __wasmDebugFilePath = __nodePath.join(__dirname, 'resolver.wasm32-wasi.debug.wasm')
3337

34-
if (!__nodeFs.existsSync(__wasmFilePath)) {
38+
if (__nodeFs.existsSync(__wasmDebugFilePath)) {
39+
__wasmFilePath = __wasmDebugFilePath
40+
} else if (!__nodeFs.existsSync(__wasmFilePath)) {
3541
try {
3642
__wasmFilePath = __nodePath.resolve('@oxc-resolver/binding-wasm32-wasi')
3743
} catch {
@@ -52,10 +58,14 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
5258
})(),
5359
wasi: __wasi,
5460
onCreateWorker() {
55-
return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), {
61+
const worker = new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), {
5662
env: process.env,
5763
execArgv: ['--experimental-wasi-unstable-preview1'],
5864
})
65+
worker.onmessage = ({ data }) => {
66+
__wasmCreateOnMessageForFsProxy(__nodeFs)(data)
67+
}
68+
return worker
5969
},
6070
overwriteImports(importObject) {
6171
importObject.env = {
@@ -79,7 +89,7 @@ function __napi_rs_initialize_modules(__napiInstance) {
7989
__napiInstance.exports['__napi_register__ResolveResult_struct_4']?.()
8090
__napiInstance.exports['__napi_register__sync_5']?.()
8191
__napiInstance.exports['__napi_register__ResolverFactory_struct_6']?.()
82-
__napiInstance.exports['__napi_register__ResolverFactory_impl_12']?.()
92+
__napiInstance.exports['__napi_register__ResolverFactory_impl_13']?.()
8393
}
8494
module.exports.ResolverFactory = __napiModule.exports.ResolverFactory
8595
module.exports.EnforceExtension = __napiModule.exports.EnforceExtension

napi/wasi-worker-browser.mjs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { instantiateNapiModuleSync, MessageHandler, WASI } from '@napi-rs/wasm-runtime'
2-
import { Volume, createFsFromVolume } from '@napi-rs/wasm-runtime/fs'
1+
import { instantiateNapiModuleSync, MessageHandler, WASI, createFsProxy } from '@napi-rs/wasm-runtime'
2+
import { memfsExported as __memfsExported } from '@napi-rs/wasm-runtime/fs'
33

4-
const fs = createFsFromVolume(
5-
Volume.fromJSON({
6-
'/': null,
7-
}),
8-
)
4+
const fs = createFsProxy(__memfsExported)
95

106
const handler = new MessageHandler({
117
onLoad({ wasmModule, wasmMemory }) {
128
const wasi = new WASI({
139
fs,
10+
preopens: {
11+
'/': '/',
12+
},
1413
print: function () {
1514
// eslint-disable-next-line no-console
1615
console.log.apply(console, arguments)

napi/wasi-worker.mjs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import fs from "node:fs";
22
import { createRequire } from "node:module";
3+
import { parse } from "node:path";
4+
import { WASI } from "node:wasi";
35
import { parentPort, Worker } from "node:worker_threads";
46

5-
import { instantiateNapiModuleSync, MessageHandler, WASI } from "@napi-rs/wasm-runtime";
6-
77
const require = createRequire(import.meta.url);
88

9+
const { instantiateNapiModuleSync, MessageHandler, getDefaultContext } = require("@napi-rs/wasm-runtime");
10+
911
if (parentPort) {
1012
parentPort.on("message", (data) => {
1113
globalThis.onmessage({ data });
@@ -26,13 +28,24 @@ Object.assign(globalThis, {
2628
},
2729
});
2830

31+
const emnapiContext = getDefaultContext();
32+
33+
const __rootDir = parse(process.cwd()).root;
34+
2935
const handler = new MessageHandler({
3036
onLoad({ wasmModule, wasmMemory }) {
31-
const wasi = new WASI({ fs });
37+
const wasi = new WASI({
38+
version: 'preview1',
39+
env: process.env,
40+
preopens: {
41+
[__rootDir]: __rootDir,
42+
},
43+
});
3244

3345
return instantiateNapiModuleSync(wasmModule, {
3446
childThread: true,
3547
wasi,
48+
context: emnapiContext,
3649
overwriteImports(importObject) {
3750
importObject.env = {
3851
...importObject.env,

npm/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
"napi": {
2323
"binaryName": "resolver",
2424
"packageName": "@oxc-resolver/binding",
25+
"wasm": {
26+
"browser": {
27+
"fs": true
28+
}
29+
},
2530
"targets": [
2631
"x86_64-pc-windows-msvc",
2732
"aarch64-pc-windows-msvc",

0 commit comments

Comments
 (0)