This article is a small guide about how to configure an enviroment for babel-plugin-module-resolver. This plugin allows you use custom prefixes for import/export declarations in JS files. E.g. import A from ‘com/A’ can be treated as import A from …/…/components/A.
- At first you should install the package
npm i -D babel-plugin-module-resolver. - Then add your
map-configto your.babelrcfile into thepluginssection. E.g.:plugins: [ // ... [ "babel-plugin-module-resolver", { "alias": { "^com(.+)": "./src/com/\\1" } } ], // ... ]
- Then install
npm i -D eslint-import-resolver-babel-modulepackage forESLint. It allowsESLintto check your rewrited imports, It assumes that you already useeslint-plugin-importpackage. If you don’t I recommend you to start using it. This package checks yourimport/exportdeclarations, it’s very convinient. - Then change in your
.eslintfile:{ "parser": "babel-eslint", "parserOptions": { "ecmaVersion": 6, "sourceType": "module", "ecmaFeatures": { "jsx": true } }, "settings": { "import/resolver": { "babel-module": {} } }, "plugins": [ // ... "import" ], // ... }
- You also need to install
babel-eslintif you didn’t do it yet. And move its declaration fromparseOptionsto the root of the.eslintrc. babel-modulepart in.babelrcsection is just{}, it’s okay.- Install
ESLintplugin invsCode - Probably you’ll need to write in your
.vcode/settings.jsonsomething like next lines. They forcevscodeto changeCWD(current directory) in the deamon ofeslint plugin:"eslint.workingDirectories": [ { "directory": "./client", "changeProcessCWD": true } ],
- If you use
path-intellisenseplugin, you should consider configurate your mapping in.vscode/settings.jsontoo. - If you have any troubles, something doesn’t work properly or doesn’t work at all:
Use force, Luke. Set vscode setting“eslint.trace.server”: “verbose”and debug, debug, debug. I’ve spent on it several hours :(