Skip to content

Commit af370e7

Browse files
committed
fix(esm): package entry TS resolution for Node 20.17 & 22.6
1 parent 1b80d5d commit af370e7

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

.nvmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20.16.0
1+
20.17.0

pnpm-lock.yaml

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/esm/hook/resolve.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,29 @@ const getMissingPathFromNotFound = (
4141

4242
const isPackagePath = nodeError.message.match(/^Cannot find package '([^']+)'/);
4343
if (isPackagePath) {
44-
const [, packageJsonPath] = isPackagePath;
45-
const packageJsonUrl = pathToFileURL(packageJsonPath);
44+
const [, packagePath] = isPackagePath;
45+
if (!path.isAbsolute(packagePath)) {
46+
return;
47+
}
48+
49+
const packageUrl = pathToFileURL(packagePath);
4650

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

51-
const packageJson = readJsonFile<PackageJson>(packageJsonUrl);
52-
if (packageJson?.main) {
53-
return new URL(packageJson.main, packageJsonUrl).toString();
57+
// Node v21+ logs the package package.json path
58+
if (packageUrl.pathname.endsWith('/package.json')) {
59+
// packageJsonUrl.pathname += '/package.json';
60+
const packageJson = readJsonFile<PackageJson>(packageUrl);
61+
if (packageJson?.main) {
62+
return new URL(packageJson.main, packageUrl).toString();
63+
}
64+
} else {
65+
// Node v22.6.0 logs the entry path so we don't need to look it up from package.json
66+
return packageUrl.toString();
5467
}
5568
}
5669
};

tests/specs/smoke.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
454454
* not match the required ones (3)
455455
*/
456456
if (!version.startsWith('18.')) {
457-
test('resolve ts in main', async () => {
457+
test('resolve ts in main', async ({ onTestFail }) => {
458458
await using fixture = await createFixture({
459459
'package.json': createPackageJson({ type: packageType }),
460460
'index.ts': `
@@ -473,6 +473,9 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
473473
const p = await tsx(['index.ts'], {
474474
cwd: fixture.path,
475475
});
476+
onTestFail(() => {
477+
console.log(p);
478+
});
476479
expect(p.failed).toBe(false);
477480
});
478481
}

tests/utils/node-versions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ export const nodeVersions = [
1313
&& process.platform !== 'win32'
1414
)
1515
? [
16-
latestMajor('22.2.0'),
16+
latestMajor('22.6.0'),
1717
'22.0.0',
1818
latestMajor('21.7.3'),
1919
'21.0.0',
20-
latestMajor('20.14.0'),
20+
latestMajor('20.17.0'),
2121
'20.0.0',
2222
latestMajor('18.20.3'),
2323
'18.0.0',

0 commit comments

Comments
 (0)