Immer: Possible improvements to tree shaking and bundle size

Created on 9 Jan 2020  ·  11Comments  ·  Source: immerjs/immer

🐛 Bug Report

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

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
complexity-medium help wanted

Most helpful comment

Released as 6.0.0. (minimal) size has been reduced from 6.1 to 3.1 kb

All 11 comments

having a small webpack test project that only imports produce would probably a great way to test the actual size

having a small webpack test project that only imports produce would probably a great way to test the actual size

I created a repo following the webpack guide:

https://webpack.js.org/guides/tree-shaking/ https://github.com/giggo1604/immer-treeshake

build output is the following:

$ yarn build
yarn run v1.21.1
$ webpack
Hash: 0319a607aced1cb18598
Version: webpack 4.41.6
Time: 124ms
Built at: 02/17/2020 6:21:29 PM
  Asset      Size  Chunks             Chunk Names
main.js  21.5 KiB       0  [emitted]  main
Entrypoint main = main.js
[2] ./src/index.js + 1 modules 344 bytes {0} [built]
    | ./src/index.js 244 bytes [built]
    | ./src/math.js 95 bytes [built]
    + 2 hidden modules
Done in 0.64s.

looks like everything is ending up in the bundle

Already started, see #536 for current progress. Now using import-size to automate that measurement process. Seems we will drop the initial size of Immer from 6KB to 3-3.5 KB

@markerikson (and others) the new version can be tried as [email protected]. Installation instructions can be found here: https://github.com/immerjs/immer/blob/multi-bundle/docs/installation.md#pick-your-immer-version

Awesome! I'll give it a shot tonight.

Looking at those docs, I think for RTK we'd probably enable ES5 support by default, but leave out Maps and Patches to save space. We'd then point users at the Immer docs with instructions to manually import Immer and flip the switches themselves if necessary.

Which actually brings up an interesting question: if one bit of code has already attempted to enable a plugin like ES5, is there a way to then turn around and _disable_ that support? Say I'm a Redux user who's only targeting modern browsers. If RTK has called enableES5() internally, is there a way for an app to override that and turn it off?

Follow-up question: so the alpha just came out now. What's your ETA on a potential final 6.0 release?

RTK 1.3 is in alpha right now as well. We're still trying to nail down some bits of behavior on our new APIs and make sure things are thoroughly documented. I'd assumed Immer 6 wouldn't be ready until after RTK 1.3 was out, but if you think Immer 6 will be out in the next few days, I might hold off so we could get that in too.

No there isn't. The problem is that at the moment RTK calls it once, it
cannot be tree-shaking out anymore. So turning it off afterwards would be
pointless anyway, as it would yield no benefits. So once RTK decides to add
it by default, it will always end up in the bundle.

On Tue, Feb 25, 2020 at 12:53 PM Mark Erikson notifications@github.com
wrote:

Follow-up question: so the alpha just came out now. What's your ETA on a
potential final 6.0 release?

RTK 1.3 is in alpha right now as well. We're still trying to nail down
some bits of behavior on our new APIs and make sure things are thoroughly
documented. I'd assumed Immer 6 wouldn't be ready until after RTK 1.3 was
out, but if you think Immer 6 will be out in the next few days, I might
hold off so we could get that in too.


You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
https://github.com/immerjs/immer/issues/500?email_source=notifications&email_token=AAN4NBC54FTLOKCQ6NZ5CF3REWAMFA5CNFSM4KE6RHX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEM5OUGY#issuecomment-591063579,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAN4NBDAGO4XNPUA4X3NVPLREWAMFANCNFSM4KE6RHXQ
.

I was thinking about releasing next Monday

On Tue, Feb 25, 2020 at 1:05 PM Michel Weststrate mweststrate@gmail.com
wrote:

No there isn't. The problem is that at the moment RTK calls it once, it
cannot be tree-shaking out anymore. So turning it off afterwards would be
pointless anyway, as it would yield no benefits. So once RTK decides to add
it by default, it will always end up in the bundle.

On Tue, Feb 25, 2020 at 12:53 PM Mark Erikson notifications@github.com
wrote:

Follow-up question: so the alpha just came out now. What's your ETA on a
potential final 6.0 release?

RTK 1.3 is in alpha right now as well. We're still trying to nail down
some bits of behavior on our new APIs and make sure things are thoroughly
documented. I'd assumed Immer 6 wouldn't be ready until after RTK 1.3 was
out, but if you think Immer 6 will be out in the next few days, I might
hold off so we could get that in too.


You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
https://github.com/immerjs/immer/issues/500?email_source=notifications&email_token=AAN4NBC54FTLOKCQ6NZ5CF3REWAMFA5CNFSM4KE6RHX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEM5OUGY#issuecomment-591063579,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAN4NBDAGO4XNPUA4X3NVPLREWAMFANCNFSM4KE6RHXQ
.

Yeah, that's what I figured.

This becomes an interesting question: assume that we have enough users who would want that behavior to enable it as the default (between RN and old browsers) and have the larger bundle size, or opt out and give instructions on how to enable it?

Given that I'd like to add this in an RTK minor, I'd say we'd probably want to go for enabling ES5 by default so that no one's code breaks (since that's how it behaves now), and consider maybe changing that in a notional future RTK 2.0.

Thanks for the info!

That makes perfect sense!

On Tue, Feb 25, 2020 at 1:09 PM Mark Erikson notifications@github.com
wrote:

Yeah, that's what I figured.

This becomes an interesting question: assume that we have enough users who
would want that behavior to enable it as the default (between RN and old
browsers) and have the larger bundle size, or opt out and give instructions
on how to enable it?

Given that I'd like to add this in an RTK minor, I'd say we'd probably
want to go for enabling ES5 by default so that no one's code breaks (since
that's how it behaves now), and consider maybe changing that in a notional
future RTK 2.0.

Thanks for the info!


You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
https://github.com/immerjs/immer/issues/500?email_source=notifications&email_token=AAN4NBDVHBHZMTZUCOXDQX3REWCJZA5CNFSM4KE6RHX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEM5QKCA#issuecomment-591070472,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAN4NBDDITGLSJ46344IX4LREWCJZANCNFSM4KE6RHXQ
.

Released as 6.0.0. (minimal) size has been reduced from 6.1 to 3.1 kb

Was this page helpful?
0 / 5 - 0 ratings