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

Commit ec7a850

Browse files
authored
Merge branch 'master' into fix/babel-plugin-name
2 parents d7e93f7 + 17f06b6 commit ec7a850

File tree

5 files changed

+100
-18
lines changed

5 files changed

+100
-18
lines changed

.babelrcArray

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"plugins": [
3+
[
4+
"babel-root-import",
5+
[
6+
{
7+
"rootPathSuffix": "test/somepath",
8+
"rootPathPrefix": "@"
9+
},
10+
{
11+
"rootPathSuffix": "test/anotherpath",
12+
"rootPathPrefix": "_"
13+
}
14+
]
15+
],
16+
]
17+
}

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-import-resolver-babel-root-import",
3-
"version": "0.1.0",
3+
"version": "1.0.0",
44
"main": "src/index.js",
55
"description": "babel-plugin-root-import resolver for eslint-plugin-import",
66
"repository": {
@@ -12,6 +12,12 @@
1212
"email": "olalonde@gmail.com",
1313
"url": "http://syskall.com"
1414
},
15+
"contributors": [
16+
{
17+
"name": "Christian Le",
18+
"url": "http://christianle.com"
19+
}
20+
],
1521
"license": "MIT",
1622
"keywords": [
1723
"eslint",
@@ -59,3 +65,4 @@
5965
]
6066
}
6167
}
68+

src/index.js

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,31 @@ if (babelRootImport.default) {
2525
}
2626

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

31-
const babelrc = path.join(start, '.babelrc');
32-
if (fs.existsSync(babelrc)) {
33-
const babelrcJson = JSON5.parse(fs.readFileSync(babelrc, 'utf8'));
31+
const packageJSONPath = path.join(start, 'package.json');
32+
const packageJSON = require(packageJSONPath);
33+
const babelConfig = packageJSON.babel;
34+
if (babelConfig) {
35+
const pluginConfig = babelConfig.plugins.find(p => (
36+
p[0] === 'babel-root-import'
37+
));
38+
process.chdir(path.dirname(packageJSONPath));
39+
return pluginConfig[1];
40+
}
41+
42+
const babelrcPath = path.join(start, babelrc);
43+
if (fs.existsSync(babelrcPath)) {
44+
const babelrcJson = JSON5.parse(fs.readFileSync(babelrcPath, 'utf8'));
3445
if (babelrcJson && Array.isArray(babelrcJson.plugins)) {
3546
const pluginConfig = babelrcJson.plugins.find(p => (
3647
p[0] === 'babel-plugin-root-import'
3748
));
3849
// The src path inside babelrc are from the root so we have
3950
// to change the working directory for the same directory
4051
// to make the mapping to work properly
41-
process.chdir(path.dirname(babelrc));
52+
process.chdir(path.dirname(babelrcPath));
4253
return pluginConfig[1];
4354
}
4455
}
@@ -54,28 +65,60 @@ exports.interfaceVersion = 2;
5465
* @param {string} source - the module to resolve; i.e './some-module'
5566
* @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
5667
* @param {object} config - the resolver options
68+
* @param {string} babelrc - the name of the babelrc file
5769
* @return {object}
5870
*/
59-
exports.resolve = (source, file, config) => {
60-
const opts = getConfigFromBabel(process.cwd());
71+
exports.resolve = (source, file, config, babelrc) => {
72+
const opts = getConfigFromBabel(process.cwd(), babelrc);
6173

62-
let rootPathSuffix = '';
63-
let rootPathPrefix = '';
74+
// [{rootPathPrefix: rootPathSuffix}]
75+
const rootPathConfig = [];
6476

65-
if (opts.rootPathSuffix && typeof opts.rootPathSuffix === 'string') {
66-
rootPathSuffix = `/${opts.rootPathSuffix.replace(/^(\/)|(\/)$/g, '')}`;
67-
}
77+
if (Array.isArray(opts)) {
78+
opts.forEach((option) => {
79+
let prefix = '';
80+
if (option.rootPathPrefix && typeof option.rootPathPrefix === 'string') {
81+
prefix = option.rootPathPrefix;
82+
}
6883

69-
if (opts.rootPathPrefix && typeof opts.rootPathPrefix === 'string') {
70-
rootPathPrefix = opts.rootPathPrefix;
84+
let suffix = '';
85+
if (option.rootPathSuffix && typeof option.rootPathSuffix === 'string') {
86+
suffix = `/${option.rootPathSuffix.replace(/^(\/)|(\/)$/g, '')}`;
87+
}
88+
89+
rootPathConfig.push({
90+
rootPathPrefix: prefix,
91+
rootPathSuffix: suffix
92+
});
93+
});
7194
} else {
72-
rootPathPrefix = '~';
95+
let rootPathPrefix = '~';
96+
if (opts.rootPathPrefix && typeof opts.rootPathPrefix === 'string') {
97+
rootPathPrefix = opts.rootPathPrefix;
98+
}
99+
100+
let rootPathSuffix = '';
101+
if (opts.rootPathSuffix && typeof opts.rootPathSuffix === 'string') {
102+
rootPathSuffix = `/${opts.rootPathSuffix.replace(/^(\/)|(\/)$/g, '')}`;
103+
}
104+
105+
rootPathConfig.push({
106+
rootPathPrefix,
107+
rootPathSuffix
108+
});
73109
}
74110

75111
let transformedSource = source;
76-
if (hasRootPathPrefixInString(source, rootPathPrefix)) {
77-
transformedSource = transformRelativeToRootPath(source, rootPathSuffix, rootPathPrefix);
112+
for (let i = 0; i < rootPathConfig.length; i += 1) {
113+
const option = rootPathConfig[i];
114+
const prefix = option.rootPathPrefix;
115+
const suffix = option.rootPathSuffix;
116+
if (hasRootPathPrefixInString(source, option.rootPathPrefix)) {
117+
transformedSource = transformRelativeToRootPath(source, suffix, prefix);
118+
break;
119+
}
78120
}
79121

80122
return nodeResolve(transformedSource, file, config);
81123
};
124+

test/anotherpath/file.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'some value';

test/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,19 @@ test((t) => {
88
found: true,
99
path: path.resolve(__dirname, 'somepath/file.js'),
1010
});
11+
12+
const result2 = resolve('@/file', __filename, {}, '.babelrcArray');
13+
t.deepEqual(result2, {
14+
found: true,
15+
path: path.resolve(__dirname, 'somepath/file.js'),
16+
});
17+
18+
const result3 = resolve('_/file', __filename, {}, '.babelrcArray');
19+
t.deepEqual(result3, {
20+
found: true,
21+
path: path.resolve(__dirname, 'anotherpath/file.js'),
22+
});
23+
1124
t.end();
1225
});
26+

0 commit comments

Comments
 (0)