Skip to content

fix: Exclude WordPress globals files from externals plugin#325

Draft
dcalhoun wants to merge 1 commit intotrunkfrom
fix/exclude-globals-from-externals
Draft

fix: Exclude WordPress globals files from externals plugin#325
dcalhoun wants to merge 1 commit intotrunkfrom
fix/exclude-globals-from-externals

Conversation

@dcalhoun
Copy link
Member

What?

Explicitly exclude wordpress-globals.js and wordpress-i18n.js from the wordPressExternals() Vite plugin transform.

Why?

The wordPressExternals() plugin transforms @wordpress/* imports into window.wp.* global reads. The wordpress-globals.js and wordpress-i18n.js files are responsible for populating window.wp — they need to import the actual modules from node_modules, not read from the globals they haven't set up yet.

Currently these files are only spared by accident: the plugin's regex matches default imports (import foo from '...') and named imports (import { foo } from '...'), but not the namespace imports (import * as foo from '...') that these files use. If someone refactored those imports to named or default style, the production build would silently break — window.wp would never be populated, causing any code that depends on it to fail.

How?

Added an early return in the wordPressExternals() transform function that skips files whose path includes wordpress-globals or wordpress-i18n, alongside the existing node_modules exclusion.

Testing Instructions

  1. Run npm run build — verify the build succeeds and dist/assets/wordpress-globals-*.js is still ~5.8 MB (confirming the WordPress modules are bundled, not externalized).
  2. Run npm run preview and open http://localhost:4173/?dev_mode=1 — verify window.wp is defined in the browser console.

@dcalhoun dcalhoun added the [Type] Build Tooling Issues or PRs related to build tooling label Feb 17, 2026
The `wordPressExternals()` Vite plugin transforms `@wordpress/*`
imports into `window.wp.*` global reads. The `wordpress-globals.js`
and `wordpress-i18n.js` files must be excluded from this transform
because they are responsible for *populating* `window.wp` — they
need to import the actual modules from node_modules.

Previously these files were only spared by accident: the plugin's
regex didn't match the `import * as ...` (namespace import) syntax
they use. If someone refactored those imports to named or default
style, the build would silently break. This makes the exclusion
explicit.
@dcalhoun dcalhoun force-pushed the fix/exclude-globals-from-externals branch from ba6278b to e66e780 Compare February 18, 2026 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Build Tooling Issues or PRs related to build tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments