Skip to content

experimentalImportSupport: Don't copy __esModule in export * from - #2281

Open
burrows99 wants to merge 1 commit into
react:mainfrom
burrows99:fix/export-all-skip-esmodule
Open

burrows99 wants to merge 1 commit into
react:mainfrom
burrows99:fix/export-all-skip-esmodule

Conversation

@burrows99

Copy link
Copy Markdown
Contributor

Summary

Under experimentalImportSupport, export * from 'pkg' becomes

Object.defineProperty(exports, '__esModule', {value: true});
var _pkg = require('pkg');
for (var _key in _pkg) {
  if (_key === "default") continue;
  exports[_key] = _pkg[_key];
}

__esModule is defined read-only first. A CommonJS module compiled by Babel in loose mode (or written by hand) sets it as an ordinary, enumerable property:

"use strict";

exports.__esModule = true;
exports.formatDate = formatDate;

so the loop copies __esModule too, and in strict mode (which the React Native preset adds) the re-exporting module throws at startup:

TypeError: Cannot assign to read only property '__esModule' of object '#<Object>'

The fix skips __esModule as well as default, which is what Babel's own export * interop does (if (key === "default" || key === "__esModule") return; in transform-modules-commonjs). The re-exporting module still has its own __esModule: true.

flowchart TD
    A[export star from a package built in Babel loose mode] --> B{Copy the enumerable es module flag}
    B -->|Before| C[Assignment to a read only property throws at startup]
    B -->|After| D[Flag skipped so the named exports come through]
Loading

Changelog: [Fix] experimentalImportSupport: export * from a module with an enumerable __esModule no longer throws in strict mode

Test plan

Added export all does not copy an enumerable __esModule of the source to import-export-plugin-test.js: it runs the transformed export * from 'foo' in strict mode against {__esModule: true, named: 'star named'}. It fails on main with the TypeError above and passes with this change. The two inline snapshots of the export * output are updated for the new condition.

yarn jest packages/metro-transform-plugins packages/metro-transform-worker packages/metro/src/integration_tests
Tests:       351 passed, 351 total

yarn flow check  -> No errors!
eslint and prettier --check on the two changed files -> clean

I did not run the full yarn test.

End to end with the Metro CLI from this branch, transform: {experimentalImportSupport: true} and the React Native Babel transformer. utils.js is export * from 'date-format', the package above, and index.js is import {formatDate} from './utils'; console.log('Released on', formatDate(new Date(0)));:

$ metro build index.js --dev false --minify false --out bundle.js > /dev/null && node bundle.js
TypeError: Cannot assign to read only property '__esModule' of object '#<Object>'   # main
Released on 1970-01-01                                                              # this branch

Narrated recording, before then after (sound on):

evidence.mp4

Edge cases and unchanged behaviour checked in the recording (the CLI from this branch):

  • the dev build and the minified build both print Released on 1970-01-01
  • import * as utils from './utils' gives Object.keys(utils) = [ 'formatDate' ] and utils.__esModule = true
  • unchanged: export * from a plain CommonJS package (no __esModule) and from one built in Babel's default mode (Object.defineProperty(exports, "__esModule", ...), not enumerable) prints hello-metro Metro, as on main

AI assistance (Claude Code) was used to find this bug, write the change and run the checks above.

🤖 Generated with Claude Code

`export * from 'pkg'` copied every enumerable key except `default` from
`pkg` onto `exports`, which already has a read-only `__esModule`. A CommonJS
module compiled by Babel in loose mode sets an enumerable
`exports.__esModule = true`, so in strict mode the re-exporting module threw
`Cannot assign to read only property '__esModule'`. Skip `__esModule` too,
as Babel's own `export *` helper does.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 23, 2026
@github-actions

Copy link
Copy Markdown

Metro review: Looks good

Risk: low · Commit: 29f27d7 · 4 commands run, in a restricted sandbox after an automated safety assessment

Under experimentalImportSupport, the export * loop now skips __esModule as well as default. The plugin always defines __esModule as read-only before that loop runs, so a source module with an enumerable __esModule used to throw in strict mode. Babel's CommonJS interop skips the same two keys. The new test fails on the base with the reported TypeError and passes on the head. The plugin file is already in the transform cache key, so no cached output goes stale.

No findings.

Verified
  • ✅ yarn jest --no-watchman packages/metro-transform-plugins — 201 tests pass at the head
  • ✅ (base checkout, with the head's test file copied in) yarn jest --no-watchman packages/metro-transform-plugins/src/__tests__/import-export-plugin-test.js (failed as expected) — The new test fails with TypeError: Cannot assign to read only property '__esModule'. The two updated inline snapshots also fail against the old template, as they should.
  • ✅ yarn jest --no-watchman packages/metro-transform-worker packages/metro/src/integration_tests — 150 tests pass
  • ✅ yarn eslint on the two changed files

Not run: No benchmark: the change adds one string comparison per re-exported key at module init, so it has no measurable cost. No Windows run: the logic is platform-neutral. The end-to-end CLI repro in the description was not repeated, because the unit test runs the same transformed code in strict mode. I didn't run yarn typecheck, since no types change.

Automated review by Claude. Advisory only: it never approves or blocks, and a maintainer makes the call.

This branch has not been deployed

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant