Skip to content

Commit

Permalink
fix(esm): resolve implicit ts paths in packages
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Jun 7, 2024
1 parent bd56e84 commit de900a1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/esm/hook/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
fileUrlPrefix,
tsExtensionsPattern,
isDirectoryPattern,
isBarePackageName,
} from '../../utils/path-utils.js';
import {
getFormatFromFileUrl,
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/path-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /^(?:@[^/]+\/)?[^/]+$/;
2 changes: 2 additions & 0 deletions tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,15 @@ export const files = {
type: 'commonjs',
}),
'index.js': syntaxLowering,
'ts.ts': syntaxLowering,
},
'pkg-module': {
'package.json': createPackageJson({
type: 'module',
main: './index.js',
}),
'index.js': `${syntaxLowering}\nexport * from "./empty-export"`,
'ts.ts': syntaxLowering,
'empty-export/index.js': 'export {}',
},
},
Expand Down
4 changes: 3 additions & 1 deletion tests/specs/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit de900a1

Please sign in to comment.