Skip to content

platform: "node" causes CJS module exports to be silently dropped (immer) #8345

@silverwind

Description

@silverwind

Filed by @silverwind via Claude Code

Bug Report

When platform: "node" is set, rolldown silently drops all exports.xxx = ... assignments from immer's CJS development build (immer.cjs.development.js). The wrapped module returns an empty {} object at runtime. The production build (immer.cjs.production.min.js) is unaffected.

Removing platform: "node" fixes the issue.

Reproduction

mkdir rolldown-repro && cd rolldown-repro
npm init -y
npm install [email protected] [email protected]

entry.js:

const immer = require("immer");
console.log("default:", typeof immer.default, "produce:", typeof immer.produce);
console.log("keys:", Object.keys(immer).slice(0, 5));

rolldown.config.mjs:

import { defineConfig } from "rolldown";

export default defineConfig({
  input: "entry.js",
  platform: "node",
  output: { format: "esm", file: "dist/bundle.js" },
});
npx rolldown -c rolldown.config.mjs && node dist/bundle.js

Expected

default: function produce: function
keys: [ 'Immer', 'applyPatches', 'castDraft', 'castImmutable', 'createDraft' ]

Actual

default: undefined produce: undefined
keys: []

Analysis

In the bundle, require_immer_cjs_development wraps the development build's source but the exports.xxx = ... assignments at the end of the file are completely removed. The wrapped callback only contains local variable declarations, ending with var produce = immer.produce; — no exports.

The production build (require_immer_cjs_production_min) retains its exports and works correctly. The key difference: the production build uses comma-separated assignments on a single expression statement (exports.Immer = nn, exports.default = tn, ...), while the development build uses separate exports.xxx = ...; statements.

Removing platform: "node" causes both builds to bundle and work correctly.

Workaround

Define process.env.NODE_ENV as "production" at build time so the broken development build is tree-shaken:

defineConfig({
  // ...
  env: { NODE_ENV: "production" },
});

Versions

  • rolldown: 1.0.0-rc.3 and 1.0.0-rc.4
  • immer: 9.0.21
  • Node.js: v22+
  • OS: macOS and Linux

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions