The Rspack React package provides a set of APIs for creating and configuring React-based Rspack applications, supporting React component development, building, and Server-Side Rendering with React Refresh hot module replacement.
Use package manager to install @esmx/rspack-react as a development dependency:
npm install @esmx/rspack-react -Dtype BuildTarget = 'node' | 'client' | 'server'Build target environment type that defines the target environment for application builds, 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 RspackAppChainContext {
esmx: Esmx
buildTarget: BuildTarget
chain: RspackChain
options: RspackAppOptions
}Rspack chain configuration context interface provides context information accessible in chain hook functions:
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 | Promise<void>
chain?: (context: RspackAppChainContext) => void | Promise<void>
}Rspack application configuration options interface:
minimize: Whether to enable code compression, defaults to automatic selection based on environmentconfig: Configuration hook function for modifying Rspack compilation configurationchain: Chain hook function using rspack-chain for flexible configuration modificationinterface 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
}Rspack HTML application configuration options interface, extends RspackAppOptions:
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 compilation optionsstyleResourcesLoader: Global style resources auto-injection configurationswcLoader: TypeScript/JavaScript compilation optionsdefinePlugin: Global constant definitions, supports different values for different build targetstarget: Build target compatibility configurationinterface RspackReactAppOptions extends RspackHtmlAppOptions {
// No additional options, inherits all options from RspackHtmlAppOptions
}Rspack React application configuration options interface, extends RspackHtmlAppOptions. All options from RspackHtmlAppOptions are available, with React-specific configurations handled automatically by the build tool.
function createRspackReactApp(esmx: Esmx, options?: RspackReactAppOptions): Promise<App>Creates a React Rspack application instance with automatic React configuration, including:
Parameters:
esmx: Esmx framework instanceoptions: React application configuration optionsReturns:
Example:
import type { EsmxOptions } from '@esmx/core';
export default {
async devApp(esmx) {
return import('@esmx/rspack-react').then((m) =>
m.createRspackReactApp(esmx, {
// Custom configuration
css: 'css',
chain(context) {
// Additional chain configuration
const { chain, buildTarget } = context;
if (buildTarget === 'client') {
// Client-specific configuration
}
}
})
);
}
} satisfies EsmxOptions;React-Specific Features:
The createRspackReactApp function automatically configures:
.tsx and .jsx extensions to resolve configurationprocess.env.NODE_ENV based on build environmentfunction createRspackHtmlApp(esmx: Esmx, options?: RspackHtmlAppOptions): Promise<App>Creates an HTML-type Rspack application instance. This function is re-exported from @esmx/rspack and is available for use in React applications if needed.
Parameters:
esmx: Esmx framework instanceoptions: HTML application configuration optionsReturns:
function createRspackApp(esmx: Esmx, options?: RspackAppOptions): Promise<App>Creates a standard Rspack application instance. This function is re-exported from @esmx/rspack and provides the base Rspack functionality.
Parameters:
esmx: Esmx framework instanceoptions: Rspack 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 files, configured with React transform for TSX/JSX fileslightningcssLoader: Rspack built-in lightningcss loader, a high-performance compiler for processing CSS filesstyleLoader: Loader that injects CSS into the DOMcssLoader: Loader that parses CSS files and handles CSS modularizationlessLoader: Loader that compiles Less files to CSSstyleResourcesLoader: Loader that automatically imports 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-react';
export default {
async devApp(esmx) {
return import('@esmx/rspack-react').then((m) =>
m.createRspackReactApp(esmx, {
loaders: {
// Use constants to reference loaders
styleLoader: RSPACK_LOADER.styleLoader,
cssLoader: RSPACK_LOADER.cssLoader,
lightningcssLoader: RSPACK_LOADER.lightningcssLoader
}
})
);
}
};Notes:
builtinSwcLoader is automatically configured with React transform for .tsx and .jsx filesbuiltinSwcLoader) have specific configuration options; please refer to the corresponding configuration documentationRe-exports all contents from @rspack/core package, providing complete Rspack core functionality, including plugins, loaders, and utilities.
When using createRspackReactApp, the following React-specific configurations are automatically applied:
.tsx and .jsx are added to resolve extensions.tsx and .jsx files with:
process.env.NODE_ENV is set based on build environmentYou can customize React-specific behavior using the chain hook:
import type { EsmxOptions } from '@esmx/core';
export default {
async devApp(esmx) {
return import('@esmx/rspack-react').then((m) =>
m.createRspackReactApp(esmx, {
chain(context) {
const { chain, buildTarget } = context;
// Modify React loader configuration
chain.module
.rule('react-tsx')
.use('swc-loader')
.tap((options) => {
return {
...options,
jsc: {
...options.jsc,
transform: {
...options.jsc.transform,
react: {
...options.jsc.transform.react,
// Custom React transform options
pragma: 'React.createElement',
pragmaFrag: 'React.Fragment'
}
}
}
};
});
}
})
);
}
} satisfies EsmxOptions;React SSR is fully supported. React components can be rendered directly using react-dom/server without special loader configuration:
import { renderToString } from 'react-dom/server';
import { App } from './App';
export async function render(context) {
const html = renderToString(<App />);
context.html = html;
}import type { EsmxOptions } from '@esmx/core';
export default {
async devApp(esmx) {
return import('@esmx/rspack-react').then((m) =>
m.createRspackReactApp(esmx)
);
}
} satisfies EsmxOptions;import type { EsmxOptions } from '@esmx/core';
export default {
async devApp(esmx) {
return import('@esmx/rspack-react').then((m) =>
m.createRspackReactApp(esmx, {
css: 'css', // Output CSS to separate files
cssLoader: {
modules: {
localIdentName: '[name]__[local]--[hash:base64:5]'
}
}
})
);
}
} satisfies EsmxOptions;import type { EsmxOptions } from '@esmx/core';
export default {
async devApp(esmx) {
return import('@esmx/rspack-react').then((m) =>
m.createRspackReactApp(esmx, {
swcLoader: {
jsc: {
parser: {
syntax: 'typescript',
tsx: true,
decorators: true
},
transform: {
legacyDecorator: true
}
}
}
})
);
}
} satisfies EsmxOptions;