Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into fix/babel-plugin-name
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmcli committed Mar 1, 2017
2 parents d7e93f7 + 17f06b6 commit ec7a850
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 18 deletions.
17 changes: 17 additions & 0 deletions .babelrcArray
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"plugins": [
[
"babel-root-import",
[
{
"rootPathSuffix": "test/somepath",
"rootPathPrefix": "@"
},
{
"rootPathSuffix": "test/anotherpath",
"rootPathPrefix": "_"
}
]
],
]
}
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-import-resolver-babel-root-import",
"version": "0.1.0",
"version": "1.0.0",
"main": "src/index.js",
"description": "babel-plugin-root-import resolver for eslint-plugin-import",
"repository": {
Expand All @@ -12,6 +12,12 @@
"email": "olalonde@gmail.com",
"url": "http://syskall.com"
},
"contributors": [
{
"name": "Christian Le",
"url": "http://christianle.com"
}
],
"license": "MIT",
"keywords": [
"eslint",
Expand Down Expand Up @@ -59,3 +65,4 @@
]
}
}

77 changes: 60 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,31 @@ if (babelRootImport.default) {
}

// returns the root import config as an object
function getConfigFromBabel(start) {
function getConfigFromBabel(start, babelrc = '.babelrc') {
if (start === '/') return [];

const babelrc = path.join(start, '.babelrc');
if (fs.existsSync(babelrc)) {
const babelrcJson = JSON5.parse(fs.readFileSync(babelrc, 'utf8'));
const packageJSONPath = path.join(start, 'package.json');
const packageJSON = require(packageJSONPath);
const babelConfig = packageJSON.babel;
if (babelConfig) {
const pluginConfig = babelConfig.plugins.find(p => (
p[0] === 'babel-root-import'
));
process.chdir(path.dirname(packageJSONPath));
return pluginConfig[1];
}

const babelrcPath = path.join(start, babelrc);
if (fs.existsSync(babelrcPath)) {
const babelrcJson = JSON5.parse(fs.readFileSync(babelrcPath, 'utf8'));
if (babelrcJson && Array.isArray(babelrcJson.plugins)) {
const pluginConfig = babelrcJson.plugins.find(p => (
p[0] === 'babel-plugin-root-import'
));
// The src path inside babelrc are from the root so we have
// to change the working directory for the same directory
// to make the mapping to work properly
process.chdir(path.dirname(babelrc));
process.chdir(path.dirname(babelrcPath));
return pluginConfig[1];
}
}
Expand All @@ -54,28 +65,60 @@ exports.interfaceVersion = 2;
* @param {string} source - the module to resolve; i.e './some-module'
* @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
* @param {object} config - the resolver options
* @param {string} babelrc - the name of the babelrc file
* @return {object}
*/
exports.resolve = (source, file, config) => {
const opts = getConfigFromBabel(process.cwd());
exports.resolve = (source, file, config, babelrc) => {
const opts = getConfigFromBabel(process.cwd(), babelrc);

let rootPathSuffix = '';
let rootPathPrefix = '';
// [{rootPathPrefix: rootPathSuffix}]
const rootPathConfig = [];

if (opts.rootPathSuffix && typeof opts.rootPathSuffix === 'string') {
rootPathSuffix = `/${opts.rootPathSuffix.replace(/^(\/)|(\/)$/g, '')}`;
}
if (Array.isArray(opts)) {
opts.forEach((option) => {
let prefix = '';
if (option.rootPathPrefix && typeof option.rootPathPrefix === 'string') {
prefix = option.rootPathPrefix;
}

if (opts.rootPathPrefix && typeof opts.rootPathPrefix === 'string') {
rootPathPrefix = opts.rootPathPrefix;
let suffix = '';
if (option.rootPathSuffix && typeof option.rootPathSuffix === 'string') {
suffix = `/${option.rootPathSuffix.replace(/^(\/)|(\/)$/g, '')}`;
}

rootPathConfig.push({
rootPathPrefix: prefix,
rootPathSuffix: suffix
});
});
} else {
rootPathPrefix = '~';
let rootPathPrefix = '~';
if (opts.rootPathPrefix && typeof opts.rootPathPrefix === 'string') {
rootPathPrefix = opts.rootPathPrefix;
}

let rootPathSuffix = '';
if (opts.rootPathSuffix && typeof opts.rootPathSuffix === 'string') {
rootPathSuffix = `/${opts.rootPathSuffix.replace(/^(\/)|(\/)$/g, '')}`;
}

rootPathConfig.push({
rootPathPrefix,
rootPathSuffix
});
}

let transformedSource = source;
if (hasRootPathPrefixInString(source, rootPathPrefix)) {
transformedSource = transformRelativeToRootPath(source, rootPathSuffix, rootPathPrefix);
for (let i = 0; i < rootPathConfig.length; i += 1) {
const option = rootPathConfig[i];
const prefix = option.rootPathPrefix;
const suffix = option.rootPathSuffix;
if (hasRootPathPrefixInString(source, option.rootPathPrefix)) {
transformedSource = transformRelativeToRootPath(source, suffix, prefix);
break;
}
}

return nodeResolve(transformedSource, file, config);
};

1 change: 1 addition & 0 deletions test/anotherpath/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'some value';
14 changes: 14 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,19 @@ test((t) => {
found: true,
path: path.resolve(__dirname, 'somepath/file.js'),
});

const result2 = resolve('@/file', __filename, {}, '.babelrcArray');
t.deepEqual(result2, {
found: true,
path: path.resolve(__dirname, 'somepath/file.js'),
});

const result3 = resolve('_/file', __filename, {}, '.babelrcArray');
t.deepEqual(result3, {
found: true,
path: path.resolve(__dirname, 'anotherpath/file.js'),
});

t.end();
});

0 comments on commit ec7a850

Please sign in to comment.