Skip to content

Commit c49386c

Browse files
authored
feat(tsconfig-loader): extends config from node_modules without './node_modules' prefix (#106)
1 parent f289c99 commit c49386c

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/tsconfig-loader.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,22 @@ export function loadTsconfig(
120120
) {
121121
extendedConfig += ".json";
122122
}
123-
124123
const currentDir = path.dirname(configFilePath);
124+
let extendedConfigPath = path.join(currentDir, extendedConfig);
125+
if (
126+
extendedConfig.indexOf("/") !== -1 &&
127+
extendedConfig.indexOf(".") !== -1 &&
128+
!existsSync(extendedConfigPath)
129+
) {
130+
extendedConfigPath = path.join(
131+
currentDir,
132+
"node_modules",
133+
extendedConfig
134+
);
135+
}
136+
125137
const base =
126-
loadTsconfig(
127-
path.join(currentDir, extendedConfig),
128-
existsSync,
129-
readFileSync
130-
) || {};
138+
loadTsconfig(extendedConfigPath, existsSync, readFileSync) || {};
131139

132140
// baseUrl should be interpreted as relative to the base tsconfig,
133141
// but we need to update it so it is relative to the original tsconfig being loaded

test/tsconfig-loader-tests.ts

+44
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,50 @@ describe("loadConfig", () => {
168168
});
169169
});
170170

171+
it("It should load a config with extends from node_modules and overwrite all options", () => {
172+
const firstConfig = {
173+
extends: "my-package/base-config.json",
174+
compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } }
175+
};
176+
const firstConfigPath = join("/root", "dir1", "tsconfig.json");
177+
const baseConfig = {
178+
compilerOptions: {
179+
baseUrl: "olle",
180+
paths: { foo: ["bar1"] },
181+
strict: true
182+
}
183+
};
184+
const baseConfigPath = join(
185+
"/root",
186+
"dir1",
187+
"node_modules",
188+
"my-package",
189+
"base-config.json"
190+
);
191+
const res = loadTsconfig(
192+
join("/root", "dir1", "tsconfig.json"),
193+
path => path === firstConfigPath || path === baseConfigPath,
194+
path => {
195+
if (path === firstConfigPath) {
196+
return JSON.stringify(firstConfig);
197+
}
198+
if (path === baseConfigPath) {
199+
return JSON.stringify(baseConfig);
200+
}
201+
return "";
202+
}
203+
);
204+
205+
assert.deepEqual(res, {
206+
extends: "my-package/base-config.json",
207+
compilerOptions: {
208+
baseUrl: "kalle",
209+
paths: { foo: ["bar2"] },
210+
strict: true
211+
}
212+
});
213+
});
214+
171215
it("Should use baseUrl relative to location of extended tsconfig", () => {
172216
const firstConfig = { compilerOptions: { baseUrl: "." } };
173217
const firstConfigPath = join("/root", "first-config.json");

0 commit comments

Comments
 (0)