---
url: /guide/start/index.md
---
# Introduction
Rslib is a library development tool that leverages the well-designed configurations and plugins of [Rsbuild](https://rsbuild.rs), empowering library developers to take advantage of the extensive knowledge and ecosystem of webpack and Rspack.
Rslib provides a comprehensive set of build features for library development, including:
- **Compilation of diverse languages**: TypeScript, JSX, Sass, Less, CSS Modules, Wasm, and more.
- **Flexible build modes**: Bundle and bundleless options to meet varying needs.
- **Multiple output formats**: ESM, CJS, and UMD for maximum compatibility.
- **Declaration file generation**: Including isolated declarations.
- **Advanced features**: Module Federation, asset compression, PostCSS, Lightning CSS, and more.
## ✨ Why Rslib
During the development of component or utility libraries, developers need to focus not only on implementing project logic, but also on handling tasks that are separate from the code itself, such as building, debugging, documentation, and testing. Although many community tools and solutions can address some of these needs, developers who are not familiar with them often face cumbersome configuration requirements or need to coordinate multiple tools to meet these demands.
Based on Rspack and Rsbuild, Rslib offers a comprehensive solution tailored to the diverse requirements of library development, effectively addressing issues such as incomplete tool ecosystems, high costs for module standard compatibility, and insufficient output optimization. Rslib optimizes webpack's limited support for library ESM outputs, reducing redundant runtime code and generating high-quality ESM outputs that are tree-shaking friendly for library consumers. Additionally, Rslib fully leverages the build performance advantages of Rspack and capitalizes the strengths of both the webpack and Rspack ecosystems to robustly support features such as Module Federation.
Furthermore, Rslib utilizes Rsbuild's out-of-the-box configuration to facilitate configuration sharing between application and library projects, resolving the challenge of reusing build configurations between application projects and library projects, thereby reducing the configuration overhead for developers and improving development efficiency and experience.
In the future, Rslib will explore additional possibilities by leveraging the new features of Rspack.
## 🔥 Features
Rslib has the following features:
- **Easy to Configure**: Rslib aims to simplify library development by offering ready-to-use build capabilities, enabling developers to kickstart their library projects with minimal configuration.
- **Performance Oriented**: Rslib integrates high-performance Rust-based tools from the community, including [Rspack](https://rspack.rs/), [SWC](https://swc.rs/) and [Lightning CSS](https://lightningcss.dev/), to deliver first-class build speed and development experience.
- **Plugin Ecosystem**: Powered by Rsbuild, Rslib benefits from a lightweight plugin system and a collection of high-quality official plugins. Furthermore, Rsbuild's compatibility with most webpack plugins and all Rspack plugins allows library developers to seamlessly integrate existing community or in-house plugins into their library projects.
## 🎯 Ecosystem
Rslib is implemented based on Rsbuild and fully reuses the capabilities and ecosystem of Rsbuild.
The following diagram illustrates the relationship between Rslib and other tools in the ecosystem:

## 🦀 Rstack
Rstack is a unified JavaScript toolchain centered on Rspack, with high performance and consistent architecture.
| Name | Description | Version |
| ----------------------------------------------------- | ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Rspack](https://github.com/web-infra-dev/rspack) | Bundler |
|
| [Rsbuild](https://github.com/web-infra-dev/rsbuild) | Build tool |
|
| [Rslib](https://github.com/web-infra-dev/rslib) | Library development tool |
|
| [Rspress](https://github.com/web-infra-dev/rspress) | Static site generator |
|
| [Rsdoctor](https://github.com/web-infra-dev/rsdoctor) | Build analyzer |
|
| [Rstest](https://github.com/web-infra-dev/rstest) | Testing framework |
|
| [Rslint](https://github.com/web-infra-dev/rslint) | Linter |
|
## 🔗 Links
- [awesome-rstack](https://github.com/rstackjs/awesome-rstack): A curated list of awesome things related to Rstack.
- [rstack-examples](https://github.com/rstackjs/rstack-examples): Examples for Rstack.
- [storybook-rsbuild](https://github.com/rstackjs/storybook-rsbuild): Storybook builder powered by Rsbuild.
- [rsbuild-plugin-template](https://github.com/rstackjs/rsbuild-plugin-template): Use this template to create your own Rsbuild plugin.
- [rstack-design-resources](https://github.com/rstackjs/rstack-design-resources): Design resources for Rstack.
## 🧑💻 Community
Come and chat with us on [Discord](https://discord.gg/XsaKEEk4mW)! The Rstack team and users are active there, and we're always looking for contributions.
---
url: /guide/start/quick-start.md
---
# Quick start
## Environment preparation
Rslib supports using [Node.js](https://nodejs.org/), [Deno](https://deno.com/), or [Bun](https://bun.sh/) as the JavaScript runtime.
Use one of the following installation guides to set up a runtime:
- [Install Node.js](https://nodejs.org/en/download)
- [Install Bun](https://bun.com/docs/installation)
- [Install Deno](https://docs.deno.com/runtime/getting_started/installation/)
:::tip Version requirements
- Rslib >= 0.20 requires Node.js version 20.19+, 22.12+.
- Rslib \< 0.20 requires Node.js 18.12.0 or higher.
:::
## Creating an Rslib project
Use [`create-rslib`](https://www.npmjs.com/package/create-rslib) to create a new Rslib project. Run the following command:
```sh [npm]
npm create rslib@latest
```
```sh [yarn]
yarn create rslib
```
```sh [pnpm]
pnpm create rslib@latest
```
```sh [bun]
bun create rslib@latest
```
```sh [deno]
deno init --npm rslib@latest
```
Follow the prompts step by step. During project creation, you can choose a template, language, optional tools, and optional skills.
All templates include [Rstest](https://rstest.rs/) by default for testing.
After creating the project, do the following:
- Run `git init` to initialize a Git repository.
- Run `npm install` (or your package manager's install command) to install dependencies.
- Run `npm run dev` to start watch mode and begin development.
### Templates
When creating a project, you can choose from the following templates provided by `create-rslib`:
| Template | Description |
| ---------------------------- | ---------------------------- |
| Node.js dual ESM/CJS package | Node.js dual ESM/CJS package |
| Node.js pure ESM package | Node.js pure ESM package |
| React | React component library |
| Vue | Vue component library |
| Svelte | Svelte component library |
### Optional tools
`create-rslib` can help you set up the following commonly used tools. Use the arrow keys to navigate and the space bar to select. Press Enter without selecting anything to skip these tools.
| Tool | Use |
| --------------------------------------------------------- | --------------------------------------------------------- |
| [ESLint](https://eslint.org/) | Linting |
| [Rslint](https://rslint.rs/) | Linting |
| [Prettier](https://prettier.io/) | Formatting |
| [Biome](https://biomejs.dev/) | Linting and formatting |
| [Rspress](https://rspress.rs/) | Component documentation, React + TypeScript template |
| [Storybook](https://storybook.js.org/) | Component development and preview, React or Vue templates |
| [React Compiler](/guide/solution/react.md#react-compiler) | Optimizing React components, React template only |
### Optional skills
`create-rslib` can install optional skills for coding agents that support Skills. In interactive mode, your selections in “Optional tools” affect the available skill options. The currently available skills are:
| Name | Condition |
| ------------------------------------------------------------------------------------------------------- | ---------------- |
| [rslib-best-practices](https://github.com/rstackjs/agent-skills#rslib-best-practices) | Default |
| [rstest-best-practices](https://github.com/rstackjs/agent-skills#rstest-best-practices) | Default |
| [rspress-custom-theme](https://github.com/rstackjs/agent-skills#rspress-custom-theme) | Choose `Rspress` |
| [rspress-description-generator](https://github.com/rstackjs/agent-skills#rspress-description-generator) | Choose `Rspress` |
Use the arrow keys to navigate and the space bar to select. Press Enter without selecting anything to skip.
```text
◆ Select optional skills (Use to select, to continue)
│ ◻ Rslib - best practices
│ ◻ Rstest - best practices
```
For more details about Agent Skills and other AI-related capabilities, see [AI](/guide/start/ai.md).
### Current directory
To create a project in the current directory, set the target folder to `.`:
```text
◆ Create Rslib Project
│
◇ Project name or path
│ .
│
◇ "." is not empty, please choose:
│ Continue and override files
```
### Non-interactive mode
[create-rslib](https://www.npmjs.com/package/create-rslib) supports a non-interactive mode via command-line options. This mode skips prompts and creates the project directly, which is useful for scripts, CI, and automation.
For example, the following command creates a React project in the `my-project` directory:
```bash
npx -y create-rslib@latest my-project --template react
# Using abbreviations
npx -y create-rslib@latest my-project -t react
# Specify multiple tools
npx -y create-rslib@latest my-project -t react --tools storybook,biome
# Install an optional skill for coding agents
npx -y create-rslib@latest my-project -t react --skill rslib-best-practices
```
All CLI flags supported by `create-rslib`:
```text
Usage: create-rslib [dir] [options]
Options:
-h, --help display help for command
-d, --dir create project in specified directory
-t, --template specify the template to use
--tools add additional tools, comma separated
--skill add optional skills, comma separated
--override override files in target directory
--packageName specify the package name
Available templates:
node-dual-js, node-dual-ts, node-esm-js, node-esm-ts, react-js, react-ts, vue-js, vue-ts, svelte-js, svelte-ts
Optional tools:
react-compiler, eslint, rslint, biome, prettier, rspress, storybook
Optional skills:
rslib-best-practices, rstest-best-practices, rspress-custom-theme, rspress-description-generator
```
## Migrate from existing projects
To migrate from an existing project to Rslib, refer to the following guides:
- [Migrating from tsup](/guide/migration/tsup.md)
- [Migrating from Modern.js Module](/guide/migration/modernjs-module.md)
### Other projects
For other types of projects, you can manually install the [@rslib/core](https://www.npmjs.com/package/@rslib/core) package:
```sh [npm]
npm add @rslib/core -D
```
```sh [yarn]
yarn add @rslib/core -D
```
```sh [pnpm]
pnpm add @rslib/core -D
```
```sh [bun]
bun add @rslib/core -D
```
```sh [deno]
deno add npm:@rslib/core -D
```
Then refer to the guide and documentation to enable the features you need:
- See [CLI](/guide/basic/cli.md) to learn about available CLI commands.
- See [Configure Rslib](/guide/basic/configure-rslib.md) to configure Rslib.
---
url: /guide/start/glossary.md
---
# Glossary
## ESM
ESM stands for [ECMAScript modules](https://nodejs.org/api/esm.html#modules-ecmascript-modules), which is a modern module system introduced in ES2015 that allows JavaScript code to be organized into reusable, self-contained modules. ESM is now the standard for both [browser](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and [Node.js](https://nodejs.org/api/esm.html) environments, replacing older module systems like [CommonJS (CJS)](https://nodejs.org/api/modules.html) and [AMD](https://requirejs.org/docs/whyamd.html).
## CJS
CJS stands for [CommonJS modules](https://nodejs.org/api/modules.html#modules-commonjs-modules), which is a module system used in JavaScript, particularly in server-side environments like Node.js. It was created to allow JavaScript to be used outside of the browser by providing a way to manage modules and dependencies.
## UMD
UMD stands for [Universal Module Definition](https://github.com/umdjs/umd), a pattern for writing JavaScript modules that can work universally across different environments, such as both the browser and Node.js. Its primary goal is to ensure compatibility with the most popular module systems, including AMD (Asynchronous Module Definition), CommonJS (CJS), and browser globals.
## Bundleless
Bundleless means that each source file is compiled and built separately, but not bundled together. Each output file can be found with its corresponding source code file. The process of bundleless build can also be understood as the process of code transformation of source files only.
## Module Federation
Module Federation is an architectural pattern for JavaScript application decomposition (similar to microservices on the server-side), allowing you to share code and resources between multiple JavaScript applications (or micro-frontends).
See [Module Federation](https://rsbuild.rs/guide/advanced/module-federation) for more details.
## More
See more glossary in [Rsbuild - Glossary](https://rsbuild.rs/guide/start/glossary) and [Rspack - Glossary](https://rspack.rs/misc/glossary).
---
url: /guide/start/npm-packages.md
---
# Packages
This document showcases all the npm package information maintained by Rslib team.
## @rslib/core

Rslib core package that provides CLI commands and build capabilities based on Rsbuild.
- [npm](https://npmjs.com/package/@rslib/core)
- [Source Code](https://github.com/web-infra-dev/rslib/tree/main/packages/core)
## rsbuild-plugin-dts

Rsbuild plugin that supports emitting declaration files for TypeScript.
- [npm](https://npmjs.com/package/rsbuild-plugin-dts)
- [Source Code](https://github.com/web-infra-dev/rslib/tree/main/packages/plugin-dts)
## create-rslib

Used to create a new Rslib project.
- [npm](https://npmjs.com/package/create-rslib)
- [Source Code](https://github.com/web-infra-dev/rslib/tree/main/packages/create-rslib)
---
url: /guide/start/ai.md
---
# AI
To help AI better understand Rslib's features, configuration, and best practices so it can provide more accurate assistance during day-to-day development and troubleshooting, Rslib provides the following capabilities:
- [Agent Skills](#agent-skills)
- [llms.txt](#llmstxt)
- [Markdown docs](#markdown-docs)
- [AGENTS.md](#agentsmd)
## Agent Skills
Agent Skills are domain-specific knowledge packs that can be installed into Agents, enabling them to give more accurate and professional suggestions or perform actions in specific scenarios.
In the [rstackjs/agent-skills](https://github.com/rstackjs/agent-skills) repository, there are many skills for the Rstack ecosystem. The skills related to Rslib include:
- [rslib-best-practices](https://github.com/rstackjs/agent-skills#rslib-best-practices): Best practices for Rslib.
- [migrate-to-rslib](https://github.com/rstackjs/agent-skills#migrate-to-rslib): Migrate existing tsc or tsup projects to Rslib.
In Coding Agents that support skills, you can use the [skills](https://www.npmjs.com/package/skills) package to install a specific skill with the following command:
```sh [npx]
npx skills add rstackjs/agent-skills --skill migrate-to-rslib
```
```sh [yarn]
yarn dlx skills add rstackjs/agent-skills --skill migrate-to-rslib
```
```sh [pnpm]
pnpm dlx skills add rstackjs/agent-skills --skill migrate-to-rslib
```
```sh [bunx]
bunx skills add rstackjs/agent-skills --skill migrate-to-rslib
```
```sh [deno]
deno run -A npm:skills add rstackjs/agent-skills --skill migrate-to-rslib
```
After installation, simply use natural language prompts to trigger the skill, for example:
```
Help me migrate this tsc project to Rslib
```
## llms.txt
[llms.txt](https://llmstxt.org/) is a standard that helps LLMs discover and use project documentation. Rslib follows this standard and publishes the following two files:
- [llms.txt](https://rslib.rs/llms.txt): A structured index file containing the titles, links, and brief descriptions of all documentation pages.
```
https://rslib.rs/llms.txt
```
- [llms-full.txt](https://rslib.rs/llms-full.txt): A full-content file that concatenates the complete content of every documentation page into a single file.
```
https://rslib.rs/llms-full.txt
```
You can choose the file that best fits your use case:
- `llms.txt` is smaller and consumes fewer tokens, making it suitable for AI to fetch specific pages on demand.
- `llms-full.txt` contains the complete documentation content, so AI doesn't need to follow individual links — ideal when you need AI to have a comprehensive understanding of Rslib, though it consumes more tokens and is best used with AI tools that support large context windows.
## Markdown docs
Every Rslib documentation page has a corresponding `.md` plain-text version that can be provided directly to AI. On any doc page, you can click “Copy Markdown” or “Copy Markdown Link” under the title to get the Markdown content or link.
```
https://rslib.rs/guide/start/index.md
```
Providing the Markdown link or content allows AI to focus on a specific chapter, which is useful for targeted troubleshooting or looking up a particular topic.
## AGENTS.md
When you create a new project with [create-rslib](https://www.npmjs.com/package/create-rslib), the generated project includes an `AGENTS.md` file. This file follows the [AGENTS.md](https://agents.md/) specification and provides key project information to Agents.
Example `AGENTS.md` content:
```markdown wrapCode
# AGENTS.md
You are an expert in JavaScript, Rspack, Rsbuild, Rslib, and library development. You write maintainable, performant, and accessible code.
## Commands
- `npm run build` - Build the library for production
- `npm run dev` - Turn on watch mode, watch for changes and rebuild the library
## Docs
- Rslib: https://rslib.rs/llms.txt
- Rsbuild: https://rsbuild.rs/llms.txt
- Rspack: https://rspack.rs/llms.txt
```
You can also customize it for your project, adding more details about the project structure, overall architecture, and other relevant information so Agents can better understand your project.
::: tip
If you are using Claude Code, you can create a `CLAUDE.md` file and reference the `AGENTS.md` file in it.
```markdown title="CLAUDE.md"
@AGENTS.md
```
:::
---
url: /guide/solution/index.md
---
# Overview
In this chapter, we will introduce how to use Rslib to development libraries for browser and Node.js. We will also cover how to create libraries for different UI frameworks.
## Browser target
When developing a library that runs in the browser, you can package it in both [ESM](/guide/basic/output-format.md#esm--cjs) and [CJS](/guide/basic/output-format.md#esm--cjs) formats for integration with application bundlers. Configuring the package [conditional exports](https://nodejs.org/api/packages.html#conditional-exports) to ESM output allows for better tree shaking. Additionally, you can create [UMD](/guide/basic/output-format.md#umd) format output for direct browser use and even generate [Module Federation ](/guide/advanced/module-federation.md) formats for dynamic loading by other applications. Configure [Browserslist](https://rsbuild.rs/guide/advanced/browserslist) according to the target browser support to determine the downgrade syntax of the output, or add a [polyfill](/guide/advanced/output-compatibility.md) for API compatibility.
When publishing to npm, you can choose not to [minify](/config/rsbuild/output.md#outputminify) your code or to minify it while providing a [sourcemap](/config/rsbuild/output.md#outputsourcemap) to enhance the debugging experience for users of your library. For styling, you can use CSS, or CSS pre-processors like Sass, Less, or Stylus, or apply PostCSS for CSS post-processing. Tools like Tailwind CSS can also help in building your styles. Using CSS Modules to create CSS modules is another option.
In terms of resource management, Rslib handles static assets used in your code, such as SVG and PNG files. You can also build a component library of [React](/guide/solution/react.md), [Preact](https://github.com/rstackjs/rstack-examples/tree/main/rslib/preact), or other frameworks, and use [Storybook](/guide/advanced/storybook.md) for UI component development and testing.
Refer to the solutions in this chapter to learn how to use Rslib to develop browser libraries for different frameworks.
## Node.js target
Rslib set [target](/config/rsbuild/output.md#outputtarget) to `"node"` by default to development libraries for Node.js.
You can create a [pure ESM](/guide/basic/output-format.md#esm--cjs) package or a [dual package](/guide/basic/output-format.md#esm--cjs) that supports both ESM and CJS as needed. In CJS output, `import.meta.url` will be automatically [shimmed](/config/lib/shims.md) for compatibility and `__dirname` and `__filename` got optional ESM shims to ensure proper use across different module system. Node.js's built-in packages will be [externalized by default](/guide/advanced/third-party-deps.md).
---
url: /guide/solution/nodejs.md
---
# Node.js
In this document, you will learn how to build a Node.js library using Rslib. You can check out Node.js related example projects in [Examples](https://github.com/rstackjs/rstack-examples/tree/main/rslib).
## Create Node.js project
You can use `create-rslib` to create a project with Rslib + Node.js. Just execute the following command:
```sh [npm]
npm create rslib@latest
```
```sh [yarn]
yarn create rslib
```
```sh [pnpm]
pnpm create rslib@latest
```
```sh [bun]
bun create rslib@latest
```
Then select `Node.js` when prompted to "Select template".
## Use Rslib in an existing project
Rslib offers seamless support for Node.js projects, allowing you to build Node.js project effortlessly with minimal configuration.
For example, in `rslib.config.ts`:
```ts title="rslib.config.ts"
import { defineConfig } from '@rslib/core';
export default defineConfig({
lib: [
{
format: 'esm',
output: {
distPath: './dist/esm',
},
},
{
format: 'cjs',
output: {
distPath: './dist/cjs',
},
},
],
});
```
## Target for Node.js
Rslib sets [target](/config/rsbuild/output.md#outputtarget) to `"node"` by default, which is different from the default target of Rsbuild.
When target is set to `"node"`, Rslib adjusts many configurations for Node.js. For example, [output.externals](/config/rsbuild/output.md#outputtarget) will exclude built-in Node.js modules, and [shims](/config/lib/shims.md) will add a shim for `import.meta.url` in CJS output by default.
### Externals
All Node.js [built-in modules](https://nodejs.org/docs/latest/api/) are externalized by default.
### Shims
- `global`: leave it as it is, while it's recommended to use [globalThis](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis) instead.
- `__filename`: When outputting in ESM format, replace `__filename` with the result of `fileURLToPath(import.meta.url)`.
- `__dirname`: When outputting in ESM format, replace `__dirname` with the result of `dirname(fileURLToPath(import.meta.url))`.
---
url: /guide/solution/react.md
---
# React
In this document, you will learn how to build a React component library with Rslib. You can check out React related example projects in [Examples](https://github.com/rstackjs/rstack-examples/tree/main/rslib).
## Create React project
You can use `create-rslib` to create a project with Rslib + React. Just execute the following command:
```sh [npm]
npm create rslib@latest
```
```sh [yarn]
yarn create rslib
```
```sh [pnpm]
pnpm create rslib@latest
```
```sh [bun]
bun create rslib@latest
```
Then select `React` when prompted to "Select template".
## Use Rslib in an existing project
To develop a React library, you need to set the [target](/config/rsbuild/output.md#outputtarget) to `"web"` in `rslib.config.ts`. This is crucial because Rslib sets the `target` to `"node"` by default, which differs from the default target of Rsbuild.
To compile React (JSX and TSX), you need to register the Rsbuild [React Plugin](https://rsbuild.rs/plugins/list/plugin-react). The plugin will automatically add the necessary configuration for React builds.
For example, register in `rslib.config.ts`:
```ts title="rslib.config.ts" twoslash
import { defineConfig } from '@rslib/core';
import { pluginReact } from '@rsbuild/plugin-react'; // [!code highlight]
export default defineConfig({
lib: [
// ...
],
// [!code highlight:4]
output: {
target: 'web',
},
plugins: [pluginReact(/** options here */)],
});
```
## JSX transform
- **Type:** `'automatic' | 'classic' | 'preserve'`
- **Default:** `'automatic'`
React introduced a [new JSX transform](https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html) in version 17. This new transform removes the need to import `React` when using JSX.
By default, Rslib uses the new JSX transform, which is `runtime: 'automatic'`. It requires at least React `16.14.0` or higher and the `peerDependencies` should be specified as `"react": ">=16.14.0"`.
To change the JSX transform, you can set the [swcReactOptions](https://rsbuild.rs/plugins/list/plugin-react#swcreactoptionsruntime) option in `@rsbuild/plugin-react`.
For example, to use the classic runtime:
```ts title="rslib.config.ts" twoslash
import { pluginReact } from '@rsbuild/plugin-react';
import { defineConfig } from '@rslib/core';
export default defineConfig({
lib: [
// ...
],
output: {
target: 'web',
},
plugins: [
pluginReact({
// [!code highlight:3]
swcReactOptions: {
runtime: 'classic',
},
}),
],
});
```
When you need to keep native JSX in the build output, you can set the runtime to `'preserve'` to leave JSX syntax unchanged without transforming it, which is useful for subsequent processing by other bundlers.
::: warning
When using `runtime: 'preserve'`, you must set `bundle: false` to enable [bundleless mode](/guide/basic/output-structure.md#bundle--bundleless) to keep files unbundled.
:::
To emit `.jsx` files, you can configure the JS filename template through [output.filename](/config/rsbuild/output.md#outputfilename) option:
```ts title="rslib.config.ts" twoslash
import { pluginReact } from '@rsbuild/plugin-react';
import { defineConfig } from '@rslib/core';
export default defineConfig({
lib: [
{
bundle: false,
format: 'esm',
// [!code highlight:5]
output: {
filename: {
js: '[name].jsx',
},
},
},
],
plugins: [
pluginReact({
swcReactOptions: {
runtime: 'preserve',
},
}),
],
});
```
## JSX import source
- **Type**: `string`
- **Default**: `'react'`
When `runtime` is set to `'automatic'`, you can specify the import path of the JSX transform through `importSource`.
For example, when using [Emotion](https://emotion.sh/), you can set `importSource` to `'@emotion/react'`:
```ts title="rslib.config.ts" twoslash
import { pluginReact } from '@rsbuild/plugin-react';
import { defineConfig } from '@rslib/core';
export default defineConfig({
lib: [
// ...
],
output: {
target: 'web',
},
plugins: [
pluginReact({
// [!code highlight:3]
swcReactOptions: {
importSource: '@emotion/react',
},
}),
],
});
```
## React Compiler
React Compiler is a build-time tool that automatically optimizes your React app. It works with plain JavaScript, and understands the Rules of React, so you don't need to rewrite any code to use it.
Before using React Compiler, we recommend reading the [React Compiler documentation](https://react.dev/learn/react-compiler) to understand its functionality, current state, and usage.
### How to use
Steps to use React Compiler in Rslib:
1. Upgrade `react` and `react-dom` to v19. If you can't upgrade, install the [react-compiler-runtime](https://npmjs.com/package/react-compiler-runtime) package to run the compiled code on earlier versions.
2. Enable React Compiler through the `reactCompiler` option of `@rsbuild/plugin-react`:
```ts title="rslib.config.ts"
import { pluginReact } from '@rsbuild/plugin-react';
import { defineConfig } from '@rslib/core';
export default defineConfig({
plugins: [
pluginReact({
reactCompiler: true,
}),
],
});
```
This uses the Rust version of React Compiler integrated in `builtin:swc-loader`, which is around **7-13x faster** than the Babel version.
> You can also refer to the [example project](https://github.com/rstackjs/rstack-examples/tree/main/rslib/react-compiler).
### Configuration
Pass a config object to customize the React Compiler behavior. For all available options, refer to the [`reactCompiler`](https://rsbuild.rs/plugins/list/plugin-react#reactcompiler) option of `@rsbuild/plugin-react`.
```ts title="rslib.config.ts"
import { pluginReact } from '@rsbuild/plugin-react';
import { defineConfig } from '@rslib/core';
export default defineConfig({
plugins: [
pluginReact({
reactCompiler: {
compilationMode: 'annotation',
},
}),
],
});
```
For React 17 and 18 projects, install [react-compiler-runtime](https://npmjs.com/package/react-compiler-runtime) and specify the `target`:
```ts title="rslib.config.ts"
import { pluginReact } from '@rsbuild/plugin-react';
import { defineConfig } from '@rslib/core';
export default defineConfig({
plugins: [
pluginReact({
reactCompiler: {
target: '18', // '17' | '18' | '19'
},
}),
],
});
```
The `reactCompiler` options are aligned with the React Compiler configuration. For more options, refer to the official [React Compiler configuration documentation](https://react.dev/reference/react-compiler/configuration).
### Using Babel
You can also use the Babel plugin published by React Compiler. This is useful if you need Babel-specific integration or options that are not yet available in the SWC transform.
Install [@rsbuild/plugin-babel](https://rsbuild.rs/plugins/list/plugin-babel) and [babel-plugin-react-compiler](https://npmjs.com/package/babel-plugin-react-compiler), then register the Babel plugin in your Rslib config file:
```ts title="rslib.config.ts"
import { pluginBabel } from '@rsbuild/plugin-babel';
import { pluginReact } from '@rsbuild/plugin-react';
import { defineConfig } from '@rslib/core';
export default defineConfig({
plugins: [
pluginReact(),
pluginBabel({
include: /\.[jt]sx?$/,
exclude: [/[\\/]node_modules[\\/]/],
babelLoaderOptions(opts) {
opts.plugins ??= [];
opts.plugins.unshift('babel-plugin-react-compiler');
},
}),
],
});
```
## SVGR
Read [SVGR](/guide/advanced/svgr-files.md) for more details.
## Further reading
- [Rsbuild React Plugin](https://rsbuild.rs/plugins/list/plugin-react#swcreactoptionsruntime)
- [SWC Compilation - jsc.transform.react](https://swc.rs/docs/configuration/compilation#jsctransformreact)
---
url: /guide/solution/vue.md
---
# Vue
In this document, you will learn how to build a Vue component library using Rslib. You can check out Vue related example projects in [Examples](https://github.com/rstackjs/rstack-examples/tree/main/rslib).
::: note
1. Only Vue 3 is supported, Vue 2 is not supported.
2. Vue's declaration files are generated by [vue-tsc](https://www.npmjs.com/package/vue-tsc), so [lib.dts](/config/lib/dts.md) / [lib.redirect.dts](/config/lib/redirect.md#redirectdts) / [lib.banner.dts](/config/lib/banner.md#bannerdts) / [lib.footer.dts](/config/lib/footer.md#footerdts) are not effective in Vue projects.
:::
## Create Vue project
You can use `create-rslib` to create a project with Rslib + Vue. Just execute the following command:
```sh [npm]
npm create rslib@latest
```
```sh [yarn]
yarn create rslib
```
```sh [pnpm]
pnpm create rslib@latest
```
```sh [bun]
bun create rslib@latest
```
Then select `Vue` when prompted to "Select template".
## Use Rslib in an existing project
For developing Vue components, you need to set the [target](/config/rsbuild/output.md#outputtarget) to `"web"` in `rslib.config.ts`. This is crucial because Rslib sets `target` to `"node"` by default, which is different from Rsbuild's default target value.
To compile Vue (.vue single-file components), you need to register the [@rsbuild/plugin-vue](https://rsbuild.rs/plugins/list/plugin-vue) plugin. This plugin will automatically add the necessary configurations for Vue build.
For example, register in `rslib.config.ts`:
```ts title="rslib.config.ts"
import { defineConfig } from '@rslib/core';
import { pluginVue } from '@rsbuild/plugin-vue'; // [!code highlight]
export default defineConfig({
lib: [
// ...
],
// [!code highlight:4]
output: {
target: 'web',
},
plugins: [pluginVue(/** options here */)],
});
```
For more configuration options, please refer to the [@rsbuild/plugin-vue documentation](https://rsbuild.rs/plugins/list/plugin-vue).
---
url: /guide/solution/svelte.md
---
# Svelte
In this document, you will learn how to build a Svelte component library using Rslib. You can check out Svelte related example projects in [Examples](https://github.com/rstackjs/rstack-examples/tree/main/rslib).
## Create Svelte project
You can use `create-rslib` to create a project with Rslib + Svelte. Just execute the following command:
```sh [npm]
npm create rslib@latest
```
```sh [yarn]
yarn create rslib
```
```sh [pnpm]
pnpm create rslib@latest
```
```sh [bun]
bun create rslib@latest
```
Then select `Svelte` when prompted to "Select template".
## Use Rslib in an existing project
For developing Svelte components, you need to set the [target](/config/rsbuild/output.md#outputtarget) to `"web"` in `rslib.config.ts`. This is crucial because Rslib sets `target` to `"node"` by default, which is different from Rsbuild's default target value.
To compile Svelte (`.svelte` files), you need to register the [@rsbuild/plugin-svelte](https://rsbuild.rs/plugins/list/plugin-svelte) plugin. This plugin integrates [svelte-loader](https://github.com/sveltejs/svelte-loader) internally and will automatically add the necessary configurations for Svelte build.
For example, register in `rslib.config.ts`:
```ts title="rslib.config.ts"
import { defineConfig } from '@rslib/core';
import { pluginSvelte } from '@rsbuild/plugin-svelte'; // [!code highlight]
export default defineConfig({
lib: [
// ...
],
// [!code highlight:4]
output: {
target: 'web',
},
plugins: [pluginSvelte(/** options here */)],
});
```
For more configuration options, refer to the [@rsbuild/plugin-svelte documentation](https://rsbuild.rs/plugins/list/plugin-svelte).
## Declaration files
::: note
Svelte declaration files are generated by [`svelte2tsx`](https://www.npmjs.com/package/svelte2tsx), so [lib.dts](/config/lib/dts.md) / [lib.redirect.dts](/config/lib/redirect.md#redirectdts) / [lib.banner.dts](/config/lib/banner.md#bannerdts) / [lib.footer.dts](/config/lib/footer.md#footerdts) are not effective in Svelte projects.
:::
Rslib provides a plugin example for generating types in Svelte projects: [svelteDtsPlugin](https://github.com/web-infra-dev/rslib/blob/main/packages/create-rslib/template-svelte-ts/scripts/rslib-plugin-svelte-dts.ts). The plugin calls the `emitDts` method provided by [svelte2tsx](https://www.npmjs.com/package/svelte2tsx) after build to generate declaration files for `.svelte` files.
If you create a Svelte TypeScript project with `create-rslib`, this plugin is already included in the template.
For an existing project, you can use it like this:
```ts title="rslib.config.ts"
import { svelteDtsPlugin } from './scripts/rslib-plugin-svelte-dts';
export default {
plugins: [svelteDtsPlugin()],
};
```
`svelteDtsPlugin` passes the following options through to the [svelte2tsx](https://www.npmjs.com/package/svelte2tsx) [emitDts](https://github.com/sveltejs/language-tools/blob/master/packages/svelte2tsx/src/emitDts.ts) config:
- `declarationDir`: The output directory for declaration files. Defaults to `./dist`.
- `libRoot`: The source directory to emit declaration files for. Defaults to `./src`.
- `tsconfig`: The tsconfig path used for declaration generation. Defaults to [source.tsconfigPath](/config/rsbuild/source.md#sourcetsconfigpath).
- `svelteShimsPath`: The path to the svelte2tsx shims type file. Defaults to `svelte2tsx/svelte-shims-v4.d.ts`.
If you need type checking, install the [svelte-check](https://www.npmjs.com/package/svelte-check) npm package and add a `check` command to `package.json`:
```json title="package.json"
{
"scripts": {
"check": "svelte-check"
},
"devDependencies": {
"svelte-check": "^4.4.8"
}
}
```
---
url: /guide/basic/cli.md
---
# CLI
Rslib comes with a lightweight CLI that includes commands such as [rslib](#rslib) and [rslib inspect](#rslib-inspect).
## All commands
To view all available CLI commands, run the following command in the project directory:
```bash
npx rslib -h
```
The output is shown below:
```bash
Usage:
$ rslib [command] [options]
Commands:
build build the library for production (default if no command is given)
inspect inspect the Rsbuild / Rspack configs of Rslib projects
mf-dev start Rsbuild dev server of Module Federation format
```
## Common flags
Rslib CLI provides several common flags that can be used with all commands:
| Flag | Description |
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `-c, --config ` | Specify the configuration file, can be a relative or absolute path, see [Specify config file](/guide/basic/configure-rslib.md#specify-config-file) |
| `--config-loader ` | Set the config file loader (`auto` \| `jiti` \| `native`), see [Specify config loader](/guide/basic/configure-rslib.md#specify-config-loader) |
| `--env-dir ` | Specify the directory to load `.env` files, see [Rsbuild - Env directory](https://rsbuild.rs/guide/advanced/env-vars#env-directory) |
| `--env-mode ` | Specify the env mode to load the `.env.[mode]` file, see [Rsbuild - Env mode](https://rsbuild.rs/guide/advanced/env-vars#env-mode) |
| `-h, --help` | Display help for command |
| `--lib ` | Specify the library to run commands (repeatable, e.g. `--lib esm --lib cjs`), see [lib.id](/config/lib/id.md) to learn how to get or set the ID of the library |
| `--log-level ` | Set the log level (`info` \| `warn` \| `error` \| `silent`), see [logLevel](/config/rsbuild/log-level.md) |
| `--no-env` | Disable loading of `.env` files |
| `-r, --root ` | Specify the project root directory, can be an absolute path or a path relative to cwd |
## rslib
The `rslib` command will build the outputs for production in the `dist/` directory by default. `rslib build` is an alias for the `rslib` command.
```bash
Usage:
$ rslib
Options:
-w, --watch turn on watch mode, watch for changes and rebuild
--entry set entry file or pattern (repeatable)
--dist-path set output directory
--bundle enable bundle mode (use --no-bundle to disable)
--format specify the output format (esm | cjs | umd | mf | iife)
--syntax set build syntax target (repeatable)
--target set runtime target (web | node)
--dts emit declaration files (use --no-dts to disable)
--externals add package to externals (repeatable)
--minify minify output (use --no-minify to disable)
--clean clean output directory before build (use --no-clean to disable)
--auto-extension control automatic extension redirect (use --no-auto-extension to disable)
--auto-external control automatic dependency externalization (use --no-auto-external to disable)
--tsconfig use specific tsconfig (relative to project root)
```
:::note
If the [Rslib configuration file](/guide/basic/configure-rslib.md#configuration-file) is not present in your project, the CLI will automatically use the default configuration containing only a single [lib](/config/lib/index.md) and apply all build options from the command line. You can add a configuration file once you need a more complex configuration or want to build outputs in multiple formats.
:::
### Environment variables
Rslib supports injecting environment variables or expressions into the code during the build, which is helpful for distinguishing running environments or replacing constants. You can see more details in [Rsbuild - Environment variables](https://rsbuild.rs/guide/advanced/env-vars).
By default, Rslib sets the `process.env.NODE_ENV` environment variable, which is always `'production'` during the build. If you need to distinguish watch mode to dynamically set different configurations, you can set as follows:
```ts title="rslib.config.ts"
import { defineConfig } from '@rslib/core';
const isWatch = process.argv.includes('--watch');
export default defineConfig({
lib: [
{
format: 'esm',
},
],
source: {
alias: {
'@request': isWatch ? './src/request.dev.js' : './src/request.prod.js',
},
},
});
```
::: note
- If [format](/config/lib/format.md) is `esm` or `cjs`, `process.env.NODE_ENV` in source code will be preserved in the build output.
- If [format](/config/lib/format.md) is `mf` or `umd`, `process.env.NODE_ENV` in source code will be replaced to ensure that the output can run in the browser.
:::
## rslib inspect
The `rslib inspect` command is used to view the Rsbuild config and Rspack config of the Rslib project.
```bash
Usage:
$ rslib inspect
Options:
--output