1
+ namespace ts {
2
+ describe ( "unittests:: tsbuild:: Public API with custom transformers when passed to build" , ( ) => {
3
+ let sys : TscCompileSystem ;
4
+ before ( ( ) => {
5
+ const initialFs = getFsWithTime ( loadProjectFromFiles ( {
6
+ "/src/tsconfig.json" : JSON . stringify ( {
7
+ references : [
8
+ { path : "./shared/tsconfig.json" } ,
9
+ { path : "./webpack/tsconfig.json" }
10
+ ] ,
11
+ files : [ ]
12
+ } ) ,
13
+ "/src/shared/tsconfig.json" : JSON . stringify ( {
14
+ compilerOptions : { composite : true } ,
15
+ } ) ,
16
+ "/src/shared/index.ts" : `export function f1() { }
17
+ export class c { }
18
+ export enum e { }
19
+ // leading
20
+ export function f2() { } // trailing` ,
21
+ "/src/webpack/tsconfig.json" : JSON . stringify ( {
22
+ compilerOptions : {
23
+ composite : true ,
24
+ } ,
25
+ references : [ { path : "../shared/tsconfig.json" } ]
26
+ } ) ,
27
+ "/src/webpack/index.ts" : `export function f2() { }
28
+ export class c2 { }
29
+ export enum e2 { }
30
+ // leading
31
+ export function f22() { } // trailing` ,
32
+ } ) ) . fs . makeReadonly ( ) ;
33
+ const inputFs = initialFs . shadow ( ) ;
34
+ inputFs . makeReadonly ( ) ;
35
+ const fs = inputFs . shadow ( ) ;
36
+
37
+ // Create system
38
+ sys = new fakes . System ( fs , { executingFilePath : "/lib/tsc" } ) as TscCompileSystem ;
39
+ fakes . patchHostForBuildInfoReadWrite ( sys ) ;
40
+ const commandLineArgs = [ "--b" , "/src/tsconfig.json" ] ;
41
+ sys . write ( `${ sys . getExecutingFilePath ( ) } ${ commandLineArgs . join ( " " ) } \n` ) ;
42
+ sys . exit = exitCode => sys . exitCode = exitCode ;
43
+ const writtenFiles = sys . writtenFiles = new Set ( ) ;
44
+ const originalWriteFile = sys . writeFile ;
45
+ sys . writeFile = ( fileName , content , writeByteOrderMark ) => {
46
+ const path = toPathWithSystem ( sys , fileName ) ;
47
+ assert . isFalse ( writtenFiles . has ( path ) ) ;
48
+ writtenFiles . add ( path ) ;
49
+ return originalWriteFile . call ( sys , fileName , content , writeByteOrderMark ) ;
50
+ } ;
51
+ const { cb, getPrograms } = commandLineCallbacks ( sys , /*originalReadCall*/ undefined , originalWriteFile ) ;
52
+ const buildHost = createSolutionBuilderHost (
53
+ sys ,
54
+ /*createProgram*/ undefined ,
55
+ createDiagnosticReporter ( sys , /*pretty*/ true ) ,
56
+ createBuilderStatusReporter ( sys , /*pretty*/ true ) ,
57
+ errorCount => sys . write ( getErrorSummaryText ( errorCount , sys . newLine ) )
58
+ ) ;
59
+ buildHost . afterProgramEmitAndDiagnostics = cb ;
60
+ buildHost . afterEmitBundle = cb ;
61
+ const builder = createSolutionBuilder ( buildHost , [ commandLineArgs [ 1 ] ] , { verbose : true } ) ;
62
+ const exitStatus = builder . build ( /*project*/ undefined , /*cancellationToken*/ undefined , /*writeFile*/ undefined , getCustomTransformers ) ;
63
+ sys . exit ( exitStatus ) ;
64
+ sys . write ( `exitCode:: ExitStatus.${ ExitStatus [ sys . exitCode as ExitStatus ] } \n` ) ;
65
+ const baseline : string [ ] = [ ] ;
66
+ tscWatch . baselinePrograms ( baseline , getPrograms , emptyArray , /*baselineDependencies*/ false ) ;
67
+ sys . write ( baseline . join ( "\n" ) ) ;
68
+ fs . makeReadonly ( ) ;
69
+ sys . baseLine = ( ) => {
70
+ const baseFsPatch = inputFs . diff ( /*base*/ undefined , { baseIsNotShadowRoot : true } ) ;
71
+ const patch = fs . diff ( inputFs , { includeChangedFileWithSameContent : true } ) ;
72
+ return {
73
+ file : `tsbuild/$publicAPI/${ BuildKind . Initial } /${ "build with custom transformers" . split ( " " ) . join ( "-" ) } .js` ,
74
+ text : `Input::
75
+ ${ baseFsPatch ? vfs . formatPatch ( baseFsPatch ) : "" }
76
+
77
+ Output::
78
+ ${ sys . output . join ( "" ) }
79
+
80
+ ${ patch ? vfs . formatPatch ( patch ) : "" } `
81
+ } ;
82
+ } ;
83
+
84
+ function getCustomTransformers ( project : string ) : CustomTransformers {
85
+ const before : TransformerFactory < SourceFile > = context => {
86
+ return file => visitEachChild ( file , visit , context ) ;
87
+ function visit ( node : Node ) : VisitResult < Node > {
88
+ switch ( node . kind ) {
89
+ case SyntaxKind . FunctionDeclaration :
90
+ return visitFunction ( < FunctionDeclaration > node ) ;
91
+ default :
92
+ return visitEachChild ( node , visit , context ) ;
93
+ }
94
+ }
95
+ function visitFunction ( node : FunctionDeclaration ) {
96
+ addSyntheticLeadingComment ( node , SyntaxKind . MultiLineCommentTrivia , `@before${ project } ` , /*hasTrailingNewLine*/ true ) ;
97
+ return node ;
98
+ }
99
+ } ;
100
+
101
+ const after : TransformerFactory < SourceFile > = context => {
102
+ return file => visitEachChild ( file , visit , context ) ;
103
+ function visit ( node : Node ) : VisitResult < Node > {
104
+ switch ( node . kind ) {
105
+ case SyntaxKind . VariableStatement :
106
+ return visitVariableStatement ( < VariableStatement > node ) ;
107
+ default :
108
+ return visitEachChild ( node , visit , context ) ;
109
+ }
110
+ }
111
+ function visitVariableStatement ( node : VariableStatement ) {
112
+ addSyntheticLeadingComment ( node , SyntaxKind . SingleLineCommentTrivia , `@after${ project } ` ) ;
113
+ return node ;
114
+ }
115
+ } ;
116
+ return { before : [ before ] , after : [ after ] } ;
117
+ }
118
+ } ) ;
119
+ after ( ( ) => {
120
+ sys = undefined ! ;
121
+ } ) ;
122
+ verifyTscBaseline ( ( ) => sys ) ;
123
+ } ) ;
124
+ }
0 commit comments