Make WordPress Core


Ignore:
Timestamp:
07/28/2021 10:05:01 AM (5 years ago)
Author:
gziolo
Message:

Build: Split packages and blocks to their webpack configs

It aligns with the changes proposed added in Gutenberg: https://github.com/WordPress/gutenberg/pull/33293.

The idea here is to split the growing webpack config into two parts: blocks and packages.

We need to add handling for JavaScript files that are going to be used with blocks on the frontend. They didn't work quite well with the current setup for entry points created for packages.

As part of the effort, it adds support for viewScript in block.json metadata file that is later translated to $view_script in WP_Block_Type class and exposed as view_script from the REST API endpoint for block types.

Props youknowriad, desrosj, aristath.
Fixes #53690.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r51477 r51501  
    4343                        $asset_handle .= '-editor';
    4444                }
     45                if ( 0 === strpos( $field_name, 'view' ) ) {
     46                        $asset_handle .= '-view';
     47                }
    4548                return $asset_handle;
    4649        }
     
    4952                'editorScript' => 'editor-script',
    5053                'script'       => 'script',
     54                'viewScript'   => 'view-script',
    5155                'editorStyle'  => 'editor-style',
    5256                'style'        => 'style',
     
    97101                return false;
    98102        }
    99         $script_asset = require $script_asset_path;
    100         $result       = wp_register_script(
     103        $is_core_block       = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], ABSPATH . WPINC );
     104        $script_uri          = $is_core_block ?
     105                includes_url( str_replace( ABSPATH . WPINC, '', realpath( dirname( $metadata['file'] ) . '/' . $script_path ) ) ) :
     106                plugins_url( $script_path, $metadata['file'] );
     107        $script_asset        = require $script_asset_path;
     108        $script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
     109        $result              = wp_register_script(
    101110                $script_handle,
    102                 plugins_url( $script_path, $metadata['file'] ),
    103                 $script_asset['dependencies'],
    104                 $script_asset['version']
     111                $script_uri,
     112                $script_dependencies,
     113                isset( $script_asset['version'] ) ? $script_asset['version'] : false
    105114        );
    106115        if ( ! $result ) {
     
    108117        }
    109118
    110         if ( ! empty( $metadata['textdomain'] ) ) {
     119        if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $script_dependencies ) ) {
    111120                wp_set_script_translations( $script_handle, $metadata['textdomain'] );
    112121        }
     
    183192 *
    184193 * @since 5.5.0
     194 * @since 5.9.0 Added support for the `viewScript` field.
    185195 *
    186196 * @param string $file_or_folder Path to the JSON file with metadata definition for
     
    305315        }
    306316
     317        if ( ! empty( $metadata['viewScript'] ) ) {
     318                $settings['view_script'] = register_block_script_handle(
     319                        $metadata,
     320                        'viewScript'
     321                );
     322        }
     323
    307324        if ( ! empty( $metadata['editorStyle'] ) ) {
    308325                $settings['editor_style'] = register_block_style_handle(
Note: See TracChangeset for help on using the changeset viewer.