Skip to content

Commit

Permalink
fix(resolve): Don't run findFiles if we can already see an extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
EntraptaJ committed Apr 5, 2020
1 parent 2f108e2 commit b0e2b85
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ const rootModulePath = `${process.cwd()}/`;
const baseURL = pathToFileURL(rootModulePath).href;

const relativePathRegex = /^\.{1,2}[/]/;
const hasExtensionRegex = /\.\w+$/;

// TODO: Allow customization of extensions
const extensions = ['.ts', '.tsx'];
const extensionsRegex = new RegExp(`\\${extensions.join('$|\\')}`);
const extensionsRegex = new RegExp(`\\${extensions.join('|\\')}`);

// Custom resolver to allow `.ts` and `.tsx` extensions, along with finding files if no extension is provided.
export async function resolve(
Expand All @@ -39,14 +40,14 @@ export async function resolve(
// Node.js normally errors on unknown file extensions, so return a URL for
// specifiers ending in the TypeScript file extensions.
return {
url: new URL(specifier, parentURL).href,
url: resolvedUrl.href,
};
}

/**
* If no extension is passed and is a relative import then let's try to find a `.ts` or `.tsx` file at the path
*/
if (relativePathRegex.test(specifier)) {
if (relativePathRegex.test(specifier) && !hasExtensionRegex.test(fileName)) {
const filePath = fileURLToPath(resolvedUrl);

const file = await findFiles(dirname(filePath), {
Expand Down

0 comments on commit b0e2b85

Please sign in to comment.