Skip to content

Commit 38a0db4

Browse files
committed
Add support for resolving extensions in relative paths
1 parent 7055a40 commit 38a0db4

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

src/resolvePath.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ function getRelativePath(sourcePath, currentFile, absFileInRoot, opts) {
1717
return toLocalPath(toPosixPath(relativePath));
1818
}
1919

20+
function resolvePathFromRelativeConfig(sourcePath, currentFile, opts) {
21+
if (!isRelativePath(sourcePath)) {
22+
return null;
23+
}
24+
25+
const absFileInRoot = nodeResolvePath(sourcePath, path.dirname(currentFile), opts.extensions);
26+
27+
if (!absFileInRoot) {
28+
return null;
29+
}
30+
31+
return getRelativePath(sourcePath, currentFile, absFileInRoot, opts);
32+
}
33+
2034
function findPathInRoots(sourcePath, { extensions, root }) {
2135
// Search the source path inside every custom root directory
2236
let resolvedSourceFile;
@@ -78,15 +92,12 @@ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) {
7892
}
7993

8094
const resolvers = [
95+
resolvePathFromRelativeConfig,
8196
resolvePathFromAliasConfig,
8297
resolvePathFromRootConfig,
8398
];
8499

85100
export default function resolvePath(sourcePath, currentFile, opts) {
86-
if (isRelativePath(sourcePath)) {
87-
return sourcePath;
88-
}
89-
90101
const normalizedOpts = normalizeOptions(currentFile, opts);
91102

92103
// File param is a relative path from the environment current working directory

test/index.test.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,14 @@ describe('module-resolver', () => {
298298
rootTransformerOpts,
299299
);
300300
});
301+
302+
it('should resolve the relative file path with a known defined extension', () => {
303+
testWithImport(
304+
'./test/testproject/src/rn/file',
305+
'./test/testproject/src/rn/file.android.js',
306+
rootTransformerOpts,
307+
);
308+
});
301309
});
302310

303311
describe('root and alias', () => {
@@ -321,6 +329,28 @@ describe('module-resolver', () => {
321329
);
322330
});
323331
});
332+
333+
describe('with the plugin applied twice', () => {
334+
const doubleAliasTransformerOpts = {
335+
babelrc: false,
336+
plugins: [
337+
[plugin, { root: '.' }],
338+
[plugin, {
339+
alias: {
340+
'^@namespace/foo-(.+)': './packages/\\1',
341+
},
342+
}],
343+
],
344+
};
345+
346+
it('should support replacing parts of a path', () => {
347+
testWithImport(
348+
'@namespace/foo-bar',
349+
'./packages/bar',
350+
doubleAliasTransformerOpts,
351+
);
352+
});
353+
});
324354
});
325355

326356
describe('alias', () => {
@@ -590,28 +620,6 @@ describe('module-resolver', () => {
590620
});
591621
});
592622

593-
describe('with the plugin applied twice', () => {
594-
const doubleAliasTransformerOpts = {
595-
babelrc: false,
596-
plugins: [
597-
[plugin, { root: '.' }],
598-
[plugin, {
599-
alias: {
600-
'^@namespace/foo-(.+)': './packages/\\1',
601-
},
602-
}],
603-
],
604-
};
605-
606-
it('should support replacing parts of a path', () => {
607-
testWithImport(
608-
'@namespace/foo-bar',
609-
'./packages/bar',
610-
doubleAliasTransformerOpts,
611-
);
612-
});
613-
});
614-
615623
describe('missing packages warning', () => {
616624
const mockWarn = jest.fn();
617625
jest.mock('../src/log', () => ({

test/testproject/src/rn/file.android.js

Whitespace-only changes.

0 commit comments

Comments
 (0)