Skip to content

Commit

Permalink
fix(esm): package entry TS resolution for Node 20.17 & 22.6
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Aug 22, 2024
1 parent 1b80d5d commit af370e7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.16.0
20.17.0
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 20 additions & 7 deletions src/esm/hook/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,29 @@ const getMissingPathFromNotFound = (

const isPackagePath = nodeError.message.match(/^Cannot find package '([^']+)'/);
if (isPackagePath) {
const [, packageJsonPath] = isPackagePath;
const packageJsonUrl = pathToFileURL(packageJsonPath);
const [, packagePath] = isPackagePath;
if (!path.isAbsolute(packagePath)) {
return;
}

const packageUrl = pathToFileURL(packagePath);

if (!packageJsonUrl.pathname.endsWith('/package.json')) {
packageJsonUrl.pathname += '/package.json';
// Node v20.0.0 logs the package directory
// Slash check / works on Windows as well because it's a path URL
if (packageUrl.pathname.endsWith('/')) {
packageUrl.pathname += 'package.json';
}

const packageJson = readJsonFile<PackageJson>(packageJsonUrl);
if (packageJson?.main) {
return new URL(packageJson.main, packageJsonUrl).toString();
// Node v21+ logs the package package.json path
if (packageUrl.pathname.endsWith('/package.json')) {
// packageJsonUrl.pathname += '/package.json';
const packageJson = readJsonFile<PackageJson>(packageUrl);
if (packageJson?.main) {
return new URL(packageJson.main, packageUrl).toString();
}
} else {
// Node v22.6.0 logs the entry path so we don't need to look it up from package.json
return packageUrl.toString();
}
}
};
Expand Down
5 changes: 4 additions & 1 deletion tests/specs/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
* not match the required ones (3)
*/
if (!version.startsWith('18.')) {
test('resolve ts in main', async () => {
test('resolve ts in main', async ({ onTestFail }) => {
await using fixture = await createFixture({
'package.json': createPackageJson({ type: packageType }),
'index.ts': `
Expand All @@ -473,6 +473,9 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
const p = await tsx(['index.ts'], {
cwd: fixture.path,
});
onTestFail(() => {
console.log(p);
});
expect(p.failed).toBe(false);
});
}
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/node-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export const nodeVersions = [
&& process.platform !== 'win32'
)
? [
latestMajor('22.2.0'),
latestMajor('22.6.0'),
'22.0.0',
latestMajor('21.7.3'),
'21.0.0',
latestMajor('20.14.0'),
latestMajor('20.17.0'),
'20.0.0',
latestMajor('18.20.3'),
'18.0.0',
Expand Down

0 comments on commit af370e7

Please sign in to comment.