Fast Rust-based web bundler with webpack-compatible API
—
Core bundling functions and compiler creation for building JavaScript applications with webpack-compatible API.
Creates a compiler or multi-compiler instance for building applications.
/**
* Create a compiler instance for building applications
* @param options - Build configuration options
* @returns Compiler instance for single config or MultiCompiler for array
*/
function rspack(options: Configuration): Compiler;
function rspack(options: Configuration[]): MultiCompiler;
function rspack(
options: Configuration | Configuration[],
callback: (err: Error | null, stats?: Stats | MultiStats) => void
): Compiler | MultiCompiler | null;Usage Examples:
import rspack from "@rspack/core";
// Single configuration
const compiler = rspack({
entry: "./src/index.js",
output: {
path: "./dist",
filename: "bundle.js"
},
mode: "production"
});
// Multiple configurations
const multiCompiler = rspack([
{ entry: "./src/app.js", output: { filename: "app.js" } },
{ entry: "./src/worker.js", output: { filename: "worker.js" } }
]);
// With callback for immediate execution
rspack(config, (err, stats) => {
if (err) {
console.error("Build failed:", err);
return;
}
console.log("Build completed:", stats?.toString());
});Direct compiler creation functions for advanced use cases.
/**
* Create a single compiler instance
* @param options - Configuration for the compiler
* @returns Compiler instance
*/
function createCompiler(options: Configuration): Compiler;
/**
* Create a multi-compiler for multiple configurations
* @param options - Array of configurations
* @returns MultiCompiler instance managing multiple compilers
*/
function createMultiCompiler(options: Configuration[]): MultiCompiler;Main compilation controller managing the build process and plugin system.
class Compiler {
/** Full build configuration options */
options: Configuration;
/** Tapable hooks system for plugin integration */
hooks: CompilerHooks;
/** Output directory path */
outputPath: string;
/** Optional compiler name for multi-compiler setups */
name?: string;
/** Input file system interface */
inputFileSystem?: any;
/** Output file system interface */
outputFileSystem?: any;
/** Watch file system interface */
watchFileSystem?: any;
/**
* Execute a single build
* @param callback - Called when build completes or fails
*/
run(callback: (err: Error | null, stats?: Stats) => void): void;
/**
* Start watch mode for continuous building
* @param options - Watch configuration options
* @param callback - Called on each build completion
* @returns Watching instance for controlling watch mode
*/
watch(
options: WatchOptions,
callback: (err: Error | null, stats?: Stats) => void
): Watching;
/**
* Close the compiler and cleanup resources
* @param callback - Called when cleanup is complete
*/
close(callback: () => void): void;
/**
* Create a new compilation instance
* @returns Compilation instance for this build
*/
createCompilation(): Compilation;
}Usage Examples:
import { createCompiler } from "@rspack/core";
const compiler = createCompiler({
entry: "./src/index.js",
output: { path: "./dist", filename: "bundle.js" },
mode: "development"
});
// Single build
compiler.run((err, stats) => {
if (err) {
console.error("Compilation failed:", err);
return;
}
if (stats?.hasErrors()) {
console.error("Build errors:", stats.toJson().errors);
return;
}
console.log("Build successful!");
compiler.close(() => {
console.log("Compiler closed");
});
});
// Watch mode
const watching = compiler.watch({
aggregateTimeout: 300,
poll: undefined
}, (err, stats) => {
console.log("Build completed at", new Date().toISOString());
});
// Stop watching
watching.close(() => {
console.log("Watching stopped");
});Manages multiple parallel or sequential compilations.
class MultiCompiler {
/** Array of individual compilers */
compilers: Compiler[];
/** Multi-compiler specific hooks */
hooks: MultiCompilerHooks;
/**
* Run all compilers
* @param callback - Called when all builds complete
*/
run(callback: (err: Error | null, stats?: MultiStats) => void): void;
/**
* Start watch mode for all compilers
* @param options - Watch options for each compiler
* @param callback - Called on each build completion
* @returns MultiWatching instance
*/
watch(
options: WatchOptions | WatchOptions[],
callback: (err: Error | null, stats?: MultiStats) => void
): MultiWatching;
/**
* Set dependencies between compilers
* @param compiler - Dependent compiler
* @param dependencies - Array of compiler names it depends on
*/
setDependencies(compiler: Compiler, dependencies: string[]): void;
}Build statistics and reporting.
class Stats {
/** Associated compilation instance */
compilation: Compilation;
/**
* Convert stats to formatted string
* @param options - Formatting options
* @returns Formatted build statistics
*/
toString(options?: StatsOptions): string;
/**
* Convert stats to JSON object
* @param options - Serialization options
* @returns Build statistics as JSON
*/
toJson(options?: StatsOptions): StatsCompilation;
/**
* Check if build has errors
* @returns True if compilation has errors
*/
hasErrors(): boolean;
/**
* Check if build has warnings
* @returns True if compilation has warnings
*/
hasWarnings(): boolean;
}
class MultiStats {
/** Array of individual stats */
stats: Stats[];
/**
* Convert all stats to formatted string
* @param options - Formatting options
* @returns Formatted multi-build statistics
*/
toString(options?: StatsOptions): string;
/**
* Convert all stats to JSON
* @param options - Serialization options
* @returns Multi-build statistics as JSON
*/
toJson(options?: StatsOptions): any;
/**
* Check if any build has errors
* @returns True if any compilation has errors
*/
hasErrors(): boolean;
/**
* Check if any build has warnings
* @returns True if any compilation has warnings
*/
hasWarnings(): boolean;
}interface WatchOptions {
/** Delay before rebuilding after change detected */
aggregateTimeout?: number;
/** Polling interval in milliseconds (for polling mode) */
poll?: boolean | number;
/** Ignore files/directories for watching */
ignored?: string | RegExp | (string | RegExp)[];
}
interface Watching {
/** Stop watching and cleanup */
close(callback: () => void): void;
/** Invalidate current build and trigger rebuild */
invalidate(): void;
}
interface MultiWatching {
/** Stop all watching and cleanup */
close(callback: () => void): void;
/** Invalidate all builds and trigger rebuilds */
invalidate(): void;
}
interface StatsOptions {
/** Include asset information */
assets?: boolean;
/** Include chunk information */
chunks?: boolean;
/** Include module information */
modules?: boolean;
/** Include error details */
errors?: boolean;
/** Include warning details */
warnings?: boolean;
/** Color output */
colors?: boolean;
/** Preset configuration */
preset?: "none" | "minimal" | "normal" | "detailed" | "verbose";
}Install with Tessl CLI
npx tessl i tessl/npm-rspack--core