
Architecture proposals look great in a slide deck when presented to the team. But in reality, often when there are deadlines, pressure, or sometimes maybe just a lack of experience on the team, the architecture can be bypassed and ignored and suddenly you are reviewing a PR where business logic is written directly into the UI components.
On large projects, there should be an opinionated way about how you should structure your code, naming and coding conventions, architectural rules, tech stack …etc.
A successful architecture is one where the team can independently answer the question: "What changed, and where does this change belong?" without needing the architect's presence.
Front-end projects rarely have anything enforcing architecture. There's no compiler stopping you from importing the wrong thing across layers, and code review only catches what the reviewer happens to notice that day.
The last thing you need is being the policeman who manually checks the PRs if the architecture is used correctly. Instead of manually reviewing every PR if the architecture if used corectly, write architecture tests to enforce your front-end architecture.
For front-end, there are several standard packages you can leverage for this:
eslint-plugin-boundaries: This allows you to define your layers and thus enforce rules checking if there are incorrect imports between these boundaries. For example, preventing the Domain layer from importing external libraries.
eslint-plugin-check-file: Used for enforcing file/folder naming conventions. It helps establish a standardized way of naming your components and folders.
Write a high-level architecture document with do/don't code snippets that clarifies exactly how the code should be written.
Install the packages using your preferred package manager:
npm install --save-dev eslint-plugin-boundaries eslint-plugin-check-fileConfigure your esling.config.mjs file. Just remember that these rules will be specific to your application/library and will differ based on your chosen architecture, tech stack, etc.
import boundaries from 'eslint-plugin-boundaries';
import checkFile from 'eslint-plugin-check-file';
import baseConfig from '../../eslint.config.mjs';
import {
boundariesElements,
boundariesDependencies,
} from './lint-rules/architectural-boundaries.js';
import {
fileNamingConventions,
folderNamingConventions
} from './lint-rules/naming-conventions.js'
export default [
...baseConfig,
{
// Match ts and tsx files in my app/lib
files: ['src/**/*.{ts,tsx}'],
// and use the new plugins
plugins: {
boundaries,
'check-file': checkFile,
},
settings: {
// Use node in import/resolver to be able to load the files
// Or you can provide your custom resolver
'import/resolver': {
node: { extensions: ['.js', '.ts', '.tsx'] },
},
// Use the layers that we defined
'boundaries/elements': boundariesElements,
// =====================================================================
// FILE/FOLDER NAMING CONVENTION - kebab-case everywhere
// =====================================================================
'check-file/filename-naming-convention': fileNamingConventions,
'check-file/folder-naming-convention': folderNamingConventions
},
rules: {
// Report the rules as errors
'boundaries/dependencies': ['error', boundariesDependencies],
},
},
{
ignores: ['**/out-tsc'],
},
];Here is how does the file/folder naming conventions could look like:
export const fileNamingConventions = [
'error',
{ '**/*.{ts,tsx}': 'KEBAB_CASE' },
{ ignoreMiddleExtensions: true }, // allows product.entity.ts, product.repository.ts, etc.
];
export const folderNamingConventions = [
'error',
{
'**/': 'KEBAB_CASE',
},
{
ignoreWords: ['__mocks__'],
},
];How to define your architectural boundaries
First, define your layers (elements). The easiest way is to reflect your existing folder structure. The folder structure I am using here as an example, comes from the clean architecture course that I am currently working on, these rules are derived from it. I have a core library where I keep the domain and application layers.
Here is an example how the elements (layers/groups) look like:
/** @type {import('eslint-plugin-boundaries').ElementDescriptor[]} */
export const boundariesElements = [
{
type: 'domain-business-rules',
pattern: 'src/domain/*/business-rules',
capture: ['entity'],
},
// The order matters, it must be defined before the entity pattern
// or otherwise it will be captured as an entity
{
type: 'domain-shared-kernel',
pattern: 'src/domain/shared',
},
{
type: 'domain',
pattern: 'src/domain/*',
capture: ['entity'],
},
// other elements
]Then you can define your rules:
export const boundariesDependencies = {
default: 'disallow',
checkAllOrigins: true,
message: `Cross-domain violation: "{{from.type}}" in domain "{{from.captured.entity}}" must not depend on "{{to.type}}" in domain "{{to.captured.entity}}". See docs/architecture.md.`,
policies: [
// entities/index.ts: any other entity (cross-feature ok), local shared.ts (same feature), global kernel
{
from: { element: { type: 'domain' } },
allow: { to: { element: { type: ['domain', 'domain-shared-kernel'] } } },
},
// allow imports from local shared.ts files
{
from: { type: 'domain' },
allow: {
to: { type: 'domain-local-shared' },
},
},
// business-rules: it's own entity + shared
{
from: { element: { type: ['domain-business-rules'] } },
allow: {
to: {
element: {
type: 'domain',
captured: { entity: '{{ from.element.captured.entity }}' },
},
},
},
},
{
from: { element: { type: 'domain-business-rules' } },
allow: { to: { element: { type: 'domain-shared-kernel' } } },
},
// rest of the rules
]
}Conclusion
The setup takes a bit of upfront work, but it pays for itself fast: no more being a bottleneck for every code review, and a much clearer answer to "what changed, and where does this belong?" — because now, the questions can be answered without someone having to ask you and the architecture belongs to the team.
Even with this in place, you can automate it and check the linting rules on your CI/CD pipeline or in your IDE.