Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): init oxnode #23

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,55 +62,55 @@ jobs:
settings:
- host: macos-latest
target: x86_64-apple-darwin
build: pnpm build --target x86_64-apple-darwin
build: pnpm --filter=@oxc-node/core build --target x86_64-apple-darwin
- host: windows-latest
build: pnpm build --target x86_64-pc-windows-msvc
build: pnpm --filter=@oxc-node/core build --target x86_64-pc-windows-msvc
target: x86_64-pc-windows-msvc
- host: windows-latest
build: pnpm build --target i686-pc-windows-msvc
build: pnpm --filter=@oxc-node/core build --target i686-pc-windows-msvc
target: i686-pc-windows-msvc
- host: ubuntu-latest
target: x86_64-unknown-linux-gnu
build: pnpm build --target x86_64-unknown-linux-gnu --use-napi-cross
build: pnpm --filter=@oxc-node/core build --target x86_64-unknown-linux-gnu --use-napi-cross
- host: ubuntu-latest
target: x86_64-unknown-linux-musl
build: pnpm build --target x86_64-unknown-linux-musl -x
build: pnpm --filter=@oxc-node/core build --target x86_64-unknown-linux-musl -x
- host: macos-latest
target: aarch64-apple-darwin
build: pnpm build --target aarch64-apple-darwin
build: pnpm --filter=@oxc-node/core build --target aarch64-apple-darwin
- host: ubuntu-latest
target: aarch64-unknown-linux-gnu
build: pnpm build --target aarch64-unknown-linux-gnu --use-napi-cross
build: pnpm --filter=@oxc-node/core build --target aarch64-unknown-linux-gnu --use-napi-cross
- host: ubuntu-latest
target: armv7-unknown-linux-gnueabihf
build: pnpm build --target armv7-unknown-linux-gnueabihf --use-napi-cross
build: pnpm --filter=@oxc-node/core build --target armv7-unknown-linux-gnueabihf --use-napi-cross
- host: ubuntu-latest
target: aarch64-linux-android
build: pnpm build --target aarch64-linux-android
build: pnpm --filter=@oxc-node/core build --target aarch64-linux-android
- host: ubuntu-latest
target: armv7-linux-androideabi
build: pnpm build --target armv7-linux-androideabi
build: pnpm --filter=@oxc-node/core build --target armv7-linux-androideabi
- host: ubuntu-latest
target: aarch64-unknown-linux-musl
build: pnpm build --target aarch64-unknown-linux-musl -x
build: pnpm --filter=@oxc-node/core build --target aarch64-unknown-linux-musl -x
- host: windows-latest
target: aarch64-pc-windows-msvc
build: pnpm build --target aarch64-pc-windows-msvc
build: pnpm --filter=@oxc-node/core build --target aarch64-pc-windows-msvc
- host: ubuntu-latest
target: powerpc64le-unknown-linux-gnu
build: |
sudo apt-get update
sudo apt-get install -y gcc-powerpc64le-linux-gnu
pnpm build --target powerpc64le-unknown-linux-gnu
pnpm --filter=@oxc-node/core build --target powerpc64le-unknown-linux-gnu
- host: ubuntu-latest
target: s390x-unknown-linux-gnu
build: |
sudo apt-get update
sudo apt-get install -y gcc-s390x-linux-gnu
pnpm build --target s390x-unknown-linux-gnu
pnpm --filter=@oxc-node/core build --target s390x-unknown-linux-gnu
- host: ubuntu-latest
target: wasm32-wasip1-threads
build: pnpm build --target wasm32-wasip1-threads
build: pnpm --filter=@oxc-node/core build --target wasm32-wasip1-threads
name: stable - ${{ matrix.settings.target }} - node@20
runs-on: ${{ matrix.settings.host }}
steps:
Expand Down Expand Up @@ -204,14 +204,15 @@ jobs:
env
sudo corepack enable
pnpm install
pnpm build --target x86_64-unknown-freebsd
pnpm --filter=@oxc-node/core build --target x86_64-unknown-freebsd
pnpm --filter="@oxc-node/cli" build
pnpm --filter="\!integrate-ava" run test
rm -rf node_modules
rm -rf target
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: bindings-freebsd
name: bindings-x86_64-unknown-freebsd
path: ./packages/core/*.node
if-no-files-found: error

Expand Down Expand Up @@ -259,6 +260,10 @@ jobs:
- name: List packages
run: ls -R .
shell: bash
- name: Build cli
run: pnpm --filter="@oxc-node/cli" build
- name: Rebuild
run: pnpm rb
- name: Test bindings
run: pnpm test
env:
Expand Down Expand Up @@ -329,6 +334,8 @@ jobs:
- name: List packages
run: ls -R .
shell: bash
- name: Build cli
run: pnpm --filter="@oxc-node/cli" build
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
with:
Expand All @@ -339,7 +346,11 @@ jobs:
with:
image: ${{ steps.docker.outputs.IMAGE }}
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 }}
run: corepack enable && npm run test
run: >-
corepack enable &&
pnpm config set store-dir ${{ steps.docker.outputs.PNPM_STORE_PATH }} --global &&
pnpm rb &&
pnpm test
test-wasi:
name: Test WASI target
needs:
Expand All @@ -364,6 +375,10 @@ jobs:
- name: List packages
run: ls -R .
shell: bash
- name: Build cli
run: pnpm --filter="@oxc-node/cli" build
- name: Rebuild
run: pnpm rb
- name: Test bindings
run: pnpm test
env:
Expand Down Expand Up @@ -399,6 +414,8 @@ jobs:
- name: List packages
run: ls -R ./npm
shell: bash
- name: Build cli
run: pnpm --filter="@oxc-node/cli" build
- name: Publish
run: |
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"devDependencies": {
"@napi-rs/cli": "^3.0.0-alpha.62",
"@napi-rs/wasm-runtime": "^0.2.4",
"@oxc-node/cli": "workspace:*",
"@oxc-node/core": "workspace:*",
"@taplo/cli": "^0.7.0",
"@types/node": "^20.14.9",
"ava": "^6.1.3",
"cross-env": "^7.0.3",
"emnapi": "^1.2.0",
"husky": "^9.0.11",
"lerna": "8.1.4",
"lint-staged": "^15.2.7",
"npm-run-all2": "^6.2.2",
"oxlint": "^0.5.2",
Expand All @@ -22,7 +22,6 @@
},
"scripts": {
"bench": "pnpm --filter=bench bench",
"build": "napi build --platform --release -o packages/core --package-json-path packages/core/package.json",
"format": "taplo fmt && prettier --write . && cargo fmt",
"lint": "oxlint",
"test": "pnpm --sequential --filter=integrate* run test",
Expand Down
18 changes: 14 additions & 4 deletions packages/bench/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'

import { transformSync } from '@swc/core'
import { transformSync as swc } from '@swc/core'
import { transform as oxc } from '@oxc-node/core'
import { transformSync as esbuild } from 'esbuild'
import { Bench } from 'tinybench'
import ts from 'typescript'

const bench = new Bench({ time: 1000 })
// https://github.com/tinylibs/tinybench/issues/83
const bench = new Bench({ iterations: 1000 })

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

bench
.add('@swc/core', () => {
transformSync(fixture, {
swc(fixture, {
filename: 'ajax.ts',
jsc: {
target: 'esnext',
Expand All @@ -26,10 +28,18 @@ bench
decorators: false,
},
},
sourceMaps: true,
})
})
.add('oxc', () => {
oxc('ajax.ts', fixture)
const { source: _source, sourceMap: _sourceMap } = oxc('ajax.ts', fixture)
})
.add('esbuild', () => {
esbuild(fixture, {
loader: 'ts',
format: 'esm',
target: 'esnext',
})
})
.add('typescript', () => {
ts.transpileModule(fixture, {
Expand Down
7 changes: 4 additions & 3 deletions packages/bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"private": true,
"type": "module",
"scripts": {
"bench": "node --import @oxc-node/core/register index.ts"
"bench": "oxnode index.ts"
},
"devDependencies": {
"@oxc-node/core": "workspace:*",
"@swc/core": "=1.6.7",
"@oxc-node/cli": "workspace:*",
"@swc/core": "^1.7.0",
"esbuild": "^0.23.0",
"tinybench": "^2.8.0",
"rxjs": "^7.8.1",
"typescript": "^5.5.3"
Expand Down
34 changes: 34 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@oxc-node/cli",
"version": "0.0.0",
"license": "MIT",
"type": "module",
"description": "OXC Node cli",
"bin": {
"oxnode": "./dist/index.js"
},
"dependencies": {
"@oxc-node/core": "workspace:*"
},
"devDependencies": {
"clipanion": "^4.0.0-rc.3",
"rolldown": "^0.12.2"
},
"files": [
"dist"
],
"scripts": {
"build": "rolldown --config ./rolldown.config.js",
"dev": "node --import @oxc-node/core/register ./src/index.ts"
},
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
},
{
"type": "github",
"url": "https://github.com/sponsors/Boshen"
}
]
}
16 changes: 16 additions & 0 deletions packages/cli/rolldown.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'rolldown'

export default defineConfig({
input: './src/index.ts',
resolve: {
conditionNames: ['module', 'node'],
mainFields: ['module', 'main'],
},
platform: 'node',
external: ['@oxc-node/core'],
treeshake: true,
output: {
format: 'esm',
assetFileNames: '[name].js',
},
})
83 changes: 83 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import process from 'node:process'
import { exec, execSync } from 'node:child_process'

import { Command, Option, Cli, Usage, Builtins } from 'clipanion'

import pkgJson from '../package.json' with { type: 'json' }

const [node, app, ...stdArgs] = process.argv

class MainCommand extends Command {
static paths = [[]]

static usage: Usage = {
description: `Run a script with oxc transformer and oxc-resolver`,
details: `oxnode is a CLI tool that runs a script with oxc transformer and oxc-resolver.
The esm module is resolved by oxc-resolver and transformed by oxc transformer.
The cjs module support will be added in the future.
`,
examples: [
[`Run a script`, `oxnode ./src/index.ts`],
[`repl`, `oxnode`],
],
}

help = Option.Boolean(`-h,--help`, false, {
description: `Show help`,
})

nodeHelp = Option.Boolean(`--node-help`, false, {
description: `Show Node.js help`,
})

args = Option.Rest()

async execute() {
if (this.help) {
this.context.stdout.write(this.cli.usage())
return
}
if (this.nodeHelp) {
this.args.push(`--help `)
}
const args = this.args.length ? ` ${this.args.join(' ')}` : ''
if (!args.length) {
execSync(`node --import @oxc-node/core/register`, {
env: process.env,
cwd: process.cwd(),
stdio: `inherit`,
})
return
}
const cp = exec(`node --import @oxc-node/core/register ${args}`, {
env: process.env,
cwd: process.cwd(),
})
cp.addListener(`error`, (error) => {
console.error(error)
})
if (cp.stdin) {
this.context.stdin.pipe(cp.stdin)
}
if (cp.stdout) {
cp.stdout.pipe(this.context.stdout)
}
if (cp.stderr) {
cp.stderr.pipe(this.context.stderr)
}
cp.addListener(`exit`, (code) => {
process.exit(code ?? 0)
})
}
}

const cli = new Cli({
binaryLabel: `oxnode`,
binaryName: `${node} ${app}`,
binaryVersion: pkgJson.version,
})

cli.register(MainCommand)
cli.register(Builtins.HelpCommand)
cli.register(Builtins.VersionCommand)
cli.runExit(stdArgs)
11 changes: 11 additions & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "./dist",
"rootDir": "./src",
"resolveJsonModule": true
},
"include": ["package.json", "./src"],
"exclude": ["__tests__", "./dist"]
}
3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"register.mjs",
"browser.js"
],
"scripts": {
"build": "napi build --platform --release --manifest-path ../../Cargo.toml -o ."
},
"exports": {
".": {
"import": "./index.js",
Expand Down
Loading