.agents/ARCHITECTURE.md
High-level architecture of Rspack, including core components, data flow, and design decisions.
Rspack is a high-performance JavaScript bundler written in Rust that maintains strong compatibility with the webpack ecosystem. The architecture leverages Rust's performance while providing a webpack-compatible API through Node.js bindings.
JavaScript/TypeScript Layer (@rspack/core, plugins, loaders)
↓ NAPI (Node-API)
Rust Core Layer (rspack_core, compilation engine)
packages/): Webpack-compatible API, configuration, file system operationscrates/): Core compilation engine, module system, plugin/loader executionMain entry point orchestrating the build process.
Responsibilities:
Key Hooks:
beforeRun, run, compile, make, emit, doneRepresents a single build instance managing module graph, chunks, and assets.
Responsibilities:
Key Data Structures:
ModuleGraph: Tracks modules, dependencies, and connectionsChunkGraph: Relationship between chunks and modulesAssets: Output files and contentKey Hooks:
buildModule, succeedModule, processAssets, optimizeChunks, afterSealModules are the basic unit of code organization.
Module Types:
NormalModule: Regular JavaScript/TypeScript modulesContextModule: Dynamic require contextsExternalModule: External dependenciesConcatenatedModule: Concatenated modules for optimizationModule Lifecycle:
Manages modules and their relationships through dependencies and connections.
Key Concepts:
DependencyId and types (ModuleDependency, ContextDependency, etc.)ModuleGraphConnection linking modules with dependency_id, original_module_identifier, resolved_moduleGraph Construction:
Dependency objectsModuleGraphConnection objectsChunks are groups of modules bundled together.
Chunk Types:
Chunk Splitting:
optimization.splitChunks1. Initialize → Load config, create compiler, register plugins
2. Compile → Build module graph, resolve dependencies
3. Optimize → Tree shaking, code splitting, minification
4. Generate → Code generation, asset creation, output
Plugins extend functionality by hooking into compilation lifecycle.
Plugin Types:
Hooks allow plugins to intercept and modify compilation.
Hook Types:
Loaders transform source code before adding to dependency graph.
Loader Execution:
Loader Types:
Determines how module paths resolve to actual files.
Resolution Process:
Resolution Strategies:
./module, ../module/path/to/modulemodule-name (from node_modules)Multi-level caching for performance.
Cache Levels:
Cache Invalidation:
Cross-platform file system abstraction.
File System Types:
Implementation:
Unified error system.
Error Types:
Error Propagation:
compilation.errorscompilation.warningsUser Code → Configuration → Compiler.apply() → Plugin Registration
→ Compiler.run() → Compilation → Module Graph → Optimization
→ Code Generation → Asset Emission → Output Files
Source File → Loader Chain → Parsed AST → Dependency Extraction
→ Module Graph Node → Code Generation → Runtime Code
#[plugin] attribute#[plugin_hook] attributePlugin traitapply methoddefine_hook! macro