From 44ed37f19fa576d57aec6a17cdcd43aa17a98fb4 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Thu, 12 Sep 2024 14:35:51 +0900 Subject: [PATCH] fix(cjs): resolve ts extensions from js when namespaced --- src/cjs/api/module-resolve-filename/index.ts | 8 +++++++- tests/specs/api.ts | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/cjs/api/module-resolve-filename/index.ts b/src/cjs/api/module-resolve-filename/index.ts index 9e88026a2..46454a54d 100644 --- a/src/cjs/api/module-resolve-filename/index.ts +++ b/src/cjs/api/module-resolve-filename/index.ts @@ -78,7 +78,13 @@ export const createResolveFilename = ( nextResolveSimple = createTsExtensionResolver( nextResolveSimple, - Boolean(parent?.filename && tsExtensionsPattern.test(parent.filename)), + Boolean( + // If register.namespace is used (e.g. tsx.require()) + namespace + + // If parent is a TS file + || (parent?.filename && tsExtensionsPattern.test(parent.filename)), + ), ); nextResolveSimple = createImplicitResolver(nextResolveSimple); diff --git a/tests/specs/api.ts b/tests/specs/api.ts index 4bfb85614..edb02ee20 100644 --- a/tests/specs/api.ts +++ b/tests/specs/api.ts @@ -2,6 +2,7 @@ import path from 'node:path'; import { execaNode } from 'execa'; import { testSuite, expect } from 'manten'; import { createFixture } from 'fs-fixture'; +import { outdent } from 'outdent'; import { tsxCjsPath, tsxCjsApiPath, @@ -11,7 +12,6 @@ import { type NodeApis, } from '../utils/tsx.js'; import { createPackageJson, createTsconfig, expectErrors } from '../fixtures.js'; -import { outdent } from 'outdent'; const tsFiles = { 'file.ts': outdent` @@ -266,7 +266,8 @@ export default testSuite(({ describe }, node: NodeApis) => { ...tsFiles, }); - const { stdout } = await execaNode(fixture.getPath('require.cjs'), [], { + const { stdout } = await execaNode('./require.cjs', [], { + cwd: fixture.path, nodePath: node.path, nodeOptions: [], });