Skip to content

Commit a4cff68

Browse files
author
Tommy Leunen
committed
fix: Fix the root resolver when both a file and directory have the same name
When both a file and a directory share the same name, the resolver was wrongly resolving the path to the folder instead of the file. In Node, the resolver mechanism resolves the path to a .js file before trying to resolve the directory/index.js
1 parent 1f6245b commit a4cff68

10 files changed

Lines changed: 166 additions & 140 deletions

File tree

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"presets": ["es2015"],
3+
"plugins": [
4+
"transform-object-rest-spread"
5+
],
36
"env": {
47
"test": {
58
"plugins": ["istanbul"]

.eslintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"extends": "airbnb-base",
3+
"parserOptions": {
4+
"ecmaFeatures": {
5+
"experimentalObjectRestSpread": true
6+
}
7+
},
38
"rules": {
49
"comma-dangle": 0,
510
"indent": [2, 4, {"SwitchCase": 1}],

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"babel-cli": "^6.10.1",
3535
"babel-core": "^6.10.4",
3636
"babel-plugin-istanbul": "^2.0.0",
37+
"babel-plugin-transform-object-rest-spread": "^6.8.0",
3738
"babel-preset-es2015": "^6.6.0",
3839
"babel-register": "^6.8.0",
3940
"cross-env": "^2.0.0",

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'path';
22
import resolve from 'resolve';
33
import glob from 'glob';
44
import mapToRelative from './mapToRelative';
5+
import { toLocalPath } from './utils';
56

67
function createAliasFileMap(pluginOpts) {
78
const alias = pluginOpts.alias || {};
@@ -36,7 +37,7 @@ export function mapModule(source, file, pluginOpts) {
3637
const sourceFileExt = path.extname(source);
3738
// map the source and keep its extension if the import/require had one
3839
const ext = realFileExt === sourceFileExt ? realFileExt : '';
39-
return mapToRelative(file, replaceExt(fileAbsPath, ext));
40+
return toLocalPath(replaceExt(mapToRelative(file, fileAbsPath), ext));
4041
} catch (e) {
4142
// empty...
4243
}
@@ -70,7 +71,7 @@ export function mapModule(source, file, pluginOpts) {
7071
return newPath;
7172
}
7273
// relative alias
73-
return mapToRelative(file, newPath);
74+
return toLocalPath(mapToRelative(file, newPath));
7475
}
7576

7677

src/mapToRelative.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
import path from 'path';
2+
import { toPosixPath } from './utils';
23

34
function resolve(filename) {
45
if (path.isAbsolute(filename)) return filename;
56
return path.resolve(process.cwd(), filename);
67
}
78

8-
function toPosixPath(modulePath) {
9-
return modulePath.replace(/\\/g, '/');
10-
}
11-
129
export default function mapToRelative(currentFile, module) {
1310
let from = path.dirname(currentFile);
1411
let to = path.normalize(module);
1512

1613
from = resolve(from);
1714
to = resolve(to);
1815

19-
let moduleMapped = path.relative(from, to);
20-
21-
moduleMapped = toPosixPath(moduleMapped);
22-
23-
if (moduleMapped[0] !== '.') moduleMapped = `./${moduleMapped}`;
24-
25-
return moduleMapped;
16+
const moduleMapped = path.relative(from, to);
17+
return toPosixPath(moduleMapped);
2618
}

src/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function toPosixPath(modulePath) {
2+
return modulePath.replace(/\\/g, '/');
3+
}
4+
5+
export function toLocalPath(p) {
6+
return (p[0] !== '.')
7+
? `./${p}`
8+
: p;
9+
}

test/examples/foo/bar.js

Whitespace-only changes.

test/examples/foo/bar/index.js

Whitespace-only changes.

test/examples/foo/bar/x.js

Whitespace-only changes.

0 commit comments

Comments
 (0)