refactor(ivy): minor re-organization of ngcc `PackageTransformer#transform` (#25406)

In preparation of adding support for transforming `.d.ts` files.

PR Close #25406
This commit is contained in:
George Kalpakas 2018-07-30 23:12:30 +03:00 committed by Matias Niemelä
parent d6e91ba545
commit a3158bff27
1 changed files with 33 additions and 11 deletions

View File

@ -10,7 +10,7 @@ import {dirname, relative, resolve} from 'path';
import {mkdir} from 'shelljs'; import {mkdir} from 'shelljs';
import * as ts from 'typescript'; import * as ts from 'typescript';
import {Analyzer} from '../analyzer'; import {AnalyzedFile, Analyzer} from '../analyzer';
import {Esm2015ReflectionHost} from '../host/esm2015_host'; import {Esm2015ReflectionHost} from '../host/esm2015_host';
import {Esm5ReflectionHost} from '../host/esm5_host'; import {Esm5ReflectionHost} from '../host/esm5_host';
import {NgccReflectionHost} from '../host/ngcc_host'; import {NgccReflectionHost} from '../host/ngcc_host';
@ -47,12 +47,14 @@ export class PackageTransformer {
const entryPoints = getEntryPoints(packagePath, format); const entryPoints = getEntryPoints(packagePath, format);
entryPoints.forEach(entryPoint => { entryPoints.forEach(entryPoint => {
const outputFiles: FileInfo[] = [];
const options: ts.CompilerOptions = { const options: ts.CompilerOptions = {
allowJs: true, allowJs: true,
maxNodeModuleJsDepth: Infinity, maxNodeModuleJsDepth: Infinity,
rootDir: entryPoint.entryFileName, rootDir: entryPoint.entryFileName,
}; };
// Create the TS program and necessary helpers.
const host = ts.createCompilerHost(options); const host = ts.createCompilerHost(options);
const packageProgram = ts.createProgram([entryPoint.entryFileName], options, host); const packageProgram = ts.createProgram([entryPoint.entryFileName], options, host);
const typeChecker = packageProgram.getTypeChecker(); const typeChecker = packageProgram.getTypeChecker();
@ -62,18 +64,17 @@ export class PackageTransformer {
const analyzer = new Analyzer(typeChecker, reflectionHost); const analyzer = new Analyzer(typeChecker, reflectionHost);
const renderer = this.getRenderer(format, packageProgram, reflectionHost); const renderer = this.getRenderer(format, packageProgram, reflectionHost);
// Parse and analyze the files.
const entryPointFile = packageProgram.getSourceFile(entryPoint.entryFileName) !; const entryPointFile = packageProgram.getSourceFile(entryPoint.entryFileName) !;
const parsedFiles = parser.parseFile(entryPointFile); const parsedFiles = parser.parseFile(entryPointFile);
parsedFiles.forEach(parsedFile => { const analyzedFiles = parsedFiles.map(parsedFile => analyzer.analyzeFile(parsedFile));
const analyzedFile = analyzer.analyzeFile(parsedFile);
const targetPath = resolve( // Transform the source files and source maps.
targetNodeModules, relative(sourceNodeModules, analyzedFile.sourceFile.fileName)); outputFiles.push(...this.transformSourceFiles(
const {source, map} = renderer.renderFile(analyzedFile, targetPath); analyzedFiles, sourceNodeModules, targetNodeModules, renderer));
this.writeFile(source);
if (map) { // Write out all the transformed files.
this.writeFile(map); outputFiles.forEach(file => this.writeFile(file));
}
});
}); });
} }
@ -123,6 +124,27 @@ export class PackageTransformer {
return src; 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 { writeFile(file: FileInfo): void {
mkdir('-p', dirname(file.path)); mkdir('-p', dirname(file.path));
writeFileSync(file.path, file.contents, 'utf8'); writeFileSync(file.path, file.contents, 'utf8');