Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit ed05390

Browse files
try to resolve includes from nearest tsconfig
1 parent ed5dc97 commit ed05390

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/processors/typescript.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ export class TypeScriptFileProcessor implements FileProcessor {
111111
filesList.push(TypeScriptFileProcessor.getEntryPointImport(file));
112112
}
113113

114+
const includes = this.includesFromNearestTsconfigFile(file);
115+
if (includes.length) {
116+
// only include non-globbed includes
117+
// TODO: support globbed includes
118+
const nonGlobIncludes = includes.filter(maybeGlob => !maybeGlob.includes('*'));
119+
filesList.push(...nonGlobIncludes);
120+
}
121+
114122
for (const fileName of filesList) {
115123
const referencedFile = dependencyTree.transformReference(fileName, file);
116124
if (Array.isArray(referencedFile)) {
@@ -189,6 +197,25 @@ export class TypeScriptFileProcessor implements FileProcessor {
189197
},
190198
);
191199

200+
/**
201+
* Finds the tsconfig.json that this file is included by. Does this by searching in the same directory,
202+
* and then searching in each dir all the way to the root directory.
203+
*/
204+
private includesFromNearestTsconfigFile = memoize(async (file: Path): Promise<string[]> => {
205+
const segments = path.dirname(path.relative(this.rootDir, file)).split(path.sep);
206+
while (segments.length > 0) {
207+
const maybeTsconfig = path.join(this.rootDir, ...segments, 'tsconfig.json');
208+
try {
209+
const contents = JSON.parse(await fs.promises.readFile(file, 'utf8'));
210+
return 'includes' in contents ? (contents['includes'] as string[]) : [];
211+
} catch (err) {
212+
segments.pop(); // go up one dir
213+
continue;
214+
}
215+
}
216+
throw new Error(`could not find tsconfig.json for ${file}`);
217+
});
218+
192219
// Finds an implicit 'import' in the entry point object literal, like:
193220
//
194221
// export const entryPoint: DynamicEntryPoint = {

0 commit comments

Comments
 (0)