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: Relative file path imports now supports extensions #317

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/mapToAbsolute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import path from 'path';

import { toPosixPath } from './utils';

export default function mapToAbsolute(currentFile, relativePath) {
const currentDirectory = path.dirname(currentFile);
const absolutePath = path.resolve(currentDirectory, relativePath);

return toPosixPath(absolutePath);
}
19 changes: 17 additions & 2 deletions src/resolvePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import path from 'path';

import { warn } from './log';
import mapToRelative from './mapToRelative';
import mapToAbsolute from './mapToAbsolute';
import normalizeOptions from './normalizeOptions';
import { nodeResolvePath, replaceExtension, isRelativePath, toLocalPath, toPosixPath } from './utils';
import { nodeResolvePath, nodeResolveRelativePath, replaceExtension, isRelativePath, toLocalPath, toPosixPath } from './utils';

function getRelativePath(sourcePath, currentFile, absFileInRoot, opts) {
const realSourceFileExtension = path.extname(absFileInRoot);
Expand Down Expand Up @@ -77,13 +78,27 @@ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) {
return aliasedSourceFile;
}

function resolvePathFromRelativeConfig(sourcePath, currentFile, opts) {
if (isRelativePath(sourcePath)) {
const absFilePath = mapToAbsolute(currentFile, sourcePath);
const resolvedSourceFile = nodeResolveRelativePath(absFilePath, opts.extensions);

if (resolvedSourceFile) {
return getRelativePath(sourcePath, currentFile, resolvedSourceFile, opts);
}
}

return null;
}

const resolvers = [
resolvePathFromAliasConfig,
resolvePathFromRelativeConfig,
resolvePathFromRootConfig,
];

export default function resolvePath(sourcePath, currentFile, opts) {
if (isRelativePath(sourcePath)) {
if (isRelativePath(sourcePath) && path.extname(sourcePath)) {
return sourcePath;
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ export function nodeResolvePath(modulePath, basedir, extensions) {
}
}

export function nodeResolveRelativePath(modulePath, extensions) {
try {
return resolve.sync(modulePath, { extensions });
} catch (e) {
return null;
}
}

export function isRelativePath(nodePath) {
return nodePath.match(/^\.?\.\//);
}
Expand Down
42 changes: 42 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,22 @@ describe('module-resolver', () => {
);
});

it('should not resolve the relative file path with an unknown extension', () => {
testWithImport(
'./test/testproject/src/text',
'./test/testproject/src/text',
rootTransformerOpts,
);
});

it('should resolve the relative file path with a known defined extension & strip the extension', () => {
testWithImport(
'./test/testproject/src/rn',
'./test/testproject/src/rn',
rootTransformerOpts,
);
});

it('should resolve the file path with a known defined extension & strip the extension', () => {
testWithImport(
'rn',
Expand All @@ -262,13 +278,22 @@ describe('module-resolver', () => {
);
});


it('should resolve the file path with an explicit extension and not strip the extension', () => {
testWithImport(
'rn/index.ios.js',
'./test/testproject/src/rn/index.ios.js',
rootTransformerOpts,
);
});

it('should resolve the relative file path with an explicit extension and not strip the extension', () => {
testWithImport(
'./test/testproject/src/rn/index.ios.js',
'./test/testproject/src/rn/index.ios.js',
rootTransformerOpts,
);
});
});

describe('non-standard double extensions with strip extensions', () => {
Expand All @@ -291,13 +316,30 @@ describe('module-resolver', () => {
);
});

it('should not resolve the relative file path with an unknown extension', () => {
testWithImport(
'./test/testproject/src/text',
'./test/testproject/src/text',
rootTransformerOpts,
);
});

it('should resolve the file path with a known defined extension', () => {
testWithImport(
'rn',
'./test/testproject/src/rn/index.ios.js',
rootTransformerOpts,
);
});


it('should resolve the relative file path with a known defined extension', () => {
testWithImport(
'./test/testproject/src/rn/index',
'./test/testproject/src/rn/index.ios.js',
rootTransformerOpts,
);
});
});

describe('root and alias', () => {
Expand Down