Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tsconfig-loader): fix baseUrl when extends with paths relative to node_modules #172

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 6 additions & 6 deletions src/tsconfig-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ export function loadTsconfig(
if (
extendedConfig.indexOf("/") !== -1 &&
extendedConfig.indexOf(".") !== -1 &&
!existsSync(extendedConfigPath)
!existsSync(extendedConfigPath) &&
existsSync(path.join(currentDir, "node_modules", extendedConfig))
) {
extendedConfigPath = path.join(
currentDir,
"node_modules",
extendedConfig
);
// update extendedConfig from path relative node_modules to relative to
// current dir
extendedConfig = path.join("node_modules", extendedConfig);
extendedConfigPath = path.join(currentDir, extendedConfig);
}

const base =
Expand Down
57 changes: 51 additions & 6 deletions test/tsconfig-loader-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,15 @@ describe("loadConfig", () => {
it("It should load a config with extends from node_modules and overwrite all options", () => {
const firstConfig = {
extends: "my-package/base-config.json",
compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } }
compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
};
const firstConfigPath = join("/root", "dir1", "tsconfig.json");
const baseConfig = {
compilerOptions: {
baseUrl: "olle",
paths: { foo: ["bar1"] },
strict: true
}
strict: true,
},
};
const baseConfigPath = join(
"/root",
Expand All @@ -190,8 +190,8 @@ describe("loadConfig", () => {
);
const res = loadTsconfig(
join("/root", "dir1", "tsconfig.json"),
path => path === firstConfigPath || path === baseConfigPath,
path => {
(path) => path === firstConfigPath || path === baseConfigPath,
(path) => {
if (path === firstConfigPath) {
return JSON.stringify(firstConfig);
}
Expand All @@ -207,8 +207,53 @@ describe("loadConfig", () => {
compilerOptions: {
baseUrl: "kalle",
paths: { foo: ["bar2"] },
strict: true
strict: true,
},
});
});

it("It should load a config with extends from node_modules and resolve relative baseUrl", () => {
const firstConfig = {
extends: "my-package/node/base-config.json",
compilerOptions: { paths: { foo: ["bar2"] } },
};
const firstConfigPath = join("/root", "dir1", "tsconfig.json");
const baseConfig = {
compilerOptions: {
baseUrl: "../../..",
paths: { foo: ["bar1"] },
strict: true,
},
};
const baseConfigPath = join(
"/root",
"dir1",
"node_modules",
"my-package",
"node",
"base-config.json"
);
const res = loadTsconfig(
join("/root", "dir1", "tsconfig.json"),
(path) => path === firstConfigPath || path === baseConfigPath,
(path) => {
if (path === firstConfigPath) {
return JSON.stringify(firstConfig);
}
if (path === baseConfigPath) {
return JSON.stringify(baseConfig);
}
return "";
}
);

assert.deepEqual(res, {
extends: "my-package/node/base-config.json",
compilerOptions: {
baseUrl: ".",
paths: { foo: ["bar2"] },
strict: true,
},
});
});

Expand Down