<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>Rslib 博客</title>
        <link>https://rslib.rs/</link>
        <description>Rslib is a library development tool that leverages the well-designed configurations and plugins of Rsbuild.</description>
        <lastBuildDate>Mon, 09 Nov 2026 06:31:00 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>zh-CN</language>
        <item>
            <title><![CDATA[Rslib 1.0 正式发布]]></title>
            <link>https://rslib.rs/zh/blog/v1-0</link>
            <guid isPermaLink="false">/zh/blog/v1-0</guid>
            <pubDate>Thu, 03 Sep 2026 15:00:00 GMT</pubDate>
            <description><![CDATA[Rslib 1.0 正式发布，围绕面向不同场景的库构建能力和开发者体验展开，并介绍升级指南与后续规划。]]></description>
            <content:encoded><![CDATA[<!--$--><h1 class="rp-toc-include" id="rslib-10-正式发布"><a href="#rslib-10-正式发布" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Rslib 1.0 正式发布<!-- --> </h1>
<p><em>2026 年 9 月 3 日</em></p>
<div class="group-n_jNI7 root-YZxMH8"><div class="root-Sr_5x3 groupItem-m5U5nn"><img src="https://github.com/Timeless0911.png" alt="Chenwei Dai" class="avatar-XCZJhu" loading="lazy" decoding="async"/><div class="content-BhgM4E"><div class="nameRow-U9Q3S8"><div class="name-k91bx6">Chenwei Dai</div><div class="links-jK83Yg"><a href="https://github.com/Timeless0911" target="_blank" rel="noopener noreferrer" class="link-yDHY4Y" aria-label="Chenwei Dai&#x27;s GitHub"><svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path fill="currentColor" d="M12 .297c-6.63 0-12 5.373-12 12c0 5.303 3.438 9.8 8.205 11.385c.6.113.82-.258.82-.577c0-.285-.01-1.04-.015-2.04c-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729c1.205.084 1.838 1.236 1.838 1.236c1.07 1.835 2.809 1.305 3.495.998c.108-.776.417-1.305.76-1.605c-2.665-.3-5.466-1.332-5.466-5.93c0-1.31.465-2.38 1.235-3.22c-.135-.303-.54-1.523.105-3.176c0 0 1.005-.322 3.3 1.23c.96-.267 1.98-.399 3-.405c1.02.006 2.04.138 3 .405c2.28-1.552 3.285-1.23 3.285-1.23c.645 1.653.24 2.873.12 3.176c.765.84 1.23 1.91 1.23 3.22c0 4.61-2.805 5.625-5.475 5.92c.42.36.81 1.096.81 2.22c0 1.606-.015 2.896-.015 3.286c0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"></path></svg></a></div></div><div class="title-MMKZdf">@Timeless0911</div></div></div><div class="root-Sr_5x3 groupItem-m5U5nn"><img src="https://github.com/elecmonkey.png" alt="Elecmonkey" class="avatar-XCZJhu" loading="lazy" decoding="async"/><div class="content-BhgM4E"><div class="nameRow-U9Q3S8"><div class="name-k91bx6">Elecmonkey</div><div class="links-jK83Yg"><a href="https://github.com/elecmonkey" target="_blank" rel="noopener noreferrer" class="link-yDHY4Y" aria-label="Elecmonkey&#x27;s GitHub"><svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path fill="currentColor" d="M12 .297c-6.63 0-12 5.373-12 12c0 5.303 3.438 9.8 8.205 11.385c.6.113.82-.258.82-.577c0-.285-.01-1.04-.015-2.04c-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729c1.205.084 1.838 1.236 1.838 1.236c1.07 1.835 2.809 1.305 3.495.998c.108-.776.417-1.305.76-1.605c-2.665-.3-5.466-1.332-5.466-5.93c0-1.31.465-2.38 1.235-3.22c-.135-.303-.54-1.523.105-3.176c0 0 1.005-.322 3.3 1.23c.96-.267 1.98-.399 3-.405c1.02.006 2.04.138 3 .405c2.28-1.552 3.285-1.23 3.285-1.23c.645 1.653.24 2.873.12 3.176c.765.84 1.23 1.91 1.23 3.22c0 4.61-2.805 5.625-5.475 5.92c.42.36.81 1.096.81 2.22c0 1.606-.015 2.896-.015 3.286c0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"></path></svg></a></div></div><div class="title-MMKZdf">@elecmonkey</div></div></div></div>
<p><img src="https://assets.rspack.rs/rslib/rslib-og-1-0.png" alt="Rslib 1.0，2026 年 9 月发布的基于 Rsbuild 的库开发工具"/></p>
<p>我们很高兴地宣布 Rslib 1.0 已经正式发布！</p>
<p>Rslib 是一款基于 <a href="https://rsbuild.rs/zh" target="_blank" rel="noopener noreferrer" class="rp-link">Rsbuild</a> 的库开发工具，目标是帮助开发者用简单直接的方式开发各种类型的 JavaScript 库，包括工具库、UI 组件库、CLI 以及 Agent 应用等。</p>
<h2 class="rp-toc-include" id="why-rslib"><a href="#why-rslib" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>为什么需要 Rslib</h2>
<p>Rslib 面向不同的库发布与消费场景，让库开发也能受益于应用构建中成熟的能力与生态。其核心优势体现在以下三个方面：</p>
<ul>
<li><strong>复用 Rspack 与 webpack 生态</strong>：Rslib 可以使用 Rsbuild 插件，以及 <a href="https://rspack.rs/zh" target="_blank" rel="noopener noreferrer" class="rp-link">Rspack</a> 和 <a href="https://webpack.js.org/" target="_blank" rel="noopener noreferrer" class="rp-link">webpack</a> 生态中的许多插件与 loader。如果应用项目同样使用 Rsbuild 或 Rspack，开发者可以在应用与库之间共享配置和插件，并延续已有的工程经验，减少重复配置与维护成本。</li>
<li><strong>支持构建模块联邦产物</strong>：除了 ESM、CJS 等常见格式，Rslib 还可以输出 <a href="/zh/guide/advanced/module-federation" class="rp-link">模块联邦产物</a>，使库能够作为远程模块被多个应用在运行时加载，并提供配套的本地开发与联调能力。</li>
<li><strong>统一的库构建流程</strong>：Rslib 可以在一次构建中完成 JavaScript 编译、框架语法转换、类型生成和静态资源处理，无需在多个工具之间切换或自行拼接构建流程。同时，Rslib 持续跟进 TypeScript 和框架生态的发展，支持 TypeScript 7 类型生成、React Compiler 等新能力。</li>
</ul>
<h2 class="rp-toc-include" id="from-v0-to-v1"><a href="#from-v0-to-v1" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>从 0.x 到 1.0</h2>
<p>自 <a href="/zh/blog/introducing-rslib" class="rp-link">Rslib 0.7 正式对外发布</a> 以来，我们先后发布了 16 个 minor 版本。在此期间，Rslib 推出了多项功能和优化，包括：</p>
<ul>
<li>
<p><strong>更快的构建与更小的产物</strong>：在包含 1 万个 React 组件的基准项目中，相较 Rslib 0.7.0，Rslib 1.0 的无缓存构建耗时<strong>减少约 24.3%</strong>，有缓存构建耗时<strong>减少约 56.7%</strong>，产物体积在 Gzip 前<strong>减少约 32.2%</strong>，在 Gzip 后<strong>减少约 4.1%</strong>。</p>
<div class="rp-table-scroll-container rp-scrollbar"><table><thead><tr><th>Rslib 版本</th><th>生产构建（无缓存）</th><th>生产构建（有缓存）</th><th>产物体积（Gzip 前/后）</th></tr></thead><tbody><tr><td>0.7.0</td><td><strong>1.212 s</strong></td><td><strong>1.125 s</strong></td><td><strong>7.64 MiB / 1.40 MiB</strong></td></tr><tr><td>1.0.0</td><td><strong>0.918 s</strong></td><td><strong>0.487 s</strong></td><td><strong>5.19 MiB / 1.35 MiB</strong></td></tr></tbody></table></div>
</li>
<li>
<p><strong>更快的类型生成</strong>：支持使用 <a href="https://devblogs.microsoft.com/typescript/announcing-typescript-7-0/" target="_blank" rel="noopener noreferrer" class="rp-link">TypeScript 7</a> 或 <a href="/zh/config/lib/dts#dtsisolated" class="rp-link">Isolated Declarations</a> 加快类型声明文件生成，实际收益会因项目而异。以 Rsbuild 仓库为例，不同方案的耗时如下：</p>
<div class="rp-table-scroll-container rp-scrollbar"><table><thead><tr><th>方案</th><th>耗时</th><th>性能提升</th></tr></thead><tbody><tr><td>TypeScript 6</td><td><strong>9.7 s</strong></td><td>基准</td></tr><tr><td>TypeScript 7</td><td><strong>4.1 s</strong></td><td><strong>约 2.4 倍</strong></td></tr><tr><td>Isolated Declarations</td><td><strong>2.3 s</strong></td><td><strong>约 4.2 倍</strong></td></tr></tbody></table></div>
</li>
<li>
<p><strong>更完整的组件库支持</strong>：支持构建 React、Vue、Svelte 或 Solid 组件库，并进一步完善样式与资源处理能力，覆盖 <code>new URL()</code>、Web Worker 和 Wasm 等场景。</p>
</li>
<li>
<p><strong>更灵活的使用方式</strong>：简单项目无需配置文件即可构建，也可以根据需要编写配置；既可以通过 CLI 直接使用，也可以通过 JavaScript API 集成到脚本和其他工具中。</p>
</li>
<li>
<p><strong>更流畅的开发体验</strong>：可以与 <a href="https://rstest.rs/zh/" target="_blank" rel="noopener noreferrer" class="rp-link">Rstest</a>、<a href="https://rspress.rs/zh/" target="_blank" rel="noopener noreferrer" class="rp-link">Rspress</a> 和 <a href="https://rsdoctor.rs/zh/" target="_blank" rel="noopener noreferrer" class="rp-link">Rsdoctor</a> 配合完成测试、文档和产物诊断，并通过 Agent Skills 为 Coding Agent 提供 Rslib 相关最佳实践。</p>
</li>
</ul>
<p>随着这些能力不断完善并经过大量生产项目的验证，Rslib 的配置结构与 JavaScript API 也逐步稳定。从 1.0 开始，Rslib 将以稳定的公开 API 为基础，遵循语义化版本（SemVer）规范持续演进。</p>
<h2 class="rp-toc-include" id="output-formats"><a href="#output-formats" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>面向不同场景的产物输出</h2>
<p>一个库既可以作为 npm 包发布，也可以基于 <a href="https://module-federation.io/zh/" target="_blank" rel="noopener noreferrer" class="rp-link">模块联邦</a> 构建为远程模块，供多个应用在运行时加载，从而实现独立部署和更新。常见的库构建工具主要面向 npm 包交付，而 Rslib 还支持构建模块联邦产物，并提供支持 HMR 的开发模式，便于与宿主应用或 Storybook 联调。</p>
<p>针对上述不同的运行环境和消费方式，Rslib 提供的产物格式如下：</p>
<div class="rp-table-scroll-container rp-scrollbar"><table><thead><tr><th>产物格式</th><th>使用场景</th><th>使用方式</th></tr></thead><tbody><tr><td><a href="/zh/guide/basic/output-format#esm--cjs" class="rp-link">ESM</a>、<a href="/zh/guide/basic/output-format#esm--cjs" class="rp-link">CJS</a></td><td>Node.js 与下游构建</td><td>由 Node.js 直接加载，或作为依赖交给应用构建工具继续处理</td></tr><tr><td><a href="/zh/guide/basic/output-format#umd" class="rp-link">UMD</a>、<a href="/zh/guide/basic/output-format#iife" class="rp-link">IIFE</a></td><td>浏览器直接加载</td><td>通过 <code>&lt;script&gt;</code> 标签在浏览器中直接使用</td></tr><tr><td><a href="/zh/guide/advanced/module-federation" class="rp-link">Module Federation</a></td><td>跨应用运行时加载</td><td>作为远程模块在运行时加载</td></tr></tbody></table></div>
<p>需要同时生成多种格式的产物时，可以通过 <a href="/zh/config/lib/format" class="rp-link">format</a> 配置分别指定，并在同一份配置中按需设置各自的构建方式，无需维护多套构建脚本。</p>
<p>在这些产物格式中，Rslib 对 ESM 产物进行了重点优化，使其对静态分析更友好并支持代码分割，方便下游构建工具进行 tree shaking 和二次构建。对于 ESM 产物中的 external 依赖，Rslib 也优化了默认处理方式，能够更准确地保留源码中的模块加载语义，减少构建转换带来的行为差异。</p>
<p>此外，除了常见的模块产物，Rslib 还提供了实验性的 <a href="/zh/config/lib/experiments#experimentsexe" class="rp-link">可执行文件生成</a> 能力。该能力基于 <a href="https://nodejs.org/api/single-executable-applications.html" target="_blank" rel="noopener noreferrer" class="rp-link">Node.js SEA</a>，适用于 bundle 模式下的单入口 Node.js 产物。生成的可执行文件可以在未安装 Node.js 的目标系统中运行，适合分发 CLI 等 Node.js 程序。</p>
<h2 class="rp-toc-include" id="build-modes"><a href="#build-modes" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>灵活的构建模式</h2>
<p>产物格式对应不同的加载方式，构建模式则决定库内部模块的组织方式。Rslib 支持 <a href="/zh/guide/basic/output-structure#bundle--bundleless" class="rp-link">bundle 和 bundleless</a> 两种构建模式，以适应不同的交付需求：</p>
<ul>
<li><strong>bundle 模式</strong>（<code>bundle: true</code>）：Rslib 从入口出发，将内部模块打包为较少的文件，也可以结合代码分割按需输出多个 chunk，适合 SDK、CLI 和 Node.js 工具库等希望简化产物结构、便于分发的场景。</li>
<li><strong>bundleless 模式</strong>（<code>bundle: false</code>）：Rslib 逐个编译源文件，保留与源码对应的目录和模块结构，并处理模块引用、文件扩展名、样式与静态资源等，适合组件库、工具函数库和 monorepo 内部包，便于调试，也有利于下游进行按需加载和二次构建。</li>
</ul>
<p>以一个包含三个源码文件的单入口库为例，两种模式的产物结构如下：</p>
<img src="https://assets.rspack.rs/rslib/assets/rslib-v1-0-bundle-mode.png" alt="bundle 与 bundleless 模式的产物结构对比" width="550" style="display:block;margin:0 auto"/>
<p>可以根据库的交付方式和使用场景选择合适的构建模式。需要兼顾不同消费场景时，也可以在同一个项目中分别生成 bundle 和 bundleless 产物。</p>
<h2 class="rp-toc-include" id="declaration-generation"><a href="#declaration-generation" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>快速的类型生成</h2>
<p>对于 TypeScript 库，类型声明文件不仅影响编辑器中的类型提示，也关系到下游项目能否正确解析库的类型。通过 <a href="/zh/config/lib/dts" class="rp-link">dts</a> 配置，Rslib 可以在构建 JavaScript 产物的同时生成类型声明文件。在 bundleless 模式下，Rslib 还会根据实际产物处理声明文件中的路径别名和导入扩展名，使类型声明与 JavaScript 产物保持一致，并适配 NodeNext 等模块解析方式。</p>
<p>随着项目规模扩大，类型生成可能逐渐成为构建中的耗时环节。Rslib 提供了两种提速方式：</p>
<ul>
<li><strong>使用 TypeScript 7</strong>：当项目使用 TypeScript 7 及以上版本时，Rslib 会自动使用 <a href="/zh/config/lib/dts#dtstsgo" class="rp-link">native TypeScript (tsgo)</a> 生成声明文件，在保留类型检查的同时加快类型生成。</li>
<li><strong>使用 Isolated Declarations</strong>：对于希望进一步缩短构建时间的项目，可以使用实验性的 <a href="/zh/config/lib/dts#dtsisolated" class="rp-link">Isolated Declarations</a> 类型生成方式。Rslib 会在 Rspack 构建过程中，为构建依赖图中的模块快速生成声明文件，进一步减少类型生成的开销。</li>
</ul>
<div class="rp-codeblock language-ts"><div class="rp-codeblock__title">rslib.config.ts</div><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="ts" data-title="rslib.config.ts"><code><span class="line"><span style="color:var(--shiki-token-keyword)">export</span><span style="color:var(--shiki-token-keyword)"> default</span><span style="color:var(--shiki-foreground)"> {</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  dts</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-foreground)"> {</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    isolated</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-constant)"> true</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  }</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">};</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="切换代码换行" aria-label="切换代码换行"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="复制代码"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<p>需要注意的是，该模式不会执行类型检查，因此适合与独立的高性能类型检查流程配合使用。例如在 monorepo 中，日常构建由 Rslib 快速生成各个包的类型声明文件，并在 CI 或 <code>pre-commit</code> hook 中通过 <a href="https://rslint.rs/guide/type-checking" target="_blank" rel="noopener noreferrer" class="rp-link">rslint --type-check</a> 统一执行完整的类型检查。</p>
<h2 class="rp-toc-include" id="framework-support"><a href="#framework-support" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>开箱即用的多框架支持</h2>
<p>组件库是 Rslib 重点支持的场景之一。React、Vue、Svelte 和 Solid 在组件文件、编译方式和运行时约定上各有不同。Rslib 通过对应的 <a href="/zh/config/rsbuild/plugins" class="rp-link">Rsbuild 插件</a> 接入这些框架的编译能力，产物格式、构建模式等库构建能力统一通过 Rslib 配置，框架语法则由相应的插件和编译器处理，使不同框架组件库可以沿用相近的配置方式和构建流程。</p>
<p>创建新项目时，可以通过 <code>create-rslib</code> 选择对应的框架模板，直接获得所需插件和基础配置；已有项目则可以按需注册相关插件。</p>
<p>以 React 组件库为例，注册 <a href="https://rsbuild.rs/zh/plugins/list/plugin-react" target="_blank" rel="noopener noreferrer" class="rp-link">@rsbuild/plugin-react</a> 后即可编译 JSX 和 TSX。通过该插件，还可以启用集成在 SWC 中的 Rust 版 <a href="/zh/guide/solution/react#react-compiler" class="rp-link">React Compiler</a>，在构建阶段自动优化组件代码，无需额外接入 Babel：</p>
<div class="rp-codeblock language-ts"><div class="rp-codeblock__title">rslib.config.ts</div><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="ts" data-title="rslib.config.ts"><code><span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> { pluginReact } </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;@rsbuild/plugin-react&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> { defineConfig } </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;@rslib/core&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">export</span><span style="color:var(--shiki-token-keyword)"> default</span><span style="color:var(--shiki-token-function)"> defineConfig</span><span style="color:var(--shiki-foreground)">({</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  bundle</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-constant)"> false</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  output</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-foreground)"> {</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    target</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-string-expression)"> &#x27;web&#x27;</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  }</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  plugins</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-foreground)"> [</span></span>
<span class="line"><span style="color:var(--shiki-token-function)">    pluginReact</span><span style="color:var(--shiki-foreground)">({</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">      reactCompiler</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-constant)"> true</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    })</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  ]</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">});</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="切换代码换行" aria-label="切换代码换行"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="复制代码"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<p>对于希望将 JSX 交给应用侧继续编译的组件库，Rslib 也支持在 bundleless 模式下 <a href="/zh/guide/solution/react#jsx-transform" class="rp-link">保留 JSX</a> 并输出 <code>.jsx</code> 文件，由下游应用根据自身的目标环境和构建配置完成转换。</p>
<blockquote>
<p>查看 <a href="/zh/guide/solution/react" class="rp-link">React 方案</a>、<a href="/zh/guide/solution/vue" class="rp-link">Vue 方案</a>、<a href="/zh/guide/solution/svelte" class="rp-link">Svelte 方案</a>、<a href="/zh/guide/solution/solid" class="rp-link">Solid 方案</a> 了解更多。</p>
</blockquote>
<h2 class="rp-toc-include" id="styles-and-assets"><a href="#styles-and-assets" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>丰富的样式与资源处理</h2>
<p>Rslib 可以在构建 JavaScript 产物的同时处理样式、静态资源、Web Worker 和 Wasm，并根据产物格式和构建配置处理文件之间的引用关系，使生成的产物既可以被直接使用，也可以交由下游构建工具继续处理。</p>
<p><strong>样式</strong></p>
<p>Rslib 开箱支持 CSS Modules、PostCSS、样式提取、内联和压缩等能力，并会根据构建模式选择合适的样式输出方式；Sass、Less、Stylus 和 Tailwind CSS 则可通过 Rsbuild 插件接入。</p>
<div class="rp-codeblock language-js"><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="js"><code><span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-token-string-expression)"> &#x27;./style.scss&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> styles </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;./button.module.css&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="切换代码换行" aria-label="切换代码换行"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="复制代码"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<p><strong>静态资源</strong></p>
<p>图片、字体和音视频等静态资源可以在 JavaScript 中通过 <code>import</code> 引用，也可以在 CSS 中通过 <code>url()</code> 使用。对于 JSON 文件，Rslib 支持使用 <a href="https://github.com/tc39/proposal-import-attributes" target="_blank" rel="noopener noreferrer" class="rp-link">Import Attributes</a> 将其作为 JSON 模块导入。此外，还可以使用 <code>new URL()</code> 引用本地资源，Rslib 会输出相应文件并更新产物中的引用路径。</p>
<div class="rp-codeblock language-js"><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="js"><code><span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> data </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;./data.json&#x27;</span><span style="color:var(--shiki-token-keyword)"> with</span><span style="color:var(--shiki-foreground)"> { type</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-string-expression)"> &#x27;json&#x27;</span><span style="color:var(--shiki-foreground)"> };</span></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> logo </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;./logo.svg&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">const</span><span style="color:var(--shiki-token-constant)"> dataFile</span><span style="color:var(--shiki-token-keyword)"> =</span><span style="color:var(--shiki-token-keyword)"> new</span><span style="color:var(--shiki-token-function)"> URL</span><span style="color:var(--shiki-foreground)">(</span><span style="color:var(--shiki-token-string-expression)">&#x27;./data.txt&#x27;</span><span style="color:var(--shiki-token-punctuation)">,</span><span style="color:var(--shiki-token-keyword)"> import</span><span style="color:var(--shiki-foreground)">.</span><span style="color:var(--shiki-token-constant)">meta</span><span style="color:var(--shiki-foreground)">.url);</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="切换代码换行" aria-label="切换代码换行"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="复制代码"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<p><strong>Web Worker 与 Wasm</strong></p>
<p>Rslib 可以识别标准的 Web Worker 声明，构建 Worker 入口及其依赖，无需额外维护入口和复制脚本。对于 Wasm，Rslib 支持 <a href="https://github.com/WebAssembly/esm-integration" target="_blank" rel="noopener noreferrer" class="rp-link">WebAssembly ESM Integration</a> 和 <a href="https://github.com/tc39/proposal-source-phase-imports" target="_blank" rel="noopener noreferrer" class="rp-link">Source Phase Imports</a> 等语法，既可以生成加载和实例化所需的代码，也可以保留 Wasm import，交给支持相应能力的下游构建工具或目标运行时继续处理。</p>
<div class="rp-codeblock language-js"><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="js"><code><span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> { add } </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;./add.wasm&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> source addModule </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;./add.wasm&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">const</span><span style="color:var(--shiki-token-constant)"> worker</span><span style="color:var(--shiki-token-keyword)"> =</span><span style="color:var(--shiki-token-keyword)"> new</span><span style="color:var(--shiki-token-function)"> Worker</span><span style="color:var(--shiki-foreground)">(</span><span style="color:var(--shiki-token-keyword)">new</span><span style="color:var(--shiki-token-function)"> URL</span><span style="color:var(--shiki-foreground)">(</span><span style="color:var(--shiki-token-string-expression)">&#x27;./worker.ts&#x27;</span><span style="color:var(--shiki-token-punctuation)">,</span><span style="color:var(--shiki-token-keyword)"> import</span><span style="color:var(--shiki-foreground)">.</span><span style="color:var(--shiki-token-constant)">meta</span><span style="color:var(--shiki-foreground)">.url));</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="切换代码换行" aria-label="切换代码换行"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="复制代码"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<blockquote>
<p>查看 <a href="/zh/guide/advanced/css" class="rp-link">CSS</a>、<a href="/zh/guide/advanced/static-assets" class="rp-link">静态资源</a>、<a href="/zh/guide/advanced/web-workers" class="rp-link">Web Workers</a> 和 <a href="/zh/guide/advanced/wasm" class="rp-link">Wasm</a> 了解更多。</p>
</blockquote>
<h2 class="rp-toc-include" id="usage-options"><a href="#usage-options" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>按需扩展的使用方式</h2>
<p>对于简单项目，Rslib 支持通过 <a href="/zh/guide/basic/cli#rslib" class="rp-link">命令行参数</a> 直接构建，无需创建配置文件：</p>
<div class="rp-codeblock language-bash"><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="bash"><code><span class="line"><span style="color:var(--shiki-token-function)">npx</span><span style="color:var(--shiki-token-string)"> rslib</span><span style="color:var(--shiki-token-string)"> build</span><span style="color:var(--shiki-token-string)"> --entry</span><span style="color:var(--shiki-token-string)"> src/index.ts</span><span style="color:var(--shiki-token-string)"> --format</span><span style="color:var(--shiki-token-string)"> esm</span><span style="color:var(--shiki-token-string)"> --dts</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="切换代码换行" aria-label="切换代码换行"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="复制代码"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<p>随着构建需求增加，可以再通过配置文件控制更多构建行为。Rslib 1.0 简化了配置结构：如果只需生成一份默认的 ESM 产物，可以省略 <code>lib</code> 字段，直接在顶层编写配置；需要输出多份产物时，则通过 <code>lib</code> 数组分别设置每份产物的格式和构建方式。</p>
<p>多个产物共用的 <code>syntax</code>、<code>plugins</code> 等配置可以放在顶层，各个 <code>lib</code> 只需配置不同的部分，也可以按需覆盖顶层配置，减少重复。</p>
<div class="rp-codeblock language-ts"><div class="rp-codeblock__title">rslib.config.ts</div><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="ts" data-title="rslib.config.ts"><code><span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> { defineConfig } </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;@rslib/core&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">export</span><span style="color:var(--shiki-token-keyword)"> default</span><span style="color:var(--shiki-token-function)"> defineConfig</span><span style="color:var(--shiki-foreground)">({</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  lib</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-foreground)"> [</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    {</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">      format</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-string-expression)"> &#x27;esm&#x27;</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    }</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    {</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">      format</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-string-expression)"> &#x27;cjs&#x27;</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">      syntax</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-string-expression)"> &#x27;es2020&#x27;</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    }</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  ]</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-token-comment)">  // 应用于所有产物，可在 lib 中单独覆盖</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  syntax</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-string-expression)"> &#x27;es2023&#x27;</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">});</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="切换代码换行" aria-label="切换代码换行"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="复制代码"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<p>如果需要在脚本或其他工具中使用 Rslib，可以通过 <a href="/zh/api/start/" class="rp-link">JavaScript API</a> 创建实例并执行构建，例如批量构建工作区中的多个包、封装内部构建命令或接入发布流水线。JavaScript API 可以在 Node.js、Deno 和 Bun 中使用：</p>
<div class="rp-codeblock language-ts"><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="ts"><code><span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> { createRslib } </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;@rslib/core&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">const</span><span style="color:var(--shiki-token-constant)"> rslib</span><span style="color:var(--shiki-token-keyword)"> =</span><span style="color:var(--shiki-token-keyword)"> await</span><span style="color:var(--shiki-token-function)"> createRslib</span><span style="color:var(--shiki-foreground)">();</span></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">const</span><span style="color:var(--shiki-token-constant)"> result</span><span style="color:var(--shiki-token-keyword)"> =</span><span style="color:var(--shiki-token-keyword)"> await</span><span style="color:var(--shiki-token-constant)"> rslib</span><span style="color:var(--shiki-token-function)">.build</span><span style="color:var(--shiki-foreground)">();</span></span>
<span class="line"></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">await</span><span style="color:var(--shiki-token-constant)"> result</span><span style="color:var(--shiki-token-function)">.close</span><span style="color:var(--shiki-foreground)">();</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="切换代码换行" aria-label="切换代码换行"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="复制代码"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<h2 class="rp-toc-include" id="library-development-workflow"><a href="#library-development-workflow" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>协同的库开发流程</h2>
<p>Rslib 可以与 Rstack 生态中的工具和插件配合，覆盖库开发流程中的功能测试、文档开发、构建分析和发布前检查等环节。</p>
<p><strong>测试</strong></p>
<p>使用 <code>create-rslib</code> 创建的新项目默认配置了 Rstest。通过 <a href="/zh/guide/advanced/rstest#复用-rslib-配置" class="rp-link">@rstest/adapter-rslib</a>，Rstest 可以复用 Rslib 的相关配置，减少重复维护，也让测试时的模块解析和源码处理更接近实际构建。</p>
<div class="rp-codeblock language-ts"><div class="rp-codeblock__title">rstest.config.ts</div><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="ts" data-title="rstest.config.ts"><code><span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> { withRslibConfig } </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;@rstest/adapter-rslib&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> { defineConfig } </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;@rstest/core&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">export</span><span style="color:var(--shiki-token-keyword)"> default</span><span style="color:var(--shiki-token-function)"> defineConfig</span><span style="color:var(--shiki-foreground)">({</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  extends</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-function)"> withRslibConfig</span><span style="color:var(--shiki-foreground)">()</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">});</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="切换代码换行" aria-label="切换代码换行"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="复制代码"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<p><strong>文档</strong></p>
<p>Rslib 项目可以使用 Rspress 搭建文档站点。对于组件库，可以使用 <a href="https://rspress.rs/zh/plugin/official-plugins/preview" target="_blank" rel="noopener noreferrer" class="rp-link">@rspress/plugin-preview</a> 在 MDX 中编写可运行的组件示例，并通过 <a href="https://rspress.rs/zh/plugin/official-plugins/api-docgen" target="_blank" rel="noopener noreferrer" class="rp-link">@rspress/plugin-api-docgen</a> 从源码生成组件 API 文档，方便在同一站点中维护使用说明、组件示例和 API 信息。</p>
<p><strong>产物检查</strong></p>
<p>发布前，可以通过 <a href="https://github.com/rstackjs/rsbuild-plugin-publint" target="_blank" rel="noopener noreferrer" class="rp-link">rsbuild-plugin-publint</a> 检查 <code>package.json</code>、包结构和导出配置等常见问题，并通过 <a href="https://github.com/rstackjs/rsbuild-plugin-arethetypeswrong" target="_blank" rel="noopener noreferrer" class="rp-link">rsbuild-plugin-arethetypeswrong</a> 检查类型声明能否在不同的模块解析方式下正确使用。这些检查可以只在 CI 环境中启用：</p>
<div class="rp-codeblock language-ts"><div class="rp-codeblock__title">rslib.config.ts</div><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="ts" data-title="rslib.config.ts"><code><span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> { defineConfig } </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;@rslib/core&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> { pluginAreTheTypesWrong } </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;rsbuild-plugin-arethetypeswrong&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">import</span><span style="color:var(--shiki-foreground)"> { pluginPublint } </span><span style="color:var(--shiki-token-keyword)">from</span><span style="color:var(--shiki-token-string-expression)"> &#x27;rsbuild-plugin-publint&#x27;</span><span style="color:var(--shiki-foreground)">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">export</span><span style="color:var(--shiki-token-keyword)"> default</span><span style="color:var(--shiki-token-function)"> defineConfig</span><span style="color:var(--shiki-foreground)">({</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  dts</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-constant)"> true</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  plugins</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-foreground)"> [</span></span>
<span class="line"><span style="color:var(--shiki-token-function)">    pluginPublint</span><span style="color:var(--shiki-foreground)">({</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">      enable</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-function)"> Boolean</span><span style="color:var(--shiki-foreground)">(</span><span style="color:var(--shiki-token-constant)">process</span><span style="color:var(--shiki-foreground)">.</span><span style="color:var(--shiki-token-constant)">env</span><span style="color:var(--shiki-foreground)">.</span><span style="color:var(--shiki-token-constant)">CI</span><span style="color:var(--shiki-foreground)">)</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    })</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-token-function)">    pluginAreTheTypesWrong</span><span style="color:var(--shiki-foreground)">({</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">      enable</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-function)"> Boolean</span><span style="color:var(--shiki-foreground)">(</span><span style="color:var(--shiki-token-constant)">process</span><span style="color:var(--shiki-foreground)">.</span><span style="color:var(--shiki-token-constant)">env</span><span style="color:var(--shiki-foreground)">.</span><span style="color:var(--shiki-token-constant)">CI</span><span style="color:var(--shiki-foreground)">)</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    })</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  ]</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">});</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="切换代码换行" aria-label="切换代码换行"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="复制代码"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<p><strong>构建分析</strong></p>
<p>需要进一步分析构建过程和产物时，可以使用 Rsdoctor 查看构建耗时、依赖关系和产物体积，帮助定位构建性能、依赖体积和产物结构方面的问题。</p>
<blockquote>
<p>查看 <a href="/zh/guide/advanced/rstest" class="rp-link">使用 Rstest</a>、<a href="/zh/guide/advanced/rspress" class="rp-link">使用 Rspress</a>、<a href="/zh/guide/advanced/rsdoctor" class="rp-link">使用 Rsdoctor</a> 了解更多。</p>
</blockquote>
<h2 class="rp-toc-include" id="agent-friendly-development"><a href="#agent-friendly-development" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Agent 友好的开发体验</h2>
<p>随着 Coding Agent 越来越多地参与到日常开发流程中，为其提供准确的工具知识和项目上下文也变得更加重要。</p>
<p>为此，Rslib 提供了面向 Agent 的 Skills：</p>
<ul>
<li><a href="https://github.com/rstackjs/agent-skills#rslib-best-practices" target="_blank" rel="noopener noreferrer" class="rp-link">rslib-best-practices</a>：提供 Rslib 项目配置与构建的最佳实践。</li>
<li><a href="https://github.com/rstackjs/agent-skills#rslib-modern-package" target="_blank" rel="noopener noreferrer" class="rp-link">rslib-modern-package</a>：提供现代 JavaScript 包的设计和发布建议。</li>
<li><a href="https://github.com/rstackjs/agent-skills#migrate-to-rslib" target="_blank" rel="noopener noreferrer" class="rp-link">migrate-to-rslib</a>：辅助将使用 tsc 或 tsup 构建的项目迁移到 Rslib。</li>
</ul>
<p>在支持 Skills 的 Coding Agent 中，可以为已有项目安装指定的 Skill，也可以在使用 <code>create-rslib</code> 创建新项目时直接完成安装：</p>
<div class="rp-codeblock language-bash"><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="bash"><code><span class="line"><span style="color:var(--shiki-token-comment)"># 为已有项目安装</span></span>
<span class="line"><span style="color:var(--shiki-token-function)">npx</span><span style="color:var(--shiki-token-string)"> skills</span><span style="color:var(--shiki-token-string)"> add</span><span style="color:var(--shiki-token-string)"> rstackjs/agent-skills</span><span style="color:var(--shiki-token-string)"> --skill</span><span style="color:var(--shiki-token-string)"> rslib-best-practices</span></span>
<span class="line"></span>
<span class="line"><span style="color:var(--shiki-token-comment)"># 创建新项目时安装</span></span>
<span class="line"><span style="color:var(--shiki-token-function)">npx</span><span style="color:var(--shiki-token-string)"> -y</span><span style="color:var(--shiki-token-string)"> create-rslib@latest</span><span style="color:var(--shiki-token-string)"> my-project</span><span style="color:var(--shiki-token-string)"> -t</span><span style="color:var(--shiki-token-string)"> react</span><span style="color:var(--shiki-token-string)"> --skill</span><span style="color:var(--shiki-token-string)"> rslib-best-practices</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="切换代码换行" aria-label="切换代码换行"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="复制代码"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<p>除了 Skills，Rslib 官网文档还提供了 <a href="https://rslib.rs/llms.txt" target="_blank" rel="noopener noreferrer" class="rp-link">llms.txt</a> 和 <a href="https://rslib.rs/llms-full.txt" target="_blank" rel="noopener noreferrer" class="rp-link">llms-full.txt</a>，分别用于按需查找文档和获取完整的文档上下文。每个文档页面也提供对应的 Markdown 版本，方便将特定内容提供给 Agent。</p>
<p>通过 <code>create-rslib</code> 创建的新项目还会包含 <code>AGENTS.md</code>，其中记录了常用命令和相关文档入口。开发者可以在此基础上继续补充项目结构、开发流程和工程约定，让 Agent 同时了解 Rslib 的通用实践和当前项目的具体上下文。</p>
<blockquote>
<p>查看 <a href="/zh/guide/start/ai" class="rp-link">AI</a> 了解更多。</p>
</blockquote>
<h2 class="rp-toc-include" id="using-rslib-v1"><a href="#using-rslib-v1" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>如何使用 Rslib 1.0</h2>
<ul>
<li>如果你还未使用过 Rslib，可以通过 <a href="https://stackblitz.com/~/github.com/rstackjs/rslib-stackblitz-example" target="_blank" rel="noopener noreferrer" class="rp-link">StackBlitz 示例</a> 来体验，也可以参考 <a href="/zh/guide/start/quick-start" class="rp-link">快速上手</a> 创建一个新的 Rslib 项目。</li>
<li>如果你正在使用 Rslib 0.x，请留意 1.0 版本包含一些不兼容更新，可以参考 <a href="/zh/guide/upgrade/v0-to-v1" class="rp-link">从 0.x 升级到 v1</a> 文档进行升级。</li>
<li>如果你计划从 tsc、tsup 等工具迁移到 Rslib，可以参考 <a href="/zh/guide/start/quick-start#从现有项目迁移" class="rp-link">从现有项目迁移</a>。</li>
</ul>
<h2 class="rp-toc-include" id="whats-next"><a href="#whats-next" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>下一步</h2>
<p>Rslib 1.0 是一个稳定起点，而不是终点。接下来，我们会继续围绕两个方向推进：</p>
<ol>
<li><strong>持续提升构建与产物能力</strong>，包括优化 ESM 和 CJS 产物、探索更完整的 Node.js 打包能力与更灵活的模块结构保留方式，并持续提升类型生成和打包的效率与体验。</li>
<li><strong>完善库开发工作流</strong>，加强 Rslib 与测试、文档、质量检查和发布流程的协同，减少不同环节之间的重复配置与维护工作。</li>
</ol>
<h2 class="rp-toc-include" id="acknowledgments"><a href="#acknowledgments" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>致谢</h2>
<p>Rslib 的部分实现和 API 设计参考或改编自 <a href="https://github.com/evanw/esbuild" target="_blank" rel="noopener noreferrer" class="rp-link">esbuild</a>、<a href="https://github.com/webpack/mini-css-extract-plugin" target="_blank" rel="noopener noreferrer" class="rp-link">mini-css-extract-plugin</a>、<a href="https://github.com/rolldown/tsdown" target="_blank" rel="noopener noreferrer" class="rp-link">tsdown</a>、<a href="https://github.com/egoist/tsup" target="_blank" rel="noopener noreferrer" class="rp-link">tsup</a> 和 <a href="https://github.com/webpack/webpack" target="_blank" rel="noopener noreferrer" class="rp-link">webpack</a> 等优秀开源项目，感谢这些项目及其贡献者所提供的经验、思路与实现。</p>
<p>Rslib 的成长也离不开社区中的每一份参与，感谢所有为 Rslib 贡献代码和文档、提交 Issue、参与讨论和分享反馈的开发者，以及每一位选择和使用 Rslib 的用户。</p>
<p>在使用 Rslib 的过程中如果遇到问题或有任何建议，欢迎通过 <a href="https://github.com/web-infra-dev/rslib/issues" target="_blank" rel="noopener noreferrer" class="rp-link">GitHub Issues</a>、<a href="https://github.com/web-infra-dev/rslib/discussions" target="_blank" rel="noopener noreferrer" class="rp-link">GitHub Discussions</a> 或社区渠道与我们交流。</p><!--/$-->]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rspack 生态升级！全新库开发工具 Rslib 发布]]></title>
            <link>https://rslib.rs/zh/blog/introducing-rslib</link>
            <guid isPermaLink="false">/zh/blog/introducing-rslib</guid>
            <pubDate>Wed, 14 May 2025 10:00:00 GMT</pubDate>
            <description><![CDATA[Rslib 正式发布，帮助开发者以简单直观的方式创建 JavaScript 库和 UI 组件库。]]></description>
            <content:encoded><![CDATA[<!--$--><h1 class="rp-toc-include" id="rspack-生态升级全新库开发工具-rslib-发布"><a href="#rspack-生态升级全新库开发工具-rslib-发布" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Rspack 生态升级！全新库开发工具 Rslib 发布<!-- --> </h1>
<p><em>2025 年 5 月 14 日</em></p>
<div class="group-n_jNI7 root-YZxMH8"><div class="root-Sr_5x3 groupItem-m5U5nn"><img src="https://github.com/fi3ework.png" alt="fi3ework" class="avatar-XCZJhu" loading="lazy" decoding="async"/><div class="content-BhgM4E"><div class="nameRow-U9Q3S8"><div class="name-k91bx6">fi3ework</div><div class="links-jK83Yg"><a href="https://github.com/fi3ework" target="_blank" rel="noopener noreferrer" class="link-yDHY4Y" aria-label="fi3ework&#x27;s GitHub"><svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path fill="currentColor" d="M12 .297c-6.63 0-12 5.373-12 12c0 5.303 3.438 9.8 8.205 11.385c.6.113.82-.258.82-.577c0-.285-.01-1.04-.015-2.04c-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729c1.205.084 1.838 1.236 1.838 1.236c1.07 1.835 2.809 1.305 3.495.998c.108-.776.417-1.305.76-1.605c-2.665-.3-5.466-1.332-5.466-5.93c0-1.31.465-2.38 1.235-3.22c-.135-.303-.54-1.523.105-3.176c0 0 1.005-.322 3.3 1.23c.96-.267 1.98-.399 3-.405c1.02.006 2.04.138 3 .405c2.28-1.552 3.285-1.23 3.285-1.23c.645 1.653.24 2.873.12 3.176c.765.84 1.23 1.91 1.23 3.22c0 4.61-2.805 5.625-5.475 5.92c.42.36.81 1.096.81 2.22c0 1.606-.015 2.896-.015 3.286c0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"></path></svg></a></div></div><div class="title-MMKZdf">@fi3ework</div></div></div><div class="root-Sr_5x3 groupItem-m5U5nn"><img src="https://github.com/Timeless0911.png" alt="Chenwei Dai" class="avatar-XCZJhu" loading="lazy" decoding="async"/><div class="content-BhgM4E"><div class="nameRow-U9Q3S8"><div class="name-k91bx6">Chenwei Dai</div><div class="links-jK83Yg"><a href="https://github.com/Timeless0911" target="_blank" rel="noopener noreferrer" class="link-yDHY4Y" aria-label="Chenwei Dai&#x27;s GitHub"><svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path fill="currentColor" d="M12 .297c-6.63 0-12 5.373-12 12c0 5.303 3.438 9.8 8.205 11.385c.6.113.82-.258.82-.577c0-.285-.01-1.04-.015-2.04c-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729c1.205.084 1.838 1.236 1.838 1.236c1.07 1.835 2.809 1.305 3.495.998c.108-.776.417-1.305.76-1.605c-2.665-.3-5.466-1.332-5.466-5.93c0-1.31.465-2.38 1.235-3.22c-.135-.303-.54-1.523.105-3.176c0 0 1.005-.322 3.3 1.23c.96-.267 1.98-.399 3-.405c1.02.006 2.04.138 3 .405c2.28-1.552 3.285-1.23 3.285-1.23c.645 1.653.24 2.873.12 3.176c.765.84 1.23 1.91 1.23 3.22c0 4.61-2.805 5.625-5.475 5.92c.42.36.81 1.096.81 2.22c0 1.606-.015 2.896-.015 3.286c0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"></path></svg></a></div></div><div class="title-MMKZdf">@Timeless0911</div></div></div></div>
<p><img src="https://assets.rspack.rs/rslib/rslib-banner.png" alt="banner"/></p>
<p>我们很高兴地向大家介绍 Rslib —— <strong>一款基于 Rspack 的库开发工具</strong>。Rslib 由字节跳动 Web Infra 团队开发，能够帮助开发者以简单直观的方式创建 JavaScript 库和 UI 组件库，并享受 <a href="https://rspack.rs/zh/" target="_blank" rel="noopener noreferrer" class="rp-link">Rspack</a> 和 <a href="https://rsbuild.rs/zh/" target="_blank" rel="noopener noreferrer" class="rp-link">Rsbuild</a> 带来的极致开发体验。</p>
<blockquote>
<p>仓库地址：<a href="https://github.com/web-infra-dev/rslib" target="_blank" rel="noopener noreferrer" class="rp-link">https://github.com/web-infra-dev/rslib</a></p>
</blockquote>
<h2 class="rp-toc-include" id="为什么要做-rslib"><a href="#为什么要做-rslib" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>为什么要做 Rslib</h2>
<p>过去，webpack 主要用于打包 web 应用，当开发 JavaScript 库时，开发者通常会引入 esbuild 或 Rollup 等优秀的打包工具，它们能够输出高质量的 ESM 和 CJS 产物，更好地满足 JavaScript 库的打包需求。</p>
<p>在字节跳动内部，我们的研发团队已开发超过 1 万个 JavaScript 库，在这一过程中，我们观察到多种打包工具并存导致了生态的割裂。此外，在更复杂的库开发场景中，仍存在许多尚未得到良好解决的需求，例如：</p>
<ul>
<li><strong>配置割裂</strong>：库构建配置与应用开发存在明显差异，开发者需要学习不同的配置规则和方法。</li>
<li><strong>生态复用受限</strong>：希望复用生态中沉淀的 webpack 插件或 loader。</li>
<li><strong>扩展能力不足</strong>：希望有丰富的构建生命周期钩子，以满足自定义构建需求。</li>
<li><strong>多格式输出成本高</strong>：库同时打包为 ESM、CJS、UMD 及 Module Federation 等多种格式的产物，需要使用不同工具重复配置。</li>
<li><strong>非 JS 资源处理薄弱</strong>：样式方案和静态资源缺乏标准化处理方案。</li>
<li><strong>类型文件支持缺失</strong>：希望在构建时同时打包依赖的 d.ts 文件。</li>
</ul>
<p>面对这些痛点，我们希望理想的库开发工具能够：</p>
<ul>
<li><strong>易于配置：</strong> 提供开箱即用的配置覆盖大部分场景，通过插件机制实现高级功能扩展，安装即用。</li>
<li><strong>功能全面：</strong> 不仅支持 JavaScript / TypeScript 转换与多格式产物打包，还提供 web 应用构建般全面的样式和资源处理方案。</li>
<li><strong>生态共享：</strong> 库构建与应用构建配置统一，复用 Rspack 繁荣社区，推动统一工具链生态发展。</li>
<li><strong>性能优先：</strong> 底层构建器基于 Rust 实现，具有良好的构建性能，提升开发者体验。</li>
</ul>
<p>为此，我们创建了 Rslib。<strong>它基于 <a href="https://rsbuild.rs/zh/" target="_blank" rel="noopener noreferrer" class="rp-link">Rsbuild</a> 精心设计的配置和插件，使库开发者得以复用 webpack 和 Rspack 繁荣的知识和生态系统，为开发者提供了完备的库开发能力。</strong></p>
<p>我们希望 Rslib 能为库开发者提供更强大的能力，并成为 Rspack 生态中的重要组成部分，持续推动基于 Rspack 的统一工具链的发展。</p>
<img src="https://assets.rspack.rs/others/assets/rslib/rslib-in-rstack.png" alt="Rslib 在 Rstack 生态中的位置" width="760" style="display:block;margin:0 auto"/>
<p style="text-align:center;font-size:0.8rem;margin:0.5rem 0;color:#999">Rslib 在 Rstack 生态中的位置</p>
<h2 class="rp-toc-include" id="完备的库开发能力"><a href="#完备的库开发能力" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>完备的库开发能力</h2>
<p>Rslib 提供了一套全面的库构建解决方案，涵盖了目前大部分库构建场景的需求：</p>
<ul>
<li>
<p><strong>开箱即用的配置</strong></p>
<p>Rslib 提供了开箱即用的库构建配置。只需对配置进行少量调整，即可满足绝大部分库构建需求。不仅如此，对于更高级的集成需求，大多数情况下，只需安装相应的 Rsbuild 插件即可完成，避免了繁琐的配置过程。</p>
<p>Rslib 的配置基于 Rsbuild 进行扩展，使开发者能继续使用 Rsbuild 的配置及生态，在应用开发和库开发时能够保持一致的心智模型，并能复用构建配置相关的代码。这在需要维护多种构建配置的大型 monorepo 项目中尤为重要。</p>
</li>
<li>
<p><strong>多类型产物支持</strong></p>
<p>Rslib 支持将库打包成多种模块系统格式，包括 ESM（ES modules）、CJS（CommonJS）、UMD（Universal Module Definition）与模块联邦（Module Federation）。只需一个构建工具和一份配置文件，即可应对所有构建场景，特别适合需要跨项目和跨框架复用的库开发场景。同时，通过对 Rspack ESM 格式构建产物的优化，Rslib 也已经能构建出规范纯净的 ESM 产物。</p>
<p>对于 TypeScript 类型声明文件（d.ts），除了支持基于 TypeScript Compiler API 生成 d.ts 文件及相关后处理，也支持基于 <a href="https://www.npmjs.com/package/@microsoft/api-extractor" target="_blank" rel="noopener noreferrer" class="rp-link">@microsoft/api-extractor</a> 对 d.ts 文件进行打包，适合处理依赖复杂的场景。</p>
<img src="https://assets.rspack.rs/others/assets/rslib/multiple-formats-config.png" alt="一个配置文件中包含多种格式" width="400" style="display:block;margin:0 auto"/>
<p style="text-align:center;font-size:0.8rem;margin:0.5rem 0;color:#999">一个配置文件中包含多种格式</p>
<img src="https://assets.rspack.rs/others/assets/rslib/simple-esm-output.png" alt="纯净的 ESM 产物" width="400" style="display:block;margin:0 auto"/>
<p style="text-align:center;font-size:0.8rem;margin:0.5rem 0;color:#999">纯净的 ESM 产物</p>
</li>
<li>
<p><strong>bundle &amp; bundleless</strong></p>
<p>Rslib 支持在构建时选择打包（bundle）或仅做转换（bundleless）这两种常见的构建模式。对于 bundleless 场景，还支持了 <a href="/zh/config/lib/redirect" class="rp-link">产物路径重定向</a> 功能，从而解决了常见的 JavaScript 和 d.ts 产物路径中别名替换以及 ESM 要求严格引用路径的问题。</p>
<img src="https://assets.rspack.rs/rslib/rslib-bundleless-mode.png" alt="bundleless 产物结构" width="760" style="display:block;margin:0 auto"/>
<p style="text-align:center;font-size:0.8rem;margin:0.5rem 0;color:#999">bundleless 产物结构</p>
</li>
<li>
<p><strong>样式解决方案</strong></p>
<p>在 UI 组件库的开发过程中，样式的构建一直是个难题。与应用构建时使用 Less loader、Sass loader、PostCSS loader 相比，用户在构建库产物时往往需要手动使用对应样式解决方案的原生工具来进行转换。</p>
<p>借助 Rsbuild 的插件系统和生态，Rslib 可以直接复用 Sass、Less、Stylus、CSS Modules、Tailwind CSS 的解决方案。无论是在 bundle 还是 bundleless 的场景下，用户都可以像构建 web 应用一样，直接通过配置或安装相应插件来进行样式的处理。</p>
<img src="https://assets.rspack.rs/others/assets/rslib/multiple-styles-config.png" alt="支持多种样式解决方案" width="760" style="display:block;margin:0 auto"/>
<p style="text-align:center;font-size:0.8rem;margin:0.5rem 0;color:#999">支持多种样式解决方案</p>
</li>
<li>
<p><strong>Web 资源处理</strong></p>
<p>Rslib 支持在代码中引用图片、字体、音频、视频等类型的静态资源及内联资源，还支持在 CSS 中引用静态资源。安装相应插件后，用户还可以通过 SVGR 处理 SVG 文件，同时支持 JSON、TOML、YAML 格式。这项功能在构建含有资源的 UI 组件库时，能够提供极大的便利。</p>
</li>
<li>
<p><strong>框架无关</strong></p>
<p>Rslib 是一个框架无关的库构建解决方案，支持 Node.js 和多种 UI 组件库的构建。不同 DSL 的 UI 框架只需要通过接入相应插件（包括 React、Preact、Vue、Solid 等）即可完成底层构建配置进行开发。</p>
</li>
</ul>
<h2 class="rp-toc-include" id="解锁-rspack-生态可能性"><a href="#解锁-rspack-生态可能性" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>解锁 Rspack 生态可能性</h2>
<p>除了常见的 JavaScript 库开发解决方案，基于 Rspack 以及 Rsbuild 生态，Rslib 为开发者们提供了更高级的能力：</p>
<ul>
<li>
<p><strong>Module Federation</strong></p>
<p>Rslib 内置提供对 <a href="/zh/guide/advanced/module-federation" class="rp-link">模块联邦</a>（Module Federation）的一等公民支持，用户只需简单安装对应的 Rsbuild 插件，即可启动对模块联邦的调试及构建 MF 格式的产物，用户可以在构建库的同时将其作为远程模块发布到远程服务器上，也可以直接使用远程模块。</p>
</li>
<li>
<p><strong>复用 Rsbuild 能力</strong></p>
<p>Rslib 可以复用 Rsbuild 的大部分配置，支持的高级功能包括但不限于：<a href="https://rsbuild.rs/zh/config/source/transform-import" target="_blank" rel="noopener noreferrer" class="rp-link">引入路径转换</a>，<a href="https://rsbuild.rs/zh/config/output/inline-styles" target="_blank" rel="noopener noreferrer" class="rp-link">内联样式</a>，<a href="https://rsbuild.rs/zh/config/output/polyfill" target="_blank" rel="noopener noreferrer" class="rp-link">polyfill</a> 等，在未来 Rslib 能持续享受 Rsbuild 迭代提供的更多能力。</p>
</li>
<li>
<p><strong>共享 Rspack 和 Rsbuild 生态</strong></p>
<p>基于 Rspack 和 Rsbuild 生态，Rslib 能复用一系列生态能力，包括但不限于：</p>
<ul>
<li>使用 <a href="https://storybook.rsbuild.rs/guide/integrations/rslib.html" target="_blank" rel="noopener noreferrer" class="rp-link">Storybook</a> 直接读取 Rslib 的配置文件进行 UI 组件库开发调试。</li>
<li>使用 <a href="https://rsdoctor.rs/" target="_blank" rel="noopener noreferrer" class="rp-link">Rsdoctor</a> 进行构建性能及产物分析。</li>
<li>使用 <a href="https://github.com/rstackjs/rsbuild-plugin-node-polyfill" target="_blank" rel="noopener noreferrer" class="rp-link">Node.js polyfill</a> 插件开发跨运行时的库。</li>
<li>使用 <a href="https://github.com/rstackjs/rsbuild-plugin-eslint" target="_blank" rel="noopener noreferrer" class="rp-link">ESLint 插件</a> 在开发时进行 ESLint 校验。</li>
<li>使用 <a href="https://github.com/rstackjs/rsbuild-plugin-publint" target="_blank" rel="noopener noreferrer" class="rp-link">publint 插件</a> 检查开发的库的 package.json 是否配置正确。</li>
</ul>
<img src="https://assets.rspack.rs/others/assets/rslib/publint-screenshot.png" alt="使用 publint 检查 package.json 正确性" width="760" style="display:block;margin:0 auto"/>
<p style="text-align:center;font-size:0.8rem;margin:0.5rem 0;color:#999">使用 publint 检查 package.json 的正确性</p>
<blockquote>
<p>更多插件的信息请参考 <a href="https://rsbuild.rs/zh/plugins/list/" target="_blank" rel="noopener noreferrer" class="rp-link">Rsbuild 插件总览</a>。</p>
</blockquote>
</li>
</ul>
<h2 class="rp-toc-include" id="当前状态及版本演进"><a href="#当前状态及版本演进" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>当前状态及版本演进</h2>
<p>目前 Rstack 工具链中的 Rspack / Rsbuild / Rspress 等项目已经使用 Rslib 进行构建，周下载量数万，在字节内部已经服务了多个业务，同时也是 Module Federation 模块的 <a href="https://module-federation.io/zh/integrations/build-tool/rslib" target="_blank" rel="noopener noreferrer" class="rp-link">推荐解决方案</a>。</p>
<p>Rslib 目前处于 0.x 阶段，我们计划在达成以下关键目标后发布 v1.0 版本：</p>
<ul>
<li>确认最终配置和 API 的设计</li>
<li>优化构建产物的质量</li>
<li>支持 Vue、Solid 等更多框架的 bundleless 开发模式</li>
</ul>
<p>你可以通过在 GitHub 中查看 v1.0 版本的 <a href="https://github.com/web-infra-dev/rslib/milestone/1" target="_blank" rel="noopener noreferrer" class="rp-link">里程碑</a> 追踪进展。</p>
<h2 class="rp-toc-include" id="开始使用-rslib"><a href="#开始使用-rslib" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>开始使用 Rslib</h2>
<p>我们提供了脚手架工具 <a href="/zh/guide/start/quick-start" class="rp-link">create-rslib</a> 用于快速创建 Rslib 项目。该脚手架支持创建 Node.js / React 库项目及配套的开发工具。此外，我们还提供了 <a href="/zh/guide/migration/tsup" class="rp-link">迁移文档</a>，帮助用户从其他构建工具迁移到 Rslib。阅读 <a href="/zh/guide/start/quick-start" class="rp-link">快速上手</a>，进一步了解如何开始使用 Rslib。</p>
<p>我们相信，基于 Rspack 的统一工具链，将为开发者提供更多的可能性。期待你的反馈与贡献，帮助我们共同构建更完善的前端工具链生态。</p><!--/$-->]]></content:encoded>
        </item>
    </channel>
</rss>