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.
Use package manager to install @esmx/rspack as a development dependency:
npm install @esmx/rspack -Dtype 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 environmentclient: Build code to run in browser environmentserver: Build code to run in server environmentinterface 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 instancebuildTarget: Current build target (client/server/node)config: Rspack configuration objectoptions: Application configuration optionsinterface 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 HMRloaders: Custom loader configurationstyleLoader: style-loader configuration optionscssLoader: css-loader configuration optionstarget: Build target compatibility configurationdefinePlugin: Global constant definitionsconfig: Configuration hook functionInherits from RspackAppOptions, used to configure specific options for HTML applications.
function createRspackApp(esmx: Esmx, options?: RspackAppOptions): Promise<App>Create a standard Rspack application instance.
Parameters:
esmx: Esmx framework instanceoptions: Rspack application configuration optionsReturns:
function createRspackHtmlApp(esmx: Esmx, options?: RspackHtmlAppOptions): Promise<App>Create an HTML-type Rspack application instance.
Parameters:
esmx: Esmx framework instanceoptions: HTML application configuration optionsReturns:
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 fileslightningcssLoader: Rspack built-in lightningcss loader, a high-performance compiler for processing CSS filesstyleLoader: Loader for injecting CSS into the DOMcssLoader: Loader for parsing CSS files and handling CSS modularizationlessLoader: Loader for compiling Less files to CSSstyleResourcesLoader: Loader for automatically importing global style resources (variables, mixins, etc.)workerRspackLoader: Loader for processing Web Worker filesUsing these constants can reference built-in loaders in configuration, avoiding manual string input:
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:
builtinSwcLoader) have specific configuration options, please refer to the corresponding configuration documentationRe-exports all contents from @rspack/core package, providing complete Rspack core functionality.