Rslib Best Practices

rstackjs/agent-skills/skills/rslib-best-practices

作者 rstackjs2966acbef746无许可证95 个星标收录于 2026年10月8日更新于 2026年10月8日仓库今天更新

Configure, review, or troubleshoot Rslib library builds, output formats, declarations, dependency handling, and integrations.

AI 生成的概览

用于配置、审查和排查 Rslib 库构建、输出格式、声明文件与依赖的规则。

功能
提供 Rslib 库项目的最佳实践清单,涵盖配置文件与命令行命令、输出格式与 exports 设置、声明文件生成、依赖放置与外部化、构建优化、工具链集成以及调试。它仅提供指导,本身不生成文件或脚本。
适用场景
在编写或审查 Rslib 库项目时使用,或在排查其构建、输出格式、声明文件或依赖问题时使用。也适用于在 bundle 与 bundleless 模式之间做选择,或集成测试与文档工具时。
运行要求
不含脚本或附带资源,代理只需技能说明。应用这些建议的前提是项目使用 Rslib、Rsbuild、Rspack、TypeScript,并可选使用 @typescript/native-preview、@rstest/adapter-rslib 和 Rspress。仅在阅读所链接的 Rslib 文档时需要网络访问。

Rslib best practices

Apply these rules when writing or reviewing Rslib library projects.

Configuration

  • Use rslib.config.ts and defineConfig
  • Check Rslib-specific configurations first (e.g., lib.*), and also leverage Rsbuild configurations (e.g., source.*, output.*, tools.*) as needed
  • For deep-level or advanced configuration needs, use tools.rspack or tools.bundlerChain to access Rspack's native configurations
  • In TypeScript projects, prefer tsconfig.json path aliases

CLI

  • Use rslib to build
  • Use rslib --watch to build in watch mode for local development
  • Use rslib inspect to inspect final Rslib/Rsbuild/Rspack configs

Output

  • Prefer to build pure-ESM package with "type": "module" in package.json
  • Prefer to use bundleless mode with output.target set to 'web' when building component libraries
  • Prefer to use bundle mode when building Node.js utility libraries
  • Ensure exports field in package.json is correctly configured and matches the actual JavaScript output and declaration files output of different formats (ESM, CJS, etc.)

Declaration files

  • Prefer to enable declaration file generation with lib.dts: true or detailed configurations
  • For faster type generation, enable lib.dts.tsgo experimental feature with @typescript/native-preview installed

Dependencies

  • Prefer to place dependencies to be bundled in devDependencies in bundle mode and dependencies in dependencies and peerDependencies will be automatically externalized (not bundled) by default
  • Verify the build output and dependency specifiers in package.json carefully to ensure no missing dependency errors occur when consumers install and use this package

Build optimization

  • Keep syntax target in lib.syntax aligned with real compatibility requirements to enable better optimizations
  • Avoid format-specific APIs in source code for better compatibility with different output formats
  • Prefer lightweight dependencies to reduce bundle size

Toolchain integration

  • Prefer to use Rstest with @rstest/adapter-rslib for writing tests
  • Prefer to use Rspress for writing library documentation, with @rspress/plugin-preview and @rspress/plugin-api-docgen for component previews and API docs

Debugging

  • Run with DEBUG=rsbuild when diagnosing config resolution or plugin behavior
  • Read generated files in dist/.rsbuild to confirm final Rsbuild/Rspack config, not assumed config

Documentation

来源与署名

来源:rstackjs/agent-skills位于skills/rslib-best-practices提交2966acb

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架