Next.js 增加eslint 代码规范约束

11 min read

Next.js 增加eslint 代码规范约束

next lint

动行完命令后,会自动在项目根目录下生成eslint配置文件

默认情况下,Next.js 将为 src 和 目录中的所有pages/文件components/运行lib/ESLint src/。但是,您可以使用配置中的dirs选项指定哪些目录用于生产构建:eslint``next.config.js

module.exports = {
  eslint: {
    dirs: ['pages', 'utils'], // Only run ESLint on the 'pages' and 'utils' directories during production builds (next build)
  },
}

类似地,--dirand--file标志可用于对next lint特定目录和文件进行 lint:

next lint --dir pages --dir utils --file bar.js

缓存

为了提高性能,ESLint 处理的文件信息默认被缓存。这存储在.next/cache您定义的构建目录中。如果您包含任何依赖于多个源文件内容的 ESLint 规则并且需要禁用缓存,请使用--no-cache带有next lint.

next lint --no-cache

关闭规则

//.eslintrc
{
  "extends": "next",
  "rules": {
    "react/no-unescaped-entities": "off",
    "@next/next/no-page-custom-font": "off"
  }
}