Allows you to enforce that aliased modules are not using relative paths.
Given the babel configuration below
...
"plugins": [
[
"module-resolver",
{
"root": ["."],
"alias": {
"action": "./actions"
}
}
]
]
...
The following patterns are considered warnings:
import fetchData from '../../actions/fetchData'
const fetchData = require('../../actions/fetchData')
const fetchData = await import('../../actions/fetchData')
The following patterns are not considered warnings:
import fetchData from 'actions/fetchData'
const fetchData = require('actions/fetchData')
const fetchData = await import('actions/fetchData')
...
"module-resolver/use-alias": [<enabled>, {
"ignoreDepth": <number>,
"projectRoot": <string>,
"extensions": <array>,
"chainedExtensions": <array>,
"allowDepthMoreOrLessThanEquality": <boolean>,
"alias": <object>
}]
...
Number representing a depth that can be ignored for aliased imports. Performs strict equality by default. If you want to allow depth of exact or lesser number, check allowDepthMoreOrLessThanEquality
. By default this option is unused.
With the below ignoreDepth
set, all of the above patterns causing warnings would no longer. The other cases would continue being valid as well.
"module-resolver/use-alias": ["error", {
"ignoreDepth": 2
}]
Boolean allowDepthMoreOrLessThanEquality
sets the equality rule for ignoreDepth
. By default this option is unused (strict equality is performed).
With the below allowDepthMoreOrLessThanEquality
set to true
, all the pattern with depth equal or less than the ignoreDepth
will be allowed.
"module-resolver/use-alias": ["error", {
"allowDepthMoreOrLessThanEquality": false
}]
String representing a path from your workspace directory to your projectRoot directory where the .babelrc
and .eslintrc
files exist. By default this option is unused.
This is useful in a monorepo or projects that have these files nested in a subdirectory (e.g. <rootDir>/package/project/.eslintrc
).
"module-resolver/use-alias": ["error", {
"projectRoot": "/package/project"
}]
Array of additional extensions to look for when linting. By default, files without extensions are considered to be .js
. This expands the resolution for extensionless imports. Possible values are .ts
, .tsx
, and .jsx
.
With the below extensions
array, TypeScript files will also be resolved.
"module-resolver/use-alias": ["error", {
"extensions": [".ts"]
}]
Array of extensions to check for chaining. For example, React projects use .jss.js
files to generate CSS styles for components. By default, files with extensions are considered to have only those extensions, while files without default to .js
(and anything included in extensions
as described above. This expands the resolution for imports with chained extensions. Any string value is supported.
With the below chainedExtensions
array, JSS files will also be resolved.
"module-resolver/use-alias": ["error", {
"chainedExtensions": [".jss"]
}]
Object that represents projects alias mappings. This is used to pass the alias directly in and prevent searching for it within a .babelrc
file. By default, this is unused and the alias is looked for within a .babelrc
file. See #172 for motivation.
The alias object below will be used even if the project has a .babelrc
file.
"module-resolver/use-alias": ["error", {
"alias": {
"root": ["."],
"alias": {
"#foo": "./src/foo",
"#bar": "./bar",
}
}
}]