eslint-import-resolver-alias-array v1.2.2
eslint-import-resolver-alias
This is a simple Node.js module import resolution plugin for eslint-plugin-import, which supports native Node.js module resolution, module alias/mapping and custom file extensions.
NOTE: This package is an update of the original, modified to allow for arrays of aliases. At this time, it is provided as a convenience only, until the original maintainer is able to return and address the PR containing this code. Any issues or bug reports not explicitly related to the alias array modifications are better addressed against the original repository.
Installation
Prerequisites: Node.js >=4.x and corresponding version of npm.
npm install eslint-plugin-import eslint-import-resolver-alias-array --save-devUsage
Pass this resolver and its parameters to eslint-plugin-import using your eslint config file, .eslintrc or .eslintrc.js.
// .eslintrc.js
module.exports = {
settings: {
'import/resolver': {
'alias-array': {
map: [
['babel-polyfill', 'babel-polyfill/dist/polyfill.min.js'],
['helper', './utils/helper'],
['material-ui/DatePicker', '../custom/DatePicker'],
['material-ui', 'material-ui-ie10'],
['lib', ['app/lib', 'common/lib']]
],
extensions: ['.ts', '.js', '.jsx', '.json']
}
}
}
};Note:
- The alias config object contains two properties,
mapandextensions, both of which are array types - The item of
maparray is also array type which contains 2 string- The first string represents the alias of module name or path
- The second string represents the actual module name or path
- The second string may also be an array that is evaluated in order, with the first found used as resolved
- The
mapitem['helper', './utils/helper']means that the modules which matchhelperorhelper/*will be resolved to./utils/helperor./utils/helper/*which are located relative to theprocess current working directory(almost the project root directory). If you just want to resolvehelperto./utils/helper, use['^helper$', './utils/helper']instead. See issue #3 - The order of 'material-ui/DatePicker' and 'material-ui' cannot be reversed, otherwise the alias rule 'material-ui/DatePicker' does not work
- The default value of
extensionsproperty is['.js', '.json', '.node']if it is assigned to an empty array or not specified
If the extensions property is not specified, the config object can be simplified to the map array.
// .eslintrc.js
module.exports = {
settings: {
'import/resolver': {
'alias-array': [
['babel-polyfill', 'babel-polyfill/dist/polyfill.min.js'],
['helper', './utils/helper'],
['material-ui/DatePicker', '../custom/DatePicker'],
['material-ui', 'material-ui-ie10']
]
}
}
};When the config is not a valid object (such as true), the resolver falls back to native Node.js module resolution.
// .eslintrc.js
module.exports = {
settings: {
'import/resolver': {
alias: true
}
}
};CHANGELOG
References
- eslint-plugin-import/no-extraneous-dependencies
- eslint-plugin-import/no-unresolved
- eslint-module-utils/resolve
- resolve
- eslint-import-resolver-node