Section 8 of 17
Enforcing the rules
Contents
A rule that is not checked is a suggestion. Nx checks it with tags and one lint rule.
8.1. Tags
Every project.json gets one domain:* and one type:* tag, and in a mixed workspace one framework:* tag.
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"]
}
Generators set the tags when the library is created:
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. The lint rule
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'] },
],
},
],
},
},
];
How the dimensions combine: an import must satisfy every constraint of every tag of the importing library. A maintenance feature may import fleet/api because its type rule allows type:api and its domain rule allows type:api. It may not import fleet/data-access: the type rule would allow data-access, but the domain rule does not allow domain:fleet. A maintenance ui library may not import fleet/api either: the domain rule would allow it, but the type rule does not.
The same rule also rejects relative imports into another library, imports of a library's internals instead of its index.ts, and circular dependencies between libraries.
8.3. The framework dimension
Fleetline's customer application could move to Next.js and backoffice to Nuxt without moving a single model or util library. Those are tagged framework:agnostic and may import only other agnostic code, so matchesFilter and isOverdue are the same function in all three apps. A React library that tries to import an Angular ui library fails lint.
8.4. Without Nx: Sheriff
Not every team runs Nx. Sheriff, from the same ANGULARarchitects team, enforces the same tag rules inside a single TypeScript project, by folder instead of by library. It suits a Next.js or Nuxt application that wants the boundaries before it wants a monorepo.
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. In CI
nx affected -t lint typecheck test build --base=origin/main
nx graph --affected
Because the dependency graph is explicit, a change in maintenance/ui-checklist lints, tests and builds the libraries and applications that depend on it, and nothing else. In Fleetline a typical pull request touches one domain and runs a fifth of the workspace. With remote caching, an unchanged library is never rebuilt on any machine.