Repository navigation
Build: Skip esbuild's per-file copy of externals imported by name - #85181
youknowriad wants to merge 2 commits into
Conversation
Externals are CommonJS modules (`module.exports = window.wp.data`), so esbuild converts them with `__toESM()` once per importing file, defining a getter on a new object for every export of the script. Bundles call it thousands of times while they load. When a file only imports exports by name, resolve the external to a module exporting an object that inherits from the global instead: there is nothing to copy, and reading an export calls no getter. Files using the namespace object as a whole keep the existing module. 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: +6.25 kB (+0.07%) Total Size: 8.96 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
|
|
Apparently that failure doesn't apply to the updated patch here. This PR doesn’t touch how the globals are built: they keep their getters and __esModule, so window.wp.isShallowEqual.__esModule is still true. It changes how our own bundles read another script’s global, and only in files that import it by name. |
|
This pushes esbuild over the limits 🙂 Apparently we don't like at all the In webpack this would be much easier, a plugin can modify almost everything, and in particular the source blob would be available directly. Maybe we should upgrade to another bundler. We were talking about rolldown, maybe it has a different plugin architecture. By the way, do we really need to check the type of the imports and avoid The really needed check could be whether the |
|
This is a very complex optimization that works around an inefficiency in esbuild. I'm not very eager to merge it and maintain it permanently. It should be an upstream issue instead. Most importantly, other bundlers do better in this area and similar optimizations (this one and #85180) are built-in. Webpack tracks which module imports what from other modules and is able to optimize out all intermediate proxies. An actual usage of As we probably don't want to go back to webpack, it's even more important that Rolldown does a similar optimization, too! No getter proxy for CJS import into an ESM file. Tracking of imports and proxying only default and namespace imports, and accessing named ones directly. I just started a task to migrate the entire repo to Rolldown, curious what it comes up with. |
|
@jsnajdr sounds good, curious about that migration too. |
What?
Changes how
@wordpress/buildbundles imports of other scripts (@wordpress/*packages and the vendor globals such as React). A file that only imports exports by name now reads them through an object that inherits from the script's global. esbuild no longer builds a getter-per-export copy of that script for every file that imports it.This is one of several independent changes that speed up the post editor's first block load metric, and the largest of them.
Why?
The externals plugin loads each external as CommonJS:
module.exports = window.wp.data;. For an ES import of a CommonJS module, esbuild emitsvar import_data = __toESM( require_data(), 1 )once per importing file.__toESMcreates a new object and defines a getter on it for every own property of the module, so the whole ofwindow.wp.components(hundreds of exports) is copied for each file that imports anything from it. The built bundles call__toESMthat way about 3,000 times each inblock-editor,block-libraryandeditor, and about 1,150 times incomponents.All of that runs while the scripts are evaluated, before the editor renders anything. In CPU profiles of the post editor loading the large performance-test post,
__copyPropsalone was ~15ms of self time in each ofblock-editor,block-libraryandeditor. Every later read of an import then also goes through one of those getters.Measured on top of #85180, over 6 loads of the large post each:
domContentLoadedfrom ~219ms to ~183ms.How?
In
onResolve, the plugin reads the importing file and checks how it imports the script (importsByNameOnlyInSource(), unit tested). If every import of that script in the file is a braces-onlyimport { … } fromorexport { … } fromthat doesn't namedefault, the import resolves to a separateexternal-by-namemodule:__toESMthen creates its object on top of that one. There are no own properties to copy, and named imports resolve through the prototype to the global's property. The value read is the same as before, and it is still read at access time.Everything else keeps the existing module:
import * as,export *andexport * as;{ default as … }. With node-mode interop,defaultis the module object itself, so it has to stay the real global;require(), dynamicimport(), and imports from non-file modules.That keeps namespace objects with their exports as own enumerable properties wherever code can observe them, for example
{ ...ns },Object.keys( ns )or object rest. The source check errs towards the existing module: anything it can't tell apart, such as a statement with an unusual shape, falls back to it. The source cache is cleared on each (re)build in watch mode.Testing Instructions
npm run build.build/scripts/block-editor/index.js), most externals are now listed asexternal-by-name:@wordpress/…. The few files that import a namespace or default export still usepackage-external:orvendor-external:.I compared trunk and this change on the large post. The rendered canvas HTML was byte-identical, console output on the post editor, site editor and widgets screen was identical, and a type/format/list/save/reload round trip produced the same content.
Use of AI Tools
Profiled, implemented and measured with Claude Code (Claude Opus 5.5).
🤖 Generated with Claude Code