Skip to content

Commit

Permalink
feat(tsconfig-loader): extends config from node_modules without './no…
Browse files Browse the repository at this point in the history
…de_modules' prefix (#106)
  • Loading branch information
zorji authored Jul 6, 2021
1 parent f289c99 commit c49386c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/tsconfig-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,22 @@ export function loadTsconfig(
) {
extendedConfig += ".json";
}

const currentDir = path.dirname(configFilePath);
let extendedConfigPath = path.join(currentDir, extendedConfig);
if (
extendedConfig.indexOf("/") !== -1 &&
extendedConfig.indexOf(".") !== -1 &&
!existsSync(extendedConfigPath)
) {
extendedConfigPath = path.join(
currentDir,
"node_modules",
extendedConfig
);
}

const base =
loadTsconfig(
path.join(currentDir, extendedConfig),
existsSync,
readFileSync
) || {};
loadTsconfig(extendedConfigPath, existsSync, readFileSync) || {};

// baseUrl should be interpreted as relative to the base tsconfig,
// but we need to update it so it is relative to the original tsconfig being loaded
Expand Down
44 changes: 44 additions & 0 deletions test/tsconfig-loader-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,50 @@ 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"] } }
};
const firstConfigPath = join("/root", "dir1", "tsconfig.json");
const baseConfig = {
compilerOptions: {
baseUrl: "olle",
paths: { foo: ["bar1"] },
strict: true
}
};
const baseConfigPath = join(
"/root",
"dir1",
"node_modules",
"my-package",
"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/base-config.json",
compilerOptions: {
baseUrl: "kalle",
paths: { foo: ["bar2"] },
strict: true
}
});
});

it("Should use baseUrl relative to location of extended tsconfig", () => {
const firstConfig = { compilerOptions: { baseUrl: "." } };
const firstConfigPath = join("/root", "first-config.json");
Expand Down

0 comments on commit c49386c

Please sign in to comment.