diff --git a/src/loaders.ts b/src/loaders.ts index 7895d1b..a8a163a 100644 --- a/src/loaders.ts +++ b/src/loaders.ts @@ -17,6 +17,11 @@ const sourcemaps = installSourceMapSupport(); const tsconfig = getTsconfig(); const tsconfigRaw = tsconfig?.config; +type Resolved = { + url: string; + format: ModuleFormat; +}; + type resolve = ( specifier: string, context: { @@ -24,10 +29,7 @@ type resolve = ( parentURL: string | undefined; }, defaultResolve: resolve, -) => MaybePromise<{ - url: string; - format: ModuleFormat; -}>; +) => MaybePromise; const hasExtensionPattern = /\.\w+$/; @@ -69,25 +71,9 @@ export const resolve: resolve = async function ( } } - if (tsExtensionsPattern.test(specifier)) { - const resolved = await defaultResolve(specifier, context, defaultResolve); - const format = getFormatFromExtension(resolved.url) ?? await getPackageType(resolved.url); - - return { - ...resolved, - format, - }; - } - - if (specifier.endsWith('.json')) { - return { - ...(await defaultResolve(specifier, context, defaultResolve)), - format: 'json', - }; - } - + let resolved: Resolved; try { - return await defaultResolve(specifier, context, defaultResolve); + resolved = await defaultResolve(specifier, context, defaultResolve); } catch (error) { if (error instanceof Error) { if ((error as any).code === 'ERR_UNSUPPORTED_DIR_IMPORT') { @@ -114,6 +100,23 @@ export const resolve: resolve = async function ( throw error; } + + if (resolved.url.endsWith('.json')) { + return { + ...resolved, + format: 'json', + }; + } + + if (tsExtensionsPattern.test(resolved.url)) { + const format = getFormatFromExtension(resolved.url) ?? await getPackageType(resolved.url); + return { + ...resolved, + format, + }; + } + + return resolved; }; type load = ( @@ -149,12 +152,7 @@ export const load: load = async function ( const loaded = await defaultLoad(url, context, defaultLoad); - if ( - !loaded.source - - // node_modules don't need to be transformed - || url.includes('/node_modules/') - ) { + if (!loaded.source) { return loaded; } diff --git a/tests/fixtures/package-module/node_modules/package-module/ts.ts b/tests/fixtures/package-module/node_modules/package-module/ts.ts new file mode 100644 index 0000000..091f609 --- /dev/null +++ b/tests/fixtures/package-module/node_modules/package-module/ts.ts @@ -0,0 +1,2 @@ +export default 'ts default export'; +export const namedExport: string = 'ts named export'; diff --git a/tests/fixtures/package-module/node_modules/package-typescript-export/index.ts b/tests/fixtures/package-module/node_modules/package-typescript-export/index.ts new file mode 100644 index 0000000..091f609 --- /dev/null +++ b/tests/fixtures/package-module/node_modules/package-typescript-export/index.ts @@ -0,0 +1,2 @@ +export default 'ts default export'; +export const namedExport: string = 'ts named export'; diff --git a/tests/fixtures/package-module/node_modules/package-typescript-export/package.json b/tests/fixtures/package-module/node_modules/package-typescript-export/package.json new file mode 100644 index 0000000..507f6aa --- /dev/null +++ b/tests/fixtures/package-module/node_modules/package-typescript-export/package.json @@ -0,0 +1,7 @@ +{ + "name": "package-typescript-export", + "type": "module", + "exports": { + "import": "./index.ts" + } +} diff --git a/tests/specs/typescript/dependencies.ts b/tests/specs/typescript/dependencies.ts new file mode 100644 index 0000000..e7b9621 --- /dev/null +++ b/tests/specs/typescript/dependencies.ts @@ -0,0 +1,20 @@ +import { testSuite, expect } from 'manten'; +import type { NodeApis } from '../../utils/node-with-loader'; + +export default testSuite(async ({ describe }, node: NodeApis) => { + describe('Dependencies', ({ describe }) => { + describe('TypeScript dependency', ({ test }) => { + const output = '{"default":"ts default export","namedExport":"ts named export"}'; + + test('Import', async () => { + const nodeProcess = await node.import('package-module/ts.ts'); + expect(nodeProcess.stdout).toBe(output); + }); + + test('Import', async () => { + const nodeProcess = await node.import('package-typescript-export'); + expect(nodeProcess.stdout).toBe(output); + }); + }); + }); +}); diff --git a/tests/specs/typescript/index.ts b/tests/specs/typescript/index.ts index 390f977..e18bf9f 100644 --- a/tests/specs/typescript/index.ts +++ b/tests/specs/typescript/index.ts @@ -9,5 +9,6 @@ export default testSuite(async ({ describe }, node: NodeApis) => { runTestSuite(import('./mts'), node); runTestSuite(import('./cts'), node); runTestSuite(import('./tsconfig'), node); + runTestSuite(import('./dependencies'), node); }); });