Skip to content

Commit 7dc2da6

Browse files
amosyuenfatfisz
authored andcommitted
fix: Fix path transformation for dot files that are siblings (#253)
1 parent 7b618e0 commit 7dc2da6

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

src/resolvePath.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path';
33
import { warn } from './log';
44
import mapToRelative from './mapToRelative';
55
import normalizeOptions from './normalizeOptions';
6-
import { nodeResolvePath, replaceExtension, toLocalPath, toPosixPath } from './utils';
6+
import { nodeResolvePath, replaceExtension, isRelativePath, toLocalPath, toPosixPath } from './utils';
77

88
function getRelativePath(sourcePath, currentFile, absFileInRoot, opts) {
99
const realSourceFileExtension = path.extname(absFileInRoot);
@@ -64,7 +64,7 @@ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) {
6464
return null;
6565
}
6666

67-
if (aliasedSourceFile[0] === '.') {
67+
if (isRelativePath(aliasedSourceFile)) {
6868
return toLocalPath(toPosixPath(
6969
mapToRelative(opts.cwd, currentFile, aliasedSourceFile)),
7070
);
@@ -83,7 +83,7 @@ const resolvers = [
8383
];
8484

8585
export default function resolvePath(sourcePath, currentFile, opts) {
86-
if (sourcePath[0] === '.') {
86+
if (isRelativePath(sourcePath)) {
8787
return sourcePath;
8888
}
8989

src/utils.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ export function nodeResolvePath(modulePath, basedir, extensions) {
1111
}
1212
}
1313

14+
export function isRelativePath(nodePath) {
15+
return nodePath.match(/^\.?\.\//);
16+
}
17+
1418
export function toPosixPath(modulePath) {
1519
return modulePath.replace(/\\/g, '/');
1620
}
1721

1822
export function toLocalPath(modulePath) {
19-
return modulePath
20-
.replace(/\/index$/, '') // remove trailing /index
21-
.replace(/^(?!\.)/, './'); // insert `./` to make it a local path
23+
let localPath = modulePath.replace(/\/index$/, ''); // remove trailing /index
24+
if (!isRelativePath(localPath)) {
25+
localPath = `./${localPath}`; // insert `./` to make it a relative path
26+
}
27+
return localPath;
2228
}
2329

2430
export function stripExtension(modulePath, stripExtensions) {

test/index.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,45 @@ describe('module-resolver', () => {
769769
);
770770
});
771771
});
772+
773+
describe('dot files', () => {
774+
const dotFileAliasTransformerOpts = {
775+
babelrc: false,
776+
plugins: [
777+
[plugin, {
778+
alias: {
779+
'.babel': 'babel-core',
780+
elintrc: './.eslintrc',
781+
folderdot: './src/folder.',
782+
},
783+
}],
784+
],
785+
};
786+
787+
it('should not match folder names with dot at end', () => {
788+
testWithImport(
789+
'folderdot/file',
790+
'./src/folder./file',
791+
dotFileAliasTransformerOpts,
792+
);
793+
});
794+
795+
it('should resolve alias with dot', () => {
796+
testWithImport(
797+
'.babel/register',
798+
'babel-core/register',
799+
dotFileAliasTransformerOpts,
800+
);
801+
});
802+
803+
it('should resolve sibling dot files using alias', () => {
804+
testWithImport(
805+
'elintrc',
806+
'./.eslintrc',
807+
dotFileAliasTransformerOpts,
808+
);
809+
});
810+
});
772811
});
773812

774813
describe('with custom cwd', () => {

0 commit comments

Comments
 (0)