fix(ngcc): do not emit ES2015 code in ES5 files (#33514)
Previously, ngcc's `Renderer` would add some constants in the processed files which were emitted as ES2015 code (e.g. `const` declarations). This would result in invalid ES5 generated code that would break when run on browsers that do not support the emitted format. This commit fixes it by adding a `printStatement()` method to `RenderingFormatter`, which can convert statements to JavaScript code in a suitable format for the corresponding `RenderingFormatter`. Additionally, the `translateExpression()` and `translateStatement()` ngtsc helper methods are augmented to accept an extra hint to know whether the code needs to be translated to ES5 format or not. Fixes #32665 PR Close #33514
This commit is contained in:

committed by
Kara Erickson

parent
704775168d
commit
033aba9351
@ -64,8 +64,9 @@ class IvyVisitor extends Visitor {
|
||||
|
||||
res.forEach(field => {
|
||||
// Translate the initializer for the field into TS nodes.
|
||||
const exprNode =
|
||||
translateExpression(field.initializer, this.importManager, this.defaultImportRecorder);
|
||||
const exprNode = translateExpression(
|
||||
field.initializer, this.importManager, this.defaultImportRecorder,
|
||||
ts.ScriptTarget.ES2015);
|
||||
|
||||
// Create a static property declaration for the new field.
|
||||
const property = ts.createProperty(
|
||||
@ -73,7 +74,9 @@ class IvyVisitor extends Visitor {
|
||||
undefined, exprNode);
|
||||
|
||||
field.statements
|
||||
.map(stmt => translateStatement(stmt, this.importManager, this.defaultImportRecorder))
|
||||
.map(
|
||||
stmt => translateStatement(
|
||||
stmt, this.importManager, this.defaultImportRecorder, ts.ScriptTarget.ES2015))
|
||||
.forEach(stmt => statements.push(stmt));
|
||||
|
||||
members.push(property);
|
||||
@ -218,7 +221,8 @@ function transformIvySourceFile(
|
||||
// Generate the constant statements first, as they may involve adding additional imports
|
||||
// to the ImportManager.
|
||||
const constants = constantPool.statements.map(
|
||||
stmt => translateStatement(stmt, importManager, defaultImportRecorder));
|
||||
stmt =>
|
||||
translateStatement(stmt, importManager, defaultImportRecorder, ts.ScriptTarget.ES2015));
|
||||
|
||||
// Preserve @fileoverview comments required by Closure, since the location might change as a
|
||||
// result of adding extra imports and constant pool statements.
|
||||
|
Reference in New Issue
Block a user