Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/transpilation/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,18 @@ function resolveDependency(
// Check if file is a file in the project
const resolvedPath = path.join(fileDirectory, dependency);

if (isProjectFile(resolvedPath, program)) {
// JSON files need their extension as part of the import path, caught by this branch
return resolvedPath;
}

const resolvedFile = resolvedPath + ".ts";
if (isProjectFile(resolvedFile, program)) {
return resolvedFile;
}

const projectIndexPath = path.resolve(resolvedPath, "index.ts");
if (isProjectFile(projectIndexPath, program)) {
return projectIndexPath;
const possibleProjectFiles = [
resolvedPath, // JSON files need their extension as part of the import path, caught by this branch,
resolvedPath + ".ts", // Regular ts file
path.join(resolvedPath, "index.ts"), // Index ts file,
resolvedPath + ".tsx", // tsx file
path.join(resolvedPath, "index.tsx"), // tsx index
];

for (const possibleFile of possibleProjectFiles) {
if (isProjectFile(possibleFile, program)) {
return possibleFile;
}
}

// Check if this is a sibling of a required lua file
Expand Down Expand Up @@ -215,7 +214,9 @@ function isProjectFile(file: string, program: ts.Program): boolean {
function hasSourceFileInProject(filePath: string, program: ts.Program) {
const pathWithoutExtension = trimExtension(filePath);
return (
isProjectFile(pathWithoutExtension + ".ts", program) || isProjectFile(pathWithoutExtension + ".json", program)
isProjectFile(pathWithoutExtension + ".ts", program) ||
isProjectFile(pathWithoutExtension + ".tsx", program) ||
isProjectFile(pathWithoutExtension + ".json", program)
);
}

Expand Down
15 changes: 15 additions & 0 deletions test/transpile/module-resolution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,18 @@ describe("module resolution project with dependencies built by tstl library mode
.expectToEqual(expectedResult);
});
});

// Test fix for https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1037
describe("module resolution with tsx", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-tsx");

test("project with tsx files", () => {
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "main.tsx"))
.debug()
.expectToEqual({
result: "hello from other.tsx",
indexResult: "hello from dir/index.tsx",
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function indexf() {
return "hello from dir/index.tsx";
}
5 changes: 5 additions & 0 deletions test/transpile/module-resolution/project-with-tsx/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { f } from "./other";
import { indexf } from "./dir";

export const result = f();
export const indexResult = indexf();
3 changes: 3 additions & 0 deletions test/transpile/module-resolution/project-with-tsx/other.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function f() {
return "hello from other.tsx";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"jsx": "react"
}
}