@@ -53,6 +53,19 @@ function getEffectiveRootDir(
5353 ) ;
5454}
5555
56+ const getDependentFiles = (
57+ rootFiles : string [ ] ,
58+ configContent : typescript . ParsedCommandLine ,
59+ rootDir : string ,
60+ ) : string [ ] => {
61+ const program = typescript . createProgram ( rootFiles , configContent . options ) ;
62+ const sourceFiles = program . getSourceFiles ( ) ;
63+ const dependentFiles = sourceFiles
64+ . map ( ( file ) => file . fileName )
65+ . filter ( ( file ) => ! file . endsWith ( '.d.ts' ) && file . startsWith ( rootDir ) ) ;
66+ return dependentFiles . length ? dependentFiles : rootFiles ;
67+ } ;
68+
5669const readTsConfig = (
5770 {
5871 tsConfigPath,
@@ -121,20 +134,24 @@ const readTsConfig = (
121134 ) ;
122135
123136 const excludeExtensions = [ '.mdx' , '.md' ] ;
124- const filesToCompile = [
137+ const rootFiles = [
125138 ...Object . values ( mapComponentsToExpose ) ,
139+ ...additionalFilesToCompile ,
140+ ] . filter (
141+ ( filename ) => ! excludeExtensions . some ( ( ext ) => filename . endsWith ( ext ) ) ,
142+ ) ;
143+
144+ const filesToCompile = [
145+ ...getDependentFiles ( rootFiles , configContent , rootDir ) ,
126146 ...configContent . fileNames . filter (
127147 ( filename ) =>
128148 filename . endsWith ( '.d.ts' ) &&
129149 ! filename . startsWith ( outDirWithoutTypesFolder ) ,
130150 ) ,
131- ...additionalFilesToCompile ,
132- ] . filter (
133- ( filename ) => ! excludeExtensions . some ( ( ext ) => filename . endsWith ( ext ) ) ,
134- ) ;
151+ ] ;
135152
136153 rawTsConfigJson . include = [ ] ;
137- rawTsConfigJson . files = filesToCompile ;
154+ rawTsConfigJson . files = [ ... new Set ( filesToCompile ) ] ;
138155 rawTsConfigJson . exclude = [ ] ;
139156 'references' in rawTsConfigJson && delete rawTsConfigJson . references ;
140157
0 commit comments