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:
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 RspackAppChainContext {
esmx: Esmx
buildTarget: BuildTarget
chain: import('rspack-chain').RspackChain
options: RspackAppOptions
}Rspack application chain configuration context interface for the chain hook function:
esmx: Esmx framework instancebuildTarget: Current build target (client/server/node)chain: rspack-chain configuration object for chained configurationoptions: Application configuration optionsinterface RspackAppOptions {
minimize?: boolean
config?: (context: RspackAppConfigContext) => void
chain?: (context: RspackAppChainContext) => void
}Rspack application configuration options interface:
minimize: Whether to enable code minification, true to enable, false to disable, undefined to automatically decide based on environment (enabled in production, disabled in development)config: Configuration hook function called before build starts for modifying Rspack compilation configurationchain: Chain configuration hook function using rspack-chain for flexible configuration modificationInherits from RspackAppOptions, used to configure specific options for HTML applications:
interface RspackHtmlAppOptions extends RspackAppOptions {
css?: 'css' | 'js' | false
loaders?: Partial<Record<keyof typeof RSPACK_LOADER, string>>
styleLoader?: Record<string, any>
cssLoader?: Record<string, any>
lessLoader?: Record<string, any>
styleResourcesLoader?: Record<string, any>
swcLoader?: SwcLoaderOptions
definePlugin?: Record<string, string | Partial<Record<BuildTarget, string>>>
target?: TargetSetting
}css: CSS output method, optional 'css' (separate file) or 'js' (bundled into JS), defaults to automatic selection based on environmentloaders: Custom loader configuration for replacing default loader implementationsstyleLoader: style-loader configuration optionscssLoader: css-loader configuration optionslessLoader: less-loader configuration optionsstyleResourcesLoader: Global style resources auto-injection configurationswcLoader: SWC loader configuration optionsdefinePlugin: Global constant definitionstarget: Build target compatibility configurationfunction createRspackApp(esmx: Esmx, options?: RspackAppOptions): Promise<App>Create a standard Rspack application instance.
Parameters:
esmx — Esmx framework instanceoptions — Rspack application configuration optionsReturns:
Promise that resolves to the created application instancefunction createRspackHtmlApp(esmx: Esmx, options?: RspackHtmlAppOptions): Promise<App>Create an HTML-type Rspack application instance.
Parameters:
esmx — Esmx framework instanceoptions — HTML application configuration optionsReturns:
Promise that resolves to the created HTML application instanceconst 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.