Skip to content

Commit

Permalink
feat: setup wasm parser for npm (#2221)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Jan 30, 2024
1 parent f673e41 commit 5ac61f0
Show file tree
Hide file tree
Showing 16 changed files with 492 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
uses: ./.github/actions/rustup
with:
shared-key: 'wasm'
# cache is saved from the website workflow
save-cache: ${{ github.ref_name == 'main' }}

- name: Check
run: |
Expand Down
71 changes: 71 additions & 0 deletions .github/workflows/release_wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Release WASM

on:
workflow_dispatch:
push:
branches:
- main
paths:
- wasm/parser/package.json # Please only commit this file, so we don't need to wait for all the other CI jobs to finish.

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
check:
name: Check version
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
version_changed: ${{ steps.version.outputs.changed }}
steps:
- uses: actions/checkout@v4

- name: Check version changes
uses: EndBug/version-check@v2
id: version
with:
static-checking: localIsNew
file-url: https://unpkg.com/@oxc-parser@wasm/package.json
file-name: wasm/parser/package.json

- name: Set version name
if: steps.version.outputs.changed == 'true'
run: |
echo "Version change found! New version: ${{ steps.version.outputs.version }} (${{ steps.version.outputs.version_type }})"
build:
needs: check
if: needs.check.outputs.version_changed == 'true'
name: Release WASM
runs-on: ubuntu-latest
permissions:
id-token: write # for `npm publish --provenance`
steps:
- uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20
registry-url: 'https://registry.npmjs.org'

- name: Install Rust Toolchain
uses: ./.github/actions/rustup
with:
shared-key: 'wasm'

- name: Build
working-directory: wasm/parser
run: |
rustup target add wasm32-unknown-unknown
corepack enable
pnpm install
pnpm run build
- name: Publish
working-directory: npm/parser-wasm
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npm publish --tag latest --provenance --access public
1 change: 0 additions & 1 deletion .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ jobs:
- name: Install Rust Toolchain
uses: ./.github/actions/rustup
with:
save-cache: ${{ github.ref_name == 'main' }}
shared-key: 'wasm'

- name: Install pnpm
Expand Down
44 changes: 28 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[workspace]
resolver = "2"
members = ["crates/*", "tasks/*", "napi/*"]
members = ["crates/*", "tasks/*", "napi/*", "wasm/*"]
exclude = ["tasks/lint_rules"]

[workspace.package]
authors = ["Boshen <[email protected]>", "Oxc contributors"]
categories = ["development-tools", "web-programming", "compilers"]
description = "Oxc is a JavaScript / TypeScript tooling suite."
description = "A collection of JavaScript tools written in Rust."
edition = "2021"
homepage = "https://oxc-project.github.io"
keywords = ["JavaScript", "TypeScript", "parser", "linter", "minifier"]
Expand Down
2 changes: 1 addition & 1 deletion napi/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "oxc_napi_parser"
name = "oxc_parser_napi"
version = "0.0.0"
publish = false
authors.workspace = true
Expand Down
4 changes: 4 additions & 0 deletions wasm/parser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bin/
pkg/
node_modules/
wasm-pack.log
38 changes: 38 additions & 0 deletions wasm/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "oxc_parser_wasm"
version = "0.0.1"
publish = false
authors.workspace = true
description.workspace = true
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
categories.workspace = true

[lints]
workspace = true

[lib]
crate-type = ["cdylib", "rlib"]
test = false
doctest = false

[features]
default = ["console_error_panic_hook"]

[dependencies]
oxc = { workspace = true, features = ["serde", "wasm"] }
serde = { workspace = true }

wasm-bindgen = { workspace = true }
serde-wasm-bindgen = { workspace = true }
tsify = { workspace = true }

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }
24 changes: 24 additions & 0 deletions wasm/parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## About

Experimental wasm package for the oxc parser, with full TypeScript typings support.

This package is built with `wasm-pack build --release --target web` for bundler (webpack / vite) consumption.
Checkout [oxc-parser](https://www.npmjs.com/package/oxc-parser) for usage in node.js.

Source code: https://github.com/oxc-project/oxc/tree/main/wasm/parser

## 🚴 Usage

```js
import initWasm, { parseSync } from "@oxc-parser/wasm";

async function main() {
await initWasm();

const code = "let foo";
const result = parseSync(code, { filename: "test.ts" });
console.log(result);
}

main();
```
36 changes: 36 additions & 0 deletions wasm/parser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@oxc-parser/wasm",
"version": "0.0.1",
"description": "Wasm target for the oxc parser.",
"keywords": [
"JavaScript",
"TypeScript",
"parser"
],
"author": "Boshen and oxc contributors",
"license": "MIT",
"homepage": "https://oxc-project.github.io",
"repository": {
"type": "git",
"url": "https://github.com/oxc-project/oxc",
"directory": "wasm/parser"
},
"funding": {
"url": "https://github.com/sponsors/Boshen"
},
"files": [
"oxc_parser_wasm.d.ts",
"oxc_parser_wasm.js",
"oxc_parser_wasm_bg.wasm",
"oxc_parser_wasm_bg.wasm.d.ts",
"README.md"
],
"module": "oxc_parser_wasm.js",
"types": "oxc_parser_wasm.d.ts",
"scripts": {
"build": "wasm-pack build --release --no-pack --target web --out-dir ../../npm/parser-wasm . && cp ./package.json ../../npm/parser-wasm/package.json"
},
"devDependencies": {
"wasm-pack": "^0.12.1"
}
}
Loading

0 comments on commit 5ac61f0

Please sign in to comment.