feat(compiler-cli): add watch mode to ngc
(#18818)
With this change ngc now accepts a `-w` or a `--watch` command-line option that will automatically perform a recompile whenever any source files change on disk. PR Close #18818
This commit is contained in:

committed by
Jason Aden

parent
c685cc2f0a
commit
06d01b2287
@ -42,8 +42,8 @@ export class AotCompiler {
|
||||
|
||||
analyzeModulesSync(rootFiles: string[]): NgAnalyzedModules {
|
||||
const programSymbols = extractProgramSymbols(this._symbolResolver, rootFiles, this._host);
|
||||
const analyzeResult =
|
||||
analyzeAndValidateNgModules(programSymbols, this._host, this._metadataResolver);
|
||||
const analyzeResult = analyzeAndValidateNgModules(
|
||||
programSymbols, this._host, this._symbolResolver, this._metadataResolver);
|
||||
analyzeResult.ngModules.forEach(
|
||||
ngModule => this._metadataResolver.loadNgModuleDirectiveAndPipeMetadata(
|
||||
ngModule.type.reference, true));
|
||||
@ -52,8 +52,8 @@ export class AotCompiler {
|
||||
|
||||
analyzeModulesAsync(rootFiles: string[]): Promise<NgAnalyzedModules> {
|
||||
const programSymbols = extractProgramSymbols(this._symbolResolver, rootFiles, this._host);
|
||||
const analyzeResult =
|
||||
analyzeAndValidateNgModules(programSymbols, this._host, this._metadataResolver);
|
||||
const analyzeResult = analyzeAndValidateNgModules(
|
||||
programSymbols, this._host, this._symbolResolver, this._metadataResolver);
|
||||
return Promise
|
||||
.all(analyzeResult.ngModules.map(
|
||||
ngModule => this._metadataResolver.loadNgModuleDirectiveAndPipeMetadata(
|
||||
@ -400,16 +400,24 @@ export interface NgAnalyzeModulesHost { isSourceFile(filePath: string): boolean;
|
||||
// Returns all the source files and a mapping from modules to directives
|
||||
export function analyzeNgModules(
|
||||
programStaticSymbols: StaticSymbol[], host: NgAnalyzeModulesHost,
|
||||
staticSymbolResolver: StaticSymbolResolver,
|
||||
metadataResolver: CompileMetadataResolver): NgAnalyzedModules {
|
||||
const programStaticSymbolsWithDecorators = programStaticSymbols.filter(
|
||||
symbol => !symbol.filePath.endsWith('.d.ts') ||
|
||||
staticSymbolResolver.hasDecorators(symbol.filePath));
|
||||
const {ngModules, symbolsMissingModule} =
|
||||
_createNgModules(programStaticSymbols, host, metadataResolver);
|
||||
return _analyzeNgModules(programStaticSymbols, ngModules, symbolsMissingModule, metadataResolver);
|
||||
_createNgModules(programStaticSymbolsWithDecorators, host, metadataResolver);
|
||||
return _analyzeNgModules(
|
||||
programStaticSymbols, programStaticSymbolsWithDecorators, ngModules, symbolsMissingModule,
|
||||
metadataResolver);
|
||||
}
|
||||
|
||||
export function analyzeAndValidateNgModules(
|
||||
programStaticSymbols: StaticSymbol[], host: NgAnalyzeModulesHost,
|
||||
staticSymbolResolver: StaticSymbolResolver,
|
||||
metadataResolver: CompileMetadataResolver): NgAnalyzedModules {
|
||||
const result = analyzeNgModules(programStaticSymbols, host, metadataResolver);
|
||||
const result =
|
||||
analyzeNgModules(programStaticSymbols, host, staticSymbolResolver, metadataResolver);
|
||||
if (result.symbolsMissingModule && result.symbolsMissingModule.length) {
|
||||
const messages = result.symbolsMissingModule.map(
|
||||
s =>
|
||||
@ -420,8 +428,8 @@ export function analyzeAndValidateNgModules(
|
||||
}
|
||||
|
||||
function _analyzeNgModules(
|
||||
programSymbols: StaticSymbol[], ngModuleMetas: CompileNgModuleMetadata[],
|
||||
symbolsMissingModule: StaticSymbol[],
|
||||
programSymbols: StaticSymbol[], programSymbolsWithDecorators: StaticSymbol[],
|
||||
ngModuleMetas: CompileNgModuleMetadata[], symbolsMissingModule: StaticSymbol[],
|
||||
metadataResolver: CompileMetadataResolver): NgAnalyzedModules {
|
||||
const moduleMetasByRef = new Map<any, CompileNgModuleMetadata>();
|
||||
ngModuleMetas.forEach((ngModule) => moduleMetasByRef.set(ngModule.type.reference, ngModule));
|
||||
@ -436,7 +444,10 @@ function _analyzeNgModules(
|
||||
programSymbols.forEach((symbol) => {
|
||||
const filePath = symbol.filePath;
|
||||
filePaths.add(filePath);
|
||||
});
|
||||
programSymbolsWithDecorators.forEach((symbol) => {
|
||||
if (metadataResolver.isInjectable(symbol)) {
|
||||
const filePath = symbol.filePath;
|
||||
ngInjectablesByFile.set(filePath, (ngInjectablesByFile.get(filePath) || []).concat(symbol));
|
||||
}
|
||||
});
|
||||
|
@ -250,6 +250,24 @@ export class StaticSymbolResolver {
|
||||
return this.staticSymbolCache.get(declarationFile, name, members);
|
||||
}
|
||||
|
||||
/**
|
||||
* hasDecorators checks a file's metadata for the presense of decorators without evalutating the
|
||||
* metada.
|
||||
*
|
||||
* @param filePath the absolute path to examine for decorators.
|
||||
* @returns true if any class in the file has a decorator.
|
||||
*/
|
||||
hasDecorators(filePath: string): boolean {
|
||||
const metadata = this.getModuleMetadata(filePath);
|
||||
if (metadata['metadata']) {
|
||||
return Object.keys(metadata['metadata']).some((metadataKey) => {
|
||||
const entry = metadata['metadata'][metadataKey];
|
||||
return entry && entry.__symbolic === 'class' && entry.decorators;
|
||||
});
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
getSymbolsOf(filePath: string): StaticSymbol[] {
|
||||
// Note: Some users use libraries that were not compiled with ngc, i.e. they don't
|
||||
// have summaries, only .d.ts files. So we always need to check both, the summary
|
||||
|
@ -57,8 +57,8 @@ export class Extractor {
|
||||
|
||||
extract(rootFiles: string[]): Promise<MessageBundle> {
|
||||
const programSymbols = extractProgramSymbols(this.staticSymbolResolver, rootFiles, this.host);
|
||||
const {files, ngModules} =
|
||||
analyzeAndValidateNgModules(programSymbols, this.host, this.metadataResolver);
|
||||
const {files, ngModules} = analyzeAndValidateNgModules(
|
||||
programSymbols, this.host, this.staticSymbolResolver, this.metadataResolver);
|
||||
return Promise
|
||||
.all(ngModules.map(
|
||||
ngModule => this.metadataResolver.loadNgModuleDirectiveAndPipeMetadata(
|
||||
|
Reference in New Issue
Block a user