Skip to content

Instantly share code, notes, and snippets.

@jeremija
Created October 20, 2017 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremija/046f9df54000f4059af3ec987d2dfb5f to your computer and use it in GitHub Desktop.
Save jeremija/046f9df54000f4059af3ec987d2dfb5f to your computer and use it in GitHub Desktop.
Tern plugin to work with babel-plugin-module-resolver
{
"libs": [],
"loadEagerly": [],
"plugins": {
"node": {},
"es_modules": {},
"module-resolver": {
"cwd": "packagejson",
"alias": {
"my-alias1": "./path/to/my/module1",
"my-alias2": "./path/to/my/module2"
}
}
}
}
```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 }
@jeremija
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment