Build: Add vendorScripts config to build packages from node_modules#74343
Conversation
|
I think the script bundles latest version of the scripts is causing the CI failures. |
| } = config; | ||
|
|
||
| const { createRequire } = await import( 'module' ); | ||
| const require = createRequire( import.meta.url ); |
There was a problem hiding this comment.
Instead of resolving the package ourselves, we could tell esbuild to do it for us. Just tell it to bundle this synthetic module:
export * from 'react';
where react is the ${name} value. With the iife format and globalName set to React, it will produce a script that assigns all react exports to the React global. Exactly what we want.
There was a problem hiding this comment.
@jsnajdr is this something you're planning to work on? Looks like the UMD build is the biggest blocker for the React 19 upgrade right now
There was a problem hiding this comment.
Yes, I plan to get fully immersed into everything related to React 19 once I finish my support rotation this week 🙂
c23a39b to
3d390b4
Compare
a6d0e32 to
6cb4485
Compare
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @claude. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If 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. |
|
I reworked the code a bit and now it's fully integrated in The support for generic The net result of this PR is that even React 18 is build using the bundling method, and we ignore the UMD build in React 18. Upgrade to React 19 will be seamless. |
|
Size Change: -1.34 kB (-0.04%) Total Size: 3 MB
ℹ️ View Unchanged
|
|
How to test this:
|
|
@jsnajdr, do you mind doing another rebase? I'm seeing untracked files when switching to this branch, which I believe is fixed on trunk. P.S. I've seen the same problem on other older branches, which can be a sign that the branch isn't up to date. Screenshot
|
Mamaduka
left a comment
There was a problem hiding this comment.
Followed the testing instructions and everything is working as expected ✅
Adds support for building vendor packages (like React) from node_modules instead of relying on pre-built UMD bundles. This is needed for React 19+ which no longer ships UMD builds. New `vendorScripts` config in `wpPlugin`: - `name`: Package name (e.g., 'react', 'react/jsx-runtime') - `global`: Global variable name (e.g., 'React', 'ReactJSXRuntime') - `handle`: WordPress script handle (e.g., 'react', 'react-jsx-runtime') - `dependencies`: Array of dependency handles The bundleVendorScript function: - Resolves packages from node_modules using require.resolve() - Builds IIFE bundles with proper globalName - Externalizes other vendor packages to avoid duplication - Generates .asset.php files with dependencies and version hash Default vendor externals are preserved in wordpress-externals-plugin for third-party plugins that rely on WordPress core providing these scripts.
6cb4485 to
cafed7d
Compare
|
Flaky tests detected in cafed7d. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/21634507643
|
done, and the PR continues to be green. The untracked files come from #74785 which was merged a few days ago. |
|
Thanks @jsnajdr! |
…74343) * Build: Add vendorScripts config to build packages from node_modules Adds support for building vendor packages (like React) from node_modules instead of relying on pre-built UMD bundles. This is needed for React 19+ which no longer ships UMD builds. New `vendorScripts` config in `wpPlugin`: - `name`: Package name (e.g., 'react', 'react/jsx-runtime') - `global`: Global variable name (e.g., 'React', 'ReactJSXRuntime') - `handle`: WordPress script handle (e.g., 'react', 'react-jsx-runtime') - `dependencies`: Array of dependency handles The bundleVendorScript function: - Resolves packages from node_modules using require.resolve() - Builds IIFE bundles with proper globalName - Externalizes other vendor packages to avoid duplication - Generates .asset.php files with dependencies and version hash Default vendor externals are preserved in wordpress-externals-plugin for third-party plugins that rely on WordPress core providing these scripts. * Simplify the vendor build * Move to build-vendors.mjs --------- Co-authored-by: jsnajdr <[email protected]> Co-authored-by: tyxla <[email protected]> Co-authored-by: Mamaduka <[email protected]>
…74343) * Build: Add vendorScripts config to build packages from node_modules Adds support for building vendor packages (like React) from node_modules instead of relying on pre-built UMD bundles. This is needed for React 19+ which no longer ships UMD builds. New `vendorScripts` config in `wpPlugin`: - `name`: Package name (e.g., 'react', 'react/jsx-runtime') - `global`: Global variable name (e.g., 'React', 'ReactJSXRuntime') - `handle`: WordPress script handle (e.g., 'react', 'react-jsx-runtime') - `dependencies`: Array of dependency handles The bundleVendorScript function: - Resolves packages from node_modules using require.resolve() - Builds IIFE bundles with proper globalName - Externalizes other vendor packages to avoid duplication - Generates .asset.php files with dependencies and version hash Default vendor externals are preserved in wordpress-externals-plugin for third-party plugins that rely on WordPress core providing these scripts. * Simplify the vendor build * Move to build-vendors.mjs --------- Co-authored-by: jsnajdr <[email protected]> Co-authored-by: tyxla <[email protected]> Co-authored-by: Mamaduka <[email protected]>

Note This is fully vibe-coded with my own instructions about how it should be API wise. I also didn't test it yet.
Summary
Adds support for building vendor packages (like React) directly from
node_modulesinstead of relying on pre-built UMD bundles. This addresses the React 19 upgrade blocker where UMD builds are no longer shipped.Changes
vendorScriptsconfig inwpPlugin(package.json)bundleVendorScript()function that bundles packages from node_modules into IIFE formatwordpress-externals-pluginto accept vendor scripts config while preserving defaults for third-party pluginsConfiguration
Build Output
build/scripts/react/index.min.js (~7KB) - exposes window.React
build/scripts/react-dom/index.min.js (~135KB) - exposes window.ReactDOM
build/scripts/react-jsx-runtime/index.min.js (~1KB) - exposes window.ReactJSXRuntime
Test plan
Build completes successfully with npm run build
Vendor scripts are bundled and registered in build/scripts/index.php
Asset files have correct dependencies
Test in browser that React globals are exposed correctly