question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make BrowserAnimationsModule delay-loadable

See original GitHub issue

🚀 feature request

Relevant Package

This feature request is for @angular/platform-browser/animations

Description

To make our app start as quickly as possible, particularly on mobile, I’ve done a bunch of work to minimize initial bundle size. One of the approaches I’m using is async imports (eg import('../foo/foo.module').then(m => m.FooModule)) shortly after the app is loaded, for logic that isn’t absolutely required for initial load + first render.

While we use angular animations extensively (because it’s awesome!), we don’t need it for first page view (relying on CSS animations for the first few seconds). It is currently not possible to move the BrowserAnimationsModule import to a different module from where BrowserModule is imported - because BrowserAnimationModule exports BrowserModule, and the BrowserModule constructor requires that BrowserModule hasn’t been loaded in a parent injector. Also, angular won’t start if BrowserModule isn’t imported by the root module.

I would like the ability to delay load BrowserAnimationsModule, and I think this would be a useful ability for anyone working to speed initial load.

The easiest repro is to create a new project using ng new --strict --minimal. Next, ng build --prod gives me a main bundle size of 187kB.

If I’m going to use @angular/animations in my app, I am currently required to import BrowserAnimationsModule in the root module:

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Just doing this increases the main bundle to 258 kB (+71kB or +38%).

Describe the solution you’d like

I’d like to be able to initialize the BrowserAnimationsModule shortly after app load, and have animations work after that point:

await delay(1000); import('@angular/platform-browser/animations').then(m => m.BrowserAnimationsModule );

This will probably require that the BrowserAnimationsModule no longer re-export BrowserModule.

Describe alternatives you’ve considered

The current required configuration works, it just adds unnecessary size to the initial bundle.

I did try directly importing BROWSER_ANIMATIONS_PROVIDERS (which would get around the BrowserModule re-import check), but I couldn’t access them.

Another alternative would be to fork the angular code and try to create a new version of BrowserAnimationsModule that doesn’t have these limitations, but I haven’t gotten desparate enough for that. We’ll see what the Angular team thinks about this proposal.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sodcommented, Oct 5, 2020

Here is an actual working example on stackblick how we work with native css & angular and that pipe & directive for :enter & :leave animations with *ngIf & *ngFor

https://stackblitz.com/edit/angular-ivy-bhwwjt?file=src%2Fapp%2Fapp.component.css

0reactions
angular-automatic-lock-bot[bot]commented, Sep 29, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

BrowserAnimationsModule
Angular is a platform for building mobile and desktop web applications. Join the community of millions of developers who build compelling user interfaces ......
Read more >
angular - How to add BrowserAnimationsModule or ...
Please make sure that: - Either `BrowserAnimationsModule` or `NoopAnimationsModule` are imported in your application. - There is corresponding ...
Read more >
ontimize-web-ngx
... ","__extends","__","constructor","prototype","create","__assign","assign" ... ","loaderChild4","removeStyle","delayLoad","OFormServiceComponent","Host" ...
Read more >
5 Angular Animations Examples - YouTube
5 Angular Animations Examples - Learn BrowserAnimationsModule in Angular. Watch later. Share. Copy link. Info. Shopping. Tap to unmute.
Read more >
Angular Animations Tutorial | Mosh - YouTube
Get the COMPLETE COURSE (60% OFF - LIMITED TIME): https://programmingwithmosh.com/courses/angularLearn how to use animations in your Angular ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found