@@ -2568,102 +2568,57 @@ namespace ts {
2568
2568
* @param host An EmitHost.
2569
2569
* @param targetSourceFile An optional target source file to emit.
2570
2570
*/
2571
- export function getSourceFilesToEmit ( host : EmitHost , targetSourceFile ?: SourceFile ) {
2571
+ export function getSourceFilesToEmit ( host : EmitHost , targetSourceFile ?: SourceFile ) : SourceFile [ ] {
2572
2572
const options = host . getCompilerOptions ( ) ;
2573
+ const isSourceFileFromExternalLibrary = ( file : SourceFile ) => host . isSourceFileFromExternalLibrary ( file ) ;
2573
2574
if ( options . outFile || options . out ) {
2574
2575
const moduleKind = getEmitModuleKind ( options ) ;
2575
2576
const moduleEmitEnabled = moduleKind === ModuleKind . AMD || moduleKind === ModuleKind . System ;
2576
- const sourceFiles = getAllEmittableSourceFiles ( ) ;
2577
2577
// 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 ) ) ;
2579
2580
}
2580
2581
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 ) ) ;
2583
2584
}
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 ) ;
2605
2585
}
2606
2586
2607
- function isBundleEmitNonExternalModule ( sourceFile : SourceFile ) {
2608
- return isNonDeclarationFile ( sourceFile ) && ! isExternalModule ( sourceFile ) ;
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 ) ;
2609
2590
}
2610
2591
2611
2592
/**
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.
2593
+ * Iterates over the source files that are expected to have an emit output.
2617
2594
*
2618
2595
* @param host An EmitHost.
2619
- * @param sourceFiles The transformed source files to emit.
2620
2596
* @param action The action to execute.
2597
+ * @param sourceFilesOrTargetSourceFile
2598
+ * If an array, the full list of source files to emit.
2599
+ * Else, calls `getSourceFilesToEmit` with the (optional) target source file to determine the list of source files to emit.
2621
2600
*/
2622
- export function forEachTransformedEmitFile ( host : EmitHost , sourceFiles : SourceFile [ ] ,
2623
- action : ( jsFilePath : string , sourceMapFilePath : string , declarationFilePath : string , sourceFiles : SourceFile [ ] , isBundledEmit : boolean ) => void ,
2601
+ export function forEachEmittedFile (
2602
+ host : EmitHost , action : ( emitFileNames : EmitFileNames , sourceFiles : SourceFile [ ] , isBundledEmit : boolean , emitOnlyDtsFiles : boolean ) => void ,
2603
+ sourceFilesOrTargetSourceFile ?: SourceFile [ ] | SourceFile ,
2624
2604
emitOnlyDtsFiles ?: boolean ) {
2605
+
2606
+ const sourceFiles = isArray ( sourceFilesOrTargetSourceFile ) ? sourceFilesOrTargetSourceFile : getSourceFilesToEmit ( host , sourceFilesOrTargetSourceFile ) ;
2625
2607
const options = host . getCompilerOptions ( ) ;
2626
- // Emit on each source file
2627
2608
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
2609
if ( sourceFiles . length ) {
2663
2610
const jsFilePath = options . outFile || options . out ;
2664
2611
const sourceMapFilePath = getSourceMapFilePath ( jsFilePath , options ) ;
2665
2612
const declarationFilePath = options . declaration ? removeFileExtension ( jsFilePath ) + ".d.ts" : undefined ;
2666
- action ( jsFilePath , sourceMapFilePath , declarationFilePath , sourceFiles , /*isBundledEmit*/ true ) ;
2613
+ action ( { jsFilePath, sourceMapFilePath, declarationFilePath } , sourceFiles , /*isBundledEmit*/ true , emitOnlyDtsFiles ) ;
2614
+ }
2615
+ }
2616
+ else {
2617
+ for ( const sourceFile of sourceFiles ) {
2618
+ const jsFilePath = getOwnEmitOutputFilePath ( sourceFile , host , getOutputExtension ( sourceFile , options ) ) ;
2619
+ const sourceMapFilePath = getSourceMapFilePath ( jsFilePath , options ) ;
2620
+ const declarationFilePath = ! isSourceFileJavaScript ( sourceFile ) && ( emitOnlyDtsFiles || options . declaration ) ? getDeclarationEmitOutputFilePath ( sourceFile , host ) : undefined ;
2621
+ action ( { jsFilePath, sourceMapFilePath, declarationFilePath } , [ sourceFile ] , /*isBundledEmit*/ false , emitOnlyDtsFiles ) ;
2667
2622
}
2668
2623
}
2669
2624
}
@@ -2672,77 +2627,22 @@ namespace ts {
2672
2627
return options . sourceMap ? jsFilePath + ".map" : undefined ;
2673
2628
}
2674
2629
2675
- /**
2676
- * Iterates over the source files that are expected to have an emit output. This function
2677
- * is used by the legacy emitter and the declaration emitter and should not be used by
2678
- * the tree transforming emitter.
2679
- *
2680
- * @param host An EmitHost.
2681
- * @param action The action to execute.
2682
- * @param targetSourceFile An optional target source file to emit.
2683
- */
2684
- export function forEachExpectedEmitFile ( host : EmitHost ,
2685
- action : ( emitFileNames : EmitFileNames , sourceFiles : SourceFile [ ] , isBundledEmit : boolean , emitOnlyDtsFiles : boolean ) => void ,
2686
- targetSourceFile ?: SourceFile ,
2687
- emitOnlyDtsFiles ?: boolean ) {
2688
- const options = host . getCompilerOptions ( ) ;
2689
- // Emit on each source file
2690
- if ( options . outFile || options . out ) {
2691
- onBundledEmit ( host ) ;
2692
- }
2693
- else {
2694
- const sourceFiles = targetSourceFile === undefined ? getSourceFilesToEmit ( host ) : [ targetSourceFile ] ;
2695
- for ( const sourceFile of sourceFiles ) {
2696
- if ( shouldEmitInDirectory ( sourceFile , file => host . isSourceFileFromExternalLibrary ( file ) ) ) {
2697
- onSingleFileEmit ( host , sourceFile ) ;
2698
- }
2699
- }
2700
- }
2701
-
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" ;
2630
+ // JavaScript files are always LanguageVariant.JSX, as JSX syntax is allowed in .js files also.
2631
+ // So for JavaScript files, '.jsx' is only emitted if the input was '.jsx', and JsxEmit.Preserve.
2632
+ // For TypeScript, the only time to emit with a '.jsx' extension, is on JSX input, and JsxEmit.Preserve
2633
+ function getOutputExtension ( sourceFile : SourceFile , options : CompilerOptions ) : string {
2634
+ if ( options . jsx === JsxEmit . Preserve ) {
2635
+ if ( isSourceFileJavaScript ( sourceFile ) ) {
2636
+ if ( fileExtensionIs ( sourceFile . fileName , ".jsx" ) ) {
2637
+ return ".jsx" ;
2716
2638
}
2717
2639
}
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 ) ;
2640
+ else if ( sourceFile . languageVariant === LanguageVariant . JSX ) {
2641
+ // TypeScript source file preserving JSX syntax
2642
+ return ".jsx" ;
2744
2643
}
2745
2644
}
2645
+ return ".js" ;
2746
2646
}
2747
2647
2748
2648
export function getSourceFilePathInNewDir ( sourceFile : SourceFile , host : EmitHost , newDirPath : string ) {
0 commit comments