Skip to content

Commit de900a1

Browse files
committed
fix(esm): resolve implicit ts paths in packages
1 parent bd56e84 commit de900a1

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

src/esm/hook/resolve.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
fileUrlPrefix,
1313
tsExtensionsPattern,
1414
isDirectoryPattern,
15+
isBarePackageName,
1516
} from '../../utils/path-utils.js';
1617
import {
1718
getFormatFromFileUrl,
@@ -160,12 +161,14 @@ export const resolve: resolve = async (
160161
// If `allowJs` is set in `tsconfig.json`, then we'll apply the same resolution logic
161162
// to files without a TypeScript extension.
162163
if (
163-
acceptsQuery // file path
164+
// Ignore if it's a bare package name and there's no subpath
165+
!isBarePackageName.test(specifier)
164166
&& (
165167
tsExtensionsPattern.test(context.parentURL!)
166168
|| allowJs
167169
)
168170
) {
171+
// TODO: When guessing the .ts extension in a package, should it guess if there's an export map?
169172
const tsPaths = resolveTsPath(specifier);
170173
if (tsPaths) {
171174
for (const tsPath of tsPaths) {

src/utils/path-utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,6 @@ export const tsExtensionsPattern = /\.([cm]?ts|[tj]sx)($|\?)/;
5151
export const isJsonPattern = /\.json($|\?)/;
5252

5353
export const isDirectoryPattern = /\/(?:$|\?)/;
54+
55+
// Only matches packages names without subpaths (e.g. `foo` but not `foo/bar`)
56+
export const isBarePackageName = /^(?:@[^/]+\/)?[^/]+$/;

tests/fixtures.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,15 @@ export const files = {
256256
type: 'commonjs',
257257
}),
258258
'index.js': syntaxLowering,
259+
'ts.ts': syntaxLowering,
259260
},
260261
'pkg-module': {
261262
'package.json': createPackageJson({
262263
type: 'module',
263264
main: './index.js',
264265
}),
265266
'index.js': `${syntaxLowering}\nexport * from "./empty-export"`,
267+
'ts.ts': syntaxLowering,
266268
'empty-export/index.js': 'export {}',
267269
},
268270
},

tests/specs/smoke.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ export default testSuite(async ({ describe }, { tsx, supports }: NodeApis) => {
186186
import * as pkgCommonjs from 'pkg-commonjs';
187187
import * as pkgModule from 'pkg-module';
188188
189-
// TODO: Test resolving TS files in dependencies (e.g. implicit extensions & export maps)
189+
// Resolving TS files in dependencies (e.g. implicit extensions & export maps)
190+
import 'pkg-commonjs/ts.js';
191+
import 'pkg-module/ts.js';
190192
191193
// .js
192194
import * as js from './js/index.js';

0 commit comments

Comments
 (0)