diff --git a/src/resolvePath.js b/src/resolvePath.js index fcb3818..880614f 100644 --- a/src/resolvePath.js +++ b/src/resolvePath.js @@ -77,9 +77,7 @@ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) { } if (isRelativePath(aliasedSourceFile)) { - return toLocalPath(toPosixPath( - mapToRelative(opts.cwd, currentFile, aliasedSourceFile)), - ); + return toLocalPath(toPosixPath(mapToRelative(opts.cwd, currentFile, aliasedSourceFile))); } if (process.env.NODE_ENV !== 'production') { @@ -89,13 +87,24 @@ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) { return aliasedSourceFile; } -const resolvers = [ - resolvePathFromAliasConfig, - resolvePathFromRootConfig, -]; +function resolvePathFromRelativeConfig(sourcePath, currentFile, opts) { + if (!isRelativePath(sourcePath)) { + return null; + } + + const absFileInRoot = nodeResolvePath(sourcePath, path.dirname(currentFile), opts.extensions); + + if (!absFileInRoot) { + return null; + } + + return getRelativePath(sourcePath, currentFile, absFileInRoot, opts); +} + +const resolvers = [resolvePathFromAliasConfig, resolvePathFromRootConfig]; export default function resolvePath(sourcePath, currentFile, opts) { - if (isRelativePath(sourcePath)) { + if (!opts.resolveRelativePaths && isRelativePath(sourcePath)) { return sourcePath; } @@ -106,7 +115,11 @@ export default function resolvePath(sourcePath, currentFile, opts) { const absoluteCurrentFile = path.resolve(currentFile); let resolvedPath = null; - resolvers.some((resolver) => { + const configuredResolvers = opts.resolveRelativePaths + ? [resolvePathFromRelativeConfig].concat(resolvers) + : resolvers; + + configuredResolvers.some(resolver => { resolvedPath = resolver(sourcePath, absoluteCurrentFile, normalizedOpts); return resolvedPath !== null; }); diff --git a/test/index.test.js b/test/index.test.js index 384f545..8f6d49b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -271,6 +271,49 @@ describe('module-resolver', () => { }); }); + describe('non-standard relative double extension', () => { + const rootTransformerOpts = { + babelrc: false, + plugins: [ + [plugin, { + resolveRelativePaths: true, + root: './test/testproject/src', + extensions: ['.ios.js', '.android.js', '.js'], + }], + ], + }; + + it('should not resolve the file path with an unknown extension', () => { + testWithImport( + './rn', + './test/testproject/src/rn', + rootTransformerOpts, + ); + }); + }); + + describe('non-standard relative double extension without extension stripping', () => { + const rootTransformerOpts = { + babelrc: false, + plugins: [ + [plugin, { + resolveRelativePaths: true, + root: './test/testproject/src', + extensions: ['.ios.js', '.android.js', '.js'], + stripExtensions: [], + }], + ], + }; + + it('should not resolve the file path with an unknown extension', () => { + testWithImport( + './rn', + './test/testproject/src/rn/index.ios.js', + rootTransformerOpts, + ); + }); + }); + describe('non-standard double extensions with strip extensions', () => { const rootTransformerOpts = { babelrc: false,