@@ -18,7 +18,7 @@ export const enum ShakeLevel {
1818}
1919
2020export function toStringShakeLevel ( shakeLevel : ShakeLevel ) : string {
21- switch ( shakeLevel ) {
21+ switch ( shakeLevel ) {
2222 case ShakeLevel . Files :
2323 return 'Files (0)' ;
2424 case ShakeLevel . InnerFile :
@@ -42,11 +42,6 @@ export interface ITreeShakingOptions {
4242 * Inline usages.
4343 */
4444 inlineEntryPoints : string [ ] ;
45- /**
46- * TypeScript libs.
47- * e.g. `lib.d.ts`, `lib.es2015.collection.d.ts`
48- */
49- libs : string [ ] ;
5045 /**
5146 * Other .d.ts files
5247 */
@@ -130,11 +125,7 @@ function createTypeScriptLanguageService(options: ITreeShakingOptions): ts.Langu
130125 } ) ;
131126
132127 // Resolve libs
133- const RESOLVED_LIBS : ILibMap = { } ;
134- options . libs . forEach ( ( filename ) => {
135- const filepath = path . join ( TYPESCRIPT_LIB_FOLDER , filename ) ;
136- RESOLVED_LIBS [ `defaultLib:${ filename } ` ] = fs . readFileSync ( filepath ) . toString ( ) ;
137- } ) ;
128+ const RESOLVED_LIBS = processLibFiles ( options ) ;
138129
139130 const compilerOptions = ts . convertCompilerOptionsFromJson ( options . compilerOptions , options . sourcesRoot ) . options ;
140131
@@ -205,6 +196,34 @@ function discoverAndReadFiles(options: ITreeShakingOptions): IFileMap {
205196 return FILES ;
206197}
207198
199+ /**
200+ * Read lib files and follow lib references
201+ */
202+ function processLibFiles ( options : ITreeShakingOptions ) : ILibMap {
203+
204+ const stack : string [ ] = [ ...options . compilerOptions . lib ] ;
205+ const result : ILibMap = { } ;
206+
207+ while ( stack . length > 0 ) {
208+ const filename = `lib.${ stack . shift ( ) ! . toLowerCase ( ) } .d.ts` ;
209+ const key = `defaultLib:${ filename } ` ;
210+ if ( ! result [ key ] ) {
211+ // add this file
212+ const filepath = path . join ( TYPESCRIPT_LIB_FOLDER , filename ) ;
213+ const sourceText = fs . readFileSync ( filepath ) . toString ( ) ;
214+ result [ key ] = sourceText ;
215+
216+ // precess dependencies and "recurse"
217+ const info = ts . preProcessFile ( sourceText ) ;
218+ for ( let ref of info . libReferenceDirectives ) {
219+ stack . push ( ref . fileName ) ;
220+ }
221+ }
222+ }
223+
224+ return result ;
225+ }
226+
208227interface ILibMap { [ libName : string ] : string ; }
209228interface IFileMap { [ fileName : string ] : string ; }
210229
@@ -475,7 +494,7 @@ function markNodes(languageService: ts.LanguageService, options: ITreeShakingOpt
475494 }
476495
477496 if ( black_queue . length === 0 ) {
478- for ( let i = 0 ; i < gray_queue . length ; i ++ ) {
497+ for ( let i = 0 ; i < gray_queue . length ; i ++ ) {
479498 const node = gray_queue [ i ] ;
480499 const nodeParent = node . parent ;
481500 if ( ( ts . isClassDeclaration ( nodeParent ) || ts . isInterfaceDeclaration ( nodeParent ) ) && nodeOrChildIsBlack ( nodeParent ) ) {
0 commit comments