Skip to content

Commit

Permalink
fix: Fix the root resolver when both a file and directory have the sa…
Browse files Browse the repository at this point in the history
…me 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
  • Loading branch information
Tommy Leunen committed Aug 27, 2016
1 parent 1f6245b commit a4cff68
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 140 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"presets": ["es2015"],
"plugins": [
"transform-object-rest-spread"
],
"env": {
"test": {
"plugins": ["istanbul"]
Expand Down
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"extends": "airbnb-base",
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
},
"rules": {
"comma-dangle": 0,
"indent": [2, 4, {"SwitchCase": 1}],
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"babel-cli": "^6.10.1",
"babel-core": "^6.10.4",
"babel-plugin-istanbul": "^2.0.0",
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-preset-es2015": "^6.6.0",
"babel-register": "^6.8.0",
"cross-env": "^2.0.0",
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import resolve from 'resolve';
import glob from 'glob';
import mapToRelative from './mapToRelative';
import { toLocalPath } from './utils';

function createAliasFileMap(pluginOpts) {
const alias = pluginOpts.alias || {};
Expand Down Expand Up @@ -36,7 +37,7 @@ export function mapModule(source, file, pluginOpts) {
const sourceFileExt = path.extname(source);
// map the source and keep its extension if the import/require had one
const ext = realFileExt === sourceFileExt ? realFileExt : '';
return mapToRelative(file, replaceExt(fileAbsPath, ext));
return toLocalPath(replaceExt(mapToRelative(file, fileAbsPath), ext));
} catch (e) {
// empty...
}
Expand Down Expand Up @@ -70,7 +71,7 @@ export function mapModule(source, file, pluginOpts) {
return newPath;
}
// relative alias
return mapToRelative(file, newPath);
return toLocalPath(mapToRelative(file, newPath));
}


Expand Down
14 changes: 3 additions & 11 deletions src/mapToRelative.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import path from 'path';
import { toPosixPath } from './utils';

function resolve(filename) {
if (path.isAbsolute(filename)) return filename;
return path.resolve(process.cwd(), filename);
}

function toPosixPath(modulePath) {
return modulePath.replace(/\\/g, '/');
}

export default function mapToRelative(currentFile, module) {
let from = path.dirname(currentFile);
let to = path.normalize(module);

from = resolve(from);
to = resolve(to);

let moduleMapped = path.relative(from, to);

moduleMapped = toPosixPath(moduleMapped);

if (moduleMapped[0] !== '.') moduleMapped = `./${moduleMapped}`;

return moduleMapped;
const moduleMapped = path.relative(from, to);
return toPosixPath(moduleMapped);
}
9 changes: 9 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function toPosixPath(modulePath) {
return modulePath.replace(/\\/g, '/');
}

export function toLocalPath(p) {
return (p[0] !== '.')
? `./${p}`
: p;
}
Empty file added test/examples/foo/bar.js
Empty file.
Empty file added test/examples/foo/bar/index.js
Empty file.
Empty file added test/examples/foo/bar/x.js
Empty file.
Loading

0 comments on commit a4cff68

Please sign in to comment.