Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: Reorganize code for maximum tree-shakability#1125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
steveluscher wants to merge1 commit intoimmerjs:main
base:main
Choose a base branch
Loading
fromsteveluscher:tree-shakable-af

Conversation

@steveluscher
Copy link
Contributor

Problem

Because every method is bound up in aClass, instantiated in the module factory, then exported, consumers will getevery method of Immer in their bundle, whether they use it or not.

A slight reorganization of the code would allow their optimizing compilers to tree-shake away the method implementations that they don't need.

Summary

This PR reorganizes the code such that requiringone method of Immer doesn't force you to take a hard dependency onall the others. You can now do this:

import{produce}from'immer';produce(state,draft=>{/* ... */});

…and have all of the other methods that you haven't used (produceWithPatches,setAutoFreeze,setUseStrictShallowCopy,applyPatches,createDraft,finishDraft) be reaped by your optimizing compiler.

Results

Consider this typical application that only usesproduce():

import{produce}from"immer";conststate={name:{first:"Ada",last:"Byron"},};console.log({before:state,after:produce(state,(draft)=>{draft.name.last="Lovelace";}),});

When bundled with an optimizing compiler:

pnpm dlx esbuild --bundle --define:process.env.NODE_ENV='"production"' --tree-shaking index.ts

This PR (in combination with#1124) results in a bundle that's13% smaller, gzipped (diff)

  • 4020 bytesbefore this PR (link)
  • 3496 bytes gzippedafter this PR (link)

Test plan

yarn test

aryaemami59 reacted with thumbs up emoji
@steveluschersteveluscherforce-pushed thetree-shakable-af branch 5 times, most recently fromc07b0a2 toc33d4b4CompareMay 18, 2024 23:32
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

@steveluscher

[8]ページ先頭

©2009-2025 Movatter.jp