Skip to content

NGCC fails to sniff module type for some UMD modules #43870

@AkseliKorhonen

Description

@AkseliKorhonen

Which @angular/* package(s) are the source of the bug?

compiler-cli

Is this a regression?

No

Description

I ran into this issue when using ng-packager, which uses rollup internally, to package a library that I use in my main app. The format of the packaged UMD module changed with the new rollup version 2.57 and now Angular compiler is unable to sniff the module type as UMD, defaulting to CommonJS: https://github.com/angular/angular/blob/master/packages/compiler-cli/ngcc/src/packages/entry_point.ts#L279

Old, working format had the whole module wrapped in parenthesis, while the new one has the factory function and module body wrapped separately with no all-enclosing parenthesis.

The sniffing fails here:

if (!ts.isExpressionStatement(statement) || !ts.isParenthesizedExpression(statement.expression) ||
!ts.isCallExpression(statement.expression.expression) ||
!ts.isFunctionExpression(statement.expression.expression.expression)) {

ts.isExpressionStatement -> true
ts.isParenthesizedExpression -> false
ts.isCallExpression -> false
ts.isFunctionExpression -> true

The UMD module is functionally the same, but ngcc makes assumptions about its structure and thus fails to correctly determine the module type. I asked about the change from rollup team and they said the change was intended.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

When compiling:
Compiling <my-library> : main as commonjs

In browser console:
> type <myModule> does not have 'ɵmod' property.

Please provide the environment you discovered this bug in

No response

Anything else?

No response

Metadata

Metadata

Labels

P2The issue is important to a large percentage of users, with a workaroundstate: has PR

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions