fix(compiler): compile .ngfactory.ts files even if nobody references them. (#16899)

This is especially important for library authors, as they will
not reference the .ngfactory.ts files.

Fixes #16741
This commit is contained in:
Tobias Bosch
2017-05-25 10:00:26 -07:00
committed by Chuck Jazdzewski
parent 966eb2fbd0
commit 573b8611bc
10 changed files with 165 additions and 58 deletions

View File

@ -36,18 +36,19 @@ export class CodeGenerator {
public host: ts.CompilerHost, private compiler: compiler.AotCompiler,
private ngCompilerHost: CompilerHost) {}
codegen(): Promise<any> {
codegen(): Promise<string[]> {
return this.compiler
.analyzeModulesAsync(this.program.getSourceFiles().map(
sf => this.ngCompilerHost.getCanonicalFileName(sf.fileName)))
.then(analyzedModules => this.compiler.emitAllImpls(analyzedModules))
.then(generatedModules => {
generatedModules.forEach(generatedModule => {
return generatedModules.map(generatedModule => {
const sourceFile = this.program.getSourceFile(generatedModule.srcFileUrl);
const emitPath = this.ngCompilerHost.calculateEmitPath(generatedModule.genFileUrl);
const source =
generatedModule.source || compiler.toTypeScript(generatedModule, PREAMBLE);
this.host.writeFile(emitPath, source, false, () => {}, [sourceFile]);
return emitPath;
});
});
}

View File

@ -33,6 +33,7 @@ export class CompilerHost implements AotCompilerHost {
private resolverCache = new Map<string, ModuleMetadata[]>();
private bundleIndexCache = new Map<string, boolean>();
private bundleIndexNames = new Set<string>();
private bundleRedirectNames = new Set<string>();
private moduleFileNames = new Map<string, string|null>();
protected resolveModuleNameHost: CompilerHostContext;
@ -280,7 +281,8 @@ export class CompilerHost implements AotCompilerHost {
// Check for a bundle index.
if (this.hasBundleIndex(filePath)) {
const normalFilePath = path.normalize(filePath);
return this.bundleIndexNames.has(normalFilePath);
return this.bundleIndexNames.has(normalFilePath) ||
this.bundleRedirectNames.has(normalFilePath);
}
}
return true;
@ -331,7 +333,13 @@ export class CompilerHost implements AotCompilerHost {
const metadataFile = typings.replace(DTS, '.metadata.json');
if (this.context.fileExists(metadataFile)) {
const metadata = JSON.parse(this.context.readFile(metadataFile));
if (metadata.importAs) {
if (metadata.bundleRedirect) {
this.bundleRedirectNames.add(typings);
// Note: don't set result = true,
// as this would mark this folder
// as having a bundleIndex too early without
// filling the bundleIndexNames.
} else if (metadata.importAs) {
this.bundleIndexNames.add(typings);
result = true;
}

View File

@ -21,7 +21,7 @@ import {Extractor} from './extractor';
function extract(
ngOptions: tsc.AngularCompilerOptions, cliOptions: tsc.I18nExtractionCliOptions,
program: ts.Program, host: ts.CompilerHost): Promise<void> {
program: ts.Program, host: ts.CompilerHost) {
return Extractor.create(ngOptions, program, host, cliOptions.locale)
.extract(cliOptions.i18nFormat !, cliOptions.outFile);
}

View File

@ -27,7 +27,7 @@ export class Extractor {
public host: ts.CompilerHost, private ngCompilerHost: CompilerHost,
private program: ts.Program) {}
extract(formatName: string, outFile: string|null): Promise<void> {
extract(formatName: string, outFile: string|null): Promise<string[]> {
// Checks the format and returns the extension
const ext = this.getExtension(formatName);
@ -38,6 +38,7 @@ export class Extractor {
const dstFile = outFile || `messages.${ext}`;
const dstPath = path.join(this.options.genDir, dstFile);
this.host.writeFile(dstPath, content, false);
return [dstPath];
});
}

View File

@ -89,7 +89,7 @@ export class NgTools_InternalApi_NG_2 {
* @internal
* @private
*/
static codeGen(options: NgTools_InternalApi_NG2_CodeGen_Options): Promise<void> {
static codeGen(options: NgTools_InternalApi_NG2_CodeGen_Options): Promise<any> {
const hostContext: CompilerHostContext =
new CustomLoaderModuleResolutionHostAdapter(options.readResource, options.host);
const cliOptions: NgcCliOptions = {
@ -141,7 +141,7 @@ export class NgTools_InternalApi_NG_2 {
* @internal
* @private
*/
static extractI18n(options: NgTools_InternalApi_NG2_ExtractI18n_Options): Promise<void> {
static extractI18n(options: NgTools_InternalApi_NG2_ExtractI18n_Options): Promise<any> {
const hostContext: CompilerHostContext =
new CustomLoaderModuleResolutionHostAdapter(options.readResource, options.host);