fix(ivy): ensure @nocollapse is added to static fields (#28050)

ngtsc has a hack to add @nocollapse jsdoc annotations to generated static
fields. This hack is currently broken (likely due to a TypeScript change
in the way writeFile() works).

This commit fixes the hack and introduces an ngtsc_spec test to ensure it
does not regress again.

PR Close #28050
This commit is contained in:
Alex Rickabaugh 2019-01-10 10:40:24 -08:00 committed by Andrew Kushnir
parent df292c2ce0
commit 61bc61fc59
2 changed files with 22 additions and 1 deletions

View File

@ -227,7 +227,7 @@ export class NgtscProgram implements api.Program {
if (fileName.endsWith('.d.ts')) {
data = sourceFiles.reduce(
(data, sf) => this.compilation !.transformedDtsFor(sf.fileName, data), data);
} else if (this.closureCompilerEnabled && fileName.endsWith('.ts')) {
} else if (this.closureCompilerEnabled && fileName.endsWith('.js')) {
data = nocollapseHack(data);
}
this.host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles);

View File

@ -109,6 +109,27 @@ describe('ngtsc behavioral tests', () => {
expect(jsContents).toContain('Hello World');
});
it('should add @nocollapse to static fields when closure annotations are requested', () => {
env.tsconfig({
'annotateForClosureCompiler': true,
});
env.write('test.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'test-cmp',
templateUrl: './dir/test.html',
})
export class TestCmp {}
`);
env.write('dir/test.html', '<p>Hello World</p>');
env.driveMain();
const jsContents = env.getContents('test.js');
expect(jsContents).toContain('/** @nocollapse */ TestCmp.ngComponentDef');
});
it('should compile Components with a templateUrl in a different rootDir', () => {
env.tsconfig({}, ['./extraRootDir']);
env.write('extraRootDir/test.html', '<p>Hello World</p>');