@esmx/rspack

The Rspack package provides a set of APIs for creating and configuring Rspack applications, supporting the building and development of both standard applications and HTML applications.

Installation

Install @esmx/rspack as a development dependency using your package manager:

npm
yarn
pnpm
bun
npm install @esmx/rspack -D

Type Exports

BuildTarget

type BuildTarget = 'node' | 'client' | 'server'

The build target environment type defines the target environment for application building, used to configure specific optimizations and features during the build process:

  • node: Builds code to run in Node.js environments
  • client: Builds code to run in browser environments
  • server: Builds code to run in server environments

RspackAppConfigContext

interface RspackAppConfigContext {
  esmx: Esmx
  buildTarget: BuildTarget
  config: RspackOptions
  options: RspackAppOptions
}

The Rspack application configuration context interface provides contextual information accessible within configuration hook functions:

  • esmx: Esmx framework instance
  • buildTarget: Current build target (client/server/node)
  • config: Rspack configuration object
  • options: Application configuration options

RspackAppOptions

interface RspackAppOptions {
  css?: 'css' | 'js' | false
  loaders?: {
    styleLoader?: string
  }
  styleLoader?: Record<string, any>
  cssLoader?: Record<string, any>
  target?: {
    web?: string[]
    node?: string[]
  }
  definePlugin?: Record<string, any>
  config?: (context: RspackAppConfigContext) => void | Promise<void>
}

Rspack application configuration options interface:

  • css: CSS output method, either 'css' (standalone files) or 'js' (bundled into JS). Defaults to automatic selection based on environment: production uses 'css' for cache optimization and parallel loading, development uses 'js' to support HMR
  • loaders: Custom loader configurations
  • styleLoader: style-loader configuration options
  • cssLoader: css-loader configuration options
  • target: Build target compatibility configurations
  • definePlugin: Global constant definitions
  • config: Configuration hook function

RspackHtmlAppOptions

Extends RspackAppOptions, used to configure HTML application-specific options.

Function Exports

createRspackApp

function createRspackApp(esmx: Esmx, options?: RspackAppOptions): Promise<App>

Creates a standard Rspack application instance.

Parameters:

  • esmx: Esmx framework instance
  • options: Rspack application configuration options

Returns:

  • A Promise that resolves to the created application instance

createRspackHtmlApp

function createRspackHtmlApp(esmx: Esmx, options?: RspackHtmlAppOptions): Promise<App>

Creates an HTML-type Rspack application instance.

Parameters:

  • esmx: Esmx framework instance
  • options: HTML application configuration options

Returns:

  • A Promise that resolves to the created HTML application instance

Constant Exports

RSPACK_LOADER

const RSPACK_LOADER: Record<string, string> = {
  builtinSwcLoader: 'builtin:swc-loader',
  lightningcssLoader: 'builtin:lightningcss-loader',
  styleLoader: 'style-loader',
  cssLoader: 'css-loader',
  lessLoader: 'less-loader',
  styleResourcesLoader: 'style-resources-loader',
  workerRspackLoader: 'worker-rspack-loader'
}

A mapping object of Rspack's built-in loader identifiers, providing commonly used loader name constants:

  • builtinSwcLoader: Rspack's built-in SWC loader for processing TypeScript/JavaScript files
  • lightningcssLoader: Rspack's built-in lightningcss loader, a high-performance compiler for CSS files
  • styleLoader: Loader for injecting CSS into the DOM
  • cssLoader: Loader for parsing CSS files and handling CSS modularization
  • lessLoader: Loader for compiling Less files into CSS
  • styleResourcesLoader: Loader for automatically importing global style resources (e.g., variables, mixins)
  • workerRspackLoader: Loader for processing Web Worker files

Using these constants allows referencing built-in loaders in configurations, avoiding manual string input:

src/entry.node.ts
import { RSPACK_LOADER } from '@esmx/rspack';

export default {
  async devApp(esmx) {
    return import('@esmx/rspack').then((m) =>
      m.createRspackHtmlApp(esmx, {
        loaders: {
          // Use constants to reference loaders
          styleLoader: RSPACK_LOADER.styleLoader,
          cssLoader: RSPACK_LOADER.cssLoader,
          lightningcssLoader: RSPACK_LOADER.lightningcssLoader
        }
      })
    );
  }
};

Notes:

  • These loaders are already built into Rspack and require no additional installation
  • When customizing loader configurations, these constants can be used to replace default loader implementations
  • Certain loaders (e.g., builtinSwcLoader) have specific configuration options - refer to their respective documentation

Module Exports

rspack

Re-exports all contents from the @rspack/core package, providing complete Rspack core functionality.