CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-rsbuild--core

The Rspack-based build tool providing high-performance bundling with comprehensive development features and plugin system.

Pending
Overview
Eval results
Files

core-build-system.mddocs/

Core Build System

Primary Rsbuild instance creation and build orchestration functionality. Provides production builds, development server creation, and configuration management with comprehensive lifecycle hooks.

Capabilities

Create Rsbuild Instance

Creates and configures a new Rsbuild instance with optional configuration and environment settings.

/**
 * Creates a new Rsbuild instance with the provided configuration
 * @param options - Configuration options for the Rsbuild instance
 * @returns Promise resolving to configured RsbuildInstance
 */
function createRsbuild(options?: CreateRsbuildOptions): Promise<RsbuildInstance>;

interface CreateRsbuildOptions {
  /** Project root directory */
  cwd?: string;
  /** Rsbuild configuration object or function */
  rsbuildConfig?: RsbuildConfig | (() => Promise<RsbuildConfig>);
  /** Specific environments to build */
  environment?: string[];
  /** Framework or tool name invoking Rsbuild */
  callerName?: string;
  /** Environment variable loading configuration */
  loadEnv?: boolean | LoadEnvOptions;
}

Usage Examples:

import { createRsbuild } from "@rsbuild/core";

// Basic instance creation
const rsbuild = await createRsbuild();

// With configuration
const rsbuild = await createRsbuild({
  rsbuildConfig: {
    source: { entry: { index: "./src/index.ts" } },
    output: { target: "web" },
  },
});

// With async configuration
const rsbuild = await createRsbuild({
  rsbuildConfig: async () => {
    const config = await loadConfigFromDatabase();
    return config;
  },
});

Build for Production

Executes a production build with optimizations, minification, and asset generation.

/**
 * Execute production build
 * @param options - Build options
 * @returns Promise resolving to build result with close function and stats
 */
build(options?: BuildOptions): Promise<{
  close: () => Promise<void>;
  stats?: Rspack.Stats | Rspack.MultiStats;
}>;

interface BuildOptions {
  /** Watch mode for continuous builds */
  watch?: boolean;
  /** Compiler options */
  compiler?: any;
}

Usage Examples:

const rsbuild = await createRsbuild({ rsbuildConfig: config });

// Build for production
await rsbuild.build();

Preview Production Build

Starts a local server to preview the production build output.

/**
 * Preview production build locally
 * @returns Promise that resolves when preview server starts
 */
preview(): Promise<void>;

Start Development Server

Starts the development server with hot module replacement and live reload capabilities.

/**
 * Start development server with HMR and live reload
 * @param options - Development server configuration options
 * @returns Promise resolving to server information
 */
startDevServer(options?: StartDevServerOptions): Promise<StartServerResult>;

interface StartDevServerOptions {
  /** Whether to automatically open browser */
  open?: boolean;
  /** Custom port number */
  port?: number;
  /** Custom host address */
  host?: string;
  /** Enable HTTPS */
  https?: boolean;
  /** Print server URLs */
  printUrls?: boolean | PrintUrls;
}

interface StartServerResult {
  /** Server URLs (local and network) */
  urls: string[];
  /** Server port number */
  port: number;
  /** Development server instance */
  server: RsbuildDevServer;
}

interface PrintUrls {
  local: boolean;
  network: boolean;
}

Create Development Server

Creates a development server instance without starting it, allowing for custom configuration.

/**
 * Create development server instance without starting
 * @returns Promise resolving to development server instance
 */
createDevServer(): Promise<RsbuildDevServer>;

Create Compiler

Creates the underlying bundler compiler instance (Rspack or Webpack).

/**
 * Create bundler compiler instance
 * @returns Promise resolving to compiler instance
 */
createCompiler(): Promise<Compiler>;

interface Compiler {
  /** Run a single build */
  run(): Promise<void>;
  /** Start watching for changes */
  watch(): Promise<void>;
  /** Close the compiler */
  close(): Promise<void>;
}

Initialize Configurations

Initializes and returns the internal bundler configurations for all environments.

/**
 * Initialize internal bundler configurations
 * @returns Promise resolving to configuration initialization results
 */
initConfigs(): Promise<InitConfigsResult>;

interface InitConfigsResult {
  /** Rspack configurations by environment */
  rspackConfigs: RspackConfig[];
  /** Environment contexts */
  environments: EnvironmentContext[];
}

Inspect Configuration

Debug and inspect the resolved configurations for troubleshooting and development.

/**
 * Inspect resolved configurations for debugging
 * @param options - Inspection options
 * @returns Promise resolving to inspection results
 */
inspectConfig(options?: InspectConfigOptions): Promise<InspectConfigResult>;

interface InspectConfigOptions {
  /** Environment to inspect */
  env?: string;
  /** Output format */
  outputPath?: string;
  /** Verbose output */
  verbose?: boolean;
  /** Write to file */
  writeToFile?: boolean;
}

interface InspectConfigResult {
  /** Rsbuild configuration */
  rsbuildConfig: string;
  /** Rspack configurations */
  rspackConfigs: string[];
  /** Environment configurations */
  environmentConfigs: Record<string, string>;
  /** Bundle analyzer data */
  bundlerConfigs: Record<string, string>;
}

Plugin Management

Add Plugins

Adds one or more plugins to the Rsbuild instance.

/**
 * Add plugins to the Rsbuild instance
 * @param plugins - Plugin instances or configurations to add
 */
addPlugins(plugins: RsbuildPlugins): void;

Remove Plugins

Removes plugins from the Rsbuild instance by name.

/**
 * Remove plugins by name
 * @param pluginNames - Names of plugins to remove
 */
removePlugins(pluginNames: string[]): void;

Get Plugins

Retrieves all registered plugins.

/**
 * Get all registered plugins
 * @returns Array of registered plugin instances
 */
getPlugins(): RsbuildPlugin[];

Check Plugin Existence

Checks if a plugin with the given name is registered, optionally within a specific environment.

/**
 * Check if plugin exists by name
 * @param pluginName - Name of plugin to check
 * @param options - Options including environment specification
 * @returns True if plugin is registered
 */
isPluginExists(pluginName: string, options?: { environment?: string }): boolean;

Configuration Access

Get Rsbuild Configuration

Retrieves the current Rsbuild configuration.

/**
 * Get current Rsbuild configuration
 * @returns Current Rsbuild configuration object
 */
getRsbuildConfig(): RsbuildConfig;

Get Normalized Configuration

Retrieves the normalized configuration with defaults applied.

/**
 * Get normalized configuration with defaults
 * @returns Normalized configuration object
 */
getNormalizedConfig(): NormalizedConfig;

Modify Rsbuild Configuration

Modifies the Rsbuild configuration using a callback function.

/**
 * Modify Rsbuild configuration
 * @param fn - Function to modify configuration
 */
modifyRsbuildConfig(fn: ModifyRsbuildConfigFn): void;

type ModifyRsbuildConfigFn = (
  config: RsbuildConfig,
  utils: ModifyRsbuildConfigUtils
) => RsbuildConfig | void | Promise<RsbuildConfig | void>;

interface ModifyRsbuildConfigUtils {
  mergeConfig: typeof mergeRsbuildConfig;
}

Modify Environment Configuration

Modifies environment-specific configuration.

/**
 * Modify environment-specific configuration
 * @param fn - Function to modify environment configuration
 */
modifyEnvironmentConfig(fn: ModifyEnvironmentConfigFn): void;

type ModifyEnvironmentConfigFn = (
  config: EnvironmentConfig,
  utils: ModifyEnvironmentConfigUtils
) => EnvironmentConfig | void | Promise<EnvironmentConfig | void>;

interface ModifyEnvironmentConfigUtils {
  name: string;
  mergeConfig: (config: EnvironmentConfig) => EnvironmentConfig;
}

Lifecycle Hooks

Build Lifecycle

/**
 * Hook called before production build starts
 * @param fn - Callback function
 */
onBeforeBuild(fn: OnBeforeBuildFn): void;

/**
 * Hook called after production build completes
 * @param fn - Callback function
 */
onAfterBuild(fn: OnAfterBuildFn): void;

/**
 * Hook called when build process closes
 * @param fn - Callback function
 */
onCloseBuild(fn: OnCloseBuildFn): void;

type OnBeforeBuildFn = (params: { environments: EnvironmentContext[] }) => void | Promise<void>;
type OnAfterBuildFn = (params: { environments: EnvironmentContext[]; stats: BuildStats }) => void | Promise<void>;
type OnCloseBuildFn = () => void | Promise<void>;

Development Lifecycle

/**
 * Hook called before development compilation
 * @param fn - Callback function
 */
onBeforeDevCompile(fn: OnBeforeDevCompileFn): void;

/**
 * Hook called after development compilation
 * @param fn - Callback function
 */
onAfterDevCompile(fn: OnAfterDevCompileFn): void;

/**
 * Hook called when development compilation completes successfully
 * @param fn - Callback function
 */
onDevCompileDone(fn: OnDevCompileDoneFn): void;

type OnBeforeDevCompileFn = (params: { environments: EnvironmentContext[] }) => void | Promise<void>;
type OnAfterDevCompileFn = (params: { environments: EnvironmentContext[]; stats: DevStats }) => void | Promise<void>;
type OnDevCompileDoneFn = (params: { environments: EnvironmentContext[]; stats: DevStats }) => void | Promise<void>;

Server Lifecycle

/**
 * Hook called before development server starts
 * @param fn - Callback function
 */
onBeforeStartDevServer(fn: OnBeforeStartDevServerFn): void;

/**
 * Hook called after development server starts
 * @param fn - Callback function
 */
onAfterStartDevServer(fn: OnAfterStartDevServerFn): void;

/**
 * Hook called when development server closes
 * @param fn - Callback function
 */
onCloseDevServer(fn: OnCloseDevServerFn): void;

type OnBeforeStartDevServerFn = (params: { port: number; host: string }) => void | Promise<void>;
type OnAfterStartDevServerFn = (params: { port: number; host: string; urls: string[] }) => void | Promise<void>;
type OnCloseDevServerFn = () => void | Promise<void>;

Compiler Lifecycle

/**
 * Hook called before compiler creation
 * @param fn - Callback function
 */
onBeforeCreateCompiler(fn: OnBeforeCreateCompilerFn): void;

/**
 * Hook called after compiler creation
 * @param fn - Callback function
 */
onAfterCreateCompiler(fn: OnAfterCreateCompilerFn): void;

type OnBeforeCreateCompilerFn = (params: { environments: EnvironmentContext[] }) => void | Promise<void>;
type OnAfterCreateCompilerFn = (params: { compiler: Compiler; environments: EnvironmentContext[] }) => void | Promise<void>;

Process Lifecycle

/**
 * Hook called on process exit
 * @param fn - Callback function
 */
onExit(fn: OnExitFn): void;

type OnExitFn = () => void | Promise<void>;

Core Types

interface RsbuildInstance {
  // Build methods
  build(options?: BuildOptions): Promise<{
    close: () => Promise<void>;
    stats?: Rspack.Stats | Rspack.MultiStats;
  }>;
  preview(): Promise<void>;
  startDevServer(options?: StartDevServerOptions): Promise<StartServerResult>;
  createDevServer(): Promise<RsbuildDevServer>;
  createCompiler(): Promise<Compiler>;
  initConfigs(): Promise<InitConfigsResult>;
  inspectConfig(options?: InspectConfigOptions): Promise<InspectConfigResult>;
  
  // Plugin management
  addPlugins(plugins: RsbuildPlugins): void;
  removePlugins(pluginNames: string[]): void;
  getPlugins(): RsbuildPlugin[];
  isPluginExists(pluginName: string, options?: { environment?: string }): boolean;
  
  // Configuration access
  getRsbuildConfig(): RsbuildConfig;
  getNormalizedConfig(): NormalizedConfig;
  modifyRsbuildConfig(fn: ModifyRsbuildConfigFn): void;
  modifyEnvironmentConfig(fn: ModifyEnvironmentConfigFn): void;
  
  // Lifecycle hooks
  onBeforeBuild(fn: OnBeforeBuildFn): void;
  onAfterBuild(fn: OnAfterBuildFn): void;
  onCloseBuild(fn: OnCloseBuildFn): void;
  onBeforeDevCompile(fn: OnBeforeDevCompileFn): void;
  onAfterDevCompile(fn: OnAfterDevCompileFn): void;
  onDevCompileDone(fn: OnDevCompileDoneFn): void;
  onBeforeStartDevServer(fn: OnBeforeStartDevServerFn): void;
  onAfterStartDevServer(fn: OnAfterStartDevServerFn): void;
  onCloseDevServer(fn: OnCloseDevServerFn): void;
  onBeforeCreateCompiler(fn: OnBeforeCreateCompilerFn): void;
  onAfterCreateCompiler(fn: OnAfterCreateCompilerFn): void;
  onExit(fn: OnExitFn): void;
}

interface BuildStats {
  hasErrors: boolean;
  hasWarnings: boolean;
  toJson(): any;
}

interface DevStats extends BuildStats {
  compilation: any;
}

Install with Tessl CLI

npx tessl i tessl/npm-rsbuild--core

docs

cli-integration.md

configuration-management.md

core-build-system.md

development-server.md

environment-system.md

environment-variables.md

index.md

plugin-system.md

tile.json