@@ -2568,108 +2568,25 @@ namespace ts {
25682568 * @param host An EmitHost.
25692569 * @param targetSourceFile An optional target source file to emit.
25702570 */
2571- export function getSourceFilesToEmit ( host : EmitHost , targetSourceFile ?: SourceFile ) {
2571+ export function getSourceFilesToEmit ( host : EmitHost , targetSourceFile ?: SourceFile ) : SourceFile [ ] {
25722572 const options = host . getCompilerOptions ( ) ;
2573+ const isSourceFileFromExternalLibrary = ( file : SourceFile ) => host . isSourceFileFromExternalLibrary ( file ) ;
25732574 if ( options . outFile || options . out ) {
25742575 const moduleKind = getEmitModuleKind ( options ) ;
25752576 const moduleEmitEnabled = moduleKind === ModuleKind . AMD || moduleKind === ModuleKind . System ;
2576- const sourceFiles = getAllEmittableSourceFiles ( ) ;
25772577 // Can emit only sources that are not declaration file and are either non module code or module with --module or --target es6 specified
2578- return filter ( sourceFiles , moduleEmitEnabled ? isNonDeclarationFile : isBundleEmitNonExternalModule ) ;
2578+ return filter ( host . getSourceFiles ( ) , sourceFile =>
2579+ ( moduleEmitEnabled || ! isExternalModule ( sourceFile ) ) && sourceFileMayBeEmitted ( sourceFile , options , isSourceFileFromExternalLibrary ) ) ;
25792580 }
25802581 else {
2581- const sourceFiles = targetSourceFile === undefined ? getAllEmittableSourceFiles ( ) : [ targetSourceFile ] ;
2582- return filterSourceFilesInDirectory ( sourceFiles , file => host . isSourceFileFromExternalLibrary ( file ) ) ;
2582+ const sourceFiles = targetSourceFile === undefined ? host . getSourceFiles ( ) : [ targetSourceFile ] ;
2583+ return filter ( sourceFiles , sourceFile => sourceFileMayBeEmitted ( sourceFile , options , isSourceFileFromExternalLibrary ) ) ;
25832584 }
2584-
2585- function getAllEmittableSourceFiles ( ) {
2586- return options . noEmitForJsFiles ? filter ( host . getSourceFiles ( ) , sourceFile => ! isSourceFileJavaScript ( sourceFile ) ) : host . getSourceFiles ( ) ;
2587- }
2588- }
2589-
2590- /** Don't call this for `--outFile`, just for `--outDir` or plain emit. */
2591- export function filterSourceFilesInDirectory ( sourceFiles : SourceFile [ ] , isSourceFileFromExternalLibrary : ( file : SourceFile ) => boolean ) : SourceFile [ ] {
2592- return filter ( sourceFiles , file => shouldEmitInDirectory ( file , isSourceFileFromExternalLibrary ) ) ;
2593- }
2594-
2595- function isNonDeclarationFile ( sourceFile : SourceFile ) {
2596- return ! isDeclarationFile ( sourceFile ) ;
2597- }
2598-
2599- /**
2600- * Whether a file should be emitted in a non-`--outFile` case.
2601- * Don't emit if source file is a declaration file, or was located under node_modules
2602- */
2603- function shouldEmitInDirectory ( sourceFile : SourceFile , isSourceFileFromExternalLibrary : ( file : SourceFile ) => boolean ) : boolean {
2604- return isNonDeclarationFile ( sourceFile ) && ! isSourceFileFromExternalLibrary ( sourceFile ) ;
26052585 }
26062586
2607- function isBundleEmitNonExternalModule ( sourceFile : SourceFile ) {
2608- return isNonDeclarationFile ( sourceFile ) && ! isExternalModule ( sourceFile ) ;
2609- }
2610-
2611- /**
2612- * Iterates over each source file to emit. The source files are expected to have been
2613- * transformed for use by the pretty printer.
2614- *
2615- * Originally part of `forEachExpectedEmitFile`, this functionality was extracted to support
2616- * transformations.
2617- *
2618- * @param host An EmitHost.
2619- * @param sourceFiles The transformed source files to emit.
2620- * @param action The action to execute.
2621- */
2622- export function forEachTransformedEmitFile ( host : EmitHost , sourceFiles : SourceFile [ ] ,
2623- action : ( jsFilePath : string , sourceMapFilePath : string , declarationFilePath : string , sourceFiles : SourceFile [ ] , isBundledEmit : boolean ) => void ,
2624- emitOnlyDtsFiles ?: boolean ) {
2625- const options = host . getCompilerOptions ( ) ;
2626- // Emit on each source file
2627- if ( options . outFile || options . out ) {
2628- onBundledEmit ( sourceFiles ) ;
2629- }
2630- else {
2631- for ( const sourceFile of sourceFiles ) {
2632- // Don't emit if source file is a declaration file, or was located under node_modules
2633- if ( ! isDeclarationFile ( sourceFile ) && ! host . isSourceFileFromExternalLibrary ( sourceFile ) ) {
2634- onSingleFileEmit ( host , sourceFile ) ;
2635- }
2636- }
2637- }
2638-
2639- function onSingleFileEmit ( host : EmitHost , sourceFile : SourceFile ) {
2640- // JavaScript files are always LanguageVariant.JSX, as JSX syntax is allowed in .js files also.
2641- // So for JavaScript files, '.jsx' is only emitted if the input was '.jsx', and JsxEmit.Preserve.
2642- // For TypeScript, the only time to emit with a '.jsx' extension, is on JSX input, and JsxEmit.Preserve
2643- let extension = ".js" ;
2644- if ( options . jsx === JsxEmit . Preserve ) {
2645- if ( isSourceFileJavaScript ( sourceFile ) ) {
2646- if ( fileExtensionIs ( sourceFile . fileName , ".jsx" ) ) {
2647- extension = ".jsx" ;
2648- }
2649- }
2650- else if ( sourceFile . languageVariant === LanguageVariant . JSX ) {
2651- // TypeScript source file preserving JSX syntax
2652- extension = ".jsx" ;
2653- }
2654- }
2655- const jsFilePath = getOwnEmitOutputFilePath ( sourceFile , host , extension ) ;
2656- const sourceMapFilePath = getSourceMapFilePath ( jsFilePath , options ) ;
2657- const declarationFilePath = ! isSourceFileJavaScript ( sourceFile ) && ( options . declaration || emitOnlyDtsFiles ) ? getDeclarationEmitOutputFilePath ( sourceFile , host ) : undefined ;
2658- action ( jsFilePath , sourceMapFilePath , declarationFilePath , [ sourceFile ] , /*isBundledEmit*/ false ) ;
2659- }
2660-
2661- function onBundledEmit ( sourceFiles : SourceFile [ ] ) {
2662- if ( sourceFiles . length ) {
2663- const jsFilePath = options . outFile || options . out ;
2664- const sourceMapFilePath = getSourceMapFilePath ( jsFilePath , options ) ;
2665- const declarationFilePath = options . declaration ? removeFileExtension ( jsFilePath ) + ".d.ts" : undefined ;
2666- action ( jsFilePath , sourceMapFilePath , declarationFilePath , sourceFiles , /*isBundledEmit*/ true ) ;
2667- }
2668- }
2669- }
2670-
2671- function getSourceMapFilePath ( jsFilePath : string , options : CompilerOptions ) {
2672- return options . sourceMap ? jsFilePath + ".map" : undefined ;
2587+ /** Don't call this for `--outFile`, just for `--outDir` or plain emit. `--outFile` needs additional checks. */
2588+ export function sourceFileMayBeEmitted ( sourceFile : SourceFile , options : CompilerOptions , isSourceFileFromExternalLibrary : ( file : SourceFile ) => boolean ) {
2589+ return ! ( options . noEmitForJsFiles && isSourceFileJavaScript ( sourceFile ) ) && ! isDeclarationFile ( sourceFile ) && ! isSourceFileFromExternalLibrary ( sourceFile ) ;
26732590 }
26742591
26752592 /**
@@ -2685,64 +2602,55 @@ namespace ts {
26852602 action : ( emitFileNames : EmitFileNames , sourceFiles : SourceFile [ ] , isBundledEmit : boolean , emitOnlyDtsFiles : boolean ) => void ,
26862603 targetSourceFile ?: SourceFile ,
26872604 emitOnlyDtsFiles ?: boolean ) {
2605+ forEachEmittedFile ( host , getSourceFilesToEmit ( host , targetSourceFile ) , action , emitOnlyDtsFiles ) ;
2606+ }
2607+
2608+ /**
2609+ * Iterates over each source file to emit.
2610+ */
2611+ export function forEachEmittedFile ( host : EmitHost , sourceFiles : SourceFile [ ] ,
2612+ action : ( emitFileNames : EmitFileNames , sourceFiles : SourceFile [ ] , isBundledEmit : boolean , emitOnlyDtsFiles : boolean ) => void ,
2613+ emitOnlyDtsFiles ?: boolean ) {
26882614 const options = host . getCompilerOptions ( ) ;
2689- // Emit on each source file
26902615 if ( options . outFile || options . out ) {
2691- onBundledEmit ( host ) ;
2616+ if ( sourceFiles . length ) {
2617+ const jsFilePath = options . outFile || options . out ;
2618+ const sourceMapFilePath = getSourceMapFilePath ( jsFilePath , options ) ;
2619+ const declarationFilePath = options . declaration ? removeFileExtension ( jsFilePath ) + ".d.ts" : undefined ;
2620+ action ( { jsFilePath, sourceMapFilePath, declarationFilePath } , sourceFiles , /*isBundledEmit*/ true , emitOnlyDtsFiles ) ;
2621+ }
26922622 }
26932623 else {
2694- const sourceFiles = targetSourceFile === undefined ? getSourceFilesToEmit ( host ) : [ targetSourceFile ] ;
26952624 for ( const sourceFile of sourceFiles ) {
2696- if ( shouldEmitInDirectory ( sourceFile , file => host . isSourceFileFromExternalLibrary ( file ) ) ) {
2697- onSingleFileEmit ( host , sourceFile ) ;
2698- }
2625+ const options = host . getCompilerOptions ( ) ;
2626+ const jsFilePath = getOwnEmitOutputFilePath ( sourceFile , host , getOutputExtension ( sourceFile , options ) ) ;
2627+ const sourceMapFilePath = getSourceMapFilePath ( jsFilePath , options ) ;
2628+ const declarationFilePath = ! isSourceFileJavaScript ( sourceFile ) && ( emitOnlyDtsFiles || options . declaration ) ? getDeclarationEmitOutputFilePath ( sourceFile , host ) : undefined ;
2629+ action ( { jsFilePath, sourceMapFilePath, declarationFilePath } , [ sourceFile ] , /*isBundledEmit*/ false , emitOnlyDtsFiles ) ;
26992630 }
27002631 }
2632+ }
27012633
2702- function onSingleFileEmit ( host : EmitHost , sourceFile : SourceFile ) {
2703- // JavaScript files are always LanguageVariant.JSX, as JSX syntax is allowed in .js files also.
2704- // So for JavaScript files, '.jsx' is only emitted if the input was '.jsx', and JsxEmit.Preserve.
2705- // For TypeScript, the only time to emit with a '.jsx' extension, is on JSX input, and JsxEmit.Preserve
2706- let extension = ".js" ;
2707- if ( options . jsx === JsxEmit . Preserve ) {
2708- if ( isSourceFileJavaScript ( sourceFile ) ) {
2709- if ( fileExtensionIs ( sourceFile . fileName , ".jsx" ) ) {
2710- extension = ".jsx" ;
2711- }
2712- }
2713- else if ( sourceFile . languageVariant === LanguageVariant . JSX ) {
2714- // TypeScript source file preserving JSX syntax
2715- extension = ".jsx" ;
2634+ function getSourceMapFilePath ( jsFilePath : string , options : CompilerOptions ) {
2635+ return options . sourceMap ? jsFilePath + ".map" : undefined ;
2636+ }
2637+
2638+ // JavaScript files are always LanguageVariant.JSX, as JSX syntax is allowed in .js files also.
2639+ // So for JavaScript files, '.jsx' is only emitted if the input was '.jsx', and JsxEmit.Preserve.
2640+ // For TypeScript, the only time to emit with a '.jsx' extension, is on JSX input, and JsxEmit.Preserve
2641+ function getOutputExtension ( sourceFile : SourceFile , options : CompilerOptions ) : string {
2642+ if ( options . jsx === JsxEmit . Preserve ) {
2643+ if ( isSourceFileJavaScript ( sourceFile ) ) {
2644+ if ( fileExtensionIs ( sourceFile . fileName , ".jsx" ) ) {
2645+ return ".jsx" ;
27162646 }
27172647 }
2718- const jsFilePath = getOwnEmitOutputFilePath ( sourceFile , host , extension ) ;
2719- const declarationFilePath = ! isSourceFileJavaScript ( sourceFile ) && ( emitOnlyDtsFiles || options . declaration ) ? getDeclarationEmitOutputFilePath ( sourceFile , host ) : undefined ;
2720- const emitFileNames : EmitFileNames = {
2721- jsFilePath,
2722- sourceMapFilePath : getSourceMapFilePath ( jsFilePath , options ) ,
2723- declarationFilePath
2724- } ;
2725- action ( emitFileNames , [ sourceFile ] , /*isBundledEmit*/ false , emitOnlyDtsFiles ) ;
2726- }
2727-
2728- function onBundledEmit ( host : EmitHost ) {
2729- // Can emit only sources that are not declaration file and are either non module code or module with
2730- // --module or --target es6 specified. Files included by searching under node_modules are also not emitted.
2731- const bundledSources = filter ( getSourceFilesToEmit ( host ) ,
2732- sourceFile => ! isDeclarationFile ( sourceFile ) &&
2733- ! host . isSourceFileFromExternalLibrary ( sourceFile ) &&
2734- ( ! isExternalModule ( sourceFile ) ||
2735- ! ! getEmitModuleKind ( options ) ) ) ;
2736- if ( bundledSources . length ) {
2737- const jsFilePath = options . outFile || options . out ;
2738- const emitFileNames : EmitFileNames = {
2739- jsFilePath,
2740- sourceMapFilePath : getSourceMapFilePath ( jsFilePath , options ) ,
2741- declarationFilePath : options . declaration ? removeFileExtension ( jsFilePath ) + ".d.ts" : undefined
2742- } ;
2743- action ( emitFileNames , bundledSources , /*isBundledEmit*/ true , emitOnlyDtsFiles ) ;
2648+ else if ( sourceFile . languageVariant === LanguageVariant . JSX ) {
2649+ // TypeScript source file preserving JSX syntax
2650+ return ".jsx" ;
27442651 }
27452652 }
2653+ return ".js" ;
27462654 }
27472655
27482656 export function getSourceFilePathInNewDir ( sourceFile : SourceFile , host : EmitHost , newDirPath : string ) {
0 commit comments