fix(ngcc): support alternate UMD layout when adding new imports (#43931)

In #43879, `UmdReflectionHost` was updated to deal with the new UMD
format used by Rollup, where the parenthesis is around the wrapper
function and not the wrapper function call.
For reference, this caused failures in the `ngcc-validation` repo
([example 1][1], [example 2][2]).

This commit updates `UmdRenderingFormatter` to also handle both UMD
formats. In order to validate the change, this commit also updates the
`UmdRenderingFormatter` tests to run against both UMD formats.

[1]: https://circleci.com/gh/angular/ngcc-validation/65916
[2]: https://circleci.com/gh/angular/ngcc-validation/65758

PR Close #43931
This commit is contained in:
George Kalpakas
2021-10-23 12:41:42 +03:00
committed by Jessica Janiuk
parent 8178a3028b
commit 7c5d642106
2 changed files with 537 additions and 478 deletions
@@ -61,13 +61,13 @@ export class UmdRenderingFormatter extends Esm5RenderingFormatter {
return;
}
const wrapperFunction = umdModule.wrapperFn;
const {wrapperFn, factoryFn} = umdModule;
// We need to add new `require()` calls for each import in the CommonJS initializer
renderCommonJsDependencies(output, wrapperFunction, imports);
renderAmdDependencies(output, wrapperFunction, imports);
renderGlobalDependencies(output, wrapperFunction, imports);
renderFactoryParameters(output, wrapperFunction, imports);
renderCommonJsDependencies(output, wrapperFn, imports);
renderAmdDependencies(output, wrapperFn, imports);
renderGlobalDependencies(output, wrapperFn, imports);
renderFactoryParameters(output, factoryFn, imports);
}
/**
@@ -210,20 +210,7 @@ function renderGlobalDependencies(
* Add dependency parameters to the UMD factory function.
*/
function renderFactoryParameters(
output: MagicString, wrapperFunction: ts.FunctionExpression, imports: Import[]) {
const wrapperCall = wrapperFunction.parent as ts.CallExpression;
const secondArgument = wrapperCall.arguments[1];
if (!secondArgument) {
return;
}
// Be resilient to the factory being inside parentheses
const factoryFunction =
ts.isParenthesizedExpression(secondArgument) ? secondArgument.expression : secondArgument;
if (!ts.isFunctionExpression(factoryFunction)) {
return;
}
output: MagicString, factoryFunction: ts.FunctionExpression, imports: Import[]) {
const parameters = factoryFunction.parameters;
const parameterString = imports.map(i => i.qualifier.text).join(',');
if (parameters.length > 0) {
File diff suppressed because it is too large Load Diff