fix(compiler): share identical stylesheets between components in the same file (#38213)

Prior to this commit, duplicated styles defined in multiple components in the same file were not
shared between components, thus causing extra payload size. This commit updates compiler logic to
use `ConstantPool` for the styles (while generating the `styles` array on component def), which
enables styles sharing when needed (when duplicates styles are present).

Resolves #38204.

PR Close #38213
This commit is contained in:
Andrew Kushnir
2020-07-23 18:51:55 -07:00
committed by Misko Hevery
parent 8fdfa7b604
commit 8e5969bb52
5 changed files with 70 additions and 6 deletions

View File

@ -86,13 +86,15 @@ class IvyTransformationVisitor extends Visitor {
VisitListEntryResult<ts.Statement, ts.ClassDeclaration> {
// If this class is not registered in the map, it means that it doesn't have Angular decorators,
// thus no further processing is required.
if (!this.classCompilationMap.has(node)) return {node};
if (!this.classCompilationMap.has(node)) {
return {node};
}
// There is at least one field to add.
const statements: ts.Statement[] = [];
const members = [...node.members];
this.classCompilationMap.get(node)!.forEach(field => {
for (const field of this.classCompilationMap.get(node)!) {
// Translate the initializer for the field into TS nodes.
const exprNode = translateExpression(
field.initializer, this.importManager, this.defaultImportRecorder,
@ -120,7 +122,7 @@ class IvyTransformationVisitor extends Visitor {
.forEach(stmt => statements.push(stmt));
members.push(property);
});
}
// Replace the class declaration with an updated version.
node = ts.updateClassDeclaration(