Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix path transformation for dot files that are sibling to current file #253

Merged
merged 2 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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