Skip to content

Commit 30a03a3

Browse files
hi-ogawaBoshen
andauthored
refactor(napi/playground): use napi-rs wasm for playground (#9803)
playground side PR: oxc-project/playground#71 I went ahead testing out this. Let me know if this is not the plan. I think the benefit is that this allows rust-side playground code to reuse some code from other napi crates, for example, `oxc_napi` and `oxc_parser_napi`. For now, I started as mostly copying paste it and replacing `#[wasm_bindgen]` with `#[napi]`, but I also used `oxc_napi` for `OxcError` which replaced diagnostic serialization code from playground. --------- Co-authored-by: Boshen <[email protected]>
1 parent 2b65ed2 commit 30a03a3

18 files changed

+1457
-46
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -111,37 +111,12 @@ jobs:
111111
- run: pnpm napi build --target wasm32-wasip1-threads --manifest-path ./napi/parser/Cargo.toml
112112
- run: pnpm napi build --target wasm32-wasip1-threads --manifest-path ./napi/transform/Cargo.toml
113113
- run: pnpm napi build --target wasm32-wasip1-threads --manifest-path ./napi/minify/Cargo.toml
114+
- run: pnpm napi build --target wasm32-wasip1-threads --manifest-path ./napi/playground/Cargo.toml
114115
# Fix `index.d.ts`
115116
- run: node napi/parser/scripts/fix-wasm-dts.mjs
116117
- run: cargo test --target wasm32-wasip1-threads ${TEST_FLAGS}
117118
- run: git diff --exit-code # Must commit everything
118119

119-
test-wasm32-unknown-unknown:
120-
name: Check wasm32-unknown-unknown
121-
runs-on: ubuntu-latest
122-
steps:
123-
- uses: taiki-e/checkout-action@b13d20b7cda4e2f325ef19895128f7ff735c0b3d # v1.3.1
124-
125-
- uses: oxc-project/setup-rust@cd82e1efec7fef815e2c23d296756f31c7cdc03d # v1.0.0
126-
with:
127-
cache-key: wasm
128-
save-cache: ${{ github.ref_name == 'main' }}
129-
tools: wasm-pack,just
130-
- name: Check
131-
run: |
132-
rustup target add wasm32-unknown-unknown
133-
cargo check -p oxc_wasm --target wasm32-unknown-unknown
134-
- uses: ./.github/actions/pnpm
135-
136-
- run: just build-wasm debug
137-
- working-directory: npm/oxc-wasm
138-
run: pnpm run check
139-
140-
- working-directory: wasm/parser
141-
run: pnpm run build
142-
- working-directory: npm/parser-wasm
143-
run: pnpm run check
144-
145120
typos:
146121
name: Spell Check
147122
runs-on: ubuntu-latest
@@ -319,6 +294,7 @@ jobs:
319294
if: steps.filter.outputs.src == 'true'
320295
- if: steps.filter.outputs.src == 'true'
321296
run: |
297+
rustup target add wasm32-wasip1-threads
322298
pnpm run build
323299
pnpm run test
324300
pnpm --filter e2e run test

Cargo.lock

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

dprint.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
"**/CHANGELOG.md",
1818
"pnpm-workspace.yaml",
1919
"pnpm-lock.yaml",
20-
"napi/{transform,minify}/index.js",
21-
"napi/{parser,transform,minify}/index.d.ts",
22-
"napi/{parser,transform,minify}/*.wasi-browser.js",
23-
"napi/{parser,transform,minify}/*.wasi.cjs",
24-
"napi/{parser,transform,minify}/wasi-worker-browser.mjs",
25-
"napi/{parser,transform,minify}/wasi-worker.mjs",
26-
"napi/{parser,transform,minify}/browser.js",
20+
"napi/{transform,minify,playground}/index.js",
21+
"napi/{parser,transform,minify,playground}/index.d.ts",
22+
"napi/{parser,transform,minify,playground}/*.wasi-browser.js",
23+
"napi/{parser,transform,minify,playground}/*.wasi.cjs",
24+
"napi/{parser,transform,minify,playground}/wasi-worker-browser.mjs",
25+
"napi/{parser,transform,minify,playground}/wasi-worker.mjs",
26+
"napi/{parser,transform,minify,playground}/browser.js",
2727
"napi/parser/bindings.js",
2828
"npm/*/package.json",
2929
"npm/oxlint/configuration_schema.json",

justfile

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,11 @@ test-estree *args='':
154154
install-wasm:
155155
cargo binstall wasm-pack
156156

157-
watch-wasm:
158-
just watch 'just build-wasm dev'
159-
160-
build-wasm mode="release":
161-
wasm-pack build crates/oxc_wasm --no-pack --target web --scope oxc --out-dir ../../npm/oxc-wasm --{{mode}}
162-
cp crates/oxc_wasm/package.json npm/oxc-wasm/package.json
163-
rm npm/oxc-wasm/.gitignore
164-
node ./crates/oxc_wasm/update-bindings.mjs
157+
watch-playground:
158+
just watch 'pnpm --filter oxc-playground dev'
159+
160+
build-playground mode="release":
161+
pnpm --filter oxc-playground build
165162

166163
# Generate the JavaScript global variables. See `tasks/javascript_globals`
167164
javascript-globals:

napi/playground/Cargo.toml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[package]
2+
name = "oxc_playground_napi"
3+
version = "0.0.1"
4+
authors.workspace = true
5+
categories.workspace = true
6+
edition.workspace = true
7+
homepage.workspace = true
8+
include = ["/src", "build.rs"]
9+
keywords.workspace = true
10+
license.workspace = true
11+
publish = false
12+
repository.workspace = true
13+
rust-version.workspace = true
14+
description.workspace = true
15+
16+
[lints]
17+
workspace = true
18+
19+
[lib]
20+
crate-type = ["cdylib", "lib"]
21+
test = false
22+
doctest = false
23+
24+
[dependencies]
25+
oxc = { workspace = true, features = ["ast_visit", "codegen", "minifier", "semantic", "serialize", "transformer", "isolated_declarations"] }
26+
oxc_index = { workspace = true }
27+
oxc_linter = { workspace = true }
28+
oxc_napi = { workspace = true }
29+
oxc_prettier = { workspace = true }
30+
31+
napi = { workspace = true }
32+
napi-derive = { workspace = true }
33+
serde = { workspace = true }
34+
serde_json = { workspace = true }
35+
36+
[package.metadata.cargo-shear]
37+
ignored = ["napi"]
38+
39+
[build-dependencies]
40+
napi-build = { workspace = true }

napi/playground/browser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '@oxc-playground/binding-wasm32-wasi'

napi/playground/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
napi_build::setup();
3+
}

napi/playground/index.d.ts

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/* auto-generated by NAPI-RS */
2+
/* eslint-disable */
3+
export declare class Oxc {
4+
astJson: string
5+
ir: string
6+
controlFlowGraph: string
7+
symbolsJson: string
8+
scopeText: string
9+
codegenText: string
10+
codegenSourcemapText?: string
11+
formattedText: string
12+
prettierFormattedText: string
13+
prettierIrText: string
14+
constructor()
15+
getDiagnostics2(): Array<OxcError>
16+
getDiagnostics(): Array<OxcDiagnostic>
17+
getComments(): Array<Comment>
18+
/**
19+
* # Errors
20+
* Serde serialization error
21+
*/
22+
run(sourceText: string, options: OxcOptions): void
23+
}
24+
25+
export interface Comment {
26+
type: CommentType
27+
value: string
28+
start: number
29+
end: number
30+
}
31+
32+
export declare const enum CommentType {
33+
Line = 0,
34+
Block = 1
35+
}
36+
37+
export interface ErrorLabel {
38+
message?: string
39+
start: number
40+
end: number
41+
}
42+
43+
export interface OxcCodegenOptions {
44+
indentation?: number
45+
enableTypescript?: boolean
46+
enableSourcemap?: boolean
47+
}
48+
49+
export interface OxcCompressOptions {
50+
booleans: boolean
51+
dropDebugger: boolean
52+
dropConsole: boolean
53+
evaluate: boolean
54+
joinVars: boolean
55+
loops: boolean
56+
typeofs: boolean
57+
}
58+
59+
export interface OxcControlFlowOptions {
60+
verbose?: boolean
61+
}
62+
63+
export interface OxcDiagnostic {
64+
start: number
65+
end: number
66+
severity: string
67+
message: string
68+
}
69+
70+
export interface OxcError {
71+
severity: Severity
72+
message: string
73+
labels: Array<ErrorLabel>
74+
helpMessage?: string
75+
}
76+
77+
export interface OxcLinterOptions {
78+
79+
}
80+
81+
export interface OxcMinifierOptions {
82+
whitespace?: boolean
83+
mangle?: boolean
84+
compress?: boolean
85+
compressOptions?: OxcCompressOptions
86+
}
87+
88+
export interface OxcOptions {
89+
run?: OxcRunOptions
90+
parser?: OxcParserOptions
91+
linter?: OxcLinterOptions
92+
transformer?: OxcTransformerOptions
93+
codegen?: OxcCodegenOptions
94+
minifier?: OxcMinifierOptions
95+
controlFlow?: OxcControlFlowOptions
96+
}
97+
98+
export interface OxcParserOptions {
99+
allowReturnOutsideFunction?: boolean
100+
preserveParens?: boolean
101+
allowV8Intrinsics?: boolean
102+
sourceType?: string
103+
sourceFilename?: string
104+
}
105+
106+
export interface OxcRunOptions {
107+
syntax?: boolean
108+
lint?: boolean
109+
format?: boolean
110+
prettierFormat?: boolean
111+
prettierIr?: boolean
112+
transform?: boolean
113+
typeCheck?: boolean
114+
scope?: boolean
115+
symbol?: boolean
116+
}
117+
118+
export interface OxcTransformerOptions {
119+
target?: string
120+
isolatedDeclarations?: boolean
121+
}
122+
123+
export declare const enum Severity {
124+
Error = 'Error',
125+
Warning = 'Warning',
126+
Advice = 'Advice'
127+
}

0 commit comments

Comments
 (0)