Skip to content

jamesopstad/rolldown-assignment-repro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rolldown CJS-ESM Interop Bug: Property Assignment on CJS Module Exports

Description

When importing a CommonJS module and assigning to a property on module.exports, Rolldown generates code that fails at runtime. This reproduction uses the debug package as an example, but the issue affects any CJS module where you need to assign to a property on module.exports.

Error

TypeError: Cannot set property log of [object Object] which has only a getter

Root Cause

Rolldown generates inconsistent access patterns when wrapping CJS modules for ESM:

  • For function calls: correctly uses import_src.default()
  • For property assignments: incorrectly uses import_src.prop = ...

The ESM wrapper object created by __toESM has getter-only properties, so direct assignment fails.

Expected: import_src.default.prop = ... (consistent with function calls)

Reproduction

pnpm install
pnpm build
pnpm preview   # Fails with TypeError

Source Code

import debug from "debug";

debug.log = (...args: any[]) => {
  console.log("[custom]", ...args);
};

const testNamespace = debug("test");
testNamespace("Hello from test namespace");

Bundled Output (Problematic)

var import_src = /* @__PURE__ */ __toESM(require_cjs_module(), 1);
import_src.log = (...args) => { ... };        // FAILS - getter-only property
(0, import_src.default)("test");              // Works - uses .default

Expected Bundled Output

var import_src = /* @__PURE__ */ __toESM(require_cjs_module(), 1);
import_src.default.log = (...args) => { ... }; // Works - accesses actual module.exports
(0, import_src.default)("test");               // Works

Comparison

Bundler Property Assignment Function Call Result
Rolldown import_src.prop import_src.default() FAILS
esbuild import_src.default.prop import_src.default() Works
Rollup mod.prop mod() Works

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors