From de900a196a5a66f5363f40fd6d38b879aee96875 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Fri, 7 Jun 2024 09:48:31 +0900 Subject: [PATCH] fix(esm): resolve implicit ts paths in packages --- src/esm/hook/resolve.ts | 5 ++++- src/utils/path-utils.ts | 3 +++ tests/fixtures.ts | 2 ++ tests/specs/smoke.ts | 4 +++- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/esm/hook/resolve.ts b/src/esm/hook/resolve.ts index 167390d76..7a1959b81 100644 --- a/src/esm/hook/resolve.ts +++ b/src/esm/hook/resolve.ts @@ -12,6 +12,7 @@ import { fileUrlPrefix, tsExtensionsPattern, isDirectoryPattern, + isBarePackageName, } from '../../utils/path-utils.js'; import { getFormatFromFileUrl, @@ -160,12 +161,14 @@ export const resolve: resolve = async ( // If `allowJs` is set in `tsconfig.json`, then we'll apply the same resolution logic // to files without a TypeScript extension. if ( - acceptsQuery // file path + // Ignore if it's a bare package name and there's no subpath + !isBarePackageName.test(specifier) && ( tsExtensionsPattern.test(context.parentURL!) || allowJs ) ) { + // TODO: When guessing the .ts extension in a package, should it guess if there's an export map? const tsPaths = resolveTsPath(specifier); if (tsPaths) { for (const tsPath of tsPaths) { diff --git a/src/utils/path-utils.ts b/src/utils/path-utils.ts index 68a5a1813..1f26e5916 100644 --- a/src/utils/path-utils.ts +++ b/src/utils/path-utils.ts @@ -51,3 +51,6 @@ export const tsExtensionsPattern = /\.([cm]?ts|[tj]sx)($|\?)/; export const isJsonPattern = /\.json($|\?)/; export const isDirectoryPattern = /\/(?:$|\?)/; + +// Only matches packages names without subpaths (e.g. `foo` but not `foo/bar`) +export const isBarePackageName = /^(?:@[^/]+\/)?[^/]+$/; diff --git a/tests/fixtures.ts b/tests/fixtures.ts index beed9c0f0..1963c3250 100644 --- a/tests/fixtures.ts +++ b/tests/fixtures.ts @@ -256,6 +256,7 @@ export const files = { type: 'commonjs', }), 'index.js': syntaxLowering, + 'ts.ts': syntaxLowering, }, 'pkg-module': { 'package.json': createPackageJson({ @@ -263,6 +264,7 @@ export const files = { main: './index.js', }), 'index.js': `${syntaxLowering}\nexport * from "./empty-export"`, + 'ts.ts': syntaxLowering, 'empty-export/index.js': 'export {}', }, }, diff --git a/tests/specs/smoke.ts b/tests/specs/smoke.ts index 5c7a3c900..b5b6dfeaa 100644 --- a/tests/specs/smoke.ts +++ b/tests/specs/smoke.ts @@ -186,7 +186,9 @@ export default testSuite(async ({ describe }, { tsx, supports }: NodeApis) => { import * as pkgCommonjs from 'pkg-commonjs'; import * as pkgModule from 'pkg-module'; - // TODO: Test resolving TS files in dependencies (e.g. implicit extensions & export maps) + // Resolving TS files in dependencies (e.g. implicit extensions & export maps) + import 'pkg-commonjs/ts.js'; + import 'pkg-module/ts.js'; // .js import * as js from './js/index.js';