vue template compiler in Rust
Maybe in the long run we want the whole transform (and even the Vue compiler!) to be implemented in native Go/Rust so performance would no longer be a concern ;)
Future is now!
The original design in vue-next mixes code generation and ast parsing in the same data structure. As we can see, the transform pass will in-place mutate ast nodes, leaving the node with both code generation node and ssr code generation node.
This is typically a sign of leaky abstraction.
So in the Rust version I decided to take another approach.
The design targets at three different levels of developers in Vue ecosystem:
- Lib/App creator: every Vue developers who write component library or application code.
- Platform developer: Devs who write compiler implementation for DOM/SSR/Native platform.
- Framework author: Vue core lib author a.k.a Evan.
The core framework targets multiple platforms and can be extended to support more. Core framework components span all platforms and are hardwired to the core lib runtime.
Platforms are usually DOM or SSR environment. Hosts are browser and node, respectively. Developing a platform needs to write code for both vue-compiler and vue-runtime. Optionally platform developer can write code in host, e.g. in hybrid app or mini-program.
And finally lib or app creators can write component library, business code or application components targeted to certain platforms.
The compilation has several phases:
- Scan (output: Tokens): Hardwired in the compiler at framework level.
- Parse (output: template AST): Hardwired in the compiler at framework level.
- Convert (output: intermediate representation): Customizable for platform developers with sensible default.
- Transform (input/output: customizable IR): Customizable with default by using generic/traits.
- Code Generate (customizable output: e.g. JS/TS): Customizable with default.
Other Design different from the original compiler
- Directive parsing is implemented manually instead of by regexp.
nodeTransformsis not supported. It's too hard for app creator to use and maintain IR invariants. Platform devs can still customize by implementing converter/transformer.
directiveTransformsnow can returns not only
SimpleExpression. The extra flexibility makes a more cohesive v-bind/v-on conversion: the logic for processing the directives now resides in one single file without regard to the presence of an argument.
- Runtime helper collection
context.helper/helperStringis moved out from convert and tracked in transform phase, avoiding several methods and reducing HashMap to a bitflag.
- Rust library
- CLI binary
- napi based nodejs library
- wasm based npm package: a fallback if napi fails to work and a toy for browser.
- No Browser build No support since most features in full build are additional except for browser based expression checking or HTML escaping. Browser build removed them for size. But template compiler in browser is already for toy project. For browser specific summary see this google sheet.
- Plenty of
debug_asserts to maintain compiler state invariants.
- The library seeks minimal allocation by using
- A customized
VStris used to minimize string manipulation.
Fxhashis preferred over default hasher since hash collision is not a concern.
bitflagscrate is used to represent runtime helper and vnode patch flags.
- Use heavily optimized routines for string search primitives. (Perf reference)
- Benchmark with criterion.rs.
- Test compiler output by snapshot test.
- Use alternative allocator like wee_alloc.
- vue-next: ご本家様
- html spec is the definitive guide for parsing HTML-like files.
- Vue Template Explorer gives instant results for code generation and error reporting.
- Nu html checker is the official html validator from W3C. This is the canonical error reporter for html parsing, when there is a discrepancy between the framework and the spec.
- AST explorer can inspect AST nodes interactively.
Todo tasks grouped by scopes.
- string intern
- camel/pascal cache
- str ops
- UTF8 support
- IR converter
- slot outlet
- build props
- code generator
- code generator
- tokenizer test
- parser test
- dir parser test
- Add insta snapshot
- Add benchmark framework
- Micro benchmarks for compiler components
- Integrated benchmarks using repos like Element-Plus
- Add pre-commit hooks.
- Add Github Actions for various checks.
- Change single lib to cargo workspaces.
- TODO. not ready for contribution for now.