DEV Community

Koutaro Chikuba
Koutaro Chikuba

Posted on

mints - lightweight typescript compiler (7.8kb)

mints is most lightweight typescript compiler ever.

https://github.com/mizchi/mints

npm install @mizchi/mints --save
yarn add @mizchi/mints
Enter fullscreen mode Exit fullscreen mode
import { transformSync } from "@mizchi/mints";
const out = transformSync(`const x: number = 1;`);
console.log(out.code); // const x=1;
Enter fullscreen mode Exit fullscreen mode

My goal: Today's web based editor problem

Today's web based code editors like vscode.dev includes typescript compiler but those are very heavy bundle size.

(compressed, no gzip)

  • TypeScript: 2.9MB
  • @babel /core + @babel /preset-typescript: 1.8MB
  • sucrase: 176kb
  • esbuild-wasm: 8.4MB
  • @swc/wasm: 16MB
  • mints: 17kb

I need lightweight compiler for casual case.

  • interactive tutorial
  • browser based authoring tool

I made mints-playground for this case.

https://mints-playground.netlify.app/

mints' goal

  • Lightweight
  • Just drop type annotations like :number and transform enum, constructor's public/private/proctected and jsx.
  • Fast initial compile
  • Support parallel compile

Limitations

  • All statements except those terminated by } require ; (expect prettier format)
  • mints tokenizer is unstable for RegExp Literal /.../ yet.
    • / can not parse as RexExp (expect binary divider). use /[ ]...
    • /> can not parse as RegExp (expect jsx)
    • </ can not parse as RegExp (expect jsx)
  • Not Support
    • with
    • namespace
    • decorator

(I tested prettier formatted code.)

How it works (internal)

I wrote parser-generator at first.

https://github.com/mizchi/mints/tree/main/packages/pargen-tokenized

@mizchi/pargen generates binary snapshot of parse rules. mints runtime load it.

Benchmark with microsoft/typescrpit and esbuild

$ yarn bench
Enter fullscreen mode Exit fullscreen mode

mints_para is mints parallel version.

-------------- 2416chars
[tsc] 58ms
[esbuild] 14ms
[mints] 6ms
[mints_para] 12ms
-------------- 2981chars
[tsc] 14ms
[esbuild] 1ms
[mints] 9ms
[mints_para] 12ms
-------------- 5118chars
[tsc] 18ms
[esbuild] 1ms
[mints] 12ms
[mints_para] 22ms
-------------- 21153chars
[tsc] 55ms
[esbuild] 3ms
[mints] 59ms
[mints_para] 53ms
-------------- 18584chars
[tsc] 39ms
[esbuild] 2ms
[mints] 39ms
[mints_para] 32ms
-------------- 3844chars
[tsc] 12ms
[esbuild] 1ms
[mints] 9ms
[mints_para] 17ms
-------------- 38611chars
[tsc] 72ms
[esbuild] 3ms
[mints] 57ms
[mints_para] 45ms
-------------- 6935chars
[tsc] 13ms
[esbuild] 1ms
[mints] 13ms
[mints_para] 7ms
Enter fullscreen mode Exit fullscreen mode

mints is fastest on first case that includes compiler ignition.

Discussion (0)