Skip to content

Commit 8740e05

Browse files
authored
feat(cli): init oxnode (#23)
1 parent 6585245 commit 8740e05

File tree

13 files changed

+676
-3920
lines changed

13 files changed

+676
-3920
lines changed

Diff for: .github/workflows/CI.yml

+35-18
Original file line numberDiff line numberDiff line change
@@ -62,55 +62,55 @@ jobs:
6262
settings:
6363
- host: macos-latest
6464
target: x86_64-apple-darwin
65-
build: pnpm build --target x86_64-apple-darwin
65+
build: pnpm --filter=@oxc-node/core build --target x86_64-apple-darwin
6666
- host: windows-latest
67-
build: pnpm build --target x86_64-pc-windows-msvc
67+
build: pnpm --filter=@oxc-node/core build --target x86_64-pc-windows-msvc
6868
target: x86_64-pc-windows-msvc
6969
- host: windows-latest
70-
build: pnpm build --target i686-pc-windows-msvc
70+
build: pnpm --filter=@oxc-node/core build --target i686-pc-windows-msvc
7171
target: i686-pc-windows-msvc
7272
- host: ubuntu-latest
7373
target: x86_64-unknown-linux-gnu
74-
build: pnpm build --target x86_64-unknown-linux-gnu --use-napi-cross
74+
build: pnpm --filter=@oxc-node/core build --target x86_64-unknown-linux-gnu --use-napi-cross
7575
- host: ubuntu-latest
7676
target: x86_64-unknown-linux-musl
77-
build: pnpm build --target x86_64-unknown-linux-musl -x
77+
build: pnpm --filter=@oxc-node/core build --target x86_64-unknown-linux-musl -x
7878
- host: macos-latest
7979
target: aarch64-apple-darwin
80-
build: pnpm build --target aarch64-apple-darwin
80+
build: pnpm --filter=@oxc-node/core build --target aarch64-apple-darwin
8181
- host: ubuntu-latest
8282
target: aarch64-unknown-linux-gnu
83-
build: pnpm build --target aarch64-unknown-linux-gnu --use-napi-cross
83+
build: pnpm --filter=@oxc-node/core build --target aarch64-unknown-linux-gnu --use-napi-cross
8484
- host: ubuntu-latest
8585
target: armv7-unknown-linux-gnueabihf
86-
build: pnpm build --target armv7-unknown-linux-gnueabihf --use-napi-cross
86+
build: pnpm --filter=@oxc-node/core build --target armv7-unknown-linux-gnueabihf --use-napi-cross
8787
- host: ubuntu-latest
8888
target: aarch64-linux-android
89-
build: pnpm build --target aarch64-linux-android
89+
build: pnpm --filter=@oxc-node/core build --target aarch64-linux-android
9090
- host: ubuntu-latest
9191
target: armv7-linux-androideabi
92-
build: pnpm build --target armv7-linux-androideabi
92+
build: pnpm --filter=@oxc-node/core build --target armv7-linux-androideabi
9393
- host: ubuntu-latest
9494
target: aarch64-unknown-linux-musl
95-
build: pnpm build --target aarch64-unknown-linux-musl -x
95+
build: pnpm --filter=@oxc-node/core build --target aarch64-unknown-linux-musl -x
9696
- host: windows-latest
9797
target: aarch64-pc-windows-msvc
98-
build: pnpm build --target aarch64-pc-windows-msvc
98+
build: pnpm --filter=@oxc-node/core build --target aarch64-pc-windows-msvc
9999
- host: ubuntu-latest
100100
target: powerpc64le-unknown-linux-gnu
101101
build: |
102102
sudo apt-get update
103103
sudo apt-get install -y gcc-powerpc64le-linux-gnu
104-
pnpm build --target powerpc64le-unknown-linux-gnu
104+
pnpm --filter=@oxc-node/core build --target powerpc64le-unknown-linux-gnu
105105
- host: ubuntu-latest
106106
target: s390x-unknown-linux-gnu
107107
build: |
108108
sudo apt-get update
109109
sudo apt-get install -y gcc-s390x-linux-gnu
110-
pnpm build --target s390x-unknown-linux-gnu
110+
pnpm --filter=@oxc-node/core build --target s390x-unknown-linux-gnu
111111
- host: ubuntu-latest
112112
target: wasm32-wasip1-threads
113-
build: pnpm build --target wasm32-wasip1-threads
113+
build: pnpm --filter=@oxc-node/core build --target wasm32-wasip1-threads
114114
name: stable - ${{ matrix.settings.target }} - node@20
115115
runs-on: ${{ matrix.settings.host }}
116116
steps:
@@ -204,14 +204,15 @@ jobs:
204204
env
205205
sudo corepack enable
206206
pnpm install
207-
pnpm build --target x86_64-unknown-freebsd
207+
pnpm --filter=@oxc-node/core build --target x86_64-unknown-freebsd
208+
pnpm --filter="@oxc-node/cli" build
208209
pnpm --filter="\!integrate-ava" run test
209210
rm -rf node_modules
210211
rm -rf target
211212
- name: Upload artifact
212213
uses: actions/upload-artifact@v4
213214
with:
214-
name: bindings-freebsd
215+
name: bindings-x86_64-unknown-freebsd
215216
path: ./packages/core/*.node
216217
if-no-files-found: error
217218

@@ -259,6 +260,10 @@ jobs:
259260
- name: List packages
260261
run: ls -R .
261262
shell: bash
263+
- name: Build cli
264+
run: pnpm --filter="@oxc-node/cli" build
265+
- name: Rebuild
266+
run: pnpm rb
262267
- name: Test bindings
263268
run: pnpm test
264269
env:
@@ -329,6 +334,8 @@ jobs:
329334
- name: List packages
330335
run: ls -R .
331336
shell: bash
337+
- name: Build cli
338+
run: pnpm --filter="@oxc-node/cli" build
332339
- name: Set up QEMU
333340
uses: docker/setup-qemu-action@v3
334341
with:
@@ -339,7 +346,11 @@ jobs:
339346
with:
340347
image: ${{ steps.docker.outputs.IMAGE }}
341348
options: -v ${{ steps.docker.outputs.PNPM_STORE_PATH }}:${{ steps.docker.outputs.PNPM_STORE_PATH }} -v ${{ github.workspace }}:${{ github.workspace }} -w ${{ github.workspace }} --platform ${{ steps.docker.outputs.PLATFORM }}
342-
run: corepack enable && npm run test
349+
run: >-
350+
corepack enable &&
351+
pnpm config set store-dir ${{ steps.docker.outputs.PNPM_STORE_PATH }} --global &&
352+
pnpm rb &&
353+
pnpm test
343354
test-wasi:
344355
name: Test WASI target
345356
needs:
@@ -364,6 +375,10 @@ jobs:
364375
- name: List packages
365376
run: ls -R .
366377
shell: bash
378+
- name: Build cli
379+
run: pnpm --filter="@oxc-node/cli" build
380+
- name: Rebuild
381+
run: pnpm rb
367382
- name: Test bindings
368383
run: pnpm test
369384
env:
@@ -399,6 +414,8 @@ jobs:
399414
- name: List packages
400415
run: ls -R ./npm
401416
shell: bash
417+
- name: Build cli
418+
run: pnpm --filter="@oxc-node/cli" build
402419
- name: Publish
403420
run: |
404421
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc

Diff for: package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
"devDependencies": {
77
"@napi-rs/cli": "^3.0.0-alpha.62",
88
"@napi-rs/wasm-runtime": "^0.2.4",
9+
"@oxc-node/cli": "workspace:*",
910
"@oxc-node/core": "workspace:*",
1011
"@taplo/cli": "^0.7.0",
1112
"@types/node": "^20.14.9",
1213
"ava": "^6.1.3",
1314
"cross-env": "^7.0.3",
1415
"emnapi": "^1.2.0",
1516
"husky": "^9.0.11",
16-
"lerna": "8.1.4",
1717
"lint-staged": "^15.2.7",
1818
"npm-run-all2": "^6.2.2",
1919
"oxlint": "^0.5.2",
@@ -22,7 +22,6 @@
2222
},
2323
"scripts": {
2424
"bench": "pnpm --filter=bench bench",
25-
"build": "napi build --platform --release -o packages/core --package-json-path packages/core/package.json",
2625
"format": "taplo fmt && prettier --write . && cargo fmt",
2726
"lint": "oxlint",
2827
"test": "pnpm --sequential --filter=integrate* run test",

Diff for: packages/bench/index.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ import { readFile } from 'node:fs/promises'
22
import { join } from 'node:path'
33
import { fileURLToPath } from 'node:url'
44

5-
import { transformSync } from '@swc/core'
5+
import { transformSync as swc } from '@swc/core'
66
import { transform as oxc } from '@oxc-node/core'
7+
import { transformSync as esbuild } from 'esbuild'
78
import { Bench } from 'tinybench'
89
import ts from 'typescript'
910

10-
const bench = new Bench({ time: 1000 })
11+
// https://github.com/tinylibs/tinybench/issues/83
12+
const bench = new Bench({ iterations: 1000 })
1113

1214
const fixture = (
1315
await readFile(join(fileURLToPath(import.meta.url), '..', 'node_modules/rxjs/src/internal/ajax/ajax.ts'))
1416
).toString('utf8')
1517

1618
bench
1719
.add('@swc/core', () => {
18-
transformSync(fixture, {
20+
swc(fixture, {
1921
filename: 'ajax.ts',
2022
jsc: {
2123
target: 'esnext',
@@ -26,10 +28,18 @@ bench
2628
decorators: false,
2729
},
2830
},
31+
sourceMaps: true,
2932
})
3033
})
3134
.add('oxc', () => {
32-
oxc('ajax.ts', fixture)
35+
const { source: _source, sourceMap: _sourceMap } = oxc('ajax.ts', fixture)
36+
})
37+
.add('esbuild', () => {
38+
esbuild(fixture, {
39+
loader: 'ts',
40+
format: 'esm',
41+
target: 'esnext',
42+
})
3343
})
3444
.add('typescript', () => {
3545
ts.transpileModule(fixture, {

Diff for: packages/bench/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"private": true,
55
"type": "module",
66
"scripts": {
7-
"bench": "node --import @oxc-node/core/register index.ts"
7+
"bench": "oxnode index.ts"
88
},
99
"devDependencies": {
10-
"@oxc-node/core": "workspace:*",
11-
"@swc/core": "=1.6.7",
10+
"@oxc-node/cli": "workspace:*",
11+
"@swc/core": "^1.7.0",
12+
"esbuild": "^0.23.0",
1213
"tinybench": "^2.8.0",
1314
"rxjs": "^7.8.1",
1415
"typescript": "^5.5.3"

Diff for: packages/cli/package.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@oxc-node/cli",
3+
"version": "0.0.0",
4+
"license": "MIT",
5+
"type": "module",
6+
"description": "OXC Node cli",
7+
"bin": {
8+
"oxnode": "./dist/index.js"
9+
},
10+
"dependencies": {
11+
"@oxc-node/core": "workspace:*"
12+
},
13+
"devDependencies": {
14+
"clipanion": "^4.0.0-rc.3",
15+
"rolldown": "^0.12.2"
16+
},
17+
"files": [
18+
"dist"
19+
],
20+
"scripts": {
21+
"build": "rolldown --config ./rolldown.config.js",
22+
"dev": "node --import @oxc-node/core/register ./src/index.ts"
23+
},
24+
"funding": [
25+
{
26+
"type": "github",
27+
"url": "https://github.com/sponsors/Brooooooklyn"
28+
},
29+
{
30+
"type": "github",
31+
"url": "https://github.com/sponsors/Boshen"
32+
}
33+
]
34+
}

Diff for: packages/cli/rolldown.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { defineConfig } from 'rolldown'
2+
3+
export default defineConfig({
4+
input: './src/index.ts',
5+
resolve: {
6+
conditionNames: ['module', 'node'],
7+
mainFields: ['module', 'main'],
8+
},
9+
platform: 'node',
10+
external: ['@oxc-node/core'],
11+
treeshake: true,
12+
output: {
13+
format: 'esm',
14+
assetFileNames: '[name].js',
15+
},
16+
})

Diff for: packages/cli/src/index.ts

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import process from 'node:process'
2+
import { exec, execSync } from 'node:child_process'
3+
4+
import { Command, Option, Cli, Usage, Builtins } from 'clipanion'
5+
6+
import pkgJson from '../package.json' with { type: 'json' }
7+
8+
const [node, app, ...stdArgs] = process.argv
9+
10+
class MainCommand extends Command {
11+
static paths = [[]]
12+
13+
static usage: Usage = {
14+
description: `Run a script with oxc transformer and oxc-resolver`,
15+
details: `oxnode is a CLI tool that runs a script with oxc transformer and oxc-resolver.
16+
The esm module is resolved by oxc-resolver and transformed by oxc transformer.
17+
The cjs module support will be added in the future.
18+
`,
19+
examples: [
20+
[`Run a script`, `oxnode ./src/index.ts`],
21+
[`repl`, `oxnode`],
22+
],
23+
}
24+
25+
help = Option.Boolean(`-h,--help`, false, {
26+
description: `Show help`,
27+
})
28+
29+
nodeHelp = Option.Boolean(`--node-help`, false, {
30+
description: `Show Node.js help`,
31+
})
32+
33+
args = Option.Rest()
34+
35+
async execute() {
36+
if (this.help) {
37+
this.context.stdout.write(this.cli.usage())
38+
return
39+
}
40+
if (this.nodeHelp) {
41+
this.args.push(`--help `)
42+
}
43+
const args = this.args.length ? ` ${this.args.join(' ')}` : ''
44+
if (!args.length) {
45+
execSync(`node --import @oxc-node/core/register`, {
46+
env: process.env,
47+
cwd: process.cwd(),
48+
stdio: `inherit`,
49+
})
50+
return
51+
}
52+
const cp = exec(`node --import @oxc-node/core/register ${args}`, {
53+
env: process.env,
54+
cwd: process.cwd(),
55+
})
56+
cp.addListener(`error`, (error) => {
57+
console.error(error)
58+
})
59+
if (cp.stdin) {
60+
this.context.stdin.pipe(cp.stdin)
61+
}
62+
if (cp.stdout) {
63+
cp.stdout.pipe(this.context.stdout)
64+
}
65+
if (cp.stderr) {
66+
cp.stderr.pipe(this.context.stderr)
67+
}
68+
cp.addListener(`exit`, (code) => {
69+
process.exit(code ?? 0)
70+
})
71+
}
72+
}
73+
74+
const cli = new Cli({
75+
binaryLabel: `oxnode`,
76+
binaryName: `${node} ${app}`,
77+
binaryVersion: pkgJson.version,
78+
})
79+
80+
cli.register(MainCommand)
81+
cli.register(Builtins.HelpCommand)
82+
cli.register(Builtins.VersionCommand)
83+
cli.runExit(stdArgs)

Diff for: packages/cli/tsconfig.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"outDir": "./dist",
6+
"rootDir": "./src",
7+
"resolveJsonModule": true
8+
},
9+
"include": ["package.json", "./src"],
10+
"exclude": ["__tests__", "./dist"]
11+
}

Diff for: packages/core/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"register.mjs",
1313
"browser.js"
1414
],
15+
"scripts": {
16+
"build": "napi build --platform --release --manifest-path ../../Cargo.toml -o ."
17+
},
1518
"exports": {
1619
".": {
1720
"import": "./index.js",

0 commit comments

Comments
 (0)