Repository navigation
Conversation
Rolldown's __toESM helper copies a module's exports when it is required. In a circular import the module has not assigned its exports yet, so the copy never gets them. Return modules flagged __esModule as they are. Part of WordPress#86328.
🤖 PR meta 🤖👋 Welcome👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @Xmerlin7! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. 🎉 PropsUnlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @darageelys40@example.com, @Xmerlin7. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the |
Rolldown assigns exports at the end of each CommonJS file, after its require() calls, so a module that reads an export during a circular import gets undefined. Define the exports as getters at the top of the file, like the esbuild output did. Fixes the memo(undefined) warnings in block-editor (WordPress#86328).
Thanks, fixing the cycles themselves makes sense, and #86322 and #86323 are good either way. Two reasons I'd still consider a build fix:
If you'd rather fix this in the source, I'm happy to close this and send PRs removing the remaining cycles instead. @jsnajdr, which would you prefer? |
|
I think the initial wording of my issue implied that it should be fixed in wp-build, so I have corrected that. What clearly should be fixed is the circular imports in the packages. Preserving the previous behavior of wp-build in addition to that is only appropriate once we've established a good reason to do so, which I don't think we have yet. |
|
@Mamaduka I really wanna contribute to Wordpress, any advice or 'good first-issue' assign to me? |
What?
Closes #86328.
Fixes circular imports in wp-build's CommonJS output, both when an export is read after the cycle has loaded (
apiFetchwithper_page=-1) and when it is read while the other module is still loading (thememo(undefined)warnings in@wordpress/block-editor).Why?
Since the move to Rolldown (#85476), the CommonJS output differs from the esbuild output in two ways that break circular imports:
__toESMhelper copies a module's exports when it is required. In a cycle the module has not assigned its exports yet, so the copy never gets them.fetch-all-middleware.cjskeeps a copy ofindex.cjswithoutdefault, soper_page=-1requests fail with(0 , ___index_ts.default) is not a function, andchildren.fromDOMin@wordpress/blockssilently returns[].require()calls. A module that reads an export while the other module is still loading getsundefined, even when the export is a function declaration. That is whyblock-preview/autoandinner-blockspassundefinedtomemoforBlockList. esbuild defined the exports as getters before therequire()calls.How?
Two
renderChunkplugins in the CommonJS transpile build:es-module-interopalready rewrites Rolldown's__toESMhelper. It now returns modules flagged__esModuleas they are instead of copying them, so imports see exports assigned later. Such modules still get no syntheticdefault. Modules not flagged__esModuleandisNodeModecalls go through the original helper.exports-firstis new. It parses the chunk withrolldown/parseAst, finds theexports.name = localName;assignments at the end of the file, and defines them as getters right after the__esModuleheader, before anyrequire(). Function declarations are hoisted, so their getters return them at once. Other bindings return their value once it is assigned.Both plugins throw on output they don't expect, so a change in Rolldown's output fails the build instead of producing broken code. Neither moves the code that the source map covers: the getters go on the header's line, and the original assignments are replaced with spaces.
Each fix covers a case the other cannot. The first is still needed for re-exports such as
export { default } from './x', because Rolldown defines those after therequire()they read from. The tests cover each case.Like the esbuild output, exports are now non-writable getters. Code that assigned to a CommonJS export, such as some test mocks, would fail with this output, as it did with esbuild. Rolldown already defined re-exports this way.
Testing Instructions
npm run test:unit -- packages/wp-build/lib/test/es-module-interop.test.js. The test runs wp-build on a fixture with circular imports and executes the generated.cjs, so it does not go through the Vitest aliases to package sources. On trunk, the three circular tests fail. Without the__toESMchange, the re-exported default test fails. Withoutexports-first, the read-during-load test fails.npm run build, then in Noderequire( './packages/block-editor/build/index.cjs' ). The twomemo: The first argument must be a componentwarnings are gone (this needs a DOM, for example jsdom).require( './packages/api-fetch/build/index.cjs' ).default, set a fetch handler withsetFetchHandler, registerfetchAllMiddleware, and request/wp/v2/posts?per_page=-1. It resolves instead of throwing.I also transpiled all 127 packages with trunk and with this change and compared:
exports.nameoutside the replaced statements.api-fetchwithper_page=-1andchildren.fromDOMwork. I could not reproduce theToggleGroupControlcase in my setup, and did not render theedit-sitenavigation menu screen. Both are the delayed-read case covered by the first fix.Testing Instructions for Keyboard
Not applicable, no UI changes.
Use of AI Tools
This PR was written with Claude Code (Claude Opus 5.5). I reviewed the change and the verification described above.