当前位置: 首页 > news >正文

Vue-cli迁移Rsbuild

🛠️ Vue CLI 项目迁移到 Rsbuild 指南


📦 一、为什么迁移到 Rsbuild?

  • ⚡ 构建更快:基于 Rspack 构建,性能比 Webpack 更强。
  • 📁 配置更灵活:支持现代配置语法,更易读可维护。
  • 🧩 插件生态丰富:内置 Vue、Less、TypeScript 支持,无需额外配置。
  • 🧹 自动优化:默认启用 Tree-shaking、缓存等现代优化技术。

🚧 二、迁移准备工作 (以npm为例)

1. 卸载 Vue CLI 相关依赖

npm remove @vue/cli-service @vue/cli-plugin-babel @vue/cli-plugin-eslint core-js

2. 安装 Rsbuild 及其插件

npm install @rsbuild/core @rsbuild/plugin-vue @rsbuild/plugin-less -D

📂 三、迁移目录结构建议(可选)

保持原目录结构基本不变,大部分迁移只涉及构建配置部分:

├── public/                # 静态资源目录
├── src/
│   ├── assets/
│   ├── components/
│   ├── views/
│   └── main.ts           # 入口文件
├── index.html
├── rsbuild.config.ts     # Rsbuild 配置文件
└── tsconfig.json

⚙️ 四、核心配置迁移:vue.config.js → rsbuild.config.ts

新建 rsbuild.config.ts,将原来的 vue.config.js 中核心配置进行迁移。

✅ 示例完整配置:

// rsbuild.config.ts
import { defineConfig } from "@rsbuild/core";
import { pluginVue } from "@rsbuild/plugin-vue";
import { pluginLess } from "@rsbuild/plugin-less";
import path from "path";// 目录解析函数
const resolve = (dir: string) => path.resolve(__dirname, dir);// 环境代理配置映射(支持 dev、old_dev、pro)
const proxyMap = {dev: "https://abc.cn/",old_dev: "http://175.34.2.58:8889",pro: "https://aaa.com/"
};// 当前执行的 npm 脚本名决定代理目标
const env = process.env.npm_lifecycle_event || "dev";// ✅ Rsbuild 主配置
export default defineConfig({// 插件配置plugins: [pluginVue(),pluginLess({lessLoaderOptions: {lessOptions: {javascriptEnabled: true,modifyVars: {"primary-color": "#409EFF","success-color": "#67C23A","warning-color": "#E6A23C","error-color": "#F05F60","link-color": "#1890FF","@text-color": "#333333","@text-color-secondary": "#999999","@border-radius-base": "4px"}},// 全局样式注入additionalData: '@import "@/assets/less/global.less";'}})],// 开发配置dev: {hmr: true,writeToDisk: true, // 开发时是否写入磁盘(方便调试)cache: {type: "filesystem" // 使用磁盘缓存加速编译}},// 入口文件和别名定义source: {entry: {index: resolve("src/main.ts")},alias: {"@": resolve("src")},define: {// 设置环境变量"process.env": {API_BASE_URL: JSON.stringify(process.env.VUE_APP_API_BASE_URL)}}},// devServer 配置(替代 vue.config.js 中的 devServer)server: {port: 8080,compress: false,hmr: true,proxy: {"/api": {target: proxyMap[env],changeOrigin: true,logLevel: "debug",cookieDomainRewrite: {"*": "localhost"}}}},// 构建输出配置output: {assetPrefix: "/", // 公共资源路径manifest: true,   // 是否生成 manifest.jsondistPath: {root: "./dist"},sourceMap: {js: process.env.NODE_ENV === "development" ? "inline-source-map" : "source-map",css: false}},html: {template: "./public/index.html"},// 去除生产环境 consoleperformance: {removeConsole: true},// bundlerChain 可以访问底层 Rspack 配置,适配 Webpack 迁移项tools: {bundlerChain(config) {/*** 🖼️ 处理 SVG 图标为 sprite 图(替代 vue.config.js 中的 svg-sprite-loader)*/config.module.rules.delete("svg");config.module.rule("svg-sprite-loader").test(/\.svg$/).include.add(resolve("src/assets/icons")).end().use("svg-sprite-loader").loader("svg-sprite-loader").options({symbolId: "icon-[name]"});/*** 📦 拆包策略(等价于 vue.config.js -> chainWebpack.optimization.splitChunks)*/config.optimization.splitChunks({chunks: "all",minSize: 20000,maxAsyncRequests: 6,maxInitialRequests: 6,cacheGroups: {"vendors-base": {test: /[\\/]node_modules[\\/](vue|vue-router|axios)/,name: "vendors-base",priority: 10,reuseExistingChunk: true},"vendors-console": {test: /[\\/]node_modules[\\/](ant-design-vue|dayjs)/,name: "vendors-console",priority: 8,reuseExistingChunk: true},"vendors-jspreadsheet": {test: /[\\/]node_modules[\\/](@jspreadsheet)/,name: "vendors-jspreadsheet",priority: 8,reuseExistingChunk: true},"chunk-vendors": {test: /[\\/]node_modules[\\/]/,name: "chunk-vendors",priority: 1,reuseExistingChunk: true}}});}}
});

🔄 五、环境变量迁移

Vue CLI 使用 .env.* 文件定义变量,例如:

VUE_APP_API_BASE_URL='/api'

Rsbuild 同样支持 .env 文件,使用 process.env.VUE_APP_API_BASE_URL 获取,无需修改原代码中的变量读取逻辑


🧪 六、运行开发环境

npx rsbuild dev

🧱 七、打包构建项目

npx rsbuild build

✅ 八、注意事项

项目Vue CLIRsbuild
构建工具WebpackRspack(更快)
SVG 图标处理方式svg-sprite-loader相同(配置写法略不同)
全局 less 变量引入style-resources-loader使用 additionalData 方式引入
环境变量注入process.env.VUE_APP_同样支持,无需更改
拆包优化配置chainWebpacktools.bundlerChain 内设置

📌 九、推荐 Rsbuild 插件(按需使用)

插件用途
@rsbuild/plugin-vueVue 3 支持
@rsbuild/plugin-lessLess 支持 + 变量注入
@rsbuild/plugin-swc替代 Babel 编译器,速度更快
@rsbuild/plugin-legacy浏览器兼容插件,支持低版本浏览器
@rsbuild/plugin-analyze构建分析图(类似 webpack-bundle-analyzer)

如需进一步定制,可参考官方文档:

🔗 Rsbuild 官方文档


scripts

"scripts": {"dev": "rsbuild dev","build": "rsbuild build","analyze": "rsbuild build --analyze"
}

相关文章:

  • 重置cursor免费次数(2025.4.17可行)
  • MySQL入门:数据操作CURD
  • SSMS中如何把一个库的表移到另一个库中
  • java 多线程之Worker Thread模式(Thread Pool模式)
  • 基于Django框架的图书索引智能排序系统设计与实现(源码+lw+部署文档+讲解),源码可白嫖!
  • 大数据开发核心技术难点:数据倾斜问题深度解析
  • docker harbor私有仓库登录报错
  • CASS 用户坐标系转换到世界坐标系
  • 阿里云ECS访问不了
  • 【NLP 64、基于LLM的垂直领域【特定领域】问答方案】
  • Java与MySQL数据库连接的JDBC驱动配置教程
  • ORA-00600: internal error code, arguments: [kcratr_nab_less_than_odr], [1],
  • RabbitMQ原理及代码示例
  • ESP32之OTA固件升级流程,基于VSCode环境下的ESP-IDF开发,基于阿里云物联网平台MQTT-TLS连接通信(附源码)
  • 2025华中杯B题——AI实现
  • Ubuntu20.04配置cartographer记录
  • 函数递归:递归的概念
  • C#日志辅助类(Log4Net)实现
  • Redis之全局唯一ID
  • 2. 判断列表元素的单一性
  • 范志毅跨界归来做青训,探索中国足球人才培养新模式
  • 上海将发布新一版不予行政处罚清单、首份减轻行政处罚清单
  • 微软通讯软件Skype正式停止运营:斥资85亿美元购入,月活用户曾超3亿
  • 上海虹桥高铁站拦门事件反转,谁在带偏网友?
  • 10家A股农商行去年年报:瑞丰银行营收增速领跑,常熟银行等4家净利增速超11%
  • 巴基斯坦宣布禁止与印度的进口贸易