bug: all RxJS operators pulled into prod bundle when using lettable imports from 'rxjs/operators'
Versions
Angular CLI: 1.6.3
Node: 8.9.0
OS: darwin x64
Angular: 5.1.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router
... service-worker
@angular/cdk: 5.0.2
@angular/cli: 1.6.3
@angular/flex-layout: 2.0.0-beta.12
@angular/material: 5.0.2
@angular-devkit/build-optimizer: 0.0.36
@angular-devkit/core: 0.0.22
@angular-devkit/schematics: 0.0.42
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.3
@schematics/angular: 0.1.11
@schematics/schematics: 0.0.11
typescript: 2.5.3
webpack: 3.10.0
Repro steps
https://github.com/Toxicable/issue-lettable-bundle-size
Observed behavior
From https://github.com/angular/angular-cli/issues/8854#issuecomment-354532989, "There appears to be a bug in Webpack that is causing the CLI to import all of the RXJS operators if you use any lettable operator imports without doing each as a deep import individually.
Upgrading to lettable operators in CLI 1.6.3 caused RxJS to go from 60 KB to 122 KB for me. It sounds like a fix is 'coming soon', but I haven't been able to track down the issue to follow."
Another user mentioned a bundle size increase when moving to lettable operators in the CLI: https://github.com/angular/angular-cli/issues/8720#issuecomment-348804558
Desired behavior
From the 5.0 blog post: "These new operators eliminate the side effects and the code splitting / tree shaking problems that existed with the previous ‘patch’ method of importing operators." "RxJS now distributes a version using ECMAScript Modules. The new Angular CLI will pull in this version by default, saving considerably on bundle size."
I desire a considerable reduction in bundle size, not an increase.
Mention any other details that might be useful (optional)
I tracked this down across Slacks, GitHub, and Twitter as I was told that there was a known Webpack problem that would be fixed soon and then pulled into the CLI. As far as I can tell, any similar Webpack issue was solved in 3.9.0 and the current CLI already includes it as it is using 3.10.0 now.
I can provide source map explorer screenshots if needed, but the linked repo above should enable you to better reproduce and create your own source map explorer output.
RxJS Docs for lettable operators and builds: https://github.com/ReactiveX/rxjs/blob/master/doc/lettable-operators.md#build-and-treeshaking
Workaround
Breaking out all of your imports into individual (one import per line) deep imports seems to avoid this issue, but it is very painful and not desirable to have these extra lines of imports in projects of any significant size or scope.
/cc @IgorMinar @jasonaden @benlesh @Toxicable
I'll look into this
The problem is that you have -vc turned on. Vendor chunking results terrible size regressions. This is because webpack 3 generates a ton of cross-chunk module-stitching code that prevents uglify from dead-code-eliminating the unused operators. Once you turn off vendor chunking everything will work correctly.
If things go as planned webpack 4 should significantly improve this situation, but until then definitely always always turn off vendor chunking if you care about payload size. This is why -vc is disabled in cli by default.
with vendor chunking:

without vendor chunking:

I'm not using -vc in my project and I'm still seeing this issue. Here's the command that I'm running:
ng build --target=production --env=staging
My env for staging is the following:
export const environment = {
production: true,
firebaseConfig: { ... },
googleAnalyticsTrackingId: '...',
mapsApiKey: '...'
}
I'll post the source map explorer screenshots in a minute.
According to https://github.com/angular/angular-cli/wiki/build, --vendor-chunk (aliases: -vc) default value: true.
Perhaps the issue is that the default should now be false?
ng build --target=production -sm
Pre-lettable operators (60.63 KB for RxJS):

ng build --target=production -sm
With lettable operators (122.39 KB for RxJS):

ng build --target=production -sm --vc=false
With lettable operators (122.39 KB for RxJS):

ng build --target=production -sm --vc false
With lettable operators (122.39 KB for RxJS):

ng build --target=production -sm -vc false
With lettable operators (122.39 KB for RxJS):

ng build --target=production -sm -vc=false
With lettable operators (122.39 KB for RxJS):

ng build --target=production -sm -vc=false -cc=false
With lettable operators (122.39 KB for RxJS):

@Splaktar is your project public maybe? That way I could take a look.
Are you using lazy bundles as well? Can you try removing them and checking if there's a difference in rxjs?
@filipesilva the source isn't public. The staging and production sites are.
There is currently one major lazy loaded module. The other routes just go to simple components that are fairly static.
const routes: Routes = [
{path: '', pathMatch: 'full', component: LandingComponent, children: []},
{path: 'agents', loadChildren: './agents/agents.module#AgentsModule'},
{path: 'profile/:id', component: ProfileComponent, canActivate: [UsersService], children: []},
{path: 'terms', component: TermsComponent, children: []},
{path: 'privacy', component: PrivacyComponent, children: []},
{path: '**', component: PageNotFoundComponent, children: []}
];
@NgModule({
imports: [RouterModule.forRoot(routes, {useHash: false})],
exports: [RouterModule],
providers: []
})
export class AppRoutingModule {}
If you turn that lazy route into a normal route, or remove it altogether, does the rxjs size in the main bundle change?
Removing my lazy loaded route and re-running with ng build --target=production -sm:
RxJS size is smaller, but I still see tons of operators that I am not using (especially w/o the primary AgentsModule in the build).
Here's the full source map explorer:

Note that some operators come from libraries. So they are still needed in the bundle even if you don't use them directly.
In the AppModule, I use shareReplay, switchMap, filter, debounceTime.
In the AgentsModule (lazy loaded), I use those plus distinctUntilChanged, take, map.
@Splaktar can you create a minimal reproduction similar to how @Toxicable created the one earlier?
I see that you are using firebase in your app and you are using the cjs distro of that library. I'm wondering if that's why all the operators are retained. A minimal reproduction would confirm or reject that theory.
I'm using "firebase": "4.7.0", and including it via .angular-cli.json like this:
"scripts": [
"../node_modules/hammerjs/hammer.js",
"../node_modules/firebase/firebase.js"
],
It looks like I should be doing this instead:
"scripts": [
"../node_modules/hammerjs/hammer.js",
"../node_modules/firebase/firebase-app.js",
"../node_modules/firebase/firebase-auth.js",
"../node_modules/firebase/firebase-database.js"
],
But I'm not sure how much that will help. I looked at their web setup page and Firebase JS SDK GitHub and I don't see any way to pick different types of modules.
I'll dig into that a bit more, try out firebase 4.8.0, and then work on building a new repo for a public reproduction.
Update: Firebase 4.8.0 and deeper imports did not change anything.
Hm... importing "../node_modules/firebase/firebase.js" in scripts shouldn't result in dependencies insofar as importing is concerned, because importing should not work at all in scripts.
OK, I've created a reproduction repo here. Time to get to sleep now...
Using ng build --target=production --env=prod -sm:

@Splaktar awesome, I was just putting up a repro myself. This way it's much easier. Will take a look.
Running some baseline tests, measuring size of node_modules in main.*.bundle.js and where it comes from via ng build --target=production --env=prod -sm and source-map-explorer:
- normal build: 1.09 MB
node_modules(@angular/*575 KB,rxjs122 KB) - commenting out lazy route: 0.987 MB
node_modules(@angular/*542 KB,rxjs56KB) - forcing lazy module to be non-lazy: 1.01 MB (
@angular/*543 KB,rxjs56 KB) (lazy bundle used to be ~32kb)
I think having a lazy bundle is having the same effect on bundle size as the vendor chunk (described in https://github.com/angular/angular-cli/issues/9069#issuecomment-354896045). This makes sense because vendor chunk is not special, it's just another chunk, just like a lazy chunk is just another chunk.
This makes me think that the best hope to address this problem is Webpack 4. Support is being tracked in https://github.com/angular/angular-cli/pull/8611.
Note: regarding https://github.com/angular/angular-cli/issues/9069#issuecomment-354904486, when Build Optimizer is used then vendor chunk will default to false (https://github.com/angular/angular-cli/wiki/build#--build-optimizer-and---vendor-chunk)
@filipesilva thank you very much for looking into this!
Oh that's really interesting about Build Optimizer and Vendor Chunk options. Perhaps https://github.com/angular/angular-cli/wiki/build can be updated with that information?
I guess at this point, we should recommend that teams do not use lettable operators if they are using the Angular CLI with lazy loaded routes (this is every developer as we have been telling them for a year+ to lazy load every possible route). Do you see any alternative to this?
I need to advise multiple active clients on this atm in addition to my own projects.
@Splaktar I remember facing this problem some time ago when importing from rxjs/operators, so I switched to using rxjs/operators/<name> imports everywhere and haven't had problem with RxJS bundle size ever since. It's not a big deal, since imports are added by IDE automatically and I never have to write them manually anyway.
Maybe one option for you is to advice using deep imports (at least as a temporary solution). I've updated your reproduction from https://github.com/angular/angular-cli/issues/9069#issuecomment-354976634 to use this approach and RxJS bundle size is back to normal.