Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions packages/toolkit/config/fast-refresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,85 @@
);
}

if ( ! function_exists( __NAMESPACE__ . '\\get_block_asset_url' ) ) {
function get_block_asset_url( $path ) {
static $template_paths_norm = array();

$template = get_template();
if ( ! isset( $template_paths_norm[ $template ] ) ) {
$template_paths_norm[ $template ] = wp_normalize_path( get_template_directory() );
}

if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $template ] ) ) ) {
return get_theme_file_uri( str_replace( $template_paths_norm[ $template ], '', $path ) );
}
}

add_filter(
'block_type_metadata_settings',
static function( $settings, $metadata ) {
if ( isset( $metadata['editorStyle'] ) && str_starts_with( $metadata['editorStyle'], 'file:' ) ) {
$file = $metadata['file'];
$dir = dirname( $file );

if ( isset( $metadata['editorStyle'] ) ) {
$editor_style_path = $metadata['editorStyle'];
$editor_style_file_name = basename( $editor_style_path );

$js_editor_hmr_file = $dir . '/' . str_replace( '.css', '.js', $editor_style_file_name );

// TODO: do not enqueue this file if its name ends up being the same as editorScript
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure there might be some other edge cases, we need a way to safely load these things automatically, perhaps we add a flag per block?

if ( file_exists( $js_editor_hmr_file ) ) {
$editor_script_index = count( $settings['editor_script_handles'] ) ?? 0;
$editor_script_handle = generate_block_asset_handle( $metadata['name'], 'editorStyle', $editor_script_index );

$script_asset = include str_replace( '.js', '.asset.php', $js_editor_hmr_file );
$script_url = get_block_asset_url( $js_editor_hmr_file );

wp_register_script(
$editor_script_handle,
$script_url,
$script_asset['dependencies'],
$script_asset['version'],
true
);

$settings['editor_script_handles'][] = $editor_script_handle;
}
}

if ( isset( $metadata['style'] ) ) {
$style_path = $metadata['style'];
$style_file_name = basename( $style_path );
$js_style_hmr_file = $dir . '/' . str_replace( '.css', '.js', $style_file_name );

if ( file_exists( $js_style_hmr_file ) ) {
$editor_script_index = count( $settings['editor_script_handles'] ) ?? 0;
$script_handle = generate_block_asset_handle( $metadata['name'], 'style', $editor_script_index );

$script_asset = include str_replace( '.js', '.asset.php', $js_style_hmr_file );
$script_url = get_block_asset_url( $js_style_hmr_file );

wp_register_script(
$script_handle,
$script_url,
$script_asset['dependencies'],
$script_asset['version'],
true
);

$settings['editor_script_handles'][] = $script_handle;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabiankaegy What would be the best way to register the style handler for the frontend AND gutenberg?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as editorScript there also is a script property in block.json that loads in both places

}
}
}

return $settings;
},
10,
2
);
}

if ( ! function_exists( __NAMESPACE__ . '\\set_dist_url_path' ) ) {
$registry = [];

Expand Down
3 changes: 0 additions & 3 deletions projects/10up-theme/includes/blocks/example/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import { RichText, useBlockProps } from '@wordpress/block-editor';
// so the intent here is to test toolkit `--include` feature to manually tell toolkit to transpile this package
import { ContentPicker } from '@10up/block-components';

// Importing the block's editor styles via JS will enable hot reloading for css
import './editor.css';

const ExampleBlockEdit = (props) => {
const { attributes, setAttributes } = props;
const { title } = attributes;
Expand Down