$ cnpm install 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.
Prerequisites: Node.js >=4.x and corresponding version of npm.
npm install eslint-plugin-import eslint-import-resolver-alias --save-dev
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: {
map: [
['babel-polyfill', 'babel-polyfill/dist/polyfill.min.js'],
['helper', './utils/helper'],
['material-ui/DatePicker', '../custom/DatePicker'],
['material-ui', 'material-ui-ie10']
],
extensions: ['.ts', '.js', '.jsx', '.json']
}
}
}
};
Note:
map and extensions, both of which are array typesmap array is also array type which contains 2 string
map item ['helper', './utils/helper'] means that the modules which match helper or helper/* will be resolved to ./utils/helper or ./utils/helper/* which are located relative to the process current working directory (almost the project root directory). If you just want to resolve helper to ./utils/helper, use ['^helper$', './utils/helper'] instead. See issue #3extensions property is ['.js', '.json', '.node'] if it is assigned to an empty array or not specifiedIf the extensions property is not specified, the config object can be simplified to the map array.
// .eslintrc.js
module.exports = {
settings: {
'import/resolver': {
alias: [
['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
}
}
};
Copyright 2013 - present © cnpmjs.org | Home |