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.
any news on that? Rxjs documentation states bundle size can be fixed through Webpack configuration. Is that intended to be fixed through webpack 4 upgrade?
Ref: https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md#build-and-treeshaking
I just found this issue (should have looked for it earlier...) after spending my morning trying to figure out why I had all of rxjs being bundled on our app (@angular/cli": "1.6.7")
Also I haven't checked thouroughly yet but it seems like we have the same tree shaking problem with date-fns/esm pulling everything into the prod bundle.
I'll second @blackholegalaxy with his/her question, or if anyone can point us to an issue where we can track / fix this ?
Thanks !
@blackholegalaxy @Lakston this the the correct issue to track resolution.
The only way to truly achieve tree shaking of RxJs (in the current context) is to use Webpack 4 and have the package marked as being side effect free.
Without these two preconditions, it is only possible to treeshake RxJs operators in the main bundle, and only if the vendor bundle is disabled (as it is by default on prod builds).
Sorry about my lack of knowledge of the "under the hood angular CLI logic", but could the solution on the link I provided above, by Rxjs team, work in the CLI context? It seems CLI relies on webpack 3 right?
@blackholegalaxy we already implement that in the CLI actually.
Ok nice to know. We will then wait for webpack 4 improvements. Thanks for precision @filipesilva :)
If you have Observable and an operator or two, that's a working app as far as RxJS is concerned.
Most applications use between 8-20 operators. I'd estimate the average is around 12-15.
still have this issue in Angular CLI 6 with rxjs6 with no rxjs-compact.
I found this issue because I was also experiencing this issue with a custom Webpack 4 + Babel 6 setup. I then tried Rollup just for fun to see if it makes any difference. Using RxJS and one/two pipeable operators resulted in a ~10 kb bundle with Rollup. Webpack on the other hand created a 120 kb bundle. But I noticed that Rollup requires the following line in .babelrc:
{
"presets": [
[
"env",
{
"modules": false // <- this
}
]
]
}
After adding this line in the .babelrc file of my Webpack setup as well its created bundle file shrank by ~110 kb to about the same size that Rollup produces.
Now I guess the Angular CLI uses TypeScript instead of Babel. From reading the documentation it seems to have a similar option in tsconfig.json called compilerOptions.module and a matching value of "None".
I didn't try this (and I don't have any Angular projects right now) but maybe this helps you finding a solution? But at least to me this also means that this is not necessarily a Webpack issue, right?
Having this issue as well with all rxjs in my bundle, as well as all lodash-es when I only use 2 imports
Rx: 6.3.3 CLI: 7.0.6

I have an Angular app using v6.1.8, CLI v6.2.3 and rxjs v6.3.2. Not importing anything from the internal path and it's still appearing inside of the bundle, in addition to that I am seeing bunch of operators that we are not using at all.
@juarezpaf One of the possibilities why this could be happening is that one of the deps libraries that you use also has rxjs in their deps/devDeps. At the same time that library could be configured to use compilerOptions.module: commonjs in their tsconfig.json file.
That's what was happening in my case.
I'm not sure when the exact fix went in, but this is no longer an issue in Angular CLI 7.3.8 as you can verify in my updated fork of the original reproduction.
Facing similar issue

Environment:

Note
Earlier I was seeing the left panel (900+ KB) was occupied by polyfills(at that time Size of Rx as nearly 53KB). So to solve this I found out I had to mention "es5BrowserSupport": true in angular.json and commented all imports in polyfills.ts except import "zone.js/dist/zone";.
Now I am seeing similar amount of size is being taken by RxJs. If I uncomment the polyfill imports the RxJs is back to around 53 KB but polyfills take 900+KB.

I just want to get rid of this 900+ KB sized thing.
@KishoreBarik this issue is closed. Please open a new issue and link to this issue from your new issue.
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.