fix(ngcc): do not add trailing commas in UMD imports (#34545)
Previously, if `UmdRenderingFormatter#addImports()` was called with an empty list of imports to add (i.e. no new imports were needed), it would add trailing commas in several locations (arrays, function arguments, function parameters), thus making the code imcompatible with legacy browsers such as IE11. This commit fixes it by ensuring that no trailing commas are added if `addImports()` is called with an empty list of imports. This is a follow-up to #34353. Fixes #34525 PR Close #34545
This commit is contained in:

committed by
Alex Rickabaugh

parent
c692757029
commit
e6850a3c51
@ -30,6 +30,11 @@ export class CommonJsRenderingFormatter extends Esm5RenderingFormatter {
|
||||
* Add the imports below any in situ imports as `require` calls.
|
||||
*/
|
||||
addImports(output: MagicString, imports: Import[], file: ts.SourceFile): void {
|
||||
// Avoid unnecessary work if there are no imports to add.
|
||||
if (imports.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const insertionPoint = this.findEndOfImports(file);
|
||||
const renderedImports =
|
||||
imports.map(i => `var ${i.qualifier} = require('${i.specifier}');\n`).join('');
|
||||
|
@ -32,6 +32,10 @@ export class EsmRenderingFormatter implements RenderingFormatter {
|
||||
* Add the imports at the top of the file, after any imports that are already there.
|
||||
*/
|
||||
addImports(output: MagicString, imports: Import[], sf: ts.SourceFile): void {
|
||||
if (imports.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const insertionPoint = this.findEndOfImports(sf);
|
||||
const renderedImports =
|
||||
imports.map(i => `import * as ${i.qualifier} from '${i.specifier}';\n`).join('');
|
||||
@ -294,4 +298,4 @@ function getEndExceptSemicolon(statement: ts.Statement): number {
|
||||
const lastToken = statement.getLastToken();
|
||||
return (lastToken && lastToken.kind === ts.SyntaxKind.SemicolonToken) ? statement.getEnd() - 1 :
|
||||
statement.getEnd();
|
||||
}
|
||||
}
|
||||
|
@ -30,6 +30,10 @@ export class UmdRenderingFormatter extends Esm5RenderingFormatter {
|
||||
* Add the imports to the UMD module IIFE.
|
||||
*/
|
||||
addImports(output: MagicString, imports: Import[], file: ts.SourceFile): void {
|
||||
if (imports.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Assume there is only one UMD module in the file
|
||||
const umdModule = this.umdHost.getUmdModule(file);
|
||||
if (!umdModule) {
|
||||
|
Reference in New Issue
Block a user