如何在不 eject 脚手架的前提下,为 Vue CLI 创建的项目追加 Stylelint 配置?

解读

面试官真正想考察的是:

  1. 你是否理解 Vue CLI 的“零配置”哲学,以及它如何通过插件化机制隐藏 Webpack 细节;
  2. 你是否熟悉 Stylelint 在工程化链路中的接入点(依赖、配置、脚本、编辑器、CI);
  3. 你能否在“不改 vue.config.js 也不 eject”的硬约束下,给出一条对存量代码零侵入、对后续协作可复制的标准路径;
  4. 你是否具备“把规范落地到团队”的经验,例如自动修复、增量校验、git 卡点、IDE 实时反馈等。

一句话:不是“配出来”,而是“配得稳、配得巧、配得让团队愿意用”。

知识点

  1. Vue CLI 插件机制:@vue/cli-service 内部 webpack 配置由 chainWebpack 串联,外部可通过 vue.config.js 的 lintOnSave 字段控制 linter 执行时机,但默认只对接 ESLint。
  2. Stylelint 核心包:stylelint、stylelint-config-standard、stylelint-config-recommended-vue(Vue 单文件专用)、stylelint-order、stylelint-scss 等。
  3. PostCSS 与 Stylelint 关系:Stylelint 作为 PostCSS 插件运行,Vue CLI 已内置 postcss-loader,因此无需再引一次,只需保证配置在 .stylelintrc.js 中能被 postcss-loader 读取。
  4. 三种触发方式:
    • 开发阶段:webpack 插件 stylelint-webpack-plugin(vue.config.js 中 chainWebpack 追加);
    • 命令行:独立脚本 “stylelint "src/**/*.{vue,css,scss}" --fix”;
    • Git 提交:lint-staged + husky(pre-commit 钩子)。
  5. 配置优先级:.stylelintrc.js > stylelint.config.js > package.json 的 stylelint 字段;Vue 单文件内 <style> 标签的 lang="scss" 需要 overrides 单独指定 syntax。
  6. 性能与体验:
    • 开启 cache:true(生成 .stylelintcache)二次校验提速 70%;
    • 使用 quiet 参数屏蔽警告,只让错误 fail CI;
    • 与 VS Code 插件 stylelint-plus 结合,保存即自动修复,减少命令行切换。
  7. 国内镜像与合规:淘宝源已下线,需配置 registry.npmmirror.com;若公司内网有 Artifactory,需同步 stylelint-config-xxx 私有包并锁定版本,避免上游规则突变。

答案

步骤一:安装依赖(Vue CLI 项目根目录执行,无需 eject)
npm i -D stylelint stylelint-config-standard stylelint-config-recommended-vue stylelint-order postcss-html

步骤二:新建 .stylelintrc.js
module.exports = { extends: [ 'stylelint-config-standard', 'stylelint-config-recommended-vue' ], plugins: ['stylelint-order'], rules: { 'order/properties-order': [ 'position', 'top', 'right', 'bottom', 'left', 'z-index', // … 其余顺序 ], 'selector-class-pattern': '^[a-z][a-zA-Z0-9]$', // 强制小驼峰 'color-hex-length': 'short' }, overrides: [ { files: ['.vue', '**/*.vue'], customSyntax: 'postcss-html' } ] }

步骤三:在 vue.config.js 中追加 webpack 插件(不 eject,仅 chain)
const StylelintPlugin = require('stylelint-webpack-plugin') module.exports = { lintOnSave: false, // 关闭 ESLint 校验,避免混淆 chainWebpack: config => { config .plugin('stylelint') .use(StylelintPlugin, [{ files: ['src/**/*.{vue,html,css,scss}'], cache: true, cacheLocation: 'node_modules/.cache/stylelint.json', emitErrors: true, failOnError: false // 开发环境不阻断,CI 里再开 true }]) } }

步骤四:package.json 补充脚本
"scripts": { "lint:style": "stylelint "src//*.{vue,css,scss}" --cache --max-warnings 0", "lint:style:fix": "stylelint "src//*.{vue,css,scss}" --fix" }

步骤五:Git 提交卡点(lint-staged + husky,国内团队常用)
npm i -D husky lint-staged
package.json 追加:
"lint-staged": { "*.{vue,css,scss}": "stylelint --fix" }, "husky": { "hooks": { "pre-commit": "lint-staged" } }

步骤六:IDE 实时反馈
VS Code 安装插件 stylelint,setting.json 增加:
"stylelint.validate": ["css", "scss", "vue"], "editor.codeActionsOnSave": { "source.fixAll.stylelint": true }

至此,不 eject 的前提下,Stylelint 已完整融入 Vue CLI 工程:开发阶段保存即修复,命令行可批量校验,Git 提交自动拦截,CI 流水线可执行 npm run lint:style 作为质量门禁。

拓展思考

  1. 微前端场景:子应用独立仓库,如何在主应用 CI 阶段聚合所有子应用的 stylelint 报告?
    思路:主流水线拉取各子仓库后,统一执行 npx lerna run lint:style --stream,再把 json 格式报告上传到 SonarQube 的 generic issue 接口,实现“一处看全站样式债务”。

  2. 原子化 CSS(Tailwind、UnoCSS)冲击:
    传统规则如 “declaration-empty-line-before” 对 @apply 块不友好,可 overrides 关闭;同时新增插件 stylelint-use-logical-spec 强制使用逻辑属性(margin-inline-start),避免 RTL 适配返工。

  3. 性能再优化:
    项目大、样式文件过万时,stylelint-webpack-plugin 即使开了 cache 仍拖慢 dev 启动。可改在 vite 迁移阶段用 vite-plugin-stylelint,或在 webpack 侧用 thread-loader 把 lint 线程隔离,保证热更新 < 500 ms。

  4. 规范升级策略:
    团队初始规则较松,后续想收紧(如强制 color-function-notation: modern)。采用“渐进式灰度”:

    • 新增规则默认 warn,配合 “max-warnings 阈值” 让 CI 暂时通过;
    • 每周降阈值,配合 eslint-staged 自动 fix,四周后切 error,实现“债务零堆积”。
  5. 国内合规小坑:
    有些政企项目需离线开发,node_modules 预打包后上传。stylelint 依赖的 postcss-html 内部又依赖 syntax-parser,需手动把二级依赖写进 package.json 的 bundledDependencies,否则内网私服会丢包。