Previously I was able to import only used operators with this code:
import 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/finally';
import 'rxjs/add/observable/empty';
import 'rxjs/add/observable/throw';
This generates a small bundle (vendor.ts).
How to do this with RxJS without requiring rxjs-compat?
Changing the code above to import 'rxjs';
generates a bigger bundle.
UPDATE:
I followed all the answers you've posted but nothing works well. This is my updated vendor.ts:
import 'rxjs/Observable';
import 'rxjs/Subscription';
import 'rxjs/Subject';
import 'rxjs/observable/throw';
import 'rxjs/operators/map';
import 'rxjs/operators/mergeMap';
import 'rxjs/operators/catchError';
import 'rxjs/operators/finalize';
I also tried using 'rxjs/add/operator/*'.
This is how I'm importing rxjs:
import {Observable} from 'rxjs/Observable';
import {Subscription} from 'rxjs/Subscription';
import {Subject} from 'rxjs/Subject';
import {_throw} from 'rxjs/observable/throw';
import {map} from 'rxjs/operators/map';
import {mergeMap} from 'rxjs/operators/mergeMap';
import {catchError} from 'rxjs/operators/catchError';
import {finalize} from 'rxjs/operators/finalize';
I changed my Webpack 3 configuration according to this document (https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md#build-and-treeshaking) and nothing works.
Finally, take a look at the Webpack Bundle Analyzer result:
The bundle includes all operators. I found this related issue: https://github.com/angular/angular-cli/issues/9069
rxjs-compat
is supposed to be installed together withrxjs
, it provides the support for old-style imports.It's possible to use RxJS 6 the same way as RxJS 5:
This compatibility layer is expected to be removed in RxJS 7.
The issue for me was that I had
module
set tocommonjs
in tsconfig.json. It needs to be set toes6
, because webpack needs es6 modules in order for it to be able to do tree shaking.See more info in: https://webpack.js.org/guides/tree-shaking/
You just need this two-part of code:
In this list, you can add any operator that you need to use.
Now You need to import functions that You want to use.
Never use
Use with destructuring
Import operators from
'rxjs/operators'
Static functions from
'rxjs'
So, for example, you need to use operator 'map' you will import it
And then you use it with pipe
For better understanding read Migration Guide or watch awesome video from ng-conf