Skip to content

Commit a4c79be

Browse files
committed
support paths without baseUrl for TS 6.0+ compatibility
1 parent c72aaed commit a4c79be

File tree

8 files changed

+45
-14
lines changed

8 files changed

+45
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"lint:prettier": "prettier --check . || (echo 'Run `npm run fix:prettier` to fix it.' && exit 1)",
3232
"lint:eslint": "eslint .",
3333
"fix:prettier": "prettier --write .",
34-
"check:language-extensions": "tsc --strict language-extensions/index.d.ts",
34+
"check:language-extensions": "tsc --strict --ignoreConfig language-extensions/index.d.ts",
3535
"preversion": "npm run build && npm test",
3636
"postversion": "git push && git push --tags"
3737
},

src/CompilerOptions.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ export function validateOptions(options: CompilerOptions): ts.Diagnostic[] {
9999
diagnostics.push(diagnosticFactories.unsupportedJsxEmit());
100100
}
101101

102-
if (options.paths && !options.baseUrl) {
103-
diagnostics.push(diagnosticFactories.pathsWithoutBaseUrl());
104-
}
102+
// paths without baseUrl is now supported (TS 6.0+ resolves paths relative to tsconfig location)
105103

106104
return diagnostics;
107105
}

src/transpilation/resolve.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,19 @@ class ResolutionContext {
215215
const fileFromPath = this.getFileFromPath(resolvedPath);
216216
if (fileFromPath) return fileFromPath;
217217

218-
if (this.options.paths && this.options.baseUrl) {
218+
if (this.options.paths) {
219219
// If no file found yet and paths are present, try to find project file via paths mappings
220-
const fileFromPaths = this.tryGetModuleNameFromPaths(
221-
dependencyPath,
222-
this.options.paths,
223-
this.options.baseUrl
224-
);
225-
if (fileFromPaths) return fileFromPaths;
220+
// When baseUrl is not set, resolve paths relative to the tsconfig directory (TS 6.0+ behavior)
221+
const pathsBase = this.options.baseUrl
222+
?? (this.options.configFilePath ? path.dirname(this.options.configFilePath) : undefined);
223+
if (pathsBase) {
224+
const fileFromPaths = this.tryGetModuleNameFromPaths(
225+
dependencyPath,
226+
this.options.paths,
227+
pathsBase
228+
);
229+
if (fileFromPaths) return fileFromPaths;
230+
}
226231
}
227232

228233
// Not a TS file in our project sources, use resolver to check if we can find dependency

test/transpile/module-resolution.spec.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as util from "../util";
44
import * as ts from "typescript";
55
import { BuildMode } from "../../src";
66
import { normalizeSlashes } from "../../src/utils";
7-
import { pathsWithoutBaseUrl } from "../../src/transpilation/diagnostics";
87

98
describe("basic module resolution", () => {
109
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-node-modules");
@@ -711,8 +710,21 @@ test("supports complicated paths configuration", () => {
711710
.expectToEqual({ foo: 314, bar: 271 });
712711
});
713712

714-
test("paths without baseUrl is error", () => {
715-
util.testFunction``.setOptions({ paths: {} }).expectToHaveDiagnostics([pathsWithoutBaseUrl.code]);
713+
test("paths without baseUrl is not an error", () => {
714+
util.testFunction``.setOptions({ paths: {} }).expectToHaveNoDiagnostics();
715+
});
716+
717+
test("supports paths configuration without baseUrl", () => {
718+
const baseProjectPath = path.resolve(__dirname, "module-resolution", "paths-no-baseurl");
719+
const projectPath = path.join(baseProjectPath, "myprogram");
720+
const projectTsConfig = path.join(projectPath, "tsconfig.json");
721+
const mainFile = path.join(projectPath, "main.ts");
722+
723+
// Bundle to have all files required to execute and check result
724+
util.testProject(projectTsConfig)
725+
.setMainFileName(mainFile)
726+
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
727+
.expectToEqual({ foo: 314, bar: 271 });
716728
});
717729

718730
test("module resolution using plugin", () => {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const bar = 271;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 314;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { foo } from "myOtherPackage";
2+
import { bar } from "myOtherPackage/bar";
3+
4+
export { foo, bar };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"rootDir": "..",
4+
"outDir": "dist",
5+
"paths": {
6+
"myOtherPackage": ["../mypackage"],
7+
"myOtherPackage/*": ["../mypackage/*"]
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)