Created
October 20, 2017 19:42
-
-
Save jeremija/046f9df54000f4059af3ec987d2dfb5f to your computer and use it in GitHub Desktop.
Tern plugin to work with babel-plugin-module-resolver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"libs": [], | |
"loadEagerly": [], | |
"plugins": { | |
"node": {}, | |
"es_modules": {}, | |
"module-resolver": { | |
"cwd": "packagejson", | |
"alias": { | |
"my-alias1": "./path/to/my/module1", | |
"my-alias2": "./path/to/my/module2" | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```javascript | |
// This is a plugin for Tern which helps it resolve aliased modules when using | |
// babel-plugin-module-resolver. | |
const path = require('path') | |
const resolve = require('babel-plugin-module-resolver/lib/resolvePath').default | |
function initialize (ternDir) { | |
const tern = require(ternDir) | |
tern.registerPlugin('module-resolver', function (server, options) { | |
// do not use "cwd" if set because tern already expects an path relative | |
// .tern-config.js, which should be in the project root. | |
const opts = { | |
alias: options.alias | |
} | |
server.mod.modules.resolvers.push(function _resolve (name, parentFile) { | |
let str = resolve(name, parentFile, opts) | |
if (str) { | |
str = path.normalize(path.join(path.dirname(parentFile), str)) | |
} | |
return str | |
}) | |
}) | |
} | |
module.exports = { initialize } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The only thing left is to figure out how to use the same configuration file as babel so there is no need for duplicate definitions of aliases.