2.0.9-1 • Published 7 years ago
eslint-import-resolver-localalias v2.0.9-1
eslint-import-resolver-localalias
This is a simple Node.js module import resolution plugin for eslint-plugin-import, which supports native Node.js module resolution, module alias and custom file extensions. Also supports aliases for project modules.
Installation
Prerequisites: Node.js >=6.x and corresponding version of npm.
npm install eslint-plugin-import eslint-import-resolver-localalias --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': {
localalias: {
map: [
['babel-polyfill', 'babel-polyfill/dist/polyfill.min.js'],
['helper', './utils/helper'],
['material-ui/DatePicker', '../custom/DatePicker'],
['material-ui', 'material-ui-ie10'],
['@project-module', 'module/path/'],
['project-module2', 'module2']
],
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 mapped module name or path
- The second string represents the module alias, the actual module path or name
- The
mapitem['helper', './utils/helper']means that the modulehelper/*will be resolved to./utils/helper/*. See #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': {
localalias: [
['babel-polyfill', 'babel-polyfill/dist/polyfill.min.js'],
['helper', './utils/helper'],
['material-ui/DatePicker', '../custom/DatePicker'],
['material-ui', 'material-ui-ie10'],
['@project-module', 'module/path/'],
['project-module2', 'module2']
]
}
}
};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
}
}
};