frontend/config/stylelint-config/README.md
A comprehensive and opinionated stylelint configuration package designed for Coze's frontend projects. This package provides a standardized set of CSS/Less linting rules that enforce consistent code style, maintainability, and best practices across the codebase.
stylelint-config-standard, stylelint-config-standard-less, and stylelint-config-clean-order$block-$element_$modifier naming pattern for CSS classes:global selectors and enforce coding standardsAdd the package to your project:
# In your project's package.json
{
"devDependencies": {
"@coze-arch/stylelint-config": "workspace:*"
}
}
Then run:
rush update
Create a .stylelintrc.js file in your project root:
const { defineConfig } = require('@coze-arch/stylelint-config');
module.exports = defineConfig({
extends: [],
rules: {
// Add your custom rules here
}
});
Or use the configuration directly:
module.exports = {
extends: ['@coze-arch/stylelint-config']
};
defineConfig(config)The main configuration function that extends the base stylelint configuration.
Parameters:
config (Config): Stylelint configuration objectReturns:
Config: Extended stylelint configurationExample:
const { defineConfig } = require('@coze-arch/stylelint-config');
module.exports = defineConfig({
extends: ['stylelint-config-recommended-scss'],
rules: {
'color-hex-length': 'long',
'declaration-block-trailing-semicolon': 'always'
},
ignoreFiles: ['dist/**/*']
});
The configuration enforces BEM-style class naming with the pattern: $block-$element_$modifier
/* ✅ Good */
.button { }
.button-large { }
.button-large_disabled { }
.nav-item { }
.nav-item_active { }
/* ❌ Bad */
.Button { }
.button_large_disabled { }
.nav-item-active-disabled { }
.camelCaseClass { }
Maximum nesting depth is limited to 3 levels (excluding pseudo-classes):
/* ✅ Good */
.component {
.header {
.title {
color: blue;
}
}
}
/* ❌ Bad */
.component {
.header {
.title {
.text {
.span { // Too deep!
color: blue;
}
}
}
}
}
First-level :global selectors are disallowed:
/* ❌ Bad */
:global {
.some-class {
color: red;
}
}
/* ✅ Good */
.component {
:global {
.some-class {
color: red;
}
}
}
The use of !important is prohibited:
/* ❌ Bad */
.class {
color: red !important;
}
/* ✅ Good */
.class {
color: red;
}
// Good example following all rules
.card {
padding: 16px;
border: 1px solid #ccc;
border-radius: 4px;
&-header {
margin-bottom: 12px;
font-weight: bold;
&_featured {
background-color: #f0f0f0;
}
}
&-content {
line-height: 1.5;
}
}
.custom-component {
@apply flex items-center justify-between;
&-item {
@apply px-4 py-2 rounded;
&_active {
@apply bg-blue-500 text-white;
}
}
}
Test the configuration against example files:
rush stylelint-config example
This will run stylelint against the files in the examples/ directory.
The package includes a custom plugin located at plugins/plugin-disallow-nesting-level-one-global.js that prevents first-level :global selectors.
This package has no runtime dependencies.
Apache-2.0 License
For more information about stylelint configuration options, visit the official stylelint documentation.