From 02d3856371794ea16c6dec27489e24f2b1b5249a Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Sat, 8 Jun 2024 10:48:05 +0900 Subject: [PATCH] fix(cjs): resolve directory import relative to parent (#42) fixes https://github.com/privatenumber/tsx/issues/583 --- src/cjs/api/module-resolve-filename.ts | 5 +++-- tests/fixtures.ts | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/cjs/api/module-resolve-filename.ts b/src/cjs/api/module-resolve-filename.ts index 61629c41f..5ac896b5d 100644 --- a/src/cjs/api/module-resolve-filename.ts +++ b/src/cjs/api/module-resolve-filename.ts @@ -164,7 +164,7 @@ export const createResolveFilename = ( */ const resolved = ( tryExtensions(resolve, possiblePath) - || tryExtensions(resolve, path.resolve(possiblePath, 'index')) + || tryExtensions(resolve, `${request}${path.sep}index`) ); if (resolved) { return resolved + query; @@ -187,7 +187,8 @@ export const createResolveFilename = ( } catch (error) { const resolved = ( tryExtensions(resolve, request) - || tryExtensions(resolve, path.resolve(request, 'index')) + // Default resolve handles resovling paths relative to the parent + || tryExtensions(resolve, `${request}${path.sep}index`) ); if (resolved) { return resolved + query; diff --git a/tests/fixtures.ts b/tests/fixtures.ts index 90acdcbd9..a99f0e37d 100644 --- a/tests/fixtures.ts +++ b/tests/fixtures.ts @@ -136,6 +136,9 @@ export const files = { ${syntaxLowering} ${preserveName} export const cjsContext = ${cjsContextCheck}; + + // Implicit directory import works outside of immedaite CWD child + import '../ts/' `, 'json/index.json': JSON.stringify({ loaded: 'json' }),