Skip to content

Commit

Permalink
Add support for resolving extensions in relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
amosyuen committed Jul 11, 2018
1 parent 7055a40 commit 38a0db4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
19 changes: 15 additions & 4 deletions src/resolvePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ function getRelativePath(sourcePath, currentFile, absFileInRoot, opts) {
return toLocalPath(toPosixPath(relativePath));
}

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);
}

function findPathInRoots(sourcePath, { extensions, root }) {
// Search the source path inside every custom root directory
let resolvedSourceFile;
Expand Down Expand Up @@ -78,15 +92,12 @@ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) {
}

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

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

const normalizedOpts = normalizeOptions(currentFile, opts);

// File param is a relative path from the environment current working directory
Expand Down
52 changes: 30 additions & 22 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ describe('module-resolver', () => {
rootTransformerOpts,
);
});

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

describe('root and alias', () => {
Expand All @@ -321,6 +329,28 @@ describe('module-resolver', () => {
);
});
});

describe('with the plugin applied twice', () => {
const doubleAliasTransformerOpts = {
babelrc: false,
plugins: [
[plugin, { root: '.' }],
[plugin, {
alias: {
'^@namespace/foo-(.+)': './packages/\\1',
},
}],
],
};

it('should support replacing parts of a path', () => {
testWithImport(
'@namespace/foo-bar',
'./packages/bar',
doubleAliasTransformerOpts,
);
});
});
});

describe('alias', () => {
Expand Down Expand Up @@ -590,28 +620,6 @@ describe('module-resolver', () => {
});
});

describe('with the plugin applied twice', () => {
const doubleAliasTransformerOpts = {
babelrc: false,
plugins: [
[plugin, { root: '.' }],
[plugin, {
alias: {
'^@namespace/foo-(.+)': './packages/\\1',
},
}],
],
};

it('should support replacing parts of a path', () => {
testWithImport(
'@namespace/foo-bar',
'./packages/bar',
doubleAliasTransformerOpts,
);
});
});

describe('missing packages warning', () => {
const mockWarn = jest.fn();
jest.mock('../src/log', () => ({
Expand Down
Empty file.

0 comments on commit 38a0db4

Please sign in to comment.