components icon indicating copy to clipboard operation
components copied to clipboard

Tree-shaking for production build is not working properly

Open thomasgazzoni opened this issue 9 years ago • 51 comments

Bug, feature request, or proposal:

The production bundle size is not shrinking

What is the expected behavior?

Base on the new package structure the production bundle (ng build --prod) should not have included all the Material Components Module

What is the current behavior?

Base on the change log now we should create our own Custom MaterialModule, in my case I have this one:

import { NgModule } from '@angular/core';
import {
    MdCardModule,
    MdChipsModule,
    MdToolbarModule,
    MdButtonModule,
    MdSidenavModule,
    MdRippleModule,
    MdIconModule,
    MdListModule,
    MdProgressSpinnerModule,
} from '@angular/material';

@NgModule({
    imports: [
        MdCardModule,
        MdChipsModule,
        MdToolbarModule,
        MdButtonModule,
        MdSidenavModule,
        MdRippleModule,
        MdIconModule,
        MdListModule,
        MdProgressSpinnerModule,
    ],
    declarations: [

    ],
    providers: [

    ],
    exports: [
        MdCardModule,
        MdChipsModule,
        MdToolbarModule,
        MdButtonModule,
        MdSidenavModule,
        MdRippleModule,
        MdIconModule,
        MdListModule,
        MdProgressSpinnerModule,
    ]
})
export class CustomMaterialModule { }

that is imported in the AppModule.

Even if I remove all the module and import only, for example, MdButtonModule, the size of material library in the production vendor.js still 308Kb

Which versions of Angular, Material, OS, browsers are affected?

@angular/cli: 1.0.0 node: 7.9.0 os: linux x64 @angular/animations: 4.0.1 @angular/common: 4.0.1 @angular/compiler: 4.0.1 @angular/core: 4.0.1 @angular/forms: 4.0.1 @angular/http: 4.0.1 @angular/material: 2.0.0-beta.3 @angular/platform-browser: 4.0.1 @angular/platform-browser-dynamic: 4.0.1 @angular/router: 4.0.1 @angular/cli: 1.0.0 @angular/compiler-cli: 4.0.1

Is there anything else we should know?

This is the vendor sourcemap generated by source-map-explorer image

thomasgazzoni avatar Apr 18 '17 13:04 thomasgazzoni

Yeah I experienced the same issue when testing Material with Webpack 2.

I've been talking to some people about this and it looks like this happens due to missing @__PURE__ annotations in the FESM bundles on IIFEs.

devversion avatar Apr 18 '17 14:04 devversion

Yep, just spent a while talking about this with the rest of the Angular core team. The gist is that the new packing structure doesn't work out of the box with how webpack tries to eliminate dead code. We're working on a solution and will publish a beta.4 as soon as we have a good approach.

jelbourn avatar Apr 18 '17 23:04 jelbourn

Do we even have to import the single Modules into our CustomMaterialModule? I just exported them and everything seems to work fine.

My CustomMaterialModule:

@NgModule({
  exports: [
    MdButtonModule, MdInputModule, MdSelectModule, MdTabsModule, MdMenuModule, MdIconModule, MdTooltipModule, MdCardModule,
    MdCheckboxModule, MdSidenavModule, MdDialogModule, MdToolbarModule, MdSnackBarModule, MdProgressSpinnerModule, MdListModule
  ]
})
export class CustomMaterialModule {
}

dahaupt avatar Apr 18 '17 23:04 dahaupt

Just created a custom material module thinking to reduce the bundle size

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {
  MdDialogModule,
  MdToolbarModule,
  MdInputModule,
  MdMenuModule,
  MdIconModule,
  MdSlideToggleModule,
  MdSliderModule,
  MdSelectModule,
  MdRadioModule,
  MdCheckboxModule,
  MdButtonModule,
  MdButtonToggleModule,
  MdSidenavModule,
  MdListModule,
  MdTooltipModule

} from '@angular/material';

@NgModule({
  imports: [
    CommonModule,
    MdDialogModule,
    MdToolbarModule,
    MdInputModule,
    MdMenuModule,
    MdIconModule,
    MdSlideToggleModule,
    MdSliderModule,
    MdSelectModule,
    MdRadioModule,
    MdCheckboxModule,
    MdButtonModule,
    MdButtonToggleModule,
    MdSidenavModule,
    MdListModule,
    MdTooltipModule
  ],
  exports: [
    CommonModule,
    MdDialogModule,
    MdToolbarModule,
    MdInputModule,
    MdMenuModule,
    MdIconModule,
    MdSlideToggleModule,
    MdSliderModule,
    MdSelectModule,
    MdRadioModule,
    MdCheckboxModule,
    MdButtonModule,
    MdButtonToggleModule,
    MdSidenavModule,
    MdListModule,
    MdTooltipModule
  ]
})
export class CustomMaterialModule { }

Didn't work still getting same material size 741.28 kb. So currently no difference if I am importing my custom module or MaterialModule

image

Looking forward to bata.4 handling that case.

kuncevic avatar May 03 '17 05:05 kuncevic

Has this issue been fixed with beta.4 ?

fknop avatar May 13 '17 09:05 fknop

@fknop I've noticed some decrease in size, but not enough.

karolmie1 avatar May 13 '17 11:05 karolmie1

@fknop @karolmie1 for me it is just got increased:

image was: 741.2k kb 60.03 gziped now 850.19 kb 69.11 kb gziped And I am just importing my custom module https://github.com/angular/material2/issues/4137#issuecomment-298827979

kuncevic avatar May 15 '17 01:05 kuncevic

Love to see that working!

paulogr avatar May 15 '17 04:05 paulogr

We're working on it (experiment 1, experiment 2), but getting it to "just work" with existing tooling is somewhat tricky and we're still working out the approach.

jelbourn avatar May 15 '17 15:05 jelbourn

Any updates on a timeline for this by chance?

fetters5 avatar Jun 02 '17 13:06 fetters5

@IgorMinar and @kara are actively working on it.

jelbourn avatar Jun 02 '17 15:06 jelbourn

While it is currently being worked on, is there an other possible workaround to this problem right now?

ghost avatar Jun 09 '17 09:06 ghost

Check this one https://github.com/IgorMinar/purify it might help you

aitboudad avatar Jun 09 '17 09:06 aitboudad

Yep, that's what they're working on

jelbourn avatar Jun 09 '17 15:06 jelbourn

Would it be possible to provide separate modules: MdCardModule, MdChipsModule, instead of one blob? Then we could import directly, and circumvent the tree-shaking conondrum.

Plus material module as of now for lazy people.

Same way a angular does it -> there is no one big angular.es5 file, they are split into router module, animations module itp.

karolmie1 avatar Jun 17 '17 11:06 karolmie1

@jelbourn Any Update on tree-shaking?

deepakzoto avatar Jun 20 '17 04:06 deepakzoto

@angular-devkit/build-optimizer (formerly ngo+purify) support was added to @angular/[email protected]. You can read more about it in https://github.com/angular/angular-cli/pull/6520.

This should allow a lot of unused material classes to be dropped. Please let me know how it worked out for you - this is still experimental and it's very important to work out the kinks before 1.3.0 final.

filipesilva avatar Jul 22 '17 15:07 filipesilva

@filipesilva Very nice news. We will try it

alexzuza avatar Jul 22 '17 15:07 alexzuza

@filipesilva that cool, but there is something wrong going on with CLI producing wrong bundler stats file or due to incompatibility with scope hoisting https://github.com/angular/angular-cli/issues/7057 Tested with 1.3.0-beta.0 and 1.3.0 rc.0.

kuncevic avatar Jul 22 '17 22:07 kuncevic

Why not just split into separate submodules files? Instead

import { 
  MdAutocompleteModule
  MdButtonModule,
} from } from '@angular/material';

We could have:

import { MdAutocompleteModule } from '@angular/material/MdAutocompleteModule';
import { MdButtonModule } from '@angular/material/MdButtonModule'

And user will automatically import only what is needed. You can still re-export everything under @angular/material.

Now I have a situation that my prod bundle is 1.5mb and large part is because of Material. I will try with aot, but the app is already barely usable in IE11. We go live on Monday and It looks like I will need to rewrite this to Bootstrap.

chyzwar avatar Jul 28 '17 17:07 chyzwar