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.

Should libraries that depend on rxjs use path-mappings/tree shaking?

See original GitHub issue

👋 This might at first glance seem like an obvious question, but let me explain:

By default in v6 if you import anything it’s going to resolve the files from the project root. e.g. import { Observable } from 'rxjs' is going to come from node_modules/rxjs/Rx.js which re-exports it from node_modules/rxjs/internal/Observable.js.

If a library uses the path-mappings and tree shaking they would use custom resolve overrides. So in that case, Observable would come from node_modules/rxjs/_esm5/index.js which re-exports it from node_modules/rxjs/_esm5/internal/Observable.js. Note the fact that this is inside rxjs/_esm5.

Then let’s say that someone imports my library (e.g. https://redux-observable.js.org) which had done this, but the consumer does not set up their own webpack resolve overrides so they’ll get Observable from node_modules/rxjs/internal/Observable.js and now we have two copies of rxjs in the same project. Observable1 instanceof Observable2 === false. If they use pipeable operators they probably won’t immediately run into errors, if ever, but their bundle size will be silently larger and might later get cases where Observable1 instanceof Observable2 === false, which is quite the hard thing to diagnose for most.

If the library doesn’t use path-mappings, the same problem exists in reverse; if the library consumer do use it, they’ll again get their own copy of RxJS.


This was brought up previously when we had rxjs-es (because a redux-observable user hit it) and discussed including the esm5 versions, but I don’t recall a solution ever being found.

Yes, this sucks. AFAIK the only real solution would be have the esm5 version in its own package (or make it the default package and put the other stuff in another). Interestingly, if that was done I think people wouldn’t need to have path-mappings and instead could just alias "rxjs": require.resolve('rxjs-esm5') or whatever it would be called. This could still give you two copies but to me feels like it reduces the likelihood as long as libs set their peerDep correctly.

🔥

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:20 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
jasonadencommented, Feb 22, 2018

Yeah, that took a bit of playing with it and a bunch of testing before we were sure it would work. But it’s pretty cool! Probably deserves a blog post somewhere at some point… it’s super useful for libraries.

1reaction
jasonadencommented, Feb 22, 2018

Ah, right. No, it won’t require that. We will to distribute a multiple entry point package.

Take a look at Angular’s common package. The package.json you see in the root is the one you hit when importing from @angular/common. But if you import from @angular/common/http, you’ll get the package.json here. And similarly, you import from @angular/common/http/testing and you’ll get the one [here].

So it will be similar with the rx distribution where there will be multiple package.json files that get distributed, but all will come with a single npm install rxjs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The RxJS library - Angular
RxJS (Reactive Extensions for JavaScript) is a library for reactive programming using observables that makes it easier to compose asynchronous or ...
Read more >
RxJS Operators
Operators are functions. There are two kinds of operators: Pipeable Operators are the kind that can be piped to Observables using the syntax...
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