fix: Exclude WordPress globals files from externals plugin#325
Draft
fix: Exclude WordPress globals files from externals plugin#325
Conversation
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.
ba6278b to
e66e780
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Explicitly exclude
wordpress-globals.jsandwordpress-i18n.jsfrom thewordPressExternals()Vite plugin transform.Why?
The
wordPressExternals()plugin transforms@wordpress/*imports intowindow.wp.*global reads. Thewordpress-globals.jsandwordpress-i18n.jsfiles are responsible for populatingwindow.wp— they need to import the actual modules fromnode_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.wpwould never be populated, causing any code that depends on it to fail.How?
Added an early return in the
wordPressExternals()transformfunction that skips files whose path includeswordpress-globalsorwordpress-i18n, alongside the existingnode_modulesexclusion.Testing Instructions
npm run build— verify the build succeeds anddist/assets/wordpress-globals-*.jsis still ~5.8 MB (confirming the WordPress modules are bundled, not externalized).npm run previewand openhttp://localhost:4173/?dev_mode=1— verifywindow.wpis defined in the browser console.