Skip to content

Possible improvements to tree shaking and bundle size #500

Closed
@markerikson

Description

@markerikson

🐛 Bug Report

I have three observations about potential issues with Immer's bundle size and tree-shakeability:

  • Immer went from 4.5K min+gz in 4.x, to around 6K min+gz in v5.x. v5.2.0 is a slight improvement over 5.1.0, but still bigger than 4.x. See https://bundlephobia.com/[email protected] for size comparisons. I assume this is due to the extra code for Set/Map handling.
  • Redux Toolkit depends on Immer, and we've noted that several of our dependencies prevent RTK from being correctly tree-shakeable. Someone ran a Webpack report to get indications of why Webpack couldn't tree-shake RTK, and it report that Immer has a reference to process inside, which forces Webpack to bail out of tree-shaking. Reference: Tree shaking is hindered by dependencies reduxjs/redux-toolkit#78 (comment) . The process reference appears to be part of a minification check, at
    typeof process !== "undefined"
    . This does show up in dist/immer.js in the published package.
  • Immer does not appear to have a sideEffects: false flag in its package.json, which Webpack (and other bundlers?) need to fully calculate shakeability: https://webpack.js.org/guides/tree-shaking/#mark-the-file-as-side-effect-free .

I don't know how much improvement you'd get by adding sideEffects: false and removing the process reference, but it might be worth looking into.

I also realize that Immer's internals may not be entirely shakeable anyway, similar to how React is kind of all-or-nothing. Still, given that v5.x is bigger than v4.x, it'd be nice to see if it can be brought back down.

Link to repro

N/A, given that this is not a runtime issue. See linked items in the "report" section for details.

To Reproduce

Use Immer as currently published.

Observed behavior

Immer's size in v5 is larger than v4, and does not appear to be shakeable.

Expected behavior

Webpack would report that Immer is shakeable, and hopefully the final included size would be a bit smaller.

Environment

We only accept bug reports against the latest Immer version.

  • Immer version: v5.2.0

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions