Skip to content

Commit

Permalink
Fix path transformation for dot files that are siblings
Browse files Browse the repository at this point in the history
  • Loading branch information
amosyuen committed Dec 22, 2017
1 parent bc9fa3a commit 97dddb0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/resolvePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import { warn } from './log';
import mapToRelative from './mapToRelative';
import normalizeOptions from './normalizeOptions';
import { nodeResolvePath, replaceExtension, toLocalPath, toPosixPath } from './utils';
import { nodeResolvePath, replaceExtension, isRelativePath, toLocalPath, toPosixPath } from './utils';

function getRelativePath(sourcePath, currentFile, absFileInRoot, opts) {
const realSourceFileExtension = path.extname(absFileInRoot);
Expand Down Expand Up @@ -64,7 +64,7 @@ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) {
return null;
}

if (aliasedSourceFile[0] === '.') {
if (isRelativePath(aliasedSourceFile)) {
return toLocalPath(toPosixPath(
mapToRelative(opts.cwd, currentFile, aliasedSourceFile)),
);
Expand All @@ -83,7 +83,7 @@ const resolvers = [
];

export default function resolvePath(sourcePath, currentFile, opts) {
if (sourcePath[0] === '.') {
if (isRelativePath(sourcePath)) {
return sourcePath;
}

Expand Down
12 changes: 9 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ export function nodeResolvePath(modulePath, basedir, extensions) {
}
}

export function isRelativePath(nodePath) {
return nodePath.match(/\.?\.\//);
}

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

export function toLocalPath(modulePath) {
return modulePath
.replace(/\/index$/, '') // remove trailing /index
.replace(/^(?!\.)/, './'); // insert `./` to make it a local path
let localPath = modulePath.replace(/\/index$/, ''); // remove trailing /index
if (!isRelativePath(localPath)) {
localPath = `./${localPath}`; // insert `./` to make it a relative path
}
return localPath;
}

export function stripExtension(modulePath, stripExtensions) {
Expand Down
27 changes: 24 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,16 @@ describe('module-resolver', () => {
[plugin, {
root: './test/fakepath/',
alias: {
constants: './test/testproject/src/constants',
src: './test/testproject/src',
},
}],
],
};

it('should resolve the path using alias first and root otherwise', () => {
testWithImport(
'constants',
'./test/testproject/src/constants',
'src',
'./test/testproject/src',
aliasTransformerOpts,
);
});
Expand Down Expand Up @@ -769,6 +769,27 @@ describe('module-resolver', () => {
);
});
});

describe('dot files', () => {
const dotFileAliasTransformerOpts = {
babelrc: false,
plugins: [
[plugin, {
alias: {
elintrc: './.eslintrc',
},
}],
],
};

it('should resolve sibling dot files using alias', () => {
testWithImport(
'elintrc',
'./.eslintrc',
dotFileAliasTransformerOpts,
);
});
});
});

describe('with custom cwd', () => {
Expand Down

0 comments on commit 97dddb0

Please sign in to comment.