HyperChad JavaScript Bundler
JavaScript bundling and compilation utilities for HyperChad applications.
Overview
The HyperChad JavaScript Bundler package provides:
- Multiple Bundlers: Support for ESBuild and SWC bundlers
- Node.js Integration: Node.js runtime utilities
- Feature-Gated: Modular bundler selection
- Build Pipeline: JavaScript compilation and bundling
Features
Bundler Support
- ESBuild: Fast JavaScript bundler and minifier via external esbuild binary
- SWC: Rust-based JavaScript/TypeScript compiler with full bundling implementation
- Pluggable: Choose bundler based on requirements via feature flags
- Performance: High-performance bundling options
Node.js Integration
- Command Execution: Node.js and npm package manager command execution
- Multi-toolchain Support: Supports npm, pnpm, and bun (feature-gated)
- Process Management: Automatic binary selection and process handling
Installation
Choose one dependency declaration and add it to your Cargo.toml:
[]
= { = "../hyperchad/js_bundler" }
[]
= { = "../hyperchad/js_bundler", = ["esbuild"] }
[]
= { = "../hyperchad/js_bundler", = ["swc"] }
[]
= { = "../hyperchad/js_bundler", = ["npm"] } # or "pnpm", "bun"
Note: The default features include all-web-toolchains, esbuild, and swc.
Usage
Unified Bundler API
The package provides a unified bundle function that dispatches to the appropriate bundler:
use bundle;
use Path;
// Bundle JavaScript/TypeScript file
// Uses SWC if available, falls back to ESBuild
bundle;
ESBuild Integration (with esbuild feature)
ESBuild bundles by executing the external esbuild binary via npm:
use esbuild;
use Path;
// Bundle with ESBuild (runs npm install and esbuild binary)
bundle;
SWC Integration (with swc feature)
SWC provides full Rust-based bundling with minification support:
use swc;
use Path;
// Bundle with SWC (minify: true)
bundle;
The SWC bundler supports:
- TypeScript and JavaScript files
- Minification and code optimization
- Tree shaking and dead code elimination
- Module resolution via Node.js resolver
- Import meta URL handling
Node.js Integration (with node feature)
Execute npm/pnpm/bun commands:
use run_npm_command;
use Path;
// Run npm command (tries pnpm, bun, npm in order based on enabled features)
run_npm_command;
Feature Flags
Bundlers
esbuild: Enable ESBuild bundler support (requiresnodefeature)swc: Enable SWC compiler support
Package Managers
node: Base feature for Node.js command executionnpm: Enable npm package manager support (enablesnode)pnpm: Enable pnpm package manager support (enablesnode)bun: Enable bun package manager support (enablesnode)all-web-toolchains: Enable all package managers (npm, pnpm, bun)
Other
fail-on-warnings: Treat warnings as errorsdefault: Includesall-web-toolchains,esbuild, andswc
Bundler Comparison
ESBuild
- Implementation: Executes external esbuild binary via npm
- Speed: Extremely fast bundling and minification
- Dependencies: Requires npm/pnpm/bun installation
- Features: Automatic minification and bundling via command-line flags
SWC
- Implementation: Fully integrated Rust-based bundler
- Speed: Fast Rust-native bundling
- TypeScript: Native TypeScript stripping and compilation
- Features:
- Configurable minification with compress and mangle options
- Module resolution via Node.js resolver with caching
- Dead code elimination (DCE)
- ES module output
- Import meta property handling
Dependencies
Core dependencies (always included):
- log: Logging facade
- switchy_env: Environment variable utilities
Feature-gated dependencies:
- SWC feature: Includes swc_bundler, swc_common, swc_ecma_* crates, and anyhow
- ESBuild feature: No Rust dependencies (uses external binary)
- Node feature: No additional dependencies (command execution only)
External requirements:
- ESBuild: Requires npm/pnpm/bun and esbuild package installation
- SWC: No external requirements (fully Rust-based)
Integration
This package is designed for:
- Build Systems: JavaScript build pipeline integration
- Development Tools: Development server bundling
- Production Builds: Optimized production bundling
- HyperChad Apps: JavaScript bundling for HyperChad applications
Module Structure
The package consists of the following modules:
bundler.rs: Unified bundler interface that dispatches to SWC or ESBuildesbuild.rs: ESBuild integration via external binary executionswc.rs: Full SWC bundler implementation with custom loader and hooksnode.rs: Command execution utilities for npm/pnpm/bunlib.rs: Feature-gated module exports
Note
This package provides a modular approach to JavaScript bundling. Enable only the features you need to minimize dependencies and build times. The actual bundling implementations are feature-gated and will only be available when the corresponding features are enabled.
The unified bundle() function prioritizes SWC over ESBuild when both features are enabled, as SWC provides a fully integrated Rust-based solution without external binary dependencies.