.agents/CODE_STYLE.md
Coding standards and conventions for the Rspack project.
cargo fmt (config in rustfmt.toml)PascalCase (e.g., BannerPlugin, Compilation)snake_case (e.g., process_assets)snake_case (e.g., compilation)SCREAMING_SNAKE_CASE (e.g., MY_CONSTANT)snake_case (e.g., rspack_plugin_banner)Imports:
use std::{fmt::Debug, sync::LazyLock};
use regex::Regex;
use rspack_core::{Compilation, Plugin};
use rspack_error::Result;
Module Structure:
rspack_error::Result<T> for fallible operations? operator for error propagationasync fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
let result = some_operation().await?;
Ok(())
}
async/await over manual futuresBoxFuture for trait object methodsblock_on in async contextsLazyLock for lazy static initializationuse std::sync::LazyLock;
static MY_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"pattern").expect("invalid regexp")
});
cow_utils::CowUtils to avoid unnecessary allocationsCow<str> over String when possibleuse cow_utils::CowUtils;
let result = str.cow_replace("old", "new");
cargo clippy before committingclippy.toml/// for public API documentation#[doc(hidden)] for internal APIsPascalCase (e.g., RspackOptions)PascalCase (e.g., Compiler)camelCase (e.g., createCompiler)camelCase (e.g., compiler)SCREAMING_SNAKE_CASE or camelCasecamelCase.ts or PascalCase.ts (match main export)File Header (for webpack-derived code):
/**
* Modified from https://github.com/webpack/webpack/blob/4b4ca3b/lib
* MIT Licensed
*/
Imports:
import util from 'node:util';
import type { Callback } from '@rspack/lite-tapable';
import { Compiler } from './Compiler';
Type Definitions:
type for type aliasesinterface for object shapestype for unions/intersectionsexport typethrow new Error() for errorsif (isNil(options.context)) {
throw new Error('options.context is required');
}
async/await over promisesPromise.all for parallel operations// for single-line/* */ for multi-line#[cfg(test)] moduletest_<what>_<condition>_<expected>assert_eq!, assert!, etc.#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_wrap_comment_single_line() {
let result = wrap_comment("test");
assert_eq!(result, "/*! test */");
}
}
describeit or test for casescrates/rspack_plugin_xxx/
├── src/
│ ├── lib.rs
│ └── mod.rs
└── Cargo.toml
packages/xxx/
├── src/
│ ├── index.ts
│ └── Compiler.ts
├── dist/
├── package.json
└── tsconfig.json
Cow<str> when possible&str over String when ownership isn't neededstr::to_lowercase - use CowUtils::cow_to_lowercaseblock_on in async contextsunwrap() in production codeany type (use unknown or proper types)@ts-ignore without good reasoncargo fmtcargo clippy