rspack-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRspack Best Practices
Rspack 最佳实践
Apply these rules when writing or reviewing Rspack projects.
在编写或评审Rspack项目时,请遵循以下规则。
Configuration
配置
- Use and
rspack.config.tsdefineConfig - Define explicit values for multi-page applications
entry - Keep one main config and branch by only when needed
process.env.NODE_ENV - Keep rule conditions narrow and explicit (,
test,include,exclude)resourceQuery - Prefer built-in Rspack plugins/loaders over community JS alternatives when equivalent features exist
- 使用和
rspack.config.tsdefineConfig - 为多页面应用定义明确的值
entry - 仅在必要时保留一个主配置,并根据进行分支处理
process.env.NODE_ENV - 保持规则条件精准且明确(、
test、include、exclude)resourceQuery - 当存在等效功能时,优先使用Rspack内置的插件/加载器,而非社区JavaScript替代方案
CLI
CLI 命令
If is installed:
@rspack/cli- Use for local development
rspack dev - Use for production build
rspack build - Use only for local production preview
rspack preview
如果已安装:
@rspack/cli- 使用进行本地开发
rspack dev - 使用进行生产环境构建
rspack build - 仅使用进行本地生产环境预览
rspack preview
Type checking
类型检查
- Use for integrated dev/build type checks
ts-checker-rspack-plugin - Or run /
tsc --noEmitas an explicit script stepvue-tsc --noEmit
- 使用实现开发/构建过程中的集成式类型检查
ts-checker-rspack-plugin - 或者将/
tsc --noEmit作为显式的脚本步骤执行vue-tsc --noEmit
CSS
CSS 处理
Choose one strategy:
- Built-in CSS () for modern setups
type: 'css' | 'css/auto' | 'css/module' - +
css-loaderfor webpack migration compatibilityCssExtractRspackPlugin - for pure style-in-JS runtime injection scenarios
style-loader
Optional:
- Use when goals are syntax downgrade + vendor prefixing
builtin:lightningcss-loader - Use /
sass-loaderfor preprocessing Sass/Less filesless-loader - Use for Tailwind CSS integration
@tailwindcss/webpack
选择以下一种策略:
- 现代化项目使用内置CSS功能()
type: 'css' | 'css/auto' | 'css/module' - 迁移webpack项目时使用+
css-loader以保证兼容性CssExtractRspackPlugin - 纯JS运行时注入样式的场景使用
style-loader
可选配置:
- 当需要语法降级和浏览器厂商前缀时,使用
builtin:lightningcss-loader - 预处理Sass/Less文件时使用/
sass-loaderless-loader - 集成Tailwind CSS时使用
@tailwindcss/webpack
Bundle size optimization
包体积优化
- Prefer dynamic for non-critical code paths
import() - Prefer lightweight libraries where possible
- Keep aligned with real compatibility requirements
target
- 对非关键代码路径优先使用动态
import() - 尽可能使用轻量级库
- 使与实际兼容性需求保持一致
target
Asset management
资源管理
- Import source-managed assets from project source directories, not from
public - Reference files by absolute URL path
public - Prefer asset modules (,
asset,asset/resource,asset/inline) over legacyasset/source/file-loader/url-loaderraw-loader
- 从项目源码目录导入受源码管理的资源,而非从目录导入
public - 使用绝对URL路径引用目录下的文件
public - 优先使用资源模块(、
asset、asset/resource、asset/inline),而非传统的asset/source/file-loader/url-loaderraw-loader
Profiling
性能分析
- Use Node CPU profiling () when JavaScript-side overhead is suspected
--cpu-prof - Use and analyze trace output for compiler-phase bottlenecks
RSPACK_PROFILE=OVERVIEW - Replace known slow stacks first (, PostCSS, terser) with Rspack built-ins when feasible
babel-loader
- 当怀疑存在JavaScript层面的性能开销时,使用Node CPU分析()
--cpu-prof - 使用并分析追踪输出,以定位编译阶段的性能瓶颈
RSPACK_PROFILE=OVERVIEW - 在可行的情况下,优先使用Rspack内置功能替代已知的慢处理栈(、PostCSS、terser)
babel-loader
Security
安全规范
- Do not publish files to public servers/CDNs when production source maps are enabled
.map
- 当启用生产环境source map时,请勿将文件发布到公共服务器/CDN
.map
Documentation
文档参考
- For the latest (v2) docs, read http://rspack.rs/llms.txt
- For Rspack v1 docs, read http://v1.rspack.rs/llms.txt
- 最新(v2版本)文档请参考:http://rspack.rs/llms.txt
- Rspack v1版本文档请参考:http://v1.rspack.rs/llms.txt