-
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
File: Add experimental integration with Interactivity API #50377
Changes from all commits
a407918
720bbe1
e12f9f4
4798578
2d1c63b
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 |
---|---|---|
|
@@ -11,39 +11,46 @@ | |
* @param WP_Scripts $scripts WP_Scripts instance. | ||
*/ | ||
function gutenberg_register_interactivity_scripts( $scripts ) { | ||
gutenberg_override_script( | ||
$scripts, | ||
'wp-interactivity-runtime', | ||
gutenberg_url( | ||
'build/block-library/interactive-blocks/interactivity.min.js' | ||
), | ||
array( 'wp-interactivity-vendors' ) | ||
); | ||
// When in production, use the plugin's version as the default asset version; | ||
// else (for development or test) default to use the current time. | ||
$default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time(); | ||
|
||
gutenberg_override_script( | ||
$scripts, | ||
'wp-interactivity-vendors', | ||
gutenberg_url( | ||
'build/block-library/interactive-blocks/vendors.min.js' | ||
) | ||
); | ||
foreach ( array( 'vendors', 'runtime' ) as $script_name ) { | ||
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 wanted to explore how we could automate registering the runtime based on the asset files that we use for all other scripts built with webpack. In particular, the version generate is very important as it only changes when the file's content changes, so it's essential for proper caching in WordPress core. |
||
$script_path = "build/block-library/interactivity/$script_name.min.js"; | ||
// Replace extension with `.asset.php` to find the generated dependencies file. | ||
$asset_file = gutenberg_dir_path() . substr( $script_path, 0, -( strlen( '.js' ) ) ) . '.asset.php'; | ||
$asset = file_exists( $asset_file ) | ||
? require $asset_file | ||
: null; | ||
$dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array(); | ||
$version = isset( $asset['version'] ) ? $asset['version'] : $default_version; | ||
|
||
gutenberg_override_script( | ||
$scripts, | ||
"wp-interactivity-$script_name", | ||
gutenberg_url( $script_path ), | ||
$dependencies, | ||
$version | ||
); | ||
} | ||
} | ||
add_action( 'wp_default_scripts', 'gutenberg_register_interactivity_scripts', 10, 1 ); | ||
|
||
/** | ||
* Adds the "defer" attribute to all the interactivity script tags. | ||
* | ||
* @param string $tag The generated script tag. | ||
* @param string $handle The script handle. | ||
* | ||
* @return string The modified script tag. | ||
*/ | ||
function gutenberg_interactivity_scripts_add_defer_attribute( $tag ) { | ||
if ( str_contains( $tag, '/block-library/interactive-blocks/' ) ) { | ||
function gutenberg_interactivity_scripts_add_defer_attribute( $tag, $handle ) { | ||
if ( str_starts_with( $handle, 'wp-interactivity-' ) || str_contains( $tag, '/interactivity.min.js' ) ) { | ||
$p = new WP_HTML_Tag_Processor( $tag ); | ||
$p->next_tag( array( 'tag' => 'script' ) ); | ||
$p->set_attribute( 'defer', true ); | ||
return $p->get_updated_html(); | ||
} | ||
return $tag; | ||
} | ||
add_filter( 'script_loader_tag', 'gutenberg_interactivity_scripts_add_defer_attribute', 10, 1 ); | ||
add_filter( 'script_loader_tag', 'gutenberg_interactivity_scripts_add_defer_attribute', 10, 2 ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store } from '../utils/interactivity'; | ||
import { browserSupportsPdfs } from './utils'; | ||
|
||
store( { | ||
selectors: { | ||
core: { | ||
file: { | ||
hasNoPdfPreview() { | ||
return ! browserSupportsPdfs(); | ||
}, | ||
}, | ||
}, | ||
}, | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,16 +220,23 @@ module.exports = [ | |
].filter( Boolean ), | ||
}, | ||
{ | ||
...baseConfig, | ||
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. This brings some shared setting used for all other configs:
|
||
watchOptions: { | ||
aggregateTimeout: 200, | ||
}, | ||
name: 'interactivity', | ||
entry: { | ||
file: './packages/block-library/src/file/interactivity.js', | ||
navigation: | ||
'./packages/block-library/src/navigation/interactivity.js', | ||
}, | ||
output: { | ||
devtoolNamespace: 'wp', | ||
filename: './build/block-library/interactive-blocks/[name].min.js', | ||
path: join( __dirname, '..', '..' ), | ||
filename: './blocks/[name]/interactivity.min.js', | ||
path: join( __dirname, '..', '..', 'build', 'block-library' ), | ||
}, | ||
optimization: { | ||
...baseConfig.optimization, | ||
runtimeChunk: { | ||
name: 'vendors', | ||
}, | ||
|
@@ -238,12 +245,14 @@ module.exports = [ | |
vendors: { | ||
name: 'vendors', | ||
test: /[\\/]node_modules[\\/]/, | ||
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'd like to change this to the list of dependencies, even if we have to do it manually. Maybe in another PR :) 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. If I recall correctly, @DAreRodz tried listing dependencies, but you also need to list their dependencies and so on. It might be difficult to maintain, but it's worth trying. |
||
filename: './interactivity/[name].min.js', | ||
minSize: 0, | ||
chunks: 'all', | ||
}, | ||
interactivity: { | ||
name: 'interactivity', | ||
runtime: { | ||
name: 'runtime', | ||
test: /[\\/]utils\/interactivity[\\/]/, | ||
filename: './interactivity/[name].min.js', | ||
chunks: 'all', | ||
minSize: 0, | ||
priority: -10, | ||
|
@@ -279,5 +288,12 @@ module.exports = [ | |
}, | ||
], | ||
}, | ||
plugins: [ | ||
...plugins, | ||
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. Integrates webpack plugins used with all other configs:
|
||
new DependencyExtractionWebpackPlugin( { | ||
__experimentalInjectInteractivityRuntime: true, | ||
injectPolyfill: false, | ||
} ), | ||
].filter( Boolean ), | ||
}, | ||
]; |
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.
window.__experimentalEnableNavigationBlockInteractivity
is never used so can safely remove it.