.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)
↓
Binding Layer (@rspack/binding, rspack_binding_api, rspack_napi)
↓
Rust Core Layer (rspack_core, compilation engine)
packages/): Webpack-compatible API, configuration, file system operationscrates/): Core compilation engine, module system, plugin/loader executioncrates/node_binding, crates/rspack_binding_api, crates/rspack_napi):
Node-API packaging, conversion, native-backed objects, JavaScript callbacks, and runtime supportMain 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)Cache and Incremental are independent performance mechanisms:
The two options form orthogonal axes. Disabling Cache must not disable Incremental, and disabling Incremental must not disable Cache. This allows all four Cache on/off × Incremental on/off combinations.
Incremental does not make separate one-shot rspack build invocations incremental. Reuse across
those invocations comes from filesystem Cache. Incremental is conceptually aligned with webpack's
cacheUnaffected behavior, but Rspack exposes it independently from Cache.
Rspack currently has two internal Cache backend implementations, legacy_cache and new_cache.
Both implement Cache storage responsibilities and must remain independent from the compiler-owned
Incremental artifacts.
See Cache and Incremental Compilation for the configuration matrix, ownership model, backend boundary, and architectural invariants.
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