Repository navigation
Description
Which @angular/* package(s) are the source of the bug?
compiler-cli, compiler
Is this a regression?
No
Description
When libraries are built using esbuild (e.g. some rc versions of ng-packagr before the change was reverted) the built packages are unusable in webpack builds (verified with ng serve and ng test)
Causes ng-packagr/ng-packagr#2850 (which is closed because the move to esbuild was reverted)
Please provide a link to a minimal reproduction of the bug
ng-packagr/ng-packagr#2850 (comment)
Please provide the exception or error you saw
ReferenceError: Cannot access '_c1' before initialization
at 3347 (main.js:163526:23)
at __webpack_require__ (runtime.js:23:41)
at 25429 (main.js:6695:71)
at __webpack_require__ (runtime.js:23:41)
at 76311 (main.js:6539:92)
at __webpack_require__ (runtime.js:23:41)
at __webpack_exec__ (main.js:203398:48)
at main.js:203399:229
at __webpack_require__.O (runtime.js:57:23)
at main.js:203400:56
Please provide the environment you discovered this bug in (run ng version)
Angular CLI: 18.0.3
Node: 20.11.0
Package Manager: yarn 3.3.1
OS: darwin arm64
Angular: 18.0.2
... cdk, common, compiler, compiler-cli, core, elements, forms
... localize, platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1800.3
@angular-devkit/build-angular 18.0.3
@angular-devkit/core 18.0.3
@angular-devkit/schematics 18.0.3
@angular/cli 18.0.3
@schematics/angular 18.0.3
rxjs 7.8.1
typescript 5.4.5
zone.js 0.14.7
Anything else?
Note the line numbers in the main.js file:


(excuse the partial screenshots, the codebase is not public)
The const _cX statements are inserted way below where they're used.
This appears to be caused by esbuild not hoisting imports in its output. If I update the built "FESM" file and move all import statements to the top of the file, everything works and the _cX constants are defined before they're used.
Activity
JoostK commentedon Jun 12, 2024
This was the reason for the esbuild usage revert in ng-packagr, so I'd advise to update to not run into this problem. If esbuild is reintroduced into ng-packgr we'll look into why this happens and how it can be resolved.
bgotink commentedon Jun 12, 2024
We're not using ng-packagr, we've got our own library builder, where we switched to esbuild earlier and we now have to revert because of this same issue.
JoostK commentedon Jun 12, 2024
Could you please share (parts of) the library output that is consumed in the app?
bgotink commentedon Jun 12, 2024
Dummy library passed through our pipeline: repro.zip
Here's the FESM file, where I added a comment showing where the constants are inserted
JoostK commentedon Jun 12, 2024
Ah, thanks, I see now. The problem originates here:
angular/packages/compiler-cli/linker/babel/src/es2015_linker_plugin.ts
Lines 132 to 143 in 160171e
fix(compiler-cli): insert constant statements after the first group o…
JoostK commentedon Jun 13, 2024
@bgotink perhaps you could patch in #56431 locally to see if that resolves the issue you're having.
bgotink commentedon Jun 13, 2024
@JoostK That patch fixes everything, thanks!
fix(compiler-cli): insert constant statements after the first group o…
fix(compiler-cli): insert constant statements after the first group o…
angular-automatic-lock-bot commentedon Jul 14, 2024
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.