From b0e2b850487d91b4ef8f101d64ef481689d5c875 Mon Sep 17 00:00:00 2001 From: KristianFJones Date: Sun, 5 Apr 2020 16:15:21 +0000 Subject: [PATCH] fix(resolve): Don't run findFiles if we can already see an extension. --- src/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 00065bf..11b52b3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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( @@ -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), {