@@ -63,6 +63,14 @@ export interface IExtractorOptions {
6363 * The default value is false.
6464 */
6565 localBuild ?: boolean ;
66+
67+ /**
68+ * If specified, use typings specified in the project's compilerOptions -> lib option
69+ * from this TypeScript compiler package.
70+ *
71+ * @alpha
72+ */
73+ typescriptLibPackagePath ?: string ;
6674}
6775
6876/**
@@ -79,7 +87,7 @@ export class Extractor {
7987 private static _defaultConfig : Partial < IExtractorConfig > = JsonFile . load ( path . join ( __dirname ,
8088 './api-extractor-defaults.json' ) ) ;
8189
82- private static _outputFileExtensionRegExp : RegExp = / \. d \. t s $ / i;
90+ private static _declarationFileExtensionRegExp : RegExp = / \. d \. t s $ / i;
8391
8492 private static _defaultLogger : ILogger = {
8593 logVerbose : ( message : string ) => console . log ( '(Verbose) ' + message ) ,
@@ -124,7 +132,7 @@ export class Extractor {
124132 throw new Error ( 'Input file is not an absolute path: ' + inputFilePath ) ;
125133 }
126134
127- if ( Extractor . _outputFileExtensionRegExp . test ( inputFilePath ) ) {
135+ if ( Extractor . _declarationFileExtensionRegExp . test ( inputFilePath ) ) {
128136 analysisFilePaths . push ( inputFilePath ) ;
129137 }
130138 }
@@ -173,15 +181,22 @@ export class Extractor {
173181 tsconfig = JsonFile . load ( path . join ( this . _absoluteRootFolder , 'tsconfig.json' ) ) ;
174182 }
175183
176- const commandLine : ts . ParsedCommandLine = ts . parseJsonConfigFileContent ( tsconfig ,
177- ts . sys , this . _absoluteRootFolder ) ;
184+ const commandLine : ts . ParsedCommandLine = ts . parseJsonConfigFileContent (
185+ tsconfig ,
186+ ts . sys ,
187+ this . _absoluteRootFolder
188+ ) ;
189+
190+ this . _updateCommandLineForTypescriptLibPackage ( commandLine , options ) ;
178191
179192 const normalizedEntryPointFile : string = path . normalize (
180- path . resolve ( this . _absoluteRootFolder , this . actualConfig . project . entryPointSourceFile ) ) ;
193+ path . resolve ( this . _absoluteRootFolder , this . actualConfig . project . entryPointSourceFile )
194+ ) ;
181195
182- // Append the normalizedEntryPointFile and remove any source files from the list
183- const analysisFilePaths : string [ ] = Extractor . generateFilePathsForAnalysis ( commandLine . fileNames
184- . concat ( normalizedEntryPointFile ) ) ;
196+ // Append the normalizedEntryPointFile and remove any non-declaration files from the list
197+ const analysisFilePaths : string [ ] = Extractor . generateFilePathsForAnalysis (
198+ commandLine . fileNames . concat ( normalizedEntryPointFile )
199+ ) ;
185200
186201 this . _program = ts . createProgram ( analysisFilePaths , commandLine . options ) ;
187202
@@ -262,7 +277,7 @@ export class Extractor {
262277 throw new Error ( 'The configuration object wasn\'t normalized properly' ) ;
263278 }
264279
265- if ( ! Extractor . _outputFileExtensionRegExp . test ( projectConfig . entryPointSourceFile ) ) {
280+ if ( ! Extractor . _declarationFileExtensionRegExp . test ( projectConfig . entryPointSourceFile ) ) {
266281 throw new Error ( 'The entry point is not a declaration file: ' + projectConfig . entryPointSourceFile ) ;
267282 }
268283
@@ -442,4 +457,47 @@ export class Extractor {
442457 }
443458 return path . relative ( this . _absoluteRootFolder , absolutePath ) . replace ( / \\ / g, '/' ) ;
444459 }
460+
461+ private _updateCommandLineForTypescriptLibPackage (
462+ commandLine : ts . ParsedCommandLine ,
463+ options : IExtractorOptions
464+ ) : void {
465+ if ( options . typescriptLibPackagePath ) {
466+ commandLine . options . noLib = true ;
467+ const compilerLibDirectory : string = path . join ( options . typescriptLibPackagePath , 'lib' ) ;
468+
469+ let foundBaseLib : boolean = false ;
470+ const filesToAdd : string [ ] = [ ] ;
471+ for ( const libFilename of commandLine . options . lib || [ ] ) {
472+ if ( libFilename === 'lib.d.ts' ) {
473+ // Ignore the default lib - it'll get added later
474+ continue ;
475+ }
476+
477+ if ( libFilename === 'lib.es5.d.ts' || libFilename === 'lib.es6.d.ts' ) {
478+ foundBaseLib = true ;
479+ }
480+
481+ const libPath : string = path . join ( compilerLibDirectory , libFilename . toLowerCase ( ) ) ;
482+ if ( ! FileSystem . exists ( libPath ) ) {
483+ throw new Error ( `lib ${ libFilename } does not exist in the compiler specified in typescriptLibPackage` ) ;
484+ }
485+
486+ filesToAdd . push ( libPath ) ;
487+ }
488+
489+ if ( ! foundBaseLib ) {
490+ // If we didn't find another version of the base lib library, include the default
491+ filesToAdd . push ( path . join ( compilerLibDirectory , 'lib.d.ts' ) ) ;
492+ }
493+
494+ if ( ! commandLine . fileNames ) {
495+ commandLine . fileNames = [ ] ;
496+ }
497+
498+ commandLine . fileNames . push ( ...filesToAdd ) ;
499+
500+ commandLine . options . lib = undefined ;
501+ }
502+ }
445503}
0 commit comments