Repository navigation
Migrate from esbuild to rolldown - #85476
Conversation
🤖 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: -511 kB (-5.63%) ✅ Total Size: 8.55 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. Custom Completer: should allow newlines after multiple option completions in
|
5197b47 to
b4e282b
Compare
|
AI is amazing :) |
|
All tests passing is a great signal, I'm performing some quick AI checks (I won't pretend that I can read the full diff and understand it at this point) |
One bad signal is the unusual flakiness of the e2e tests. Maybe the GitHub infra was unstable over the weekend, or it's something real. Hard to say. |
|
@jsnajdr, I don't think it's related; the numbers are similar in other PRs, and I noticed the change after #85428 (comment). |
|
Is there a way to retry all the tests (reset the flakiness thing) |
|
According to my agent, this is the most potentially problematic difference: The npm CommonJS output drops "use strict" everywhere and loses __esModule on 29 package entries. Neither has been reported on the PR yet. It doesn't feel that problematic to me because CommonJS output is really not that important these days, though I'm not sure. |
|
Also apparently, in the PR’s CommonJS build, all 22 data stores register an extra selector and an extra action named default. In base, none do. Calling select( 'core/notices' ).default() throws selector is not a function. |
b51e406 to
18cff4c
Compare
|
This fixes all three, and the CJS output then matches trunk: +// Rolldown's `__toESM` gives namespace imports a `default` unless the module has
+// its own, even when flagged `__esModule`. Follow the flag alone, like esbuild.
+const TO_ESM_OWN_DEFAULT_CHECK = ' || !__hasOwnProp.call(mod, "default")';
+const esModuleInteropPlugin = {
+ name: 'es-module-interop',
+ renderChunk( code ) {
+ if ( ! code.includes( 'var __toESM = ' ) ) {
+ return null;
+ }
+ if ( ! code.includes( TO_ESM_OWN_DEFAULT_CHECK ) ) {
+ throw new Error( 'Unexpected Rolldown `__toESM` helper.' );
+ }
+ // A same-length replacement keeps the source map valid.
+ return {
+ code: code.replace( TO_ESM_OWN_DEFAULT_CHECK, ' '.repeat( TO_ESM_OWN_DEFAULT_CHECK.length ) ),
+ map: null,
+ };
+ },
+}; format: 'cjs',
+ esModule: true,
+ strict: true,
exports: 'named',
sourcemap: true,
+ plugins: [ esModuleInteropPlugin ], |
|
youknowriad
left a comment
There was a problem hiding this comment.
We should ship this regardless of the CJS fixes or not.
Yes, this is an unpleasant behavior of Rolldown's There's also a The conclusion is that enumerating the namespace object is unreliable. You'll get all the named exports (the selectors) but there is no guarantee that there are no extra fields. |
| // Name each entry after its path in `src`, so the output mirrors it. | ||
| const entryPoints = Object.fromEntries( | ||
| srcFiles.map( ( file ) => [ | ||
| normalizePath( path.relative( srcDir, file ) ).replace( | ||
| /\.[^./]+$/, | ||
| '' | ||
| ), | ||
| file, | ||
| ] ) | ||
| ); |
There was a problem hiding this comment.
This changes the order in which component styles are injected. With one multi-entry build per package, shared CSS modules move into _chunks/, and Rolldown hoists those imports above everything else and reorders the externals. In ui/build-module/button/button.mjs, the style chunks now load before @base-ui/react/button and ../spinner/index.mjs. Trunk keeps the source order.
The CSS itself is byte-identical, but it's injected in a different order in 33 of the 40 bundles that inject styles, wp-components included. @wordpress/ui is mostly safe thanks to @layer, but the @wordpress/components SCSS modules are unlayered, and only some of them are already hardened against insertion order. strictExecutionOrder and preserveModules don't restore it. One build per file mostly does (82 flipped pairs down to 4 in wp-components), but in my local runs the packages build went from about 14s to 26s, wiping out the speed gain.
The order also isn't deterministic: three clean builds of this branch gave different style orders in modules/core-fields/*.js, while trunk's output is identical across builds.
Is the order change intended? If so, can we mention it in the description and make the output deterministic?
(I haven't identified a clear style regression, so this is mostly just flagging a potential problem to look out for as we roll this out.)
There was a problem hiding this comment.
That's a good catch. Rolldown doesn't inline the runtime (helpers like __toESM) and the style modules like ESbuild did, but puts them into shared chunks. And the shared chunk imports are then always at the top of the module, in a different order than they were in the original.
I disabled this and instructed Rolldown to inline everything just like ESbuild did. It won't be as efficient as it could be, but keeps the compiled source layout closer to the ESbuild original and eases the migration.
In the future we could put all this into separate files: runtime as a chunk, the shared style-runtime as an external dependency, and each .module.css file transpiled into a .module.css.js file that is imported.
I applied your CJS suggestion, including the rather ugly patch that modifies the I'm afraid that the proper patch is to be skeptical about the namespace object coming from |
|
Just imagine how long it would have taken us to ship such a migration in a pre AI world. |
It's surprising how easy it was, even in human terms 🙂 |
| return readFile( filePath, 'utf8' ); | ||
| } | ||
|
|
||
| compilerPromise ??= initAsyncCompiler(); |
There was a problem hiding this comment.
With all the focus on Rolldown here, I missed that these changes also opted us into the Sass Shared Resources API ❤️ This is something I've been interested in for a while to "keep the compiler warm" while building many packages vs. one-compiler-per-package, also prototyped previously by @sirreal in #85257 .
|
Follow-up fix for translator comments at #87055 |
Claude-generated migration from esbuild to Rolldown. Looks like a great upgrade.
The built scripts are much smaller, on average about 10%.
Big improvement in perf metrics, especially the "load-like" ones:
domContentLoadedis down by 30%,firstBlockby 10%. Both for Site Editor and Post Editor.We don't need any workarounds for bundler quirks. #79792 wrapped scripts in an IIFE because of
use strict, and Rolldown does this correctly out of the box. #85180 and #85181 fixes inefficienies in ES module export getters, and Rolldown also does this efficiently already.The PR itself is quite small.