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

allow for optional relative resolve for non-standard extensions #428

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/resolvePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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;
}

Expand All @@ -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;
});
Expand Down
43 changes: 43 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down