Skip to content

Sunny-117/jsx-compilation

Repository files navigation

JSX Compilation

๐Ÿป ๅฎž็Žฐ JSX ่ฏญๆณ•่ฝฌๆˆ JS ่ฏญๆณ•็š„็ผ–่ฏ‘ๅ™จ / JSX to JavaScript Compiler Implementation

A comprehensive JSX tokenization project with both TypeScript and Rust implementations, featuring a live playground for testing and comparison.

๐ŸŒŸ Features

  • ๐Ÿ“ TypeScript Implementation: Original finite state machine tokenizer
  • ๐Ÿฆ€ Rust Implementation: High-performance, memory-safe alternative
  • ๐ŸŒ Live Playground: Interactive web interface for testing both implementations
  • โšก Performance Comparison: Benchmarking tools to compare implementations
  • ๐Ÿ”ง Multiple Integration Options: WASM, Vite plugin, and native binary support
  • ๐Ÿงช Comprehensive Testing: Unit tests and integration tests ensuring compatibility

๐Ÿš€ Quick Start

Playground

Try it online: https://sunny-117.github.io/jsx-compilation

Or run locally:

cd playground
npm install
npm run dev

TypeScript Usage

import { tokenizer } from './lib/jsx-compilation.js';

const jsx = '<h1 id="title">Hello World</h1>';
const tokens = tokenizer(jsx);
console.log(tokens);

Rust Usage

use jsx_compilation_rs::tokenizer;

let jsx = r#"<h1 id="title">Hello World</h1>"#;
let tokens = tokenizer(jsx).unwrap();
println!("{:?}", tokens);

๐Ÿ“ Project Structure

jsx-compilation/
โ”œโ”€โ”€ src/                    # TypeScript implementation
โ”‚   โ”œโ”€โ”€ tokenizer.ts       # Main tokenizer logic
โ”‚   โ”œโ”€โ”€ ast.ts            # AST node definitions
โ”‚   โ””โ”€โ”€ ...
โ”œโ”€โ”€ jsx-compilation-rs/    # Rust implementation
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ tokenizer.rs  # Rust tokenizer
โ”‚   โ”‚   โ”œโ”€โ”€ wasm.rs       # WASM bindings
โ”‚   โ”‚   โ””โ”€โ”€ lib.rs        # Library interface
โ”‚   โ””โ”€โ”€ Cargo.toml
โ”œโ”€โ”€ playground/            # Interactive web playground
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ””โ”€โ”€ App.tsx       # React playground app
โ”‚   โ””โ”€โ”€ vite.config.ts
โ”œโ”€โ”€ tests/                 # Test suites
โ”œโ”€โ”€ lib/                   # Built JavaScript library
โ””โ”€โ”€ vite-plugin-jsx-compilation-rs.js  # Vite plugin

๐Ÿ”ง Implementation Details

Token Types

Both implementations recognize these JSX token types:

  • LeftParentheses: <
  • RightParentheses: >
  • JSXIdentifier: Element names (div, span, h1)
  • AttributeKey: Attribute names (id, class, onClick)
  • AttributeStringValue: String values ("title")
  • AttributeExpressionValue: Expression values ({value})
  • JSXText: Text content between elements
  • BackSlash: / for closing tags

Finite State Machine

The tokenizer uses a finite state machine with these states:

  1. Start: Expects <
  2. FoundLeftParentheses: After <, expects identifier or /
  3. JSXIdentifier: Collecting element name
  4. Attribute: Looking for attribute key
  5. AttributeKey: Collecting attribute name
  6. AttributeValue: Expecting attribute value
  7. AttributeStringValue: Inside string value
  8. AttributeExpressionValue: Inside expression value
  9. TryLeaveAttribute: After attribute value
  10. FoundRightParentheses: After >, expecting text or new element
  11. JSXText: Collecting text content

๐ŸŽฏ Integration Options

1. Vite Plugin (Recommended)

// vite.config.js
import { jsxCompilationRs } from './vite-plugin-jsx-compilation-rs.js';

export default {
  plugins: [
    jsxCompilationRs({
      rustProjectPath: './jsx-compilation-rs',
      useWasm: false, // or true for WASM
    })
  ]
};

2. WASM Module

cd jsx-compilation-rs
./build-wasm.sh
import init, { tokenizer } from './jsx-compilation-rs/pkg/jsx_compilation_rs.js';

await init();
const tokens = tokenizer('<div>Hello</div>');

3. Native Binary

cd jsx-compilation-rs
cargo build --release --bin compare
./target/release/compare '<div>Hello</div>'

๐Ÿ“Š Performance

Performance characteristics vary by integration method:

Method Pros Cons Use Case
TypeScript Fast in-process execution Global state issues Development, simple use
Rust Binary Memory safe, no global state Process overhead (~3ms) CLI tools, isolation
WASM Near-native speed, no overhead Build complexity Production web apps

Run benchmarks:

node benchmark.js

๐Ÿงช Testing

Run All Tests

# TypeScript tests
npm test

# Rust tests
cd jsx-compilation-rs
cargo test

# Integration tests
node compare-implementations.js

Test Coverage

  • โœ… Basic JSX elements
  • โœ… Nested elements
  • โœ… String attributes
  • โœ… Expression attributes
  • โœ… Multiple attributes
  • โœ… Error cases
  • โœ… Edge cases
  • โœ… State isolation

๐Ÿ› ๏ธ Development

Prerequisites

  • Node.js 16+
  • Rust 1.70+
  • wasm-pack (for WASM builds)

Setup

# Install dependencies
npm install
cd playground && npm install

# Build TypeScript
npm run build

# Build Rust
cd jsx-compilation-rs
cargo build --release

# Build WASM
./build-wasm.sh

Development Workflow

  1. Make changes to TypeScript or Rust implementation
  2. Run tests to ensure compatibility
  3. Test in playground
  4. Run benchmarks if performance-critical
  5. Update documentation

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Implement changes with tests
  4. Ensure both implementations remain compatible
  5. Submit a pull request

๐Ÿ“„ License

MIT License - see LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Original TypeScript implementation for reference
  • Rust community for excellent tooling
  • React team for JSX specification

About

๐Ÿป ๅฎž็Žฐ JSX ่ฏญๆณ•่ฝฌๆˆ JS ่ฏญๆณ•็š„็ผ–่ฏ‘ๅ™จ

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors