logo
  • Guide
  • API
  • Blog
  • English
    • 简体中文
    • English
    • @esmx/core
      Esmx
      App
      RenderContext
      ModuleConfig
      PackConfig
      ManifestJson
      App
      @esmx/rspack
      @esmx/rspack-vue

      Last Updated: 11/20/2025, 2:27:42 AM

      Previous pageManifestJsonNext page@esmx/rspack-vue

      #@esmx/rspack

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

      #Installation

      Use package manager to install @esmx/rspack as a development dependency:

      npm
      yarn
      pnpm
      bun
      deno
      npm install @esmx/rspack -D
      yarn add @esmx/rspack -D
      pnpm add @esmx/rspack -D
      bun add @esmx/rspack -D
      deno add npm:@esmx/rspack -D

      #Type Exports

      #BuildTarget

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

      Build target environment type that defines the application's build target environment, used to configure specific optimizations and features in the build process:

      • node: Build code to run in Node.js environment
      • client: Build code to run in browser environment
      • server: Build code to run in server environment

      #RspackAppConfigContext

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

      Rspack application configuration context interface provides context information accessible in 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, optional 'css' (separate file) or 'js' (bundled into JS), defaults to automatic selection based on the environment: production uses 'css' to optimize caching and parallel loading, development uses 'js' to support HMR
      • loaders: Custom loader configuration
      • styleLoader: style-loader configuration options
      • cssLoader: css-loader configuration options
      • target: Build target compatibility configuration
      • definePlugin: Global constant definitions
      • config: Configuration hook function

      #RspackHtmlAppOptions

      Inherits from RspackAppOptions, used to configure specific options for HTML applications.

      #Function Exports

      #createRspackApp

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

      Create a standard Rspack application instance.

      Parameters:

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

      Returns:

      • Returns a Promise that resolves to the created application instance

      #createRspackHtmlApp

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

      Create an HTML-type Rspack application instance.

      Parameters:

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

      Returns:

      • 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'
      }

      Rspack built-in loader identifier mapping object that provides commonly used loader name constants:

      • builtinSwcLoader: Rspack built-in SWC loader, for processing TypeScript/JavaScript files
      • lightningcssLoader: Rspack built-in lightningcss loader, a high-performance compiler for processing 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 to CSS
      • styleResourcesLoader: Loader for automatically importing global style resources (variables, mixins, etc.)
      • workerRspackLoader: Loader for processing Web Worker files

      Using these constants can reference built-in loaders in configuration, 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, no additional installation needed
      • When customizing loader configuration, you can use these constants to replace default loader implementations
      • Some loaders (like builtinSwcLoader) have specific configuration options, please refer to the corresponding configuration documentation

      #Module Exports

      #rspack

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