ReactiveX / ReactiveX/rxjs

esbuild is unable to tree-shake rxjs under node platform

Open
#6,856 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
31.7k
Forks
3k
PR merge metrics
No merged PRs in 30d

Description

Discussed in https://github.com/ReactiveX/rxjs/discussions/6785

Originally posted by Conaclos January 28, 2022
Hi!

I discovered that esbuild is not able to tree-shake rxjs package when bundling under node platform.
esbuild priorizes exports condition 'node' over main-fields.

Demonstration

$ cat index.js
export { Subject } from 'rxjs'

$ esbuild --bundle index.js --platform=node | wc -l
9654

$ # Specifying main-fields does not change the output
$ esbuild --bundle index.js --platform=node --main-fields=es2015,module,main | wc -l
9654

$ # browser bundle is lighter
$ esbuild --bundle index.js --platform=browser | wc -l
1161

Note that we need to bundle for node since our project and most of our dependencies imports node-specific modules.

How to solve the issue?

rxjs should not use the condition "node".
It should use the condition "require":

  ...
  "exports": {
    ".": {
      "require": "./dist/cjs/index.js",
      "es2015": ...,
      "default": ...
    },
    ....

instead of:

  ...
  "exports": {
    ".": {
      "node": "./dist/cjs/index.js",
      "es2015": ...,
      "default": ...
    },
    ....
```</div>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the package exports configuration for rxjs and compare the node and require conditions shown in the issue. Reproduce the index.js esbuild commands for node and browser platforms, then verify that the node bundle tree-shakes the Subject import to the expected smaller output.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.