From 0127b60392c685850a9f03a0682071183042a14f Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Wed, 18 May 2022 11:01:23 -0400 Subject: [PATCH 1/2] feat: import package with ts export --- src/loaders.ts | 54 +++++++++---------- .../node_modules/package-module/ts.ts | 2 + .../package-typescript-export/index.ts | 2 + .../package-typescript-export/package.json | 7 +++ tests/specs/typescript/dependencies.ts | 20 +++++++ tests/specs/typescript/index.ts | 1 + 6 files changed, 58 insertions(+), 28 deletions(-) create mode 100644 tests/fixtures/package-module/node_modules/package-module/ts.ts create mode 100644 tests/fixtures/package-module/node_modules/package-typescript-export/index.ts create mode 100644 tests/fixtures/package-module/node_modules/package-typescript-export/package.json create mode 100644 tests/specs/typescript/dependencies.ts 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..5b88889 --- /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); }); }); From 6080191b7cf97ff83eefa3eea53086e89e160b12 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Wed, 18 May 2022 11:19:45 -0400 Subject: [PATCH 2/2] test: casing --- tests/specs/typescript/dependencies.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/specs/typescript/dependencies.ts b/tests/specs/typescript/dependencies.ts index 5b88889..e7b9621 100644 --- a/tests/specs/typescript/dependencies.ts +++ b/tests/specs/typescript/dependencies.ts @@ -3,7 +3,7 @@ import type { NodeApis } from '../../utils/node-with-loader'; export default testSuite(async ({ describe }, node: NodeApis) => { describe('Dependencies', ({ describe }) => { - describe('typescript dependency', ({ test }) => { + describe('TypeScript dependency', ({ test }) => { const output = '{"default":"ts default export","namedExport":"ts named export"}'; test('Import', async () => {