close
logo
Rslib
指南
配置
博客
English
简体中文
指南
配置
博客
English
简体中文
logo
Rslib

开始

介绍
快速上手
名词解释
NPM 包

解决方案

Node.js
React
Vue

基础

命令行工具
配置 Rslib
使用 TypeScript
产物输出格式
产物结构
升级 Rslib

进阶

处理三方依赖
产物兼容性
类型生成
引用静态资源
引用 SVGR
引用 JSON 文件
模块联邦
使用 Storybook

迁移

Modern.js Module
tsup

常见问题

功能类问题
📝 在 GitHub 上编辑此页
上一页React
下一页命令行工具

#Vue

在本文档中,你将学习如何使用 Rslib 构建 Vue 组件库,你可在 示例 中查看 Vue 相关演示项目。

NOTE
  1. 仅支持 Vue3 版本,Vue2 版本不支持。

  2. Vue 的类型声明文件由 vue-tsc 生成,所以 lib.dts / lib.redirect.dts / lib.banner.dts / lib.footer.dts 在 Vue 项目中不生效。

#创建 Vue 项目

你可以使用 create-rslib 创建 Rslib + Vue 项目。只需执行以下命令:

npm
yarn
pnpm
bun
npm create rslib@latest

然后,当提示 "Select template" 选择 Vue。

#在现有 Rslib 项目中使用

开发 Vue 组件,需要在 rslib.config.ts 中设置 target 为 "web"。 这一点至关重要,因为 Rslib 默认将 target 设置为 "node",这与 Rsbuild 的 target 默认值不同。

要编译 Vue(.vue 单文件组件),你需要注册基于 unplugin-vue 实现的 rsbuild-plugin-unplugin-vue 插件。该插件将自动添加 Vue 构建所需的配置。

例如,在 rslib.config.ts 中注册:

rslib.config.ts
import { defineConfig } from '@rslib/core';
import { pluginUnpluginVue } from 'rsbuild-plugin-unplugin-vue'; 

export default defineConfig({
  lib: [
    // ...
  ],
  output: {
    target: 'web',
  },
  plugins: [pluginUnpluginVue(/** options here */)],
});
NOTE

目前 rsbuild-plugin-unplugin-vue 插件还不支持打包 Vue SFC scoped CSS 样式,你可以选择 Less、Sass 等样式解决方案

更多配置可以参考 rsbuild-plugin-unplugin-vue 插件文档。