1namespace ts.tscWatch { 2 it("unittests:: tsbuildWatch:: watchMode:: Public API with custom transformers", () => { 3 const solution: File = { 4 path: `${projectRoot}/tsconfig.json`, 5 content: JSON.stringify({ 6 references: [ 7 { path: "./shared/tsconfig.json" }, 8 { path: "./webpack/tsconfig.json" } 9 ], 10 files: [] 11 }) 12 }; 13 const sharedConfig: File = { 14 path: `${projectRoot}/shared/tsconfig.json`, 15 content: JSON.stringify({ 16 compilerOptions: { composite: true }, 17 }) 18 }; 19 const sharedIndex: File = { 20 path: `${projectRoot}/shared/index.ts`, 21 content: `export function f1() { } 22export class c { } 23export enum e { } 24// leading 25export function f2() { } // trailing` 26 }; 27 const webpackConfig: File = { 28 path: `${projectRoot}/webpack/tsconfig.json`, 29 content: JSON.stringify({ 30 compilerOptions: { composite: true, }, 31 references: [{ path: "../shared/tsconfig.json" }] 32 }) 33 }; 34 const webpackIndex: File = { 35 path: `${projectRoot}/webpack/index.ts`, 36 content: `export function f2() { } 37export class c2 { } 38export enum e2 { } 39// leading 40export function f22() { } // trailing` 41 }; 42 const commandLineArgs = ["--b", "--w"]; 43 const { sys, baseline, oldSnap, cb, getPrograms } = createBaseline(createWatchedSystem([libFile, solution, sharedConfig, sharedIndex, webpackConfig, webpackIndex], { currentDirectory: projectRoot })); 44 const buildHost = createSolutionBuilderWithWatchHostForBaseline(sys, cb); 45 buildHost.getCustomTransformers = getCustomTransformers; 46 const builder = createSolutionBuilderWithWatch(buildHost, [solution.path], { verbose: true }); 47 builder.build(); 48 runWatchBaseline({ 49 scenario: "publicApi", 50 subScenario: "with custom transformers", 51 commandLineArgs, 52 sys, 53 baseline, 54 oldSnap, 55 getPrograms, 56 changes: [ 57 { 58 caption: "change to shared", 59 change: sys => sys.prependFile(sharedIndex.path, "export function fooBar() {}"), 60 timeouts: sys => { 61 sys.checkTimeoutQueueLengthAndRun(1); // Shared 62 sys.checkTimeoutQueueLengthAndRun(1); // webpack and solution 63 sys.checkTimeoutQueueLength(0); 64 } 65 } 66 ], 67 watchOrSolution: builder 68 }); 69 70 function getCustomTransformers(project: string): CustomTransformers { 71 const before: TransformerFactory<SourceFile> = context => { 72 return file => visitEachChild(file, visit, context); 73 function visit(node: Node): VisitResult<Node> { 74 switch (node.kind) { 75 case SyntaxKind.FunctionDeclaration: 76 return visitFunction(node as FunctionDeclaration); 77 default: 78 return visitEachChild(node, visit, context); 79 } 80 } 81 function visitFunction(node: FunctionDeclaration) { 82 addSyntheticLeadingComment(node, SyntaxKind.MultiLineCommentTrivia, `@before${project}`, /*hasTrailingNewLine*/ true); 83 return node; 84 } 85 }; 86 87 const after: TransformerFactory<SourceFile> = context => { 88 return file => visitEachChild(file, visit, context); 89 function visit(node: Node): VisitResult<Node> { 90 switch (node.kind) { 91 case SyntaxKind.VariableStatement: 92 return visitVariableStatement(node as VariableStatement); 93 default: 94 return visitEachChild(node, visit, context); 95 } 96 } 97 function visitVariableStatement(node: VariableStatement) { 98 addSyntheticLeadingComment(node, SyntaxKind.SingleLineCommentTrivia, `@after${project}`); 99 return node; 100 } 101 }; 102 return { before: [before], after: [after] }; 103 } 104 }); 105}