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
15 changes: 8 additions & 7 deletions src/transpilation/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,9 @@ class ResolutionContext {
if (resolvedNodeModulesFile) return resolvedNodeModulesFile;
}

// Check if file is a file in the project
const resolvedPath = this.formatPathToFile(dependencyPath, requiringFile);
const fileFromPath = this.getFileFromPath(resolvedPath);
if (fileFromPath) return fileFromPath;

if (this.options.paths) {
// If no file found yet and paths are present, try to find project file via paths mappings
// Bare specifiers: check paths mappings first, matching TypeScript's resolution order.
// TS never applies paths to relative imports, so skip for those.
if (!ts.isExternalModuleNameRelative(dependencyPath) && this.options.paths) {
// When baseUrl is not set, resolve paths relative to the tsconfig directory (TS 6.0+ behavior)
const pathsBase =
this.options.baseUrl ??
Expand All @@ -225,6 +221,11 @@ class ResolutionContext {
}
}

// Check if file is a file in the project
const resolvedPath = this.formatPathToFile(dependencyPath, requiringFile);
const fileFromPath = this.getFileFromPath(resolvedPath);
if (fileFromPath) return fileFromPath;

// Not a TS file in our project sources, use resolver to check if we can find dependency
try {
const resolveResult = resolver.resolveSync({}, fileDirectory, dependencyPath);
Expand Down
12 changes: 12 additions & 0 deletions test/transpile/module-resolution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,18 @@ test("supports complicated paths configuration", () => {
.expectToEqual({ foo: 314, bar: 271 });
});

test("paths mapping wins over sibling project file (TS resolution order)", () => {
const baseProjectPath = path.resolve(__dirname, "module-resolution", "paths-vs-project-file");
const projectPath = path.join(baseProjectPath, "program");
const projectTsConfig = path.join(projectPath, "tsconfig.json");
const mainFile = path.join(projectPath, "main.ts");

util.testProject(projectTsConfig)
.setMainFileName(mainFile)
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
.expectToEqual({ value: "paths-mapped" });
});

test("module resolution using plugin", () => {
const baseProjectPath = path.resolve(__dirname, "module-resolution", "project-with-module-resolution-plugin");
const projectTsConfig = path.join(baseProjectPath, "tsconfig.json");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = "paths-mapped";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = "project-file";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { value } from "alias";

export { value };
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"rootDir": "..",
"outDir": "dist",
"paths": {
"alias": ["../other/alias"]
}
}
}
Loading