mini-rspack is a simplified implementation of Rspack bundler using Rust and Node.js bindings. It demonstrates how to create a JavaScript bundler with Rust for improved performance.
mini-rspack is a proof-of-concept JavaScript bundler that uses Rust for the core functionality and exposes a JavaScript API similar to webpack. It demonstrates how to:
- Create Node.js native modules with Rust using napi-rs
- Implement a simplified bundler architecture
- Parse and analyze JavaScript modules
- Resolve module dependencies
- Transform code with loaders
- Extend functionality with plugins
- Generate bundled output
- Provide a familiar webpack-like API
- Webpack-compatible API with compiler.run() method
- Support for both CommonJS and ES modules
- Entry point processing with multiple entry support
- Module resolution with custom resolver
- Plugin system with hooks (similar to Tapable)
- Loader system for transforming module content
- Watch mode for development
# Clone the repository
git clone https://github.com/Sunny-117/mini-rspack.git
cd mini-rspack
# Install dependencies
npm install
# Build the Rust code
npm run build
Create a configuration file similar to webpack:
const path = require('path');
const { rspack } = require('mini-rspack');
const rspackOptions = {
mode: 'production',
entry: {
entries: {
main: './src/index.js'
}
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
resolve: {
extensions: ['.js', '.json']
},
module: {
rules: [
{
test: '\\.js$',
use: [
path.resolve(__dirname, './loaders/my-loader.js')
]
}
]
},
plugins: [
'MyPlugin'
]
};
// Create a compiler instance
const compiler = rspack(rspackOptions);
// Run the compiler
compiler.run((err, stats) => {
if (err) {
console.error('Compilation failed:', err);
return;
}
console.log('Compilation successful!');
console.log('Stats:', JSON.stringify(stats, null, 2));
});
// Or use watch mode
compiler.watch((err, stats) => {
if (err) {
console.error('Watch compilation failed:', err);
return;
}
console.log('Watch compilation successful!');
});
mini-rspack consists of the following components:
- Compiler: The main entry point that manages the compilation process. Implemented in Rust with JavaScript bindings.
- Compilation: Represents a single build of the application. Manages the build process and holds the state of the compilation.
- Module: Represents a module in the dependency graph. Contains information about the module's source code, dependencies, and how it should be processed.
- Loader: Transforms module content. Implemented as JavaScript functions that can be chained together.
- Plugin: Extends the functionality of the bundler. Uses a hook system to tap into different stages of the compilation process.
- Hook System: Similar to webpack's Tapable, provides a way for plugins to tap into different stages of the compilation.
- Resolver: Resolves module paths and handles module resolution algorithms.
- Rust Core: The core bundling logic is implemented in Rust for performance.
- napi-rs: Used to create Node.js native modules from Rust code.
- JavaScript API: Exposes a webpack-like API for configuration and usage.
- Module Parsing: Uses regex and basic AST analysis to extract dependencies.
- Dependency Resolution: Implements path resolution similar to Node.js module resolution.
- Code Generation: Generates JavaScript bundle with proper module wrapping and runtime.
# Build in debug mode
npm run build:debug
# Run tests
npm test
Detailed documentation is available in the docs
directory. You can build and view the documentation using the provided scripts:
# Make the script executable
chmod +x ./preview-docs.sh
# Run the preview script
./preview-docs.sh
This will build the documentation and start a local server at http://localhost:5173/mini-rspack/
# Make the script executable
chmod +x ./deploy-docs.sh
# Run the deployment script
./deploy-docs.sh
This script will:
- Build the documentation
- Create or update the gh-pages branch
- Push the built documentation to the gh-pages branch
- Return to your original branch
The documentation will be available at https://Sunny-117.github.io/mini-rspack/
This project is designed as an educational resource for learning about:
- Modern JavaScript bundler architecture
- Rust and JavaScript interoperability
- Module resolution algorithms
- Plugin and loader systems
- AST parsing and code transformation
- Performance optimization in build tools
MIT