immerjs / immer

Create the next immutable state by mutating the current one
https://immerjs.github.io/immer/
MIT License
27.53k stars 852 forks source link

v10 breaks ESM import. #1046

Closed gaomeng1900 closed 1 year ago

gaomeng1900 commented 1 year ago

๐Ÿ› Bug Report

According to the v10 release note:

Immer is now an ESM package that can be directly imported into the browser. CJS should still work, UMD support has been removed.

But when I import Immer in a native ESM environment. I got a ReferenceError: process is not defined error.

const mod = await import("https://unpkg.com/immer@10.0.1/dist/immer.mjs");

The error is caused by:

// src/utils/errors.ts
var errors = process.env.NODE_ENV !== "production" ? [

in dist/immer.mjs.

Link to repro

codesandbox

To Reproduce

See codesandbox above.

Observed behavior

Got ReferenceError: process is not defined error.

Expected behavior

Should not rely on process.env.NODE_ENV.

Environment

mweststrate commented 1 year ago

You are probably importing the wrong file, please check the other files available in dist.

On Fri, 5 May 2023, 14:12 Simon ยท ่ฅฟ่Œ, @.***> wrote:

๐Ÿ› Bug Report

I have been using Immer in the browser with native ESM environment. Everything was fine. After upgrading Immer to v10.0.1. I got a ReferenceError: process is not defined error.

The error is caused by:

// src/utils/errors.tsvar errors = process.env.NODE_ENV !== "production" ? [

in dist/immer.mjs.

process.env.NODE_ENV assume that there is a bundler that handles common NodeJS environment vars which are no parts of JS or ESM or browser specs.

My suggestion is that if we decide to respect ESM spec. Make NODE_ENV optional. If not. Make a declaration in the document that certain polyfills are neccessary. Link to repro

codesandbox https://codesandbox.io/s/happy-tharp-6dgzl8?file=/index.html To Reproduce

See codesandbox above. Observed behavior

V9 is ok. V10 got ReferenceError: process is not defined error. Expected behavior

Should not rely on process.env.NODE_ENV. Environment

  • chrome 113
  • v10.0.1
  • I filed this report against the latest version of Immer
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)

โ€” Reply to this email directly, view it on GitHub https://github.com/immerjs/immer/issues/1046, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN4NBGVTE2A7E3PBKAO6HLXETVDHANCNFSM6AAAAAAXXBA7NI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

gaomeng1900 commented 1 year ago

@mweststrate You are right. dist/immer.production.mjs works fine. I updated the codesandbox demo.

gaomeng1900 commented 1 year ago
const mod = await import("https://unpkg.com/immer@10.0.1/dist/immer.production.mjs");

Works.

markerikson commented 1 year ago

Yep, that immer.production.mjs file was included specifically for this use case, and has the process checks compiled away.