Skip to content

Conversation

@steveluscher
Copy link
Contributor

Consider an application that uses only produce but no other methods. This PR allows such an application to be properly tree-shaken by a bundler, to eliminate the other exports altogether.

import { produce } from "immer";
const state = {
  name: "A",
  icon: "III",
  accounts: [
    { address: "X", foo: "bax" },
    { address: "Y", foo: "bax" },
    { address: "Z", foo: "bax" },
  ],
};
const updateAccount = produce(
  (draft: typeof state, newAccount: (typeof state)["accounts"][number]) => {
    const accountIndex = draft.accounts.findIndex(
      ({ address }) => address === newAccount.address
    );
    if (accountIndex !== -1) {
      draft.accounts[accountIndex].address = newAccount.address;
      draft.accounts[accountIndex].foo = newAccount.foo;
    }
  }
);
const nextState = updateAccount(state, { address: "X", foo: "bar" });
console.log(state, nextState);

Bundle

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

Diff as compared to the version on main

658,663d657
<   var produceWithPatches = immer.produceWithPatches.bind(immer);
<   var setAutoFreeze = immer.setAutoFreeze.bind(immer);
<   var setUseStrictShallowCopy = immer.setUseStrictShallowCopy.bind(immer);
<   var applyPatches = immer.applyPatches.bind(immer);
<   var createDraft = immer.createDraft.bind(immer);
<   var finishDraft = immer.finishDraft.bind(immer);

Copy link
Collaborator

@mweststrate mweststrate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this!

@mweststrate mweststrate merged commit 85faaa2 into immerjs:main Sep 1, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2025

🎉 This PR is included in version 10.1.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants