mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
### What does this PR do? Fixes printing `import.meta.url` and others with `--bytecode`. Fixes #14954. Fixes printing `__toESM` when output module format is CJS and input module format is ESM. The key change is that `__toESM`'s `isNodeMode` parameter now depends on the **input module type** (whether the importing file uses ESM syntax like `import`/`export`) rather than the output format. This matches Node.js ESM behavior where importing CommonJS from `.mjs` files always wraps the entire `module.exports` object as the default export, ignoring `__esModule` markers. ### How did you verify your code works? Added comprehensive test suite in `test/bundler/bundler_cjs.test.ts` with **23 tests** covering: #### Core Behaviors: - ✅ Files using `import` syntax always get `isNodeMode=1`, which **ignores `__esModule`** markers and wraps the entire CJS module as default - ✅ This matches Node.js ESM semantics for importing CJS from `.mjs` files - ✅ Different CJS export patterns (`exports.x`, `module.exports = ...`, functions, primitives) - ✅ Named, default, and namespace (`import *`) imports - ✅ Different targets (node, browser, bun) - all behave the same - ✅ Different output formats (esm, cjs) - format doesn't affect the behavior - ✅ `.mjs` files re-exporting from `.cjs` - ✅ Deep re-export chains - ✅ Edge cases (non-boolean `__esModule`, `__esModule=false`, etc.) #### Test Results: - **With this PR's changes**: All 23 tests pass ✅ - **Without this PR (system bun)**: 22 pass, 1 fails (the one testing that `__esModule` is ignored with import syntax + CJS format) The failing test with system bun demonstrates the bug being fixed: currently, format=cjs with import syntax still respects `__esModule`, but it should ignore it (matching Node.js behavior). --------- Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> Co-authored-by: Claude Bot <claude-bot@bun.sh>