Barrel files are files that re-export the APIs of other files in the same directory, which is usually considered a bad design, refer to vitejs/vite#8237.
ESLint provides the no-restricted-imports
rule, but full replacement existing uses is almost impossible for large projects. This rule supports replacing references to specified barrel files with their submodules.
This rule is fixable.
This rule has an object option:
"files"
(default[]
) disallows specified files.- Unlike
paths
ofno-restricted-imports
,files
specifies absolute paths of the module files - You can obtain the paths in the configuration through
require.resolve
and other methods - If you want to make similar effects to
patterns
ofno-restricted-imports
, you can obtain these files in the configuration through something likefastglob.sync(patterns, { absolute: true })
- Unlike
Suppose we have the following files
// /path/to/barrel-exports export * from './barrel-exports-deep'// /path/to/barrel-exports-deep export const foo = 1
/* eslint galaxy/no-restricted-barrel-imports: ["error", { "files": ["/path/to/barrel-exports"] }] */
import { foo } from '/path/to/barrel-exports'
Suppose we have the following files
// /path/to/barrel-exports export * from './barrel-exports-deep'// /path/to/barrel-exports-deep export const foo = 1
/* eslint galaxy/no-restricted-barrel-imports: ["error", { "files": ["/path/to/barrel-exports"] }] */
import { foo } from '/path/to/barrel-exports-deep'