Repository navigation
fix: align window.wp globals with WordPress script packages #785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab605b2
fix: stop exposing window.wp.sync
dcalhoun 8519ed5
fix: expose window.wp.undoManager
dcalhoun 454fe42
test: verify globals cover WordPress script packages
dcalhoun 05bdc04
test: skip non-package entries when listing script packages
dcalhoun 250f649
test: check globals match core's export shape
dcalhoun 78bf0d9
test: flag globals core no longer exposes
dcalhoun 5db1e88
test: simplify WordPress globals checks
dcalhoun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { existsSync, readdirSync, readFileSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { camelCaseDash } from '@wordpress/dependency-extraction-webpack-plugin/lib/util'; | ||
|
|
||
| // Packages flagged `wpScript` that WP Admin's post editor doesn't expose, | ||
| // keyed to the reason. | ||
| const EXCLUDED_PACKAGES = { | ||
| '@wordpress/base-styles': | ||
| 'Flagged only to build its stylesheet; the editor never loads its empty script.', | ||
| }; | ||
|
|
||
| // Modules that assign `window.wp` globals. Importing them would load every | ||
| // `@wordpress` package, which Node can't evaluate, so read their source. | ||
| const GLOBALS_MODULES = [ './wordpress-globals.js', './wordpress-i18n.js' ]; | ||
|
|
||
| const SCRIPT_PACKAGES = getInstalledScriptPackages(); | ||
|
|
||
| describe( 'WordPress globals', () => { | ||
| const assignments = getAssignments(); | ||
| const scriptNames = SCRIPT_PACKAGES.map( ( { name } ) => name ); | ||
| const scriptGlobals = SCRIPT_PACKAGES.map( ( { global } ) => global ); | ||
|
|
||
| // WordPress builds a `window.wp` global for each package flagged `wpScript`, | ||
| // so the flag approximates the globals plugins expect. A missing global | ||
| // breaks every plugin script that reads it. Core exposes the default export | ||
| // of packages flagged `wpScriptDefaultExport`, and the namespace of the rest. | ||
| it.each( | ||
| SCRIPT_PACKAGES.filter( ( { name } ) => ! EXCLUDED_PACKAGES[ name ] ) | ||
| )( | ||
| 'exposes $name in the shape core does', | ||
| ( { global, defaultExport } ) => { | ||
| expect( assignments ).toHaveProperty( global ); | ||
| expect( assignments[ global ] ).toMatch( | ||
| defaultExport | ||
| ? /^(\w+)\.default \|\| \1$/ | ||
| : /^toCommonJS\( \w+ \)$|^undefined$/ | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| // A global without a flagged package is one core no longer exposes, like | ||
| // `wp.sync` after WordPress/gutenberg#81999. | ||
| it.each( Object.keys( assignments ) )( | ||
| 'exposes window.wp.%s only while a package is flagged `wpScript`', | ||
| ( global ) => { | ||
| expect( scriptGlobals ).toContain( global ); | ||
| } | ||
| ); | ||
|
|
||
| it.each( Object.keys( EXCLUDED_PACKAGES ) )( | ||
| 'excludes %s only while it is flagged `wpScript`', | ||
| ( name ) => { | ||
| expect( scriptNames ).toContain( name ); | ||
| } | ||
| ); | ||
| } ); | ||
|
|
||
| /** | ||
| * List the installed `@wordpress` packages that WordPress builds as scripts. | ||
| * | ||
| * @return {Array<{name: string, global: string, defaultExport: boolean}>} | ||
| * Packages, with the `window.wp` member core assigns each. | ||
| */ | ||
| function getInstalledScriptPackages() { | ||
| const root = join( import.meta.dirname, '../../node_modules/@wordpress' ); | ||
|
|
||
| return readdirSync( root ).flatMap( ( directory ) => { | ||
| const path = join( root, directory, 'package.json' ); | ||
|
|
||
| // Skip entries that aren't packages, e.g. `.DS_Store` or npm's | ||
| // temporary directories from an interrupted install. | ||
| if ( ! existsSync( path ) ) { | ||
| return []; | ||
| } | ||
|
|
||
| const { name, wpScript, wpScriptDefaultExport } = JSON.parse( | ||
| readFileSync( path ) | ||
| ); | ||
|
|
||
| return wpScript | ||
| ? [ | ||
| { | ||
| name, | ||
| global: camelCaseDash( directory ), | ||
| defaultExport: !! wpScriptDefaultExport, | ||
| }, | ||
| ] | ||
| : []; | ||
| } ); | ||
| } | ||
|
|
||
| /** | ||
| * Map each `window.wp` member the globals modules assign to its value's source. | ||
| * | ||
| * @return {Object<string, string>} Source keyed by global, e.g. | ||
| * `{ blockEditor: 'toCommonJS( blockEditor )' }`. | ||
| */ | ||
| function getAssignments() { | ||
| return Object.fromEntries( | ||
| GLOBALS_MODULES.flatMap( ( path ) => { | ||
| const source = readFileSync( | ||
| join( import.meta.dirname, path ), | ||
| 'utf8' | ||
| ); | ||
| return [ | ||
| ...source.matchAll( /window\.wp\.(\w+)\s*=\s*([^;]+);/g ), | ||
| ].map( ( [ , global, value ] ) => [ | ||
| global, | ||
| value.replace( /\s+/g, ' ' ).trim(), | ||
| ] ); | ||
| } ) | ||
| ); | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean packages that are built with older version WordPress packages won't be able to get wp.sync? Would that cause any issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but the impact to GBK is very narrow and the breaking change was accepted in upstream Gutenberg core in WordPress/gutenberg#81999. It's expected that plugins bundle
@wordpress/syncthemselves or access the relevant utility via@wordpress/core-data'sYoption added to the provider creator.Details explained by Claude below: