Skip to content

Instantly share code, notes, and snippets.

@layerssss
Created October 30, 2017 02:30
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 layerssss/68eb1db9a43a5cf3325fbe25d62bad12 to your computer and use it in GitHub Desktop.
Save layerssss/68eb1db9a43a5cf3325fbe25d62bad12 to your computer and use it in GitHub Desktop.
const resolve = require('resolve');
const Path = require('path');
//
// Play together with:
//
// * https://github.com/entwicklerstube/babel-plugin-root-import
// * https://github.com/benmosher/eslint-plugin-import
//
// API: https://github.com/benmosher/eslint-plugin-import/blob/master/resolvers/README.md
//
// Usage: (in .babalrc)
//
// settings: {
// 'import/resolver': {
// [Path.resolve('./lib/rootResolver.js')]: {
// rootPathSuffix: 'src',
// rootPathPrefix: '#',
// },
// },
// },
// },
//
exports.interfaceVersion = 2;
exports.resolve = function(source, file, config) {
var rootPathPrefix = config.rootPathPrefix || '~';
var rootPathSuffix = config.rootPathSuffix || '.';
if (source.indexOf(rootPathPrefix) !== 0) return { found: false };
var sourcePath =
Path.resolve(rootPathSuffix) + source.substring(rootPathPrefix.length);
try {
return { found: true, path: resolve.sync(sourcePath, config) };
} catch (error) {
return { found: false };
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment