- Rust 97.4%
- TypeScript 2.1%
- JavaScript 0.5%
This commit replaces the `swc_ecma_react_compiler` with a more flexible, manually configured SWC compiler pipeline. This provides direct control over TypeScript and React (JSX) transformations and, most importantly, introduces support for ES modules. Key changes: - Implement a full SWC transform pipeline for `.ts` and `.tsx` files. - Detect `import`/`export` statements in the compiled output and use V8's `execute_module` to run the code as an ES module. - Add `SendWrapper` to safely manage V8 state in async contexts. - Reorganize project structure by moving example files into `src/` and centralizing utility functions. |
||
|---|---|---|
| .github | ||
| src | ||
| .gitignore | ||
| Cargo.lock | ||
| Cargo.toml | ||
| README.md | ||
rust_js_parser
A high-performance JavaScript/TypeScript processing library built with Rust, featuring SWC integration and Actix-Web server-side rendering capabilities.
Features
✅ JavaScript/TypeScript Parsing: Parse modern JavaScript and TypeScript code using SWC ✅ React Component Compilation: Compile React JSX/TSX components to JavaScript ✅ Server-Side Rendering: Optional V8-based SSR capabilities using ssr_rs ✅ Actix-Web Integration: HTTP server for serving rendered components ✅ Type Safety: Full TypeScript support with interface definitions ✅ Modern Syntax: ES2022 support with decorators and advanced features
Quick Start
Basic Usage (Without SSR)
# Run examples showcasing parsing and compilation
cargo run --no-default-features
# Run tests
cargo test --no-default-features
With SSR Features
# Enable full SSR capabilities (requires V8 compilation)
cargo run --features ssr
# Run all tests including SSR
cargo test --features ssr
Examples
JavaScript Parsing
use js_processor::js_parser::JavaScriptParser;
let parser = JavaScriptParser::new();
let code = r#"
function greet(name) {
return `Hello, ${name}!`;
}
"#;
let ast = parser.parse_code(code, Some("example.js"))?;
println!("✅ JavaScript parsing successful!");
TypeScript Parsing
let ts_code = r#"
interface User {
id: number;
name: string;
}
function greetUser(user: User): string {
return `Hello, ${user.name}!`;
}
"#;
let ast = parser.parse_code(ts_code, Some("example.ts"))?;
println!("✅ TypeScript parsing successful!");
React Component Compilation
use js_processor::react_compiler::ReactCompiler;
let compiler = ReactCompiler::new();
let jsx_code = r#"
import React from 'react';
const MyComponent = ({ title }) => {
return <h1>{title}</h1>;
};
"#;
let compiled = compiler.compile_react_component(jsx_code, Some("component.tsx"))?;
println!("Compiled: {}", compiled);
// Output: const MyComponent = ({ title }) => { return /*#__PURE__*/ React.createElement("h1", null, title); };
HTTP Request Processing
use js_processor::{StringHttpRequest, ssr::http_request::SimpleHttpRequest};
let request = StringHttpRequest::new("/api/users", "example.com", "Mozilla/5.0", "https://example.com");
println!("Path: {}", request.path());
println!("Host: {}", request.host());
Architecture
js_parser: JavaScript/TypeScript parsing using SWCreact_compiler: JSX/TSX to JavaScript compilationssr: Server-side rendering utilities and HTTP request abstractionsactix_integration: Actix-Web server integrationexamples: Demonstration code and usage examples
Dependencies
Core Dependencies
swc: JavaScript/TypeScript parsing and transformationswc_common: Common utilities for SWCswc_ecma_parser: ECMAScript parsing capabilitiesswc_ecma_ast: AST definitionsanyhow: Error handlingserde: Serialization framework
Optional SSR Dependencies (feature = "ssr")
ssr_rs: V8-based server-side renderingactix-web: Web frameworktokio: Async runtime
Testing
The project includes comprehensive tests for all major functionality:
# Test basic parsing functionality
cargo test --no-default-features js_parser
# Test React compilation
cargo test --no-default-features react_compiler
# Test HTTP request handling
cargo test --no-default-features simple_tests
# Test everything including SSR (requires V8)
cargo test --features ssr
File Examples
See the examples/ directory for sample files:
examples/test.js- Basic JavaScriptexamples/typescript-example.ts- TypeScript with interfacesexamples/react-component.tsx- React component with TypeScript
Performance
- Fast parsing using SWC's Rust-based parser
- Efficient memory usage with zero-copy string handling
- Optional V8 integration for JavaScript execution
- Async/await support with Tokio runtime
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests:
cargo test --no-default-features - Submit a pull request
License
This project is open source. See LICENSE file for details.