@applint/spec

中文文档
1.2.3发布于 2022/04/24MITTypeScriptCommonJS近 30 天下载 3628

在 ICE、Rax、React 项目中更简单接入 ESLint(support TypeScript) / Stylelint / Prettier / Commitlint 规则。

原始使用文档
文档目录7 节
  1. 安装
  2. 快速迁移
  3. 使用
  4. ESLint
  5. Stylelint
  6. Prettier
  7. Commitlint

@applint/spec

在 ICE、Rax、React 项目中更简单接入 ESLint(支持 TypeScript) / Stylelint / Prettier / Commitlint 规则,规则与阿里巴巴大淘宝前端规范保持一致。

安装

npm i --save-dev @applint/spec eslint stylelint prettier @commitlint/cli husky

注意: 你不需要安装任何其他 Lint 插件或者插件集,@applint/spec 中已包含这部分依赖。

快速迁移

运行以下命令可一键迁移到 @applint/spec 中:

tnpx @applint/projectlint codemod --transform 'lint-config-to-applint-spec' --fix

使用

ESLint

在项目根目录下创建 .eslintrc.js 文件,并加入以下配置:

// .eslintrc.js
const { getESLintConfig } = require('@applint/spec');

// getESLintConfig(rule: 'common' | 'common-ts' | 'rax' | 'rax-ts' | 'react' | 'react-ts' | 'vue' | 'vue-ts', customConfig?: Linter.Config);
module.exports = getESLintConfig('react', {
  // 自定义配置
  // rules: { 'no-console': 0 }
});

ESLint 规则基于 @applint/eslint-config。

然后在 package.json 中加入脚本:

{
  "scripts": {
+   "eslint": "eslint --ext .js,.jsx,.ts,.tsx ./",
+   "eslint:fix": "npm run eslint -- --fix"
  }
}

在终端运行 npm run eslint 查看项目有哪些 Lint 问题;运行 npm run eslint:fix 会让 ESLint 尝试修复能被自动修复的问题。

更严格的 TypeScript 配置

如果你希望对项目中的 TypeScript 代码进行更严格的约束,可以使用 'rax-ts-strict' | 'react-ts-strict' | 'common-ts-strict' (依据你的具体场景)。

// .eslintrc.js
const { getESLintConfig } = require('@applint/spec');

// getESLintConfig(rule: 'common-ts-strict' | 'rax-ts-strict' | 'react-ts-strict', customConfig?: Linter.Config);
module.exports = getESLintConfig('react-ts-strict', {
  // 自定义配置
  // rules: { 'no-console': 0 }
});

注意:

  • 严格配置的侧重点在于 TypeScript 类型的书写,包括:一致性(如类型断言只允许使用 as、对象的类型声明只允许使用接口等),显性(函数的返回值类型、类成员的可访问性等需要显式注明),语法(使用可选链?.替代逻辑与&&,使用空值合并??替代逻辑或||等),TS 专有能力(使用 import type 导入类型等)。
  • 使用严格配置并不意味着放弃基础配置,严格配置同样包含了基础的 ESLint 规则。
  • 开启前,请请确保你能够接受这一程度的强约束。

Stylelint

在项目根目录下创建 .stylelintrc.js,并加入以下配置:

// .stylelintrc.js
const { getStylelintConfig } = require('@applint/spec');

// getStylelintConfig(rule: 'common' | 'rax' | 'react' | 'vue',  customConfig?: StylelintConfig);
module.exports = getStylelintConfig('react');

Stylelint 规则基于 @applint/stylelint-config。

然后在 package.json 中加入脚本:

{
  "scripts": {
+   "stylelint": "stylelint \"**/*.{css,scss,less}\"",
+   "stylelint:fix": "npm run stylelint -- --fix"
  }
}

在终端运行 npm run stylelint 查看项目有哪些 Lint 问题;运行 npm run stylelint:fix 会让 Stylelint 尝试修复能被自动修复的问题。

Prettier

在项目根目录下创建 .prettierrc.js,并加入以下配置:

// .prettierrc.js
const { getPrettierConfig } = require('@applint/spec');

// getPrettierConfig(rule: 'common' | 'rax' | 'react' | 'vue', customConfig?: PrettierConfig);
module.exports = getPrettierConfig('react');

规则基于 @applint/prettier-config。

然后在 package.json 中加入脚本:

{
  "scripts": {
+   "prettier": "prettier **/* --write"
  }
}

运行 npm run prettier 会使用 Prettier 对代码进行格式化。

配合 ESLint 使用

ESLint 的某些规则可能会跟 Prettier 格式化的结果有冲突,比如 @typescript-eslint/indent 规则。需要做以下的安装和配置来解决此问题:

安装
npm install --save-dev eslint-config-prettier eslint-plugin-prettier
配置
// .eslintrc.js
const { getESLintConfig } = require('@applint/spec');

module.exports = getESLintConfig('react-ts', {
  extends: ['prettier']
});

Commitlint

在项目根目录下创建 .commitlintrc.js,并加入以下配置:

// .commitlintrc.js
const { getCommitlintConfig } = require('@applint/spec');

// getCommitlintConfig(rule: 'common' | 'rax' | 'react' | 'vue', customConfig?: CommitlintUserConfig);
module.exports = getCommitlintConfig('react');

Commitlint 规则基于 @applint/commitlint-config。

Git Hooks

推荐查看 husky 文档了解如何创建 "commit-msg" 和 "pre-commit" 文档。

版本与发布记录

@applint/spec 共发布 32 个版本,首次发布于 2021/10/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.3latest2022/04/244 年前
1.2.3-beta.0beta2022/04/244 年前
1.2.22022/04/214 年前
1.2.2-beta.02022/04/214 年前
1.2.12022/04/194 年前
1.2.02022/03/074 年前

关于 @applint/spec 的常见问题

@applint/spec 是什么?

在 ICE、Rax、React 项目中更简单接入 ESLint(support TypeScript) / Stylelint / Prettier / Commitlint 规则。

如何安装 @applint/spec?

在项目目录运行 npm install @applint/spec,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:eslint(>=6.8.0)、stylelint(>=14.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@applint/spec 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@applint/spec 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@applint/spec 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@applint/spec 可以商用吗?

@applint/spec 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@applint/spec 的下载量有多少?

根据 npm 官方下载统计,@applint/spec 在 2026/10/07 – 2026/11/05 这 30 天内被下载 3,628 次,日均约 121 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@applint/spec 还在维护吗?

最新版本 1.2.3 发布于 2022/04/24(4 年前);至今共发布 32 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/10/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。