From a3158bff2717d94d52e1d7a3813b6772e61495e7 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Mon, 30 Jul 2018 23:12:30 +0300 Subject: [PATCH] refactor(ivy): minor re-organization of ngcc `PackageTransformer#transform` (#25406) In preparation of adding support for transforming `.d.ts` files. PR Close #25406 --- .../ngcc/src/transform/package_transformer.ts | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/packages/compiler-cli/src/ngcc/src/transform/package_transformer.ts b/packages/compiler-cli/src/ngcc/src/transform/package_transformer.ts index 22f45764fb..c500d757ea 100644 --- a/packages/compiler-cli/src/ngcc/src/transform/package_transformer.ts +++ b/packages/compiler-cli/src/ngcc/src/transform/package_transformer.ts @@ -10,7 +10,7 @@ import {dirname, relative, resolve} from 'path'; import {mkdir} from 'shelljs'; import * as ts from 'typescript'; -import {Analyzer} from '../analyzer'; +import {AnalyzedFile, Analyzer} from '../analyzer'; import {Esm2015ReflectionHost} from '../host/esm2015_host'; import {Esm5ReflectionHost} from '../host/esm5_host'; import {NgccReflectionHost} from '../host/ngcc_host'; @@ -47,12 +47,14 @@ export class PackageTransformer { const entryPoints = getEntryPoints(packagePath, format); entryPoints.forEach(entryPoint => { + const outputFiles: FileInfo[] = []; const options: ts.CompilerOptions = { allowJs: true, maxNodeModuleJsDepth: Infinity, rootDir: entryPoint.entryFileName, }; + // Create the TS program and necessary helpers. const host = ts.createCompilerHost(options); const packageProgram = ts.createProgram([entryPoint.entryFileName], options, host); const typeChecker = packageProgram.getTypeChecker(); @@ -62,18 +64,17 @@ export class PackageTransformer { const analyzer = new Analyzer(typeChecker, reflectionHost); const renderer = this.getRenderer(format, packageProgram, reflectionHost); + // Parse and analyze the files. const entryPointFile = packageProgram.getSourceFile(entryPoint.entryFileName) !; const parsedFiles = parser.parseFile(entryPointFile); - parsedFiles.forEach(parsedFile => { - const analyzedFile = analyzer.analyzeFile(parsedFile); - const targetPath = resolve( - targetNodeModules, relative(sourceNodeModules, analyzedFile.sourceFile.fileName)); - const {source, map} = renderer.renderFile(analyzedFile, targetPath); - this.writeFile(source); - if (map) { - this.writeFile(map); - } - }); + const analyzedFiles = parsedFiles.map(parsedFile => analyzer.analyzeFile(parsedFile)); + + // Transform the source files and source maps. + outputFiles.push(...this.transformSourceFiles( + analyzedFiles, sourceNodeModules, targetNodeModules, renderer)); + + // Write out all the transformed files. + outputFiles.forEach(file => this.writeFile(file)); }); } @@ -123,6 +124,27 @@ export class PackageTransformer { return src; } + transformSourceFiles( + analyzedFiles: AnalyzedFile[], sourceNodeModules: string, targetNodeModules: string, + renderer: Renderer): FileInfo[] { + const outputFiles: FileInfo[] = []; + + analyzedFiles.forEach(analyzedFile => { + // Tranform the source file based on the recorded changes. + const targetPath = + resolve(targetNodeModules, relative(sourceNodeModules, analyzedFile.sourceFile.fileName)); + const {source, map} = renderer.renderFile(analyzedFile, targetPath); + + // Add the transformed file (and source map, if available) to the list of output files. + outputFiles.push(source); + if (map) { + outputFiles.push(map); + } + }); + + return outputFiles; + } + writeFile(file: FileInfo): void { mkdir('-p', dirname(file.path)); writeFileSync(file.path, file.contents, 'utf8');