From cd673a4649615ee87df9c7b33084ad5a7510b97a Mon Sep 17 00:00:00 2001 From: Amos Yuen Date: Thu, 14 Dec 2017 21:55:32 -0800 Subject: [PATCH] Fix path transformation for dot files that are siblings --- src/resolvePath.js | 6 +++--- src/utils.js | 12 +++++++++--- test/index.test.js | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/resolvePath.js b/src/resolvePath.js index 9858489..30c73f7 100644 --- a/src/resolvePath.js +++ b/src/resolvePath.js @@ -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); @@ -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)), ); @@ -83,7 +83,7 @@ const resolvers = [ ]; export default function resolvePath(sourcePath, currentFile, opts) { - if (sourcePath[0] === '.') { + if (isRelativePath(sourcePath)) { return sourcePath; } diff --git a/src/utils.js b/src/utils.js index 4525fb9..a19e61f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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) { diff --git a/test/index.test.js b/test/index.test.js index 38bd92e..8d4f683 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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', () => {