第 8 节,共 17 节

执行规则

目录

没有被检查的规则只是建议。Nx 用标签和一条 lint 规则来检查它。

8.1. 标签

每个 project.json 都有一个 domain:* 标签和一个 type:* 标签;在混合工作区中,还有一个 framework:* 标签。

libs/maintenance/data-access/project.json

{
  "name": "maintenance-data-access",
  "sourceRoot": "libs/maintenance/data-access/src",
  "projectType": "library",
  "tags": ["domain:maintenance", "type:data-access", "framework:angular"]
}

libs/maintenance/model/project.json

{
  "name": "maintenance-model",
  "sourceRoot": "libs/maintenance/model/src",
  "projectType": "library",
  "tags": ["domain:maintenance", "type:model", "framework:agnostic"]
}

生成器在创建库时设置标签:

nx g @nx/angular:library --directory=libs/maintenance/feature-planning-board \
  --tags=domain:maintenance,type:feature,framework:angular
nx g @nx/js:library --directory=libs/maintenance/model \
  --tags=domain:maintenance,type:model,framework:agnostic --bundler=none

8.2. lint 规则

eslint.config.mjs

import nx from '@nx/eslint-plugin';

const domains = [
  'maintenance', 'fleet', 'inventory', 'booking',
  'billing', 'customers', 'identity', 'notifications',
];

const types = {
  app: ['shell', 'feature', 'ui', 'data-access', 'model', 'util'],
  shell: ['feature', 'ui', 'data-access', 'model', 'util', 'api'],
  feature: ['ui', 'data-access', 'model', 'util', 'api'],
  api: ['feature', 'ui', 'data-access', 'model', 'util'],
  ui: ['ui', 'model', 'util'],
  'data-access': ['data-access', 'model', 'util'],
  model: ['model', 'util'],
  util: ['util'],
};

export default [
  ...nx.configs['flat/base'],
  ...nx.configs['flat/typescript'],
  ...nx.configs['flat/javascript'],
  {
    files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.vue'],
    rules: {
      '@nx/enforce-module-boundaries': [
        'error',
        {
          enforceBuildableLibDependency: true,
          banTransitiveDependencies: true,
          depConstraints: [
            ...Object.entries(types).map(([type, allowed]) => ({
              sourceTag: `type:${type}`,
              onlyDependOnLibsWithTags: allowed.map((t) => `type:${t}`),
            })),
            { sourceTag: 'domain:shared', onlyDependOnLibsWithTags: ['domain:shared'] },
            ...domains.map((domain) => ({
              sourceTag: `domain:${domain}`,
              onlyDependOnLibsWithTags: [`domain:${domain}`, 'domain:shared', 'type:api'],
            })),
            { sourceTag: 'framework:agnostic', onlyDependOnLibsWithTags: ['framework:agnostic'] },
            { sourceTag: 'framework:angular', onlyDependOnLibsWithTags: ['framework:angular', 'framework:agnostic'] },
            { sourceTag: 'framework:react', onlyDependOnLibsWithTags: ['framework:react', 'framework:agnostic'] },
            { sourceTag: 'framework:vue', onlyDependOnLibsWithTags: ['framework:vue', 'framework:agnostic'] },
            { sourceTag: 'platform:client', notDependOnLibsWithTags: ['platform:server'] },
          ],
        },
      ],
    },
  },
];

各维度如何组合:一次导入必须满足导入方库每个标签的每一条约束。maintenance 的 feature 可以导入 fleet/api,因为它的类型规则允许 type:api,它的领域规则也允许 type:api。它不能导入 fleet/data-access:类型规则允许 data-access,但领域规则不允许 domain:fleet。maintenance 的 ui 库也不能导入 fleet/api:领域规则允许,但类型规则不允许。

同一条规则还会拒绝指向另一个库的相对导入、绕过 index.ts 对库内部的导入,以及库之间的循环依赖。

8.3. 框架维度

Fleetline 的 customer 应用可以迁移到 Next.js,backoffice 可以迁移到 Nuxt,而无需移动任何一个 model 或 util 库。这些库带有 framework:agnostic 标签,只能导入其他与框架无关的代码,因此 matchesFilter 和 isOverdue 在三个应用中是同一个函数。试图导入 Angular ui 库的 React 库会在 lint 时失败。

8.4. 不用 Nx:Sheriff

并不是每个团队都使用 Nx。Sheriff 出自同一个 ANGULARarchitects 团队,它在单个 TypeScript 项目内执行相同的标签规则,以文件夹而不是库为单位。它适合那些想先有边界、再考虑 monorepo 的 Next.js 或 Nuxt 应用。

sheriff.config.ts

import { sameTag, type SheriffConfig } from '@softarc/sheriff-core';

export const config: SheriffConfig = {
  modules: {
    'src/domains/<domain>': {
      'feature-<name>': ['domain:<domain>', 'type:feature'],
      'ui-<name>': ['domain:<domain>', 'type:ui'],
      'data-access': ['domain:<domain>', 'type:data-access'],
      model: ['domain:<domain>', 'type:model'],
      api: ['domain:<domain>', 'type:api'],
    },
    'src/shared/ui-<name>': ['domain:shared', 'type:ui'],
    'src/shared/util-<name>': ['domain:shared', 'type:util'],
  },
  depRules: {
    root: ['type:feature', 'type:ui', 'type:data-access', 'type:model', 'type:util', 'domain:shared'],
    'domain:*': [sameTag, 'domain:shared', 'type:api'],
    'type:feature': ['type:ui', 'type:data-access', 'type:model', 'type:util', 'type:api'],
    'type:api': ['type:feature', 'type:ui', 'type:data-access', 'type:model', 'type:util'],
    'type:ui': ['type:ui', 'type:model', 'type:util'],
    'type:data-access': ['type:data-access', 'type:model', 'type:util'],
    'type:model': ['type:model', 'type:util'],
    'type:util': ['type:util'],
  },
};

8.5. 在 CI 中

nx affected -t lint typecheck test build --base=origin/main
nx graph --affected

因为依赖图是显式的,修改 maintenance/ui-checklist 时,只会对依赖它的库和应用执行 lint、测试和构建,不涉及其他任何东西。在 Fleetline 中,一个典型的 pull request 只涉及一个领域,运行工作区的五分之一。有了远程缓存,未改动的库在任何机器上都不会被重新构建。