From 7c1d3e0f5acbe3f8108517bbd422a7eccd9af88e Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Thu, 28 Sep 2017 11:44:05 -0700 Subject: [PATCH] fix(compiler): make watch mode work with `declaration: false` closes #19464 --- .../compiler-cli/src/transformers/program.ts | 7 +++++ .../test/transformers/program_spec.ts | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/packages/compiler-cli/src/transformers/program.ts b/packages/compiler-cli/src/transformers/program.ts index 4c71c6e147..e24c4209ae 100644 --- a/packages/compiler-cli/src/transformers/program.ts +++ b/packages/compiler-cli/src/transformers/program.ts @@ -461,6 +461,13 @@ class AngularCompilerProgram implements Program { sourceFile: baseFile, }); this.emittedLibrarySummaries.push({fileName: genFile.genFileUrl, text: outData}); + if (!this.options.declaration) { + // If we don't emit declarations, still record an empty .ngfactory.d.ts file, + // as we might need it lateron for resolving module names from summaries. + const ngFactoryDts = + genFile.genFileUrl.substring(0, genFile.genFileUrl.length - 15) + '.ngfactory.d.ts'; + this.emittedLibrarySummaries.push({fileName: ngFactoryDts, text: ''}); + } } else if (outFileName.endsWith('.d.ts') && baseFile.fileName.endsWith('.d.ts')) { const dtsSourceFilePath = genFile.genFileUrl.replace(/\.ts$/, '.d.ts'); // Note: Don't use sourceFiles here as the created .d.ts has a path in the outDir, diff --git a/packages/compiler-cli/test/transformers/program_spec.ts b/packages/compiler-cli/test/transformers/program_spec.ts index dd9216cc28..9db67d4aa7 100644 --- a/packages/compiler-cli/test/transformers/program_spec.ts +++ b/packages/compiler-cli/test/transformers/program_spec.ts @@ -125,6 +125,34 @@ describe('ng program', () => { .toBe(false); }); + // Note: this is the case for watch mode with declaration:false + it('should reuse generated code from libraries from old programs with declaration:false', + () => { + compileLib('lib'); + + testSupport.writeFiles({ + 'src/main.ts': createModuleAndCompSource('main'), + 'src/index.ts': ` + export * from './main'; + export * from 'lib/index'; + ` + }); + const p1 = compile(undefined, {declaration: false}); + expect(p1.getTsProgram().getSourceFiles().some( + sf => /node_modules\/lib\/.*\.ngfactory\.ts$/.test(sf.fileName))) + .toBe(true); + expect(p1.getTsProgram().getSourceFiles().some( + sf => /node_modules\/lib2\/.*\.ngfactory.*$/.test(sf.fileName))) + .toBe(false); + const p2 = compile(p1, {declaration: false}); + expect(p2.getTsProgram().getSourceFiles().some( + sf => /node_modules\/lib\/.*\.ngfactory.*$/.test(sf.fileName))) + .toBe(false); + expect(p2.getTsProgram().getSourceFiles().some( + sf => /node_modules\/lib2\/.*\.ngfactory.*$/.test(sf.fileName))) + .toBe(false); + }); + it('should store library summaries on emit', () => { compileLib('lib'); testSupport.writeFiles({