-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Read script dependencies from generated files #15124
Changes from all commits
906250e
993c8a8
f540605
33991d6
2c3b654
27bccbe
df21c64
6de12cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,9 +114,14 @@ function gutenberg_override_translation_file( $file, $handle ) { | |
return $file; | ||
} | ||
|
||
// Only override script handles generated from the Gutenberg plugin. | ||
$packages_dependencies = include dirname( __FILE__ ) . '/packages-dependencies.php'; | ||
if ( ! isset( $packages_dependencies[ $handle ] ) ) { | ||
// Ignore scripts whose handle does not have the "wp-" prefix. | ||
if ( 'wp-' !== substr( $handle, 0, 3 ) ) { | ||
return $file; | ||
} | ||
|
||
// Ignore scripts that are not found in the expected `build/` location. | ||
$script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.js'; | ||
if ( ! file_exists( $script_path ) ) { | ||
return $file; | ||
} | ||
|
||
|
@@ -177,17 +182,37 @@ function gutenberg_override_style( $handle, $src, $deps = array(), $ver = false, | |
* @since 4.5.0 | ||
*/ | ||
function gutenberg_register_packages_scripts() { | ||
$packages_dependencies = include dirname( __FILE__ ) . '/packages-dependencies.php'; | ||
foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Depending if and how similar code would make its way to core, I'm a bit sensitive to the performance implications of a
Seems like there could be optimizations:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍A nice enhancement for a future version or simply another part of the build would be to produce what is needed in PHP without needing to hit the filesystem or parse JSON. This came up during the core-js slack chat. CC: @gziolo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related conversation on WordPress Slack (link requires registration): https://wordpress.slack.com/archives/C5UNMSU4R/p1556633084220800 We didn't discuss file discovery which would be still an issue. |
||
// Prefix `wp-` to package directory to get script handle. | ||
// For example, `…/build/a11y/index.js` becomes `wp-a11y`. | ||
$handle = 'wp-' . basename( dirname( $path ) ); | ||
|
||
// Replace `.js` extension with `.deps.json` to find the generated dependencies file. | ||
$dependencies_file = substr( $path, 0, -3 ) . '.deps.json'; | ||
|
||
$dependencies = is_readable( $dependencies_file ) | ||
? json_decode( file_get_contents( $dependencies_file ) ) | ||
: array(); | ||
|
||
// Add dependencies that cannot be detected and generated by build tools. | ||
switch ( $handle ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works for me 🤷♂️ 😅 |
||
case 'wp-block-library': | ||
array_push( $dependencies, 'editor' ); | ||
break; | ||
|
||
case 'wp-edit-post': | ||
array_push( $dependencies, 'media-models', 'media-views', 'postbox' ); | ||
break; | ||
} | ||
|
||
// Get the path from Gutenberg directory as expected by `gutenberg_url`. | ||
$gutenberg_path = substr( $path, strlen( gutenberg_dir_path() ) ); | ||
|
||
foreach ( $packages_dependencies as $handle => $dependencies ) { | ||
// Remove `wp-` prefix from the handle to get the package's name. | ||
$package_name = strpos( $handle, 'wp-' ) === 0 ? substr( $handle, 3 ) : $handle; | ||
$path = "build/$package_name/index.js"; | ||
gutenberg_override_script( | ||
$handle, | ||
gutenberg_url( $path ), | ||
array_merge( $dependencies, array( 'wp-polyfill' ) ), | ||
filemtime( gutenberg_dir_path() . $path ), | ||
gutenberg_url( $gutenberg_path ), | ||
$dependencies, | ||
filemtime( $path ), | ||
true | ||
); | ||
} | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,7 @@ const config = { | |
// WP_LIVE_RELOAD_PORT global variable changes port on which live reload works | ||
// when running watch mode. | ||
! isProduction && new LiveReloadPlugin( { port: process.env.WP_LIVE_RELOAD_PORT || 35729 } ), | ||
new DependencyExtractionWebpackPlugin(), | ||
new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Includes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Looking at the implementation, would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would yes. That said, I don't see polyfill being built as part of Gutenberg (no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we will ever scan |
||
].filter( Boolean ), | ||
stats: { | ||
children: false, | ||
|
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.
I'm unsure how to test whether this is working well, I'd appreciate some help.
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.
That's a great question. Travis seems to be happy about changes applied 🤷♂️
How about we compare all script tags in the HTML output between
master
and this branch?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.
This is rather tricky to verify, but it seems to be working. Here's what I did:
wp.i18n.__( 'Use the classic WordPress editor.' )
/wp-content/languages/plugins/gutenberg-es_ES-d83116db7e3459d9c9a90cae3216278f.json
Utiliza el editor clásico de WordPress.
) for something else (👋 Hi from the plugin language file.
)wp.i18n.__( 'Use the classic WordPress editor.' )