-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce module boundaries with no-restricted-imports
- Loading branch information
1 parent
5fa98ac
commit fd7f697
Showing
8 changed files
with
80 additions
and
36 deletions.
There are no files selected for viewing
Binary file added
BIN
+1.19 MB
bits/enforce-module-boundaries-with-no-restricted-imports/images/banner.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+306 KB
bits/enforce-module-boundaries-with-no-restricted-imports/images/banner.webp
Binary file not shown.
37 changes: 37 additions & 0 deletions
37
bits/enforce-module-boundaries-with-no-restricted-imports/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: Enforce module boundaries with no-restricted-imports | ||
slug: enforce-module-boundaries-with-no-restricted-imports | ||
date: 2024-05-21 | ||
tags: typescript, developer-experience, architecture | ||
--- | ||
|
||
# Enforce module boundaries with no-restricted-imports | ||
|
||
Define module boundaries in your project by restricting specific imports using ESLint's `no-restricted-imports` rule. | ||
The rule allows you to prevent certain modules from being imported in specific files or directories. | ||
|
||
From an architectural point of view this is useful to enforce module boundaries e.g. to prevent feature A to be imported within feature B, or prevent circular dependencies between modules. | ||
|
||
There are also some alternative tools that allow you to define module boundaries such as [Sherrif](https://github.com/softarc-consulting/sheriff) or [Nx](https://nx.dev/features/enforce-module-boundaries). | ||
If you're already using these tools, I think it's better to stick with them as they provide more flexibility and features. | ||
However, I find the ESLint approach to be straightforward and the easiest to configure, and it doesn't require an additional dependency. | ||
|
||
For more information about the ESLint rule, refer to the [official ESLint documentation](https://eslint.org/docs/rules/no-restricted-imports), or the TypeScript-variant at the [TypeScript ESLint documentation](https://typescript-eslint.io/rules/no-restricted-imports/). If you're using Biome, see the [Biome documentation](https://biomejs.dev/linter/rules/no-restricted-imports/). | ||
|
||
```js{3-13}:.eslintrc.js | ||
module.exports = { | ||
rules: { | ||
'no-restricted-imports': [ | ||
'error', | ||
{ | ||
patterns: [ | ||
{ | ||
group: ['@feature-a'], | ||
message: 'You cannot import feature A from this module.', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters