brk_bundler
Asset bundling and development server for BRK web interfaces with hot reloading and file watching.
Overview
This crate provides a thin wrapper around the Rolldown JavaScript bundler specifically designed for BRK web interface development. It handles asset bundling, file copying, template processing, and development-mode file watching with automatic rebuilds and hot reloading for efficient web development workflows.
Key Features:
- JavaScript bundling with Rolldown (Rust-based bundler)
- Automatic file watching and hot reloading in development mode
- Template processing with version injection and asset hash replacement
- Service worker generation with version management
- Source map generation for debugging
- Minification for production builds
- Async/await support with Tokio integration
Target Use Cases:
- BRK blockchain explorer web interfaces
- Development of Bitcoin analytics dashboards
- Building responsive web applications for blockchain data visualization
- Hot reloading development environment for rapid iteration
Installation
Quick Start
use bundle;
use Path;
async
API Overview
Core Functions
bundle(websites_path: &Path, source_folder: &str, watch: bool) -> io::Result<PathBuf>
Main bundling function that processes web assets and optionally starts file watching.
Bundling Process
- Directory Setup: Creates
dist/directory and copies source files - JavaScript Bundling: Processes
scripts/entry.jswith Rolldown bundler - Template Processing: Updates
index.htmlwith hashed asset references - Service Worker: Generates service worker with version injection
- File Watching: Optionally monitors source files for changes
Configuration
Rolldown Bundler Options:
- Input:
./src/scripts/entry.js(main JavaScript entry point) - Output:
./dist/scripts/directory - Minification: Enabled for production builds
- Source Maps: File-based source maps for debugging
- Asset Hashing: Automatic hash generation for cache busting
Examples
Development Mode with Hot Reloading
use bundle;
use Path;
async
Production Build
use bundle;
use Path;
async
Custom Web Application Structure
use bundle;
use Path;
// Expected directory structure:
// websites/
// ├── my_app/
// │ ├── index.html // Main HTML template
// │ ├── service-worker.js // Service worker template
// │ ├── scripts/
// │ │ └── entry.js // JavaScript entry point
// │ ├── styles/
// │ │ └── main.css // CSS files
// │ └── assets/
// │ └── images/ // Static assets
// └── dist/ // Generated output
async
Architecture
File Processing Pipeline
- Source Copying: Recursively copies all source files to dist directory
- JavaScript Bundling: Rolldown processes entry.js with dependencies
- Asset Hashing: Generates content-based hashes for cache busting
- Template Updates: Replaces placeholders in HTML templates
- Version Injection: Updates service worker with current package version
File Watching System
Development Mode Watchers:
- Source File Watcher: Monitors non-script files for changes
- Bundle Watcher: Watches JavaScript files and triggers rebuilds
- Template Watcher: Updates HTML when bundled assets change
Event Handling:
- File Creation/Modification: Automatic copying to dist directory
- Script Changes: Triggers Rolldown rebuild and template update
- Template Changes: Processes HTML and updates asset references
Template Processing
index.html Processing:
- Scans bundled JavaScript for asset hash
- Replaces
/scripts/main.jswith/scripts/main.[hash].js - Maintains cache busting while preserving template structure
service-worker.js Processing:
- Replaces
__VERSION__placeholder with current crate version - Enables version-based cache invalidation
- Maintains service worker functionality
Async Architecture
Built on Tokio async runtime:
- Non-blocking I/O: Efficient file operations and watching
- Concurrent Tasks: Parallel file watching and bundle processing
- Background Processing: Development server runs in background task
Configuration Options
Rolldown Configuration
The bundler uses optimized Rolldown settings:
BundlerOptions
File Structure Requirements
Required Files:
src/scripts/entry.js- JavaScript entry pointsrc/index.html- HTML templatesrc/service-worker.js- Service worker template
Optional Directories:
src/styles/- CSS stylesheetssrc/assets/- Static assets (images, fonts, etc.)src/components/- Additional JavaScript modules
Development Workflow
Setup
- Create web application in
websites/app_name/ - Add required files (index.html, entry.js, service-worker.js)
- Run bundler in watch mode for development
Hot Reloading
- Script Changes: Automatic bundle rebuild and browser refresh
- Template Changes: Immediate HTML update with asset hash replacement
- Asset Changes: Instant copy to dist directory
- Style Changes: Direct copy without bundling
Production Deployment
- Run bundler without watch mode
- Deploy
dist/directory contents - Assets include content hashes for cache busting
- Service worker includes version for cache management
Code Analysis Summary
Main Function: bundle() async function coordinating Rolldown bundler with file processing and watching
File Operations: Recursive directory copying with copy_dir_all() and selective file processing
Templating: String replacement for asset hash injection and version management
File Watching: Multi-watcher system using notify crate for real-time development feedback
Async Integration: Tokio-based async architecture with background task spawning
Bundler Integration: Rolldown wrapper with optimized configuration for web development
Architecture: Development-focused asset pipeline with hot reloading and production optimization
This README was generated by Claude Code