Skip to content

Instantly share code, notes, and snippets.

@layerssss
Created October 30, 2017 02:30
Show Gist options
  • 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