-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(@angular/build): add experimental chunk optimizer for production application builds #27953
Conversation
Would love to help with this issue, I have a functioning implementation on a large project and would love to cooperate. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…tion application builds An experimental chunk optimizer is now available for initial usage. To enable the optimization, script optimization must be enabled as well as an environment variable `NG_BUILD_OPTIMIZE_CHUNKS=1`. This build step uses `rollup` internally to process the build files directly in memory. The main bundling performs all resolution, bundling, and tree-shaking of the application. The chunk optimizer step then only needs to access the in-memory built files and does not need to perform any disk access or module resolution. This allows the step to be performed fairly quickly but it does add time to the overall production build. The `NG_BUILD_DEBUG_PERF=1` environment variable can be used to view how long the step takes within a build via the `OPTIMIZE_CHUNKS` entry. In the future, this optimization step may be automatically enabled based on initial file entry count and size. There are several current known issues: 1) Bundle budgets for named lazy chunks may not work as expected. 2) The console output may not show names (files will be present) for lazy chunk files. 3) The stats file (`--stats-json` option) will not exactly reflect the final written application files. This is similar to the current behavior of the `browser` builder with Webpack's stat file.
8e7bb83
to
0691746
Compare
The changes were merged into the following branches: main, 18.1.x |
@ChristopherPHolder Thank you for offering to help. This will be available for use in the release next week. If you (or anyone else interested) have the time, please try it with your application and let us know how well it works for your project. If you are interested in trying it out sooner, there are also snapshot builds available: Please provide feedback in the mentioned issue (#27715). |
Absolutely, thanks, will try it out and update the issue. Are you planing on specifying the number of initial chunks? I ask because of the environmental variable. We are if not this means we end up with one large chunk. And when using manual chunks rollup does not take into account circular dependencies which means the strategy you use needs to prevent that. Ideally we could choose the number of maximum chunks and try to distribute the bundle in the rest as evenly as possible. |
@ChristopherPHolder did you manage to try it out? if yes did it work for you? We are facing the same issue at our application (excessive amount of chunks), and I tried the rollup workaround mentioned in the #27715 issue, however this caused some unexpected side effects to our app and we had to revert it. |
@dimeloper no, not yet but i have a similar implementation in a mono repo filled with enterprise apps, so i know the general solution works.
When we implemented it, we also faced a bunch of issues, circular deps, broken sourcemaps, invalid import transformations, etc. What is the issue you are facing? Maybe i got a solution for you? I will also happily publish my solution but since @clydin is working on it internally and that would allow us to handle the build transformation in memory, that seems like a nicer solution |
@ChristopherPHolder while the esbuild production app runs fine, after running the rollup command to reduce the amount of chunks, we get errors like the following in random places, that prevent specific components from being loaded:
|
@dimeloper That issue was mostly related to circular deps in out case. Did you simply run the rollup cli on it? or did you add minChunkSize ? Unfortunately minChunkSize will not work or at lease did not for us |
@ChristopherPHolder thank you for your input. Yes I tried this with |
@clydin I have one question, so today I decided to upgrade our project to We are on an NX project though (version |
@clydin not sure if you are still following this issue, today I managed to define the necessary environment variable (btw if anyone is using NX, here is how you can do it), however I'm still getting the same (excessive) amount of chunks (I also debugged the chunk optimizer and went through some breakpoints to make sure it was being called while it was building).
My guess would be that we would need to inject some rollup parameters to that |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
An experimental chunk optimizer is now available for initial usage. To enable the optimization, script optimization must be enabled as well as an environment variable
NG_BUILD_OPTIMIZE_CHUNKS=1
. This build step usesrollup
internally to process the build files directly in memory. The main bundling performs all resolution, bundling, and tree-shaking of the application. The chunk optimizer step then only needs to access the in-memory built files and does not need to perform any disk access or module resolution. This allows the step to be performed fairly quickly but it does add time to the overall production build. TheNG_BUILD_DEBUG_PERF=1
environment variable can be used to view how long the step takes within a build via theOPTIMIZE_CHUNKS
entry. In the future, this optimization step may be automatically enabled based on initial file entry count and size. There are several current known issues:--stats-json
option) will not exactly reflect the final written application files. This is similar to the current behavior of thebrowser
builder with Webpack's stat file.