blog.dopana

Back

Trong thế giới phát triển JavaScript/TypeScript hiện đại, BiomeOxc là hai công cụ Rust nổi bật mang lại hiệu suất cao cho linting và formatting. Cả hai đều được viết bằng Rust nhưng có triết lý thiết kế và kiến trúc khác nhau. Bài viết này sẽ đi sâu vào kiến trúc của từng công cụ và so sánh chúng.

Biome là gì?#

Biome là một toolchain toàn diện cho các dự án web, cung cấp formatter và linter có thể sử dụng qua CLI và LSP. Nó được thiết kế để thay thế Prettier và ESLint với hiệu suất cao hơn trong một gói tích hợp duy nhất.

Kiến trúc Biome#

Biome sử dụng kiến trúc server-client với một daemon chạy trong nền:

graph TB
    subgraph "Biome Architecture"
        CLI[CLI Interface]
        LSP[LSP Server]
        Daemon[Daemon Process]
        Workspace[WorkspaceServer]
        Handlers[Language Handlers]
        Foundation[Foundation Layer]
    end
    
    CLI --> Daemon
    LSP --> Daemon
    Daemon --> Workspace
    Workspace --> Handlers
    Handlers --> Foundation

Các lớp kiến trúc#

  1. Foundation Layer (biome_rowan, biome_parser, biome_formatter, biome_analyze)

    • Cung cấp các nguyên tái sử dụng
    • Rowan-based parsing infrastructure
    • Định dạng IR chung
  2. Language Layer (per-language crates)

    • biome_js_parser, biome_js_formatter, biome_js_analyze
    • biome_json_parser, biome_json_formatter, biome_json_analyze
    • Tương tự cho CSS, HTML, GraphQL
  3. Service Layer (biome_service)

    • Orchestrates operations qua WorkspaceServer
    • Quản lý state và cache
  4. Interface Layer (biome_cli, biome_lsp, biome_wasm)

    • Cung cấp nhiều phương thức sử dụng

Formatter của Biome#

Biome sử dụng hệ thống two-stage formatting:

graph LR
    CST[CST from Parser] --> Lowering[Lowering Stage]
    Lowering --> IR[FormatElement IR]
    IR --> Printer[Printing Stage]
    Printer --> Output[Formatted Text]
  • Lowering Stage: Chuyển đổi CST thành IR ngôn ngữ bất kỳ (FormatElement)
  • Printing Stage: Printer tiêu thụ IR và quyết định layout, line breaks dựa trên options

Oxc là gì?#

Oxc (The Oxidation Compiler) là tập hợp các công cụ JavaScript/TypeScript hiệu suất cao viết bằng Rust. Nó được thiết kế như một bộ thành phần compiler module có thể sử dụng độc lập hoặc cùng nhau để xây dựng toolchain hoàn chỉnh.

Kiến trúc Oxc#

Oxc sử dụng kiến trúc layered design với các thành phần module:

graph TB
    subgraph "Oxc Architecture"
        Apps[Applications]
        Core[Core Libraries]
        Foundation[Foundation Libraries]
    end
    
    Apps --> Core
    Core --> Foundation
    
    Apps --> oxlint[oxlint]
    Apps --> LSP[Language Server]
    Apps --> NAPI[NAPI Bindings]
    
    Core --> Parser[Parser]
    Core --> Semantic[Semantic]
    Core --> Linter[Linter]
    Core --> Transformer[Transformer]
    Core --> Minifier[Minifier]
    Core --> Codegen[Codegen]
    
    Foundation --> AST[AST]
    Foundation --> Allocator[Allocator]
    Foundation --> Diagnostics[Diagnostics]
    Foundation --> Span[Span]
    Foundation --> Syntax[Syntax]

Nguyên tắc kiến trúc#

  1. Zero-Copy: Sử dụng arena allocator (oxc_allocator) cho zero-copy operations
  2. Visitor Pattern: AST traversal với automatic visitor generation
  3. Shared Foundation: Chia sẻ error reporting, source positions, syntax definitions

Linter của Oxc#

Oxlint sử dụng kiến trúc parallel execution với dual branch strategy:

graph TB
    LintService[LintService] --> Runtime[Runtime]
    Runtime --> process_source[process_source]
    process_source --> Parser[Parser]
    Parser --> AST[AST]
    AST --> LintContext[LintContext]
    LintContext --> Linter[Linter]
    Linter --> execute_rules[execute_rules]
    
    execute_rules --> strategy1[Strategy 1: nodes → rules]
    execute_rules --> strategy2[Strategy 2: rules → nodes]
    
    strategy1 --> RULE_BUCKETS[RULE_BUCKETS]
    strategy2 --> RULE_BUCKETS
  • Dual Branch Strategy: Chuyển đổi giữa (nodes → rules) hoặc (rules → nodes) dựa trên kích thước file
  • Parallel Execution: Sử dụng rayon để xử lý song song nhiều files
  • Type-Aware Linting: Hỗ trợ linting có nhận thức type

Formatter của Oxc (oxfmt)#

Oxc formatter sử dụng hệ thống three-stage formatting:

graph LR
    AST[AST from Parser] --> IRGen[IR Generation]
    IRGen --> IR[FormatElement IR]
    IR --> Transform[IR Transformation]
    Transform --> sort_imports[sort_imports]
    sort_imports --> Printer[Printing]
    Printer --> Output[Formatted Text]
  • IR Generation: Traversal AST và sử dụng Format trait
  • IR Transformation: Áp dụng transformations như sort_imports
  • Printing: Printer tiêu thụ IR và render kết quả

[!NOTE] Oxc formatter core được port từ Biome’s biome_formatter crate, nhưng với thiết kế language-agnostic hoàn toàn.

So sánh chi tiết#

1. Triết lý thiết kế#

Đặc điểmBiomeOxc
Mục tiêuAll-in-one toolchain thay thế Prettier + ESLintModular components có thể dùng độc lập
Multi-languageHỗ trợ JS/TS, JSON, CSS, HTML, GraphQLTập trung JS/TS với kế hoạch mở rộng
DeploymentDaemon process cho IDE integrationTraditional CLI với NAPI bindings

2. Memory Management#

Đặc điểmBiomeOxc
AllocatorRowan-based infrastructureArena allocator (oxc_allocator)
Zero-CopyCó, nhưng không rõ ràngCó, là nguyên tắc thiết kế chính
AST StorageRowan syntax treeArena-based AST nodes

3. Performance Optimization#

Đặc điểmBiomeOxc
Parallel ExecutionCó, qua WorkspaceServerCó, sử dụng rayon
CachingDaemon process cacheRuntime cache trong LintService
Rule ExecutionSequential executionDual branch strategy based on file size

4. Extensibility#

Đặc điểmBiomeOxc
Plugin SystemHỗ trợ pluginsJS plugins (alpha) cho ESLint compatibility
Language SupportExtensionHandler patternModule crates per language
Custom RulesQua biome configurationQua rule configuration

5. Formatting Architecture#

Đặc điểmBiomeOxc
Stages2 stages (Lowering + Printing)3 stages (IR Gen + Transform + Printing)
IR DesignFormatElement trong biome_formatterFormatElement trong oxc_formatter_core
Comment HandlingIntegrated trong formatterCursor-based comment system
Prettier CompatibilityCó, với testingCó, là mục tiêu thiết kế

Khi nào nên dùng cái nào?#

Chọn Biome khi#

  • Bạn muốn all-in-one solution thay thế cả Prettier và ESLint
  • Cần multi-language support (CSS, HTML, GraphQL)
  • Muốn daemon process cho IDE integration tốt hơn
  • Team muốn unified toolchain với configuration đơn giản

Chọn Oxc khi#

  • Bạn muốn modular components có thể dùng riêng lẻ
  • Cần maximum performance cho CI/CD environments
  • Muốn type-aware linting với TypeScript semantics
  • Đang xây dựng custom toolchain từ các components

Ví dụ sử dụng#

Biome#

# Cài đặt
npm install -D @biomejs/biome

# Format và lint một file
biome check src/file.ts

# Format và lint toàn bộ dự án
biome check .

# Watch mode cho development
biome check --watch .
bash

Oxc#

# Cài đặt oxlint
npm install -D oxlint

# Lint một file
oxlint src/file.ts

# Lint toàn bộ dự án với parallel execution
oxlint src/

# Type-aware linting
oxlint --tsconfig tsconfig.json src/
bash

Kết luận#

Cả Biome và Oxc đều đại diện cho tương lai của JavaScript/TypeScript tooling với hiệu suất Rust cao. Biome excels ở all-in-one experience với multi-language support, trong khi Oxc shine ở modular design và maximum performance.

Lựa chọn giữa hai công cụ phụ thuộc vào nhu cầu cụ thể của dự án: unified toolchain vs flexible components, multi-language support vs JS/TS focus, IDE integration vs CI performance.

Tài liệu tham khảo#