Skip to content
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

Build: Split packages and blocks to their webpack configs #1412

Closed
wants to merge 11 commits into from
Closed
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ wp-tests-config.php
/src/wp-includes/css/*.min.css
/src/wp-includes/css/*-rtl.css
/src/wp-includes/blocks/**/*.css
/src/wp-includes/blocks/**/*.js
Copy link
Member Author

@gziolo gziolo Jun 25, 2021

Choose a reason for hiding this comment

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

The same changes need to be reflected in SVN for the file subfolder.

/src/wp-includes/blocks/**/*.js.map
/packagehash.txt
/artifacts

Expand Down
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,7 @@ module.exports = function(grunt) {
WORKING_DIR + 'wp-{admin,includes}/**/*.js',
WORKING_DIR + 'wp-content/themes/twenty*/**/*.js',
'!' + WORKING_DIR + 'wp-content/themes/twenty*/node_modules/**/*.js',
'!' + WORKING_DIR + 'wp-includes/blocks/**/*.js',
'!' + WORKING_DIR + 'wp-includes/js/dist/**/*.js',
]
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 23 additions & 6 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ function generate_block_asset_handle( $block_name, $field_name ) {
if ( 0 === strpos( $field_name, 'editor' ) ) {
$asset_handle .= '-editor';
}
if ( 0 === strpos( $field_name, 'view' ) ) {
$asset_handle .= '-view';
}
return $asset_handle;
}

$field_mappings = array(
'editorScript' => 'editor-script',
'script' => 'script',
'viewScript' => 'view-script',
'editorStyle' => 'editor-style',
'style' => 'style',
);
Expand Down Expand Up @@ -96,18 +100,23 @@ function register_block_script_handle( $metadata, $field_name ) {
);
return false;
}
$script_asset = require $script_asset_path;
$result = wp_register_script(
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], ABSPATH . WPINC );
$script_uri = $is_core_block ?
includes_url( str_replace( ABSPATH . WPINC, '', realpath( dirname( $metadata['file'] ) . '/' . $script_path ) ) ) :
plugins_url( $script_path, $metadata['file'] );
$script_asset = require $script_asset_path;
$script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
$result = wp_register_script(
$script_handle,
plugins_url( $script_path, $metadata['file'] ),
$script_asset['dependencies'],
$script_asset['version']
$script_uri,
$script_dependencies,
isset( $script_asset['version'] ) ? $script_asset['version'] : false
);
if ( ! $result ) {
return false;
}

if ( ! empty( $metadata['textdomain'] ) ) {
if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $script_dependencies ) ) {
wp_set_script_translations( $script_handle, $metadata['textdomain'] );
}

Expand Down Expand Up @@ -182,6 +191,7 @@ function register_block_style_handle( $metadata, $field_name ) {
* Registers a block type from the metadata stored in the `block.json` file.
*
* @since 5.5.0
* @since 5.9.0 Added support for the `viewScript` field.
*
* @param string $file_or_folder Path to the JSON file with metadata definition for
* the block or path to the folder where the `block.json` file is located.
Expand Down Expand Up @@ -304,6 +314,13 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
);
}

if ( ! empty( $metadata['viewScript'] ) ) {
$settings['view_script'] = register_block_script_handle(
$metadata,
'viewScript'
);
}

if ( ! empty( $metadata['editorStyle'] ) ) {
$settings['editor_style'] = register_block_style_handle(
$metadata,
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/blocks/file/view.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('wp-polyfill'), 'version' => '499eaf2efb98327a07f222e92d742380');
1 change: 1 addition & 0 deletions src/wp-includes/blocks/file/view.min.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('wp-polyfill'), 'version' => 'e8d668b8e69d9bf1c99dc250d92f2b72');
26 changes: 18 additions & 8 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,31 +156,39 @@ class WP_Block_Type {
public $provides_context = null;

/**
* Block type editor script handle.
* Block type editor only script handle.
*
* @since 5.0.0
* @var string|null
*/
public $editor_script = null;

/**
* Block type front end script handle.
* Block type front end and editor script handle.
*
* @since 5.0.0
* @var string|null
*/
public $script = null;

/**
* Block type editor style handle.
* Block type front end only script handle.
*
* @since 5.9.0
* @var string|null
*/
public $view_script = null;

/**
* Block type editor only style handle.
*
* @since 5.0.0
* @var string|null
*/
public $editor_style = null;

/**
* Block type front end style handle.
* Block type front end and editor style handle.
*
* @since 5.0.0
* @var string|null
Expand All @@ -198,6 +206,7 @@ class WP_Block_Type {
* `uses_context`, and `provides_context` properties.
* @since 5.6.0 Added the `api_version` property.
* @since 5.8.0 Added the `variations` property.
* @since 5.9.0 Added the `view_script` property.
*
* @see register_block_type()
*
Expand Down Expand Up @@ -225,10 +234,11 @@ class WP_Block_Type {
* @type array|null $attributes Block type attributes property schemas.
* @type array $uses_context Context values inherited by blocks of this type.
* @type array|null $provides_context Context provided by blocks of this type.
* @type string|null $editor_script Block type editor script handle.
* @type string|null $script Block type front end script handle.
* @type string|null $editor_style Block type editor style handle.
* @type string|null $style Block type front end style handle.
* @type string|null $editor_script Block type editor only script handle.
* @type string|null $script Block type front end and editor script handle.
* @type string|null $view_script Block type front end only script handle.
* @type string|null $editor_style Block type editor only style handle.
* @type string|null $style Block type front end and editor style handle.
* }
*/
public function __construct( $block_type, $args = array() ) {
Expand Down
4 changes: 4 additions & 0 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ public function render( $options = array() ) {
wp_enqueue_script( $this->block_type->script );
}

if ( ! empty( $this->block_type->view_script ) && empty( $this->block_type->render_callback ) ) {
wp_enqueue_script( $this->block_type->view_script );
}

if ( ! empty( $this->block_type->style ) ) {
wp_enqueue_style( $this->block_type->style );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public function prepare_item_for_response( $block_type, $request ) {
'example',
'editor_script',
'script',
'view_script',
'editor_style',
'style',
'variations',
Expand Down Expand Up @@ -517,6 +518,13 @@ public function get_item_schema() {
'readonly' => true,
),
'script' => array(
'description' => __( 'Public facing and editor script handle.' ),
'type' => array( 'string', 'null' ),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'view_script' => array(
'description' => __( 'Public facing script handle.' ),
'type' => array( 'string', 'null' ),
'default' => null,
Expand All @@ -531,7 +539,7 @@ public function get_item_schema() {
'readonly' => true,
),
'style' => array(
'description' => __( 'Public facing style handle.' ),
'description' => __( 'Public facing and editor style handle.' ),
'type' => array( 'string', 'null' ),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/data/blocks/notice/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
},
"editorScript": "tests-notice-editor-script",
"script": "tests-notice-script",
"viewScript": "tests-notice-view-script",
"editorStyle": "tests-notice-editor-style",
"style": "tests-notice-style"
}
9 changes: 9 additions & 0 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ function test_generate_block_asset_handle() {
'unit-tests-my-block-script',
generate_block_asset_handle( $block_name, 'script' )
);
$this->assertSame(
'unit-tests-my-block-view-script',
generate_block_asset_handle( $block_name, 'viewScript' )
);
$this->assertSame(
'unit-tests-my-block-editor-style',
generate_block_asset_handle( $block_name, 'editorStyle' )
Expand All @@ -156,6 +160,10 @@ function test_generate_block_asset_handle_core_block() {
'wp-block-paragraph',
generate_block_asset_handle( $block_name, 'script' )
);
$this->assertSame(
'wp-block-paragraph-view',
generate_block_asset_handle( $block_name, 'viewScript' )
);
$this->assertSame(
'wp-block-paragraph-editor',
generate_block_asset_handle( $block_name, 'editorStyle' )
Expand Down Expand Up @@ -372,6 +380,7 @@ function test_block_registers_with_metadata_fixture() {
);
$this->assertSame( 'tests-notice-editor-script', $result->editor_script );
$this->assertSame( 'tests-notice-script', $result->script );
$this->assertSame( 'tests-notice-view-script', $result->view_script );
$this->assertSame( 'tests-notice-editor-style', $result->editor_style );
$this->assertSame( 'tests-notice-style', $result->style );

Expand Down
8 changes: 7 additions & 1 deletion tests/phpunit/tests/rest-api/rest-block-type-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public function test_get_item_invalid() {
'category' => true,
'editor_script' => true,
'script' => true,
'view_script' => true,
'editor_style' => true,
'style' => true,
'keywords' => 'invalid_keywords',
Expand All @@ -237,6 +238,7 @@ public function test_get_item_invalid() {
$this->assertNull( $data['icon'] );
$this->assertNull( $data['editor_script'] );
$this->assertNull( $data['script'] );
$this->assertNull( $data['view_script'] );
$this->assertNull( $data['editor_style'] );
$this->assertNull( $data['style'] );
$this->assertSameSets( array(), $data['provides_context'] );
Expand Down Expand Up @@ -268,6 +270,7 @@ public function test_get_item_defaults() {
'category' => false,
'editor_script' => false,
'script' => false,
'view_script' => false,
'editor_style' => false,
'style' => false,
'keywords' => false,
Expand All @@ -290,6 +293,7 @@ public function test_get_item_defaults() {
$this->assertNull( $data['icon'] );
$this->assertNull( $data['editor_script'] );
$this->assertNull( $data['script'] );
$this->assertNull( $data['view_script'] );
$this->assertNull( $data['editor_style'] );
$this->assertNull( $data['style'] );
$this->assertSameSets( array(), $data['attributes'] );
Expand Down Expand Up @@ -374,7 +378,7 @@ public function test_get_item_schema() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 21, $properties );
$this->assertCount( 22, $properties );
$this->assertArrayHasKey( 'api_version', $properties );
$this->assertArrayHasKey( 'title', $properties );
$this->assertArrayHasKey( 'icon', $properties );
Expand All @@ -389,6 +393,7 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'is_dynamic', $properties );
$this->assertArrayHasKey( 'editor_script', $properties );
$this->assertArrayHasKey( 'script', $properties );
$this->assertArrayHasKey( 'view_script', $properties );
$this->assertArrayHasKey( 'editor_style', $properties );
$this->assertArrayHasKey( 'style', $properties );
$this->assertArrayHasKey( 'parent', $properties );
Expand Down Expand Up @@ -500,6 +505,7 @@ protected function check_block_type_object( $block_type, $data, $links ) {
'category',
'editor_script',
'script',
'view_script',
'editor_style',
'style',
'title',
Expand Down
Loading