Skip to content

Commit 9ceca83

Browse files
authored
fix: don't add configDir to modules when tsconfig has no baseUrl (#587)
1 parent e6f2158 commit 9ceca83

7 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎lib/TsconfigPathsPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function tsconfigPathsToResolveOptions(configDir, paths, resolver, baseUrl) {
149149
}
150150
}
151151

152-
if (absoluteBaseUrl && !modules.includes(absoluteBaseUrl)) {
152+
if (baseUrl && absoluteBaseUrl && !modules.includes(absoluteBaseUrl)) {
153153
modules.push(absoluteBaseUrl);
154154
}
155155

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "no-baseurl-upward",
3+
"version": "1.0.0"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import value from "pkg";

‎test/fixtures/tsconfig-paths/no-baseurl-upward/subdir/node_modules/package/index.js‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/tsconfig-paths/no-baseurl-upward/subdir/node_modules/package/package.json‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2017",
4+
"module": "commonjs"
5+
}
6+
}

‎test/tsconfig-paths.test.js‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,51 @@ describe("TsconfigPathsPlugin", () => {
13271327
});
13281328
});
13291329

1330+
describe("tsconfig without baseUrl should not add configDir to module search paths", () => {
1331+
const noBaseUrlDir = path.resolve(
1332+
__dirname,
1333+
"fixtures",
1334+
"tsconfig-paths",
1335+
"no-baseurl-upward",
1336+
);
1337+
1338+
it("should resolve node_modules package instead of matching a file at tsconfig root", (done) => {
1339+
const resolver = ResolverFactory.createResolver({
1340+
fileSystem,
1341+
extensions: [".ts", ".js", ".json"],
1342+
mainFields: ["main"],
1343+
mainFiles: ["index"],
1344+
tsconfig: true,
1345+
});
1346+
1347+
// Resolve "package" from subdir/ which has node_modules/package/.
1348+
// The parent tsconfig.json (without baseUrl) should NOT cause the
1349+
// tsconfig root to become a module search path — otherwise "package"
1350+
// would incorrectly match <root>/package.json instead of the real
1351+
// node_modules/package/index.js.
1352+
resolver.resolve(
1353+
{},
1354+
path.join(noBaseUrlDir, "subdir"),
1355+
"package",
1356+
{},
1357+
(err, result) => {
1358+
if (err) return done(err);
1359+
if (!result) return done(new Error("No result"));
1360+
expect(result).toEqual(
1361+
path.join(
1362+
noBaseUrlDir,
1363+
"subdir",
1364+
"node_modules",
1365+
"package",
1366+
"index.js",
1367+
),
1368+
);
1369+
done();
1370+
},
1371+
);
1372+
});
1373+
});
1374+
13301375
describe("JSONC support (comments in tsconfig.json)", () => {
13311376
const jsoncExampleDir = path.resolve(
13321377
__dirname,

0 commit comments

Comments
 (0)