fix(ivy): add @nocollapse when writing closure-annotated code (#25775)

Closure requires @nocollapse on Ivy definition static fields in order
to not convert them to standalone constants. However tsickle, the tool
which would ordinarily be responsible for adding @nocollapse, doesn't
properly annotate fields which are added synthetically via transforms.
So this commit adds @nocollapse by applying regular expressions against
code during the final write to disk.

PR Close #25775
This commit is contained in:
Alex Rickabaugh
2018-08-28 14:13:22 -07:00
committed by Igor Minar
parent 7ba0cb7c93
commit a0c4b2d8f0
4 changed files with 92 additions and 37 deletions

View File

@ -11,6 +11,7 @@ import * as path from 'path';
import * as ts from 'typescript';
import * as api from '../transformers/api';
import {nocollapseHack} from '../transformers/nocollapse_hack';
import {ComponentDecoratorHandler, DirectiveDecoratorHandler, InjectableDecoratorHandler, NgModuleDecoratorHandler, PipeDecoratorHandler, ResourceLoader, SelectorScopeRegistry} from './annotations';
import {BaseDefDecoratorHandler} from './annotations/src/base_def';
@ -30,6 +31,7 @@ export class NgtscProgram implements api.Program {
private _reflector: TypeScriptReflectionHost|undefined = undefined;
private _isCore: boolean|undefined = undefined;
private rootDirs: string[];
private closureCompilerEnabled: boolean;
constructor(
@ -43,6 +45,7 @@ export class NgtscProgram implements api.Program {
} else {
this.rootDirs.push(host.getCurrentDirectory());
}
this.closureCompilerEnabled = !!options.annotateForClosureCompiler;
this.resourceLoader = host.readResource !== undefined ?
new HostResourceLoader(host.readResource.bind(host)) :
new FileResourceLoader();
@ -156,6 +159,8 @@ 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')) {
data = nocollapseHack(data);
}
this.host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles);
};