Skip to content

Commit

Permalink
fix: Fix path transformation for dot files that are siblings (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
amosyuen authored and fatfisz committed Jan 16, 2018
1 parent 7b618e0 commit 7dc2da6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 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
39 changes: 39 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,45 @@ describe('module-resolver', () => {
);
});
});

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

it('should not match folder names with dot at end', () => {
testWithImport(
'folderdot/file',
'./src/folder./file',
dotFileAliasTransformerOpts,
);
});

it('should resolve alias with dot', () => {
testWithImport(
'.babel/register',
'babel-core/register',
dotFileAliasTransformerOpts,
);
});

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

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

0 comments on commit 7dc2da6

Please sign in to comment.