Skip to content

Build: Skip esbuild's per-file copy of externals imported by name - #85181

Closed
youknowriad wants to merge 2 commits into
trunkfrom
perf/wp-build-externals-by-name
Closed

youknowriad wants to merge 2 commits into
trunkfrom
perf/wp-build-externals-by-name

Conversation

@youknowriad

Copy link
Copy Markdown
Contributor

What?

Changes how @wordpress/build bundles 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 emits var import_data = __toESM( require_data(), 1 ) once per importing file. __toESM creates a new object and defines a getter on it for every own property of the module, so the whole of window.wp.components (hundreds of exports) is copied for each file that imports anything from it. The built bundles call __toESM that way about 3,000 times each in block-editor, block-library and editor, and about 1,150 times in components.

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, __copyProps alone was ~15ms of self time in each of block-editor, block-library and editor. 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:

  • First block went from ~710ms to ~644ms, and domContentLoaded from ~219ms to ~183ms.
  • Total CPU time per load dropped by ~80ms.

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-only import { … } from or export { … } from that doesn't name default, the import resolves to a separate external-by-name module:

var value = window.wp.data; module.exports = value ? Object.create( value ) : value;

__toESM then 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 * and export * as;
  • default imports, including { default as … }. With node-mode interop, default is the module object itself, so it has to stay the real global;
  • require(), dynamic import(), 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

  1. npm run build.
  2. Open the post editor, site editor and widgets screen. They load and work as before, with no new console errors.
  3. In a built bundle (build/scripts/block-editor/index.js), most externals are now listed as external-by-name:@wordpress/…. The few files that import a namespace or default export still use package-external: or vendor-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

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>
@youknowriad youknowriad added [Type] Performance Related to performance efforts [Package] wp-build /packages/wp-build labels Oct 20, 2026
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>

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 props-bot label to refresh.

📦 Bundle size

Size Change: +6.25 kB (+0.07%)

Total Size: 8.96 MB

📦 View Changed
Filename Size Change
build/modules/abilities/index.min.js 44.8 kB +23 B (+0.05%)
build/modules/boot/index.min.js 58.1 kB +250 B (+0.43%)
build/modules/connectors/index.min.js 2.55 kB +53 B (+2.13%)
build/modules/core-abilities/index.min.js 918 B +11 B (+1.21%)
build/modules/core-actions/page.min.js 1.89 kB +64 B (+3.5%)
build/modules/core-actions/post_type_supports.min.js 6.32 kB +113 B (+1.82%)
build/modules/core-actions/wp_block.min.js 6.42 kB +66 B (+1.04%)
build/modules/core-actions/wp_template_part.min.js 1.31 kB +78 B (+6.35%) 🔍
build/modules/core-fields/attachment.min.js 45.5 kB +112 B (+0.25%)
build/modules/core-fields/page.min.js 6.38 kB +63 B (+1%)
build/modules/core-fields/post_type_supports.min.js 81.2 kB +167 B (+0.21%)
build/modules/core-fields/root_site.min.js 782 B +32 B (+4.27%)
build/modules/core-fields/wp_block.min.js 40.3 kB +72 B (+0.18%)
build/modules/core-fields/wp_template_part.min.js 40.3 kB +82 B (+0.2%)
build/modules/core-fields/wp_template.min.js 7.68 kB +74 B (+0.97%)
build/modules/dashboard-init/index.min.js 11.5 kB +44 B (+0.38%)
build/modules/edit-site-init/index.min.js 1.74 kB +40 B (+2.35%)
build/modules/lazy-editor/index.min.js 15.3 kB +114 B (+0.75%)
build/modules/route/index.min.js 25.2 kB +60 B (+0.24%)
build/modules/widget-primitives/index.min.js 2.35 kB +21 B (+0.9%)
build/modules/workflow/index.min.js 88.4 kB +86 B (+0.1%)
build/scripts/a11y/index.min.js 1.1 kB +11 B (+1.01%)
build/scripts/annotations/index.min.js 2.56 kB +41 B (+1.63%)
build/scripts/api-fetch/index.min.js 3.49 kB +30 B (+0.87%)
build/scripts/block-directory/index.min.js 55.5 kB +130 B (+0.23%)
build/scripts/block-editor/index.min.js 601 kB +332 B (+0.06%)
build/scripts/block-library/index.min.js 442 kB +185 B (+0.04%)
build/scripts/blocks/index.min.js 46.5 kB +137 B (+0.3%)
build/scripts/commands/index.min.js 22 kB +131 B (+0.6%)
build/scripts/components/index.min.js 371 kB +317 B (+0.09%)
build/scripts/compose/index.min.js 11.7 kB +69 B (+0.59%)
build/scripts/core-commands/index.min.js 4.05 kB +103 B (+2.61%)
build/scripts/core-data/index.min.js 79.4 kB +148 B (+0.19%)
build/scripts/customize-widgets/index.min.js 60.6 kB +231 B (+0.38%)
build/scripts/data-controls/index.min.js 836 B +13 B (+1.58%)
build/scripts/data/index.min.js 10.2 kB +51 B (+0.5%)
build/scripts/deprecated/index.min.js 789 B +11 B (+1.41%)
build/scripts/edit-post/index.min.js 53 kB +252 B (+0.48%)
build/scripts/edit-site/index.min.js 417 kB +410 B (+0.1%)
build/scripts/edit-widgets/index.min.js 67.6 kB +260 B (+0.39%)
build/scripts/editor/index.min.js 611 kB +449 B (+0.07%)
build/scripts/element/index.min.js 5.3 kB +30 B (+0.57%)
build/scripts/format-library/index.min.js 89.7 kB +115 B (+0.13%)
build/scripts/i18n/index.min.js 2.5 kB +10 B (+0.4%)
build/scripts/keyboard-shortcuts/index.min.js 1.72 kB +38 B (+2.26%)
build/scripts/keycodes/index.min.js 1.72 kB +12 B (+0.7%)
build/scripts/list-reusable-blocks/index.min.js 4.4 kB +54 B (+1.24%)
build/scripts/media-utils/index.min.js 258 kB +134 B (+0.05%)
build/scripts/notices/index.min.js 52.8 kB +107 B (+0.2%)
build/scripts/nux/index.min.js 911 B +12 B (+1.33%)
build/scripts/patterns/index.min.js 14.4 kB +142 B (+0.99%)
build/scripts/plugins/index.min.js 3.26 kB +58 B (+1.81%)
build/scripts/preferences/index.min.js 3.41 kB +82 B (+2.46%)
build/scripts/primitives/index.min.js 1.06 kB +18 B (+1.73%)
build/scripts/react-i18n/index.min.js 884 B +27 B (+3.15%)
build/scripts/reusable-blocks/index.min.js 911 B +14 B (+1.56%)
build/scripts/rich-text/index.min.js 15.4 kB +85 B (+0.55%)
build/scripts/router/index.min.js 6.05 kB +47 B (+0.78%)
build/scripts/server-side-render/index.min.js 2.19 kB +72 B (+3.39%)
build/scripts/theme/index.min.js 27 kB +46 B (+0.17%)
build/scripts/undo-manager/index.min.js 952 B +8 B (+0.85%)
build/scripts/upload-media/index.min.js 16.9 kB +71 B (+0.42%)
build/scripts/viewport/index.min.js 1.27 kB +26 B (+2.09%)
build/scripts/widgets/index.min.js 70.4 kB +111 B (+0.16%)

2c83c66 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 67932fa trunk % Change
timeToFirstByte 52.6 ms +8.17% -1.05% 52.85 ms +14.57% -1.61% -0.47%
largestContentfulPaint 88 ms +4.55% -0% 92 ms +4.35% -6.52% -4.35%
lcpMinusTtfb 35.5 ms +1.55% -2.25% 34.3 ms +18.95% -5.98% 3.5%
wpBeforeTemplate 28.56 ms +11.76% -1.23% 28.44 ms +18.99% -2.04% 0.42%
wpTemplate 20.72 ms +3.52% -2.75% 20.66 ms +3.15% -2.57% 0.29%
wpTotal 49 ms +8.63% -0.96% 49.07 ms +14.04% -1.73% -0.14%
wpMemoryUsage 7.64 MB +0% -0% 7.61 MB +0% -0% 0.46%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 67932fa trunk % Change
timeToFirstByte 41.05 ms +9.99% -1.22% 40.8 ms +7.35% -0.86% 0.61%
largestContentfulPaint 88 ms +6.82% -0% 90 ms +2.22% -2.22% -2.22%
lcpMinusTtfb 47.55 ms +6.31% -0.95% 47.4 ms +4.11% -3.06% 0.32%
wpBeforeTemplate 26.52 ms +7.96% -0.98% 26.31 ms +4.07% -1.48% 0.8%
wpTemplate 11.75 ms +4.43% -0.85% 11.86 ms +3.2% -0.93% -0.93%
wpTotal 38.24 ms +10.49% -0.94% 38.07 ms +7.8% -0.71% 0.45%
wpMemoryUsage 6.22 MB +0% -0% 6.19 MB +0% -0% 0.58%
wpDbQueries 9 +0% -0% 9 +0% -0% 0%

media-processing

Metric 67932fa trunk % Change
mediaProcessingJpeg 399.71 ms +0.14% -1.07% 405.99 ms +0.48% -0.6% -1.55%
mediaProcessingAvif 6031.44 ms +0.2% -0.03% 6061.74 ms +0.12% -0.03% -0.5%
mediaProcessingJpegToAvif 4164.66 ms +0.1% -0.3% 4182.94 ms +0.08% -0.24% -0.44%

media-upload

Metric 67932fa trunk % Change
jpegUploadProcessing 1682.95 ms +15.15% -15.05% 1416.02 ms +0.96% -0.18% 18.85%
pngUploadProcessing 179.78 ms +23.66% -8.67% 175.21 ms +22.81% -4.04% 2.61%
largeJpegUploadProcessing 1411.95 ms +1.27% -0.45% 1412.09 ms +0.68% -0.62% -0.01%
multipleImageUploadProcessing 1581.79 ms +0.34% -0.87% 1566.47 ms +2.61% -1.33% 0.98%

post-editor

Metric 67932fa trunk % Change
serverResponse 493.76 ms +7.78% -8.22% 499.08 ms +4.54% -2.6% -1.07%
firstPaint 291.73 ms +74.17% -13.43% 462.48 ms +11.98% -34.38% -36.92%
domContentLoaded 953.72 ms +2.08% -0.74% 1187.82 ms +3.74% -2.5% -19.71%
loaded 954.98 ms +2.07% -0.78% 1188.97 ms +3.73% -2.49% -19.68%
firstContentfulPaint 523.36 ms +1.95% -1.65% 513.82 ms +2.41% -18.81% 1.86%
firstBlock 3388.56 ms +0.82% -2.75% 3685.62 ms +4.2% -1.51% -8.06%
type 19.67 ms +1.32% -7.57% 18.65 ms +9.6% -5.95% 5.47%
typeWithoutInspector 17.26 ms +6.66% -3.01% 18.68 ms +1.23% -3% -7.6%
typeWithTopToolbar 24.6 ms +4.15% -4.76% 22.66 ms +6.62% -2.87% 8.56%
typeContainer 7.87 ms +2.67% -4.07% 7.95 ms +15.6% -2.77% -1.01%
focus 114.88 ms +2.15% -4.67% 123.53 ms +1.19% -4.25% -7%
firstFocus 238.79 ms +0% -0% 244.19 ms +0% -0% -2.21%
selectAll 489.84 ms +8.2% -5.4% 577.85 ms +1.8% -2.87% -15.23%
listViewOpen 71.19 ms +19.44% -4.75% 76.9 ms +12.69% -7.63% -7.43%
inserterOpen 39.8 ms +3.62% -9.22% 41.03 ms +10.5% -3.92% -3%
inserterHover 11.21 ms +11.78% -7.05% 11.73 ms +10.83% -9.46% -4.43%
inserterSearch 8.08 ms +12.62% -11.26% 8.32 ms +1.44% -14.54% -2.88%
loadPatterns 611.83 ms +9.72% -3.41% 659.07 ms +3.95% -0.73% -7.17%
wpTotal 484.3 ms +7.51% -8.33% 489.32 ms +4.66% -2.61% -1.03%
wpMemoryUsage 14.66 MB +0% -0% 14.63 MB +0% -0% 0.26%
wpDbQueries 55 +0% -1.82% 55 +0% -1.82% 0%

site-editor

Metric 67932fa trunk % Change
serverResponse 255.92 ms +3.96% -7.1% 254.13 ms +6.32% -5.47% 0.7%
firstPaint 164.78 ms +8.79% -2.09% 184.29 ms +2.34% -10.97% -10.59%
domContentLoaded 607.47 ms +2.44% -0.94% 795.29 ms +0.69% -0.56% -23.62%
loaded 608.5 ms +2.45% -0.94% 796.06 ms +0.69% -0.56% -23.56%
firstContentfulPaint 329.48 ms +1.77% -2.63% 344.15 ms +2.93% -2.37% -4.26%
firstBlock 2771.98 ms +1.05% -1.13% 2984.17 ms +0.6% -1.88% -7.11%
type 14.5 ms +6% -10.34% 14.22 ms +2.95% -1.97% 1.97%
navigate 85.67 ms +7.34% -2.84% 82.83 ms +14.95% -4.6% 3.43%
loadPatterns 881.33 ms +19.38% -8.26% 871.84 ms +18.73% -3.92% 1.09%
loadPages 966.66 ms +1.84% -1.48% 1003.37 ms +2.15% -5.46% -3.66%
wpTotal 249.33 ms +4.01% -7.31% 247.18 ms +6.6% -5.49% 0.87%
wpMemoryUsage 12.70 MB +0% -0% 12.66 MB +0% -0% 0.29%
wpDbQueries 44.5 +1.12% -1.12% 44.5 +1.12% -1.12% 0%

2c83c66 Run

🏁 Flaky tests

Some 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 /test/e2e/specs/editor/various/core-settings.spec.js, passed after 1 failed attempt.
Error: expect(received).toStrictEqual(expected) // deep equality

- Expected  - 1
+ Received  + 1

  Array [
    "rewrite_rules",
-   "SERIALIZED DATA",
+   "",
  ]
    at forEach (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/core-settings.spec.js:46:26)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/core-settings.spec.js:43:35
default to a list of pages if there are no menus in /test/e2e/specs/editor/blocks/navigation.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeVisible() failed

Locator: locator('[name="editor-canvas"]').contentFrame().getByRole('document', { name: 'Block: Page List' })
Expected: visible
Timeout: 10000ms
Error: element(s) not found

Call log:
  - Expect "toBeVisible" locator('[name="editor-canvas"]').contentFrame().getByRole('document', { name: 'Block: Page List' }) with timeout 10000ms
  - waiting for locator('[name="editor-canvas"]').contentFrame().getByRole('document', { name: 'Block: Page List' })

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/navigation.spec.js:34:34
drags a multi-selection from anywhere inside it in /test/e2e/specs/editor/various/draggable-blocks.spec.js, passed after 1 failed attempt.
TypeError: Cannot read properties of null (reading 'y')
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/draggable-blocks.spec.js:824:57
    at dragSelectionBelowLanding (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/draggable-blocks.spec.js:823:3)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/draggable-blocks.spec.js:844:3
restores the blocks when a multi-selection drag is canceled in /test/e2e/specs/editor/various/draggable-blocks.spec.js, passed after 1 failed attempt.
TypeError: Cannot read properties of null (reading 'y')
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/draggable-blocks.spec.js:824:57
    at dragSelectionBelowLanding (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/draggable-blocks.spec.js:823:3)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/draggable-blocks.spec.js:871:3
should cut and paste individual blocks with collapsed selection in /test/e2e/specs/editor/various/copy-cut-paste.spec.js, passed after 1 failed attempt.
Error: expect(string).toMatchSnapshot(expected) failed

  @@ -1,3 +1,3 @@
   <!-- wp:paragraph -->
  -<p>2Cut - collapsed selection</p>
  +<p>2</p>
   <!-- /wp:paragraph -->


    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/copy-cut-paste.spec.js:45:49

2c83c66 Run

@jsnajdr

jsnajdr commented Oct 20, 2026

Copy link
Copy Markdown
Member

@ellatrix did something that looks very similar in #78303, but it had to be reverted in #78917. Something with __esModule fields being broken.

Code-wise it was different, but the same idea: replace getter functions with constant fields.

@youknowriad

Copy link
Copy Markdown
Contributor Author

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.

@jsnajdr

jsnajdr commented Oct 20, 2026

Copy link
Copy Markdown
Member

This pushes esbuild over the limits 🙂 Apparently we don't like at all the __toESM/__toCJS machinery and its getters. On the other hand esbuild doesn't let us configure this, so we must do workarounds.

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 default imports? The Object.create() result still has all the fields accessible, via the prototype. They are just not enumerable. But is that really a problem? Maybe nobody enumerates them.

The really needed check could be whether the value is a function or an object. If it's a function, then it's the default export itself and we can't wrap it with Object.create. But an exports objects, with default or not, should be always wrappable.

@jsnajdr

jsnajdr commented Oct 23, 2026

Copy link
Copy Markdown
Member

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 select will be compiled into a direct window.wp.data.select reference. Without a chain of __commonJS/__toESM conversions.

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.

@youknowriad

Copy link
Copy Markdown
Contributor Author

@jsnajdr sounds good, curious about that migration too.

@youknowriad
youknowriad deleted the perf/wp-build-externals-by-name branch October 26, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] wp-build /packages/wp-build [Type] Performance Related to performance efforts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants