Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cab21ce

Browse filesBrowse files
petebacondarwinAndrewKushnir
authored andcommitted
fix(ngcc): support alternate wrapper function layout for UMD (#43879)
Recently rollup, used by ng-packagr, changed the position of parentheses around its generated UMD wrapper functions. This commit ensures that ngcc can handle both. Fixes #43870 PR Close #43879
1 parent 242a099 commit cab21ce
Copy full SHA for cab21ce

File tree

Expand file treeCollapse file tree

2 files changed

+44
-15
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+44
-15
lines changed

‎packages/compiler-cli/ngcc/src/host/umd_host.ts

Copy file name to clipboardExpand all lines: packages/compiler-cli/ngcc/src/host/umd_host.ts
+25-15Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -511,30 +511,40 @@ export class UmdReflectionHost extends Esm5ReflectionHost {
511511
}
512512

513513
export function parseStatementForUmdModule(statement: ts.Statement): UmdModule|null {
514-
const wrapperCall = getUmdWrapperCall(statement);
515-
if (!wrapperCall) return null;
514+
const wrapper = getUmdWrapper(statement);
515+
if (wrapper === null) return null;
516516

517-
const wrapperFn = wrapperCall.expression;
518-
if (!ts.isFunctionExpression(wrapperFn)) return null;
519-
520-
const factoryFnParamIndex = wrapperFn.parameters.findIndex(
517+
const factoryFnParamIndex = wrapper.fn.parameters.findIndex(
521518
parameter => ts.isIdentifier(parameter.name) && parameter.name.text === 'factory');
522519
if (factoryFnParamIndex === -1) return null;
523520

524-
const factoryFn = stripParentheses(wrapperCall.arguments[factoryFnParamIndex]);
521+
const factoryFn = stripParentheses(wrapper.call.arguments[factoryFnParamIndex]);
525522
if (!factoryFn || !ts.isFunctionExpression(factoryFn)) return null;
526523

527-
return {wrapperFn, factoryFn};
524+
return {wrapperFn: wrapper.fn, factoryFn};
528525
}
529526

530-
function getUmdWrapperCall(statement: ts.Statement): ts.CallExpression&
531-
{expression: ts.FunctionExpression}|null {
532-
if (!ts.isExpressionStatement(statement) || !ts.isParenthesizedExpression(statement.expression) ||
533-
!ts.isCallExpression(statement.expression.expression) ||
534-
!ts.isFunctionExpression(statement.expression.expression.expression)) {
535-
return null;
527+
function getUmdWrapper(statement: ts.Statement):
528+
{call: ts.CallExpression, fn: ts.FunctionExpression}|null {
529+
if (!ts.isExpressionStatement(statement)) return null;
530+
531+
if (ts.isParenthesizedExpression(statement.expression) &&
532+
ts.isCallExpression(statement.expression.expression) &&
533+
ts.isFunctionExpression(statement.expression.expression.expression)) {
534+
// (function () { ... } (...) );
535+
const call = statement.expression.expression;
536+
const fn = statement.expression.expression.expression;
537+
return {call, fn};
538+
}
539+
if (ts.isCallExpression(statement.expression) &&
540+
ts.isParenthesizedExpression(statement.expression.expression) &&
541+
ts.isFunctionExpression(statement.expression.expression.expression)) {
542+
// (function () { ... }) (...);
543+
const call = statement.expression;
544+
const fn = statement.expression.expression.expression;
545+
return {call, fn};
536546
}
537-
return statement.expression.expression as ts.CallExpression & {expression: ts.FunctionExpression};
547+
return null;
538548
}
539549

540550

‎packages/compiler-cli/ngcc/test/packages/entry_point_spec.ts

Copy file name to clipboardExpand all lines: packages/compiler-cli/ngcc/test/packages/entry_point_spec.ts
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,25 @@ runInEachFileSystem(() => {
747747
entryPoint.packageJson.main = './bundles/valid_entry_point';
748748
expect(getEntryPointFormat(fs, entryPoint, browserOrMain)).toBe('umd');
749749
});
750+
751+
it('should match alternate UMD format', () => {
752+
// Note that in this format, instead of the whole wrapper being enclosed in parentheses,
753+
// only the wrapper function is enclosed and the call is not inside the parentheses.
754+
loadTestFiles([{
755+
name: _(
756+
'/project/node_modules/some_package/valid_entry_point/bundles/valid_entry_point/index.js'),
757+
contents: `
758+
(function (global, factory) {
759+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) :
760+
typeof define === 'function' && define.amd ? define('@angular/common', ['exports', '@angular/core'], factory) :
761+
(global = global || self, factory((global.ng = global.ng || {}, global.ng.common = {}), global.ng.core));
762+
})(this, function (exports, core) { 'use strict'; });
763+
`
764+
}]);
765+
766+
entryPoint.packageJson.main = './bundles/valid_entry_point';
767+
expect(getEntryPointFormat(fs, entryPoint, browserOrMain)).toBe('umd');
768+
});
750769
});
751770

752771
it('should return `undefined` if the `browser` property is not a string', () => {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.