Rspack 集成示例#

按快速开始 安装最小示例;其 Rspack 固定为 1.7.12。

1// rspack.config.mjs:核心配置
2import { resolve } from 'node:path';
3import fileBasedRouter from '@feoe/fs-router/rspack';
4import { rspack } from '@rspack/core';
5
6export default {
7  mode: 'production',
8  entry: './src/main.tsx',
9  output: { path: resolve('dist/rspack'), filename: 'app.js', publicPath: '/', clean: true },
10  resolve: { extensions: ['.tsx', '.ts', '.js'] },
11  module: { rules: [{
12    test: /\.tsx?$/, exclude: /node_modules/,
13    use: { loader: 'builtin:swc-loader', options: { jsc: {
14      parser: { syntax: 'typescript', tsx: true },
15      transform: { react: { runtime: 'automatic' } },
16    } } },
17  }] },
18  plugins: [fileBasedRouter(), new rspack.HtmlRspackPlugin({ template: './bundler.html' })],
19};

实际配置还以配置文件所在目录固定 context/output,避免 cwd 变化。不要把 SWC 的 options 放在规则对象外层。

1npm --prefix examples/minimal-react run build:rspack
2npm --prefix examples/minimal-react run preview:rspack

构建脚本调用 Rspack API,检查错误并关闭 compiler,再运行 tsc;不需要全局 CLI。preview 仅提供本地生产产物与 SPA 回退,不是线上部署服务。文件结构和 loader 用法与 Vite 相同。

完整配置。旧 Rsbuild 微前端工程不作为此版本兼容性证据;额外 runtime 集成需单独验证。