rslib-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRslib Best Practices
Rslib 最佳实践
Apply these rules when writing or reviewing Rslib library projects.
在编写或评审 Rslib 库项目时请遵循以下规则。
Configuration
配置
- Use and
rslib.config.tsdefineConfig - Check Rslib-specific configurations first (e.g., ), and also leverage Rsbuild configurations (e.g.,
lib.*,source.*,output.*) as neededtools.* - For deep-level or advanced configuration needs, use or
tools.rspackto access Rspack's native configurationstools.bundlerChain - In TypeScript projects, prefer path aliases
tsconfig.json
- 使用 和
rslib.config.tsdefineConfig - 优先检查 Rslib 专属配置(例如 ),也可根据需要使用 Rsbuild 配置(例如
lib.*、source.*、output.*)tools.* - 如需深度或高级配置,可使用 或
tools.rspack访问 Rspack 原生配置tools.bundlerChain - 在 TypeScript 项目中,优先使用 的路径别名
tsconfig.json
CLI
CLI
- Use to build
rslib build - Use to build in watch mode for local development
rslib build --watch - Use to inspect final Rslib/Rsbuild/Rspack configs
rslib inspect
- 使用 执行构建
rslib build - 本地开发时使用 以监听模式构建
rslib build --watch - 使用 查看最终的 Rslib/Rsbuild/Rspack 配置
rslib inspect
Output
输出
- Prefer to build pure-ESM package with in
"type": "module"package.json - Prefer to use bundleless mode with set to
output.targetwhen building component libraries'web' - Prefer to use bundle mode when building Node.js utility libraries
- Ensure field in
exportsis correctly configured and matches the actual JavaScript output and declaration files output of different formats (ESM, CJS, etc.)package.json
- 优先构建纯 ESM 包,在 中设置
package.json"type": "module" - 构建组件库时,优先使用无打包模式,将 设置为
output.target'web' - 构建 Node.js 工具库时,优先使用打包模式
- 确保正确配置 中的
package.json字段,匹配不同格式(ESM、CJS 等)的实际 JavaScript 输出和声明文件输出exports
Declaration files
声明文件
- Prefer to enable declaration file generation with or detailed configurations
lib.dts: true - For faster type generation, enable experimental feature with
lib.dts.tsgoinstalled@typescript/native-preview
- 优先通过 或详细配置开启声明文件生成
lib.dts: true - 如需更快的类型生成速度,可安装 后开启
@typescript/native-preview实验性功能lib.dts.tsgo
Dependencies
依赖
- Prefer to place dependencies to be bundled in in bundle mode and dependencies in
devDependenciesanddependencieswill be automatically externalized (not bundled) by defaultpeerDependencies - Verify the build output and dependency specifiers in carefully to ensure no missing dependency errors occur when consumers install and use this package
package.json
- 打包模式下,优先将需要被打包的依赖放在 中,默认情况下
devDependencies和dependencies中的依赖会被自动外置(不被打包)peerDependencies - 仔细校验构建输出和 中的依赖声明,确保使用者安装和使用本包时不会出现缺失依赖的错误
package.json
Build optimization
构建优化
- Keep syntax target in aligned with real compatibility requirements to enable better optimizations
lib.syntax - Avoid format-specific APIs in source code for better compatibility with different output formats
- Prefer lightweight dependencies to reduce bundle size
- 保持 中的语法目标与实际兼容性要求一致,以获得更好的优化效果
lib.syntax - 源代码中避免使用特定格式的 API,以便更好地兼容不同的输出格式
- 优先使用轻量级依赖以减少打包体积
Toolchain integration
工具链集成
- Prefer to use Rstest with for writing tests
@rstest/adapter-rslib - Prefer to use Rspress for writing library documentation, with and
@rspress/plugin-previewfor component previews and API docs@rspress/plugin-api-docgen
- 优先搭配 使用 Rstest 编写测试
@rstest/adapter-rslib - 优先使用 Rspress 编写库文档,搭配 和
@rspress/plugin-preview实现组件预览和 API 文档生成@rspress/plugin-api-docgen
Debugging
调试
- Run with when diagnosing config resolution or plugin behavior
DEBUG=rsbuild - Read generated files in to confirm final Rsbuild/Rspack config, not assumed config
dist/.rsbuild
- 排查配置解析或插件行为问题时,添加 运行命令
DEBUG=rsbuild - 查看 下生成的文件确认最终的 Rsbuild/Rspack 配置,而非假设的配置
dist/.rsbuild
Documentation
文档
- For the latest Rslib docs, read https://rslib.rs/llms.txt
- 查看最新 Rslib 文档请访问:https://rslib.rs/llms.txt