An ESLint plugin to enforce rules related to absolute and relative path usage, public API imports in projects.
You'll first need to install ESLint:
npm i eslint --save-dev
Next, install eslint-plugin-production-project-plugin
:
npm install eslint-plugin-production-project-plugin --save-dev
Add production-project-plugin
to the plugins section of your .eslintrc
configuration file. You can omit the eslint-plugin-
prefix:
{
"plugins": [
"production-project-plugin"
]
}
Then configure the rules you want to use under the rules section.
{
"rules": {
"production-project-plugin/rule-name": ["error",{}]
}
}
This plugin does not offer any predefined configurations.
You will need to manually configure the rules you want to apply in your .eslintrc
file.
Example configuration:
{
"rules": {
"production-project-plugin/path-checker": ["error", { "alias": "@" }],
"production-project-plugin/layer-imports": [
"error",
{
"alias": "@",
"ignoreImportPatterns": ["**/StoreProvider", "**/testing"]
}
],
"production-project-plugin/public-api-imports": [
"error",
{
"alias": "@",
"testFilesPatterns": [
"**/*.test.*",
"**/*.testing.ts",
"**/*.story.*",
"**/*.stories.tsx",
"**/StoreDecorator.tsx"
]
}
]
}
}
Type: problem
This rule enforces strict import rules between project layers. It ensures that only valid imports from underlying layers are allowed, based on a defined architecture.
Incorrect:
The layer-imports rule ensures that files from specific layers (e.g., app
, entities
, features
, widgets
) only import from underlying or shared layers according to your project’s structure.
For example, a features
file can import from shared
or entities
but not from widgets
or pages
.
Type: problem This rule enforces that within the same "slice" of the project, all imports must use relative paths.
The path-checker
rule ensures that all paths within a single slice (e.g., entities/Article
) are relative.
This prevents the use of absolute imports within the same module or feature slice.
If an absolute path is detected within the same slice, the rule can automatically fix it by replacing it with a relative path.
Type: problem
This rule enforces that absolute imports are only allowed from the Public API (index.ts
) of each module, and test-related imports should be sourced from publicApi/testing.ts
.
- Absolute imports from a module should only come from its Public API (
index.ts
). If a deeper file is imported directly, this rule will flag an error. - Test-related data or utilities must be imported from the
testing
file within the module's public API (e.g.,publicApi/testing.ts
), and only test files (e.g., files matching patterns like**/*.test.*
) are allowed to import from this location.
The rule will automatically fix violations by replacing the import path with the appropriate public API path if possible
-
layer-imports
: Helps enforce a well-defined project architecture by restricting imports between layers, improving project scalability and maintainability. -
path-checker
: Ensures consistency by enforcing relative paths within the same slice, making code more readable and modular. -
public-api-imports
: Promotes encapsulation by restricting access to internal files and enforcing imports from the public API, while also ensuring that test utilities are used appropriately.