Repository navigation
Conversation
…review The CommonJS build evaluates memo( BlockListItems ) and memo( BlockList ) while block-list is still loading through the circular import, so both components were undefined there. Memoizing a wrapper reads the binding at render time instead.
🤖 PR meta 🤖🎉 PropsIf 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 📦 Bundle sizeSize Change: +17 B (0%) Total Size: 8.61 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Undo restores the post selection when another synced entity is loaded (RTC WebSocket) in
|
Co-authored-by: retrofox <77539+retrofox@users.noreply.github.com>
louwie17
left a comment
There was a problem hiding this comment.
Thanks for picking this up. The InnerBlocks change works, but the BlockPreview one only moves the failure from module load to render time. Details are inline.
What I tested
- Node, CommonJS build: on trunk,
require( 'build/index.cjs' )logs thememowarning twice. With this PR it logs it 0 times. - Node + jsdom, CommonJS build:
InnerBlocks(a group with a nested paragraph) renders with this PR. RenderingBlockPreview'sMemoizedBlockListstill throwsElement type is invalid … got: undefined. - Node + jsdom, CommonJS build, with the change suggested inline:
MemoizedBlockListrenders and the warning count stays at 0. - Browser (wp-env, PR build): nested blocks (Group > Columns > Paragraph) and the pattern inserter previews render, with no console errors. The browser loads the script bundle, not the
.cjsfiles, so this only shows that the PR doesn't break the editor. It doesn't exercise the CommonJS bug. npm run test:unitforinner-blocksandblock-previewpasses (3/3).lint:json the changed files is clean.
Testing gap
The testing instructions count memo warnings, which can't catch this case: the warning goes away while the render still fails. A full BlockPreview render in jsdom doesn't catch it either, because AutoBlockPreview only mounts the list once the container has a measured width, and jsdom always reports 0. A smoke test that requires build/index.cjs and renders the memoized list directly (or mocks a non-zero container width) would cover it. Not blocking, but without it this could break again unnoticed.
| // This is used to avoid rendering the block list if the sizes change. | ||
| const MemoizedBlockList = memo( BlockList ); | ||
| // Wrapped because the circular import leaves `BlockList` undefined when the CommonJS build runs this line. | ||
| const MemoizedBlockList = memo( function MemoizedBlockListWrapper( |
There was a problem hiding this comment.
The lazy wrapper still reads undefined here, so BlockPreview crashes when it renders from the CommonJS build.
The difference from inner-blocks is the import style. inner-blocks uses a named import, which Rolldown compiles to a live property read (___block_list_index_tsx.BlockListItems), so by render time it finds the function. This file uses a default import, which Rolldown wraps in __toESM():
let ___block_list_index_tsx = require("../block-list/index.cjs");
___block_list_index_tsx = __toESM(___block_list_index_tsx);__toESM copies getters only for the export names that exist at that moment. During the cycle, block-list has no exports yet, so the copy never gets default, even after block-list finishes loading. With this PR's build, after loading build/index.cjs, the copy in auto.cjs contains only __esModule, and .default is undefined. Rendering MemoizedBlockList throws:
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
I tested this fix, which switches to a named import so it compiles the same way as inner-blocks:
// block-list/index.tsx
export { BlockList };
// block-preview/auto.tsx
import { BlockList } from '../block-list';The build then reads ___block_list_index_tsx.BlockList live, MemoizedBlockList renders, and the warning count stays at 0. The added named export is in an internal module, so the package's public API doesn't change.
There was a problem hiding this comment.
You're right, thanks for digging into this. I only counted the warnings and never rendered BlockPreview from the CommonJS build.
Applied your fix.block-list exports BlockList by name and auto.tsx imports it that way.
Tested on a build of this branch under jsdom, with a mocked container width: InnerBlocks and BlockPreview both render and the warning count stays at 0. The previous commit fails on BlockPreview only, and trunk fails on both. I added the script to the testing instructions.
About the smoke test: the unit test job doesn't build the CommonJS output, so I'd leave a build-level check to #86328 rather than add one here for two components. Happy to add it here if you prefer.
The CommonJS build left the default import undefined
|
What if the Would that break the import cycle, too? Maybe together with making I also see another, very long cycle: This one is probably not dangerous at runtime, but it's still a cycle, I wonder if there is any way to break it. Block preview has a block list, it has an appender, which has inserter, and inserter has a block preview. That's how the UI is designed, it's not an accidental loop. |
|
Another possible improvement is splitting the Not sure if this alone fixes any errors, but is a good incremental and structural improvement of the dependency graph. |
|
@retrofox I'm curious about one thing: if you discovered and are fixing these bugs, it means that some project uses the CJS exports from the The most favored solution would be completely removing CJS from the packages, as mentioned in #86328 (comment). We are aware of the Jest consumers, but is there anything else? There are many ESM-only NPM packages already, and any Jest setup must have some way to handle them. Could that solution be extended also to the |
|
👋 also @anomiex or @tbradsha from Jetpack might be able to elaborate here regarding @jsnajdr 's question above. |
|
@jsnajdr Digging into this more, looks like it was Jest (Automattic/jetpack#54512 (comment)), and there's a fix in Automattic/jetpack#54534 |
|
If you want to drop CommonJS entirely, fine. As you said, many other packages have done the same. But as long as you are supplying CommonJS, it should work properly. Depending on what the package does (e.g. no top-level await), require(esm) might just work now if running jest with node 24.9+. Mocking might need updating though. Another solution for jest is to run it with Another is to tell it to transpile |
louwie17
left a comment
There was a problem hiding this comment.
Thanks for the updates! This works now: the memo warnings are gone, and InnerBlocks and BlockPreview both render from the CommonJS build. Nested blocks and the pattern previews also render fine in the browser on wp-env. It would be great to rebase this and get it merged. Approving for now, with one suggestion inline.
|
|
||
| const BlockListItemsMemo = memo( BlockListItems ); | ||
| // Wrapped because the circular import leaves `BlockListItems` undefined when the CommonJS build runs this line. | ||
| const BlockListItemsMemo = memo( function BlockListItemsMemoWrapper( |
There was a problem hiding this comment.
I think @jsnajdr's approach is the cleaner one: memoize BlockList and BlockListItems in block-list/index.tsx and import the memoized versions by name. That removes both wrappers and the extra export { BlockList }. I tried it locally and it works the same. Together with his split of block-list/index.tsx, it would remove the block-list ↔ inner-blocks cycle completely.
Happy for this to be a follow-up, but also happy if you'd like to make the change here right away.
What?
InnerBlocksand the autoBlockPreviewmemoize a wrapper around the block list instead of the importedBlockListItemsandBlockListbindings.BlockPreviewalso importsBlockListby name.Why?
Both modules sit in an import cycle with
block-listand callmemo()at module scope.In the CommonJS build that line runs before
block-list/index.cjshas assigned its exports, somemo()receivesundefined: loading@wordpress/block-editor/build/index.cjslogsmemo: The first argument must be a component. Instead received: undefinedtwice, and renderingInnerBlocksorBlockPreviewfrom that build fails.BlockPreviewhas a second problem. Its default import compiles to a__toESM()copy of theblock-listexports, taken while they are still empty, soBlockListstaysundefinedthere even at render time.The ESM build is unaffected.
This is a workaround for the Rolldown CommonJS output regression tracked in #86328. The cycle stays:
block-listandinner-blocksimport each other, and both sit in a 98-module cycle through thecomponentsindex.How?
The memoized component is a wrapper that renders
BlockListItemsorBlockListat render time. Memoization still skips re-renders on unchanged props.block-listalso exportsBlockListby name andBlockPreviewimports it that way, so the CommonJS build reads it from the module at render time, as it already does forBlockListItems. The package's public exports don't change.Testing Instructions
Build, then count the warnings logged on load:
On trunk this prints
2(the require then stops onwindow is not defined, which is expected outside a browser). With this change it prints0.The count doesn't cover rendering. Save the script below as
cjs-check.cjsin the repository root to render both components from the CommonJS build under jsdom:On trunk both lines end in
FAILS. With this change both end inrenders.cjs-check.cjs