oxc/wasm/parser
github-actions[bot] fa50e9737e
Release crates v0.16.1 (#3968)
## [0.16.1] - 2024-06-29

### Features

- 7b38bde parser: Parse modifiers with `parse_modifiers` (#3948)
(DonIsaac)
- f64ad4b semantic: Make jsdoc building optional (turned off by default)
(#3955) (Boshen)

### Bug Fixes

- 51e54f9 codegen: Should print `TSModuleDeclarationKind` instead of
just `module` (#3957) (Dunqing)
- 31e4c3b isolated-declarations: `declare global {}` should be kept even
if it is not exported (#3956) (Dunqing)

### Refactor

- 2705df9 linter: Improve diagnostic labeling (#3960) (DonIsaac)
- 15ec254 semantic: Remove the unused `Semantic::build2` function
(Boshen)

Co-authored-by: Boshen <Boshen@users.noreply.github.com>
2024-06-29 16:53:09 +08:00
..
src chore: improve some format by running cargo +nightly fmt 2024-06-19 00:48:30 +08:00
.gitignore feat: setup wasm parser for npm (#2221) 2024-01-30 21:40:10 +08:00
Cargo.toml chore: format wasm/parser/Cargo.toml 2024-03-14 17:25:53 +08:00
CHANGELOG.md chore: regenerate changelogs with commit id and author 2024-06-07 01:22:28 +08:00
package.json Release crates v0.16.1 (#3968) 2024-06-29 16:53:09 +08:00
pnpm-lock.yaml chore(deps): lock file maintenance npm packages (#3421) 2024-05-26 18:29:48 +00:00
README.md Publish @oxc-parser/wasm with web and node builds (#2916) 2024-04-08 15:47:02 +08:00
test-node.mjs Publish @oxc-parser/wasm with web and node builds (#2916) 2024-04-08 15:47:02 +08:00

About

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

This package is built with different wasm-pack's target builds:

  • wasm-pack build --target web for bundler (webpack / vite) consumption.
  • wasm-pack build --target nodejs for node.js

And exports the files as

"main": "./node/oxc_parser_wasm.js",
"browser": "./web/oxc_parser_wasm.js",
"types": "./node/oxc_parser_wasm.d.ts",

Checkout oxc-parser for usage in node.js via napi bindings.

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

Usage

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

async function main() {
  await initWasm();

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

main();

Notes

UTF8 vs UTF16 byte offsets

The span value returned from the ASTs and diagnostics is in UTF8 byte offsets. Converting to UTF16 byte offsets:

let sourceTextUtf8 = new TextEncoder().encode(sourceText);

const convertToUtf8 = (sourceTextUtf8, d) => {
  return new TextDecoder().decode(sourceTextUtf8.slice(0, d)).length;
}

const diagnostics = result.errors.map((d) => ({
    from: convertToUtf8(sourceTextUtf8, d.start),
    to: convertToUtf8(sourceTextUtf8, d.end),
    severity: d.severity.toLowerCase(),
    message: d.message,
}));

Vite

wasm-pack build --target web is used for the wasm build.

You may need something like https://github.com/nshen/vite-plugin-wasm-pack to get it working with vite, otherwise vite will load the wasm file as a HTML file causing a CompileError: WebAssembly.instantiate(): expected magic word error.