diff --git a/src/loaders.ts b/src/loaders.ts index f057416..d7f7d1d 100644 --- a/src/loaders.ts +++ b/src/loaders.ts @@ -33,10 +33,9 @@ type resolve = ( specifier: string, context: Context, defaultResolve: resolve, + recursiveCall?: boolean, ) => MaybePromise; -const hasExtensionPattern = /\.\w+$/; - const extensions = ['.js', '.json', '.ts', '.tsx', '.jsx'] as const; async function tryExtensions( @@ -51,6 +50,7 @@ async function tryExtensions( specifier + extension, context, defaultResolve, + true, ); } catch (_error: any) { if (error === undefined) { @@ -86,6 +86,7 @@ export const resolve: resolve = async function ( specifier, context, defaultResolve, + resursiveCall, ) { // Added in v12.20.0 // https://nodejs.org/api/esm.html#esm_node_imports @@ -106,7 +107,7 @@ export const resolve: resolve = async function ( if (tsPath) { try { - return await resolve(tsPath, context, defaultResolve); + return await resolve(tsPath, context, defaultResolve, true); } catch (error) { if ((error as any).code !== 'ERR_MODULE_NOT_FOUND') { throw error; @@ -119,15 +120,15 @@ export const resolve: resolve = async function ( try { resolved = await defaultResolve(specifier, context, defaultResolve); } catch (error) { - if (error instanceof Error) { + if ( + (error instanceof Error) + && !resursiveCall + ) { if ((error as any).code === 'ERR_UNSUPPORTED_DIR_IMPORT') { return await tryDirectory(specifier, context, defaultResolve); } - if ( - (error as any).code === 'ERR_MODULE_NOT_FOUND' - && !hasExtensionPattern.test(specifier) - ) { + if ((error as any).code === 'ERR_MODULE_NOT_FOUND') { return await tryExtensions(specifier, context, defaultResolve); } } diff --git a/tests/fixtures/package-module/lib/ts-ext-ts/index.tsx.ts b/tests/fixtures/package-module/lib/ts-ext-ts/index.tsx.ts new file mode 100644 index 0000000..c92b562 --- /dev/null +++ b/tests/fixtures/package-module/lib/ts-ext-ts/index.tsx.ts @@ -0,0 +1,26 @@ +import fs from 'node:fs'; + +console.log( + 'loaded ts-ext-ts/index.tsx.ts', + JSON.stringify({ + nodePrefix: Boolean(fs), + hasDynamicImport: Boolean(import('fs')), + ...(() => { + let nameInError; + try { + nameInError(); + } catch (error) { + return { + nameInError: error.message.includes('nameInError'), + sourceMap: error.stack.includes(':11:5'), + }; + } + })(), + }), +); + +function valueNumber(value: number) { + return value; +} + +export default valueNumber(1234); diff --git a/tests/specs/typescript/ts.ts b/tests/specs/typescript/ts.ts index 9b68b07..188c476 100644 --- a/tests/specs/typescript/ts.ts +++ b/tests/specs/typescript/ts.ts @@ -47,6 +47,21 @@ export default testSuite(async ({ describe }, node: NodeApis) => { }); }); + describe('extensionless with subextension', ({ test }) => { + const importPath = './lib/ts-ext-ts/index.tsx'; + const outputSubextension = 'loaded ts-ext-ts/index.tsx.ts {"nodePrefix":true,"hasDynamicImport":true,"nameInError":true,"sourceMap":true}'; + + test('Load', async () => { + const nodeProcess = await node.load(importPath); + expect(nodeProcess.stdout).toBe(outputSubextension); + }); + + test('Import', async () => { + const nodeProcess = await node.import(importPath); + expect(nodeProcess.stdout).toBe(`${outputSubextension}\n{"default":1234}`); + }); + }); + describe('directory', ({ test }) => { const importPath = './lib/ts-ext-ts';