Repository navigation
Build: Expose script exports on globals as data properties - #85180
Conversation
esbuild exposes an IIFE's namespace as getters that forward to the getters of the module's own exports, so every use of a script's export by another script called two getters. Replace them, at the end of the bundle, with read-only data properties holding the same values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🤖 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. Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: +7.61 kB (+0.08%) Total Size: 8.97 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. Regression: updating a specific option will only change its value and will not corrupt others in
|
|
There's one thing related to this that may cause some crashes I've seen on WordPress.com. The synthetic externals module generated by esbuild looks like this: // package-external:@wordpress/blocks
var require_blocks = __commonJS({
"package-external:@wordpress/blocks"(exports, module) {
module.exports = window.wp.blocks;
}
});It assigns the On WordPress.com, I've seem many reported crashes that are of the type: and the offending expression is like But one theoretical possibility is that at the time when the external module is initialized, It should never happen, the |
|
Should we give this a try? |
jsnajdr
left a comment
There was a problem hiding this comment.
Yes, this is a valid second attempt at #78303. The main improvement is checking and exposing the non-enumerable __esModule property. A secondary improvement is that the properties of the globals are now non-writable. With Object.assign they were writable.
We're giving up ESM live bindings, and get better performance in return. That's fair.
What?
In the scripts built by
@wordpress/build, each script's global (for examplewindow.wp.data) now holds its exports as read-only data properties, instead of esbuild's getters.This is one of several independent changes that speed up the post editor's first block load metric.
Why?
For an IIFE with a
globalName, esbuild returns__toCommonJS( src_exports ). That is an object with a getter for each export, which readssrc_exports, which is itself an object of getters (__export) returning the module's bindings. So every time a script uses another script's export (wp.data.useSelect,wp.element.createElement, …), two getters run.These reads are everywhere: each block render calls many
@wordpress/data,@wordpress/elementand@wordpress/composeexports, through these globals. While the editor loads, most of that code hasn't been optimized by the JIT yet, so each getter is a real function call. In CPU profiles of the post editor loading the large performance-test post, the bundles'getfunctions added up to ~40ms of self time per load. With this change that dropped to ~15ms, and total CPU time per load dropped by ~24ms. The remaining getters are on the importing side, which is a separate change.How?
The generated footer of each script that exposes a global now replaces the namespace object with a plain object holding the same values:
wp.data.selectstill has no effect (and throws in strict mode);__esModule: true, like before.It only applies when the global is the namespace object (
__esModule), not to packages exposing their default export (wpScriptDefaultExport, such aswp.apiFetch).The values are read once, when the script finishes evaluating. No Gutenberg script relies on a live binding: the only
export letinpackages/*/srcare type declarations incore-data/src/dynamic-entities.ts.Prior attempt
#78303 made the same kind of change and was reverted in #78917 because of #78697. That version rebuilt the global with
Object.assign( {}, namespace ), which only copies enumerable properties, so the non-enumerable__esModulewas lost. Webpack's__webpack_require__.n()then treatedwindow.wp.*as CommonJS, and default imports from plugins using@wordpress/*as webpack externals (for example WooCommerce'simport isShallowEqual from '@wordpress/is-shallow-equal') received the namespace object instead of the function.This PR defines
__esModuleexplicitly on the new object, non-enumerable, non-writable and non-configurable, exactly as esbuild's__toCommonJSdoes. It copiesdefaultalong with the other exports. In the post editor, every Gutenbergwindow.wp.*namespace has__esModule: true, and webpack's interop (m.__esModule ? () => m.default : () => m) returns theisShallowEqualfunction, which runs. Core's legacy globals that never had__esModule(wp.ajax,wp.heartbeat, …) are not touched.Testing Instructions
npm run build.wp.isShallowEqual.__esModuleistrueandwp.isShallowEqual.defaultis a function (the esbuild build pipeline strips__esModulefromwindow.wp.*globals, breaking webpack default-import interop for external consumers #78697 regression).Object.getOwnPropertyDescriptor( wp.data, 'select' )shows avalueinstead of aget.Object.keys( wp.data )lists the same exports as on trunk.I compared every
window.wp.*namespace in the post editor between trunk and this branch. Keys, value types and the absence ofundefinedexports were identical.Use of AI Tools
Profiled, implemented and measured with Claude Code (Claude Opus 5.5).
🤖 Generated with Claude Code