Skip to content

Commit 51cbe7e

Browse files
committed
Build: Split packages and blocks to their webpack configs.
This also adds support for the viewScript for blocks fixing the PDF preview for file blocks. Props desrosj, gziolo. See #53397. git-svn-id: https://develop.svn.wordpress.org/trunk@51259 602fd350-edb4-49c9-b593-d223f7449a82
1 parent da88164 commit 51cbe7e

9 files changed

Lines changed: 267 additions & 124 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ wp-tests-config.php
2929
/src/wp-includes/css/*.min.css
3030
/src/wp-includes/css/*-rtl.css
3131
/src/wp-includes/blocks/**/*.css
32+
/src/wp-includes/blocks/**/*.js
33+
/src/wp-includes/blocks/**/*.js.map
34+
/src/wp-includes/blocks/**/*.asset.php
3235
/packagehash.txt
3336
/artifacts
3437

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ module.exports = function(grunt) {
996996
WORKING_DIR + 'wp-{admin,includes}/**/*.js',
997997
WORKING_DIR + 'wp-content/themes/twenty*/**/*.js',
998998
'!' + WORKING_DIR + 'wp-content/themes/twenty*/node_modules/**/*.js',
999+
'!' + WORKING_DIR + 'wp-includes/blocks/**/*.js',
9991000
'!' + WORKING_DIR + 'wp-includes/js/dist/**/*.js',
10001001
]
10011002
}

src/wp-includes/blocks.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ function generate_block_asset_handle( $block_name, $field_name ) {
4242
if ( 0 === strpos( $field_name, 'editor' ) ) {
4343
$asset_handle .= '-editor';
4444
}
45+
if ( 0 === strpos( $field_name, 'view' ) ) {
46+
$asset_handle .= '-view';
47+
}
4548
return $asset_handle;
4649
}
4750

4851
$field_mappings = array(
4952
'editorScript' => 'editor-script',
5053
'script' => 'script',
54+
'viewScript' => 'view-script',
5155
'editorStyle' => 'editor-style',
5256
'style' => 'style',
5357
);
@@ -96,18 +100,23 @@ function register_block_script_handle( $metadata, $field_name ) {
96100
);
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 ) {
107116
return false;
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
}
113122

@@ -306,6 +315,13 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
306315
);
307316
}
308317

318+
if ( ! empty( $metadata['viewScript'] ) ) {
319+
$settings['view_script'] = register_block_script_handle(
320+
$metadata,
321+
'viewScript'
322+
);
323+
}
324+
309325
if ( ! empty( $metadata['editorStyle'] ) ) {
310326
$settings['editor_style'] = register_block_style_handle(
311327
$metadata,

src/wp-includes/class-wp-block-type.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,31 +156,39 @@ class WP_Block_Type {
156156
public $provides_context = null;
157157

158158
/**
159-
* Block type editor script handle.
159+
* Block type editor only script handle.
160160
*
161161
* @since 5.0.0
162162
* @var string|null
163163
*/
164164
public $editor_script = null;
165165

166166
/**
167-
* Block type front end script handle.
167+
* Block type front end and editor script handle.
168168
*
169169
* @since 5.0.0
170170
* @var string|null
171171
*/
172172
public $script = null;
173173

174174
/**
175-
* Block type editor style handle.
175+
* Block type front end only script handle.
176+
*
177+
* @since 5.0.0
178+
* @var string|null
179+
*/
180+
public $view_script = null;
181+
182+
/**
183+
* Block type editor only style handle.
176184
*
177185
* @since 5.0.0
178186
* @var string|null
179187
*/
180188
public $editor_style = null;
181189

182190
/**
183-
* Block type front end style handle.
191+
* Block type front end and editor style handle.
184192
*
185193
* @since 5.0.0
186194
* @var string|null
@@ -225,10 +233,11 @@ class WP_Block_Type {
225233
* @type array|null $attributes Block type attributes property schemas.
226234
* @type array $uses_context Context values inherited by blocks of this type.
227235
* @type array|null $provides_context Context provided by blocks of this type.
228-
* @type string|null $editor_script Block type editor script handle.
229-
* @type string|null $script Block type front end script handle.
230-
* @type string|null $editor_style Block type editor style handle.
231-
* @type string|null $style Block type front end style handle.
236+
* @type string|null $editor_script Block type editor only script handle.
237+
* @type string|null $script Block type front end and editor script handle.
238+
* @type string|null $view_script Block type front end only script handle.
239+
* @type string|null $editor_style Block type editor only style handle.
240+
* @type string|null $style Block type front end and editor style handle.
232241
* }
233242
*/
234243
public function __construct( $block_type, $args = array() ) {

tests/phpunit/data/blocks/notice/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"editorScript": "tests-notice-editor-script",
5050
"script": "tests-notice-script",
51+
"viewScript": "tests-notice-view-script",
5152
"editorStyle": "tests-notice-editor-style",
5253
"style": "tests-notice-style"
5354
}

tests/phpunit/tests/blocks/register.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ function test_generate_block_asset_handle() {
132132
'unit-tests-my-block-script',
133133
generate_block_asset_handle( $block_name, 'script' )
134134
);
135+
$this->assertSame(
136+
'unit-tests-my-block-view-script',
137+
generate_block_asset_handle( $block_name, 'viewScript' )
138+
);
135139
$this->assertSame(
136140
'unit-tests-my-block-editor-style',
137141
generate_block_asset_handle( $block_name, 'editorStyle' )
@@ -156,6 +160,10 @@ function test_generate_block_asset_handle_core_block() {
156160
'wp-block-paragraph',
157161
generate_block_asset_handle( $block_name, 'script' )
158162
);
163+
$this->assertSame(
164+
'wp-block-paragraph-view',
165+
generate_block_asset_handle( $block_name, 'viewScript' )
166+
);
159167
$this->assertSame(
160168
'wp-block-paragraph-editor',
161169
generate_block_asset_handle( $block_name, 'editorStyle' )
@@ -372,6 +380,7 @@ function test_block_registers_with_metadata_fixture() {
372380
);
373381
$this->assertSame( 'tests-notice-editor-script', $result->editor_script );
374382
$this->assertSame( 'tests-notice-script', $result->script );
383+
$this->assertSame( 'tests-notice-view-script', $result->view_script );
375384
$this->assertSame( 'tests-notice-editor-style', $result->editor_style );
376385
$this->assertSame( 'tests-notice-style', $result->style );
377386

tools/webpack/blocks.js

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/**
2+
* External dependencies
3+
*/
4+
const { DefinePlugin } = require( 'webpack' );
5+
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
6+
const postcss = require( 'postcss' );
7+
const UglifyJS = require( 'uglify-js' );
8+
9+
const { join, basename } = require( 'path' );
10+
const { get } = require( 'lodash' );
11+
12+
/**
13+
* WordPress dependencies
14+
*/
15+
const DependencyExtractionPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
16+
17+
const baseDir = join( __dirname, '../../' );
18+
19+
module.exports = function( env = { environment: 'production', watch: false, buildTarget: false } ) {
20+
const mode = env.environment;
21+
const suffix = mode === 'production' ? '.min' : '';
22+
let buildTarget = env.buildTarget ? env.buildTarget : ( mode === 'production' ? 'build' : 'src' );
23+
buildTarget = buildTarget + '/wp-includes';
24+
25+
const dynamicBlockFolders = [
26+
'archives',
27+
'block',
28+
'calendar',
29+
'categories',
30+
'file',
31+
'latest-comments',
32+
'latest-posts',
33+
'loginout',
34+
'page-list',
35+
'post-content',
36+
'post-date',
37+
'post-excerpt',
38+
'post-featured-image',
39+
'post-terms',
40+
'post-title',
41+
'post-template',
42+
'query',
43+
'query-pagination',
44+
'query-pagination-next',
45+
'query-pagination-numbers',
46+
'query-pagination-previous',
47+
'query-title',
48+
'rss',
49+
'search',
50+
'shortcode',
51+
'site-logo',
52+
'site-tagline',
53+
'site-title',
54+
'social-link',
55+
'tag-cloud',
56+
];
57+
const blockFolders = [
58+
'audio',
59+
'button',
60+
'buttons',
61+
'code',
62+
'column',
63+
'columns',
64+
'cover',
65+
'embed',
66+
'freeform',
67+
'gallery',
68+
'group',
69+
'heading',
70+
'html',
71+
'image',
72+
'list',
73+
'media-text',
74+
'missing',
75+
'more',
76+
'nextpage',
77+
'paragraph',
78+
'preformatted',
79+
'pullquote',
80+
'quote',
81+
'separator',
82+
'social-links',
83+
'spacer',
84+
'table',
85+
'text-columns',
86+
'verse',
87+
'video',
88+
...dynamicBlockFolders,
89+
];
90+
const blockPHPFiles = {
91+
'widgets/src/blocks/legacy-widget/index.php': 'wp-includes/blocks/legacy-widget.php',
92+
...dynamicBlockFolders.reduce( ( files, blockName ) => {
93+
files[ `block-library/src/${ blockName }/index.php` ] = `wp-includes/blocks/${ blockName }.php`;
94+
return files;
95+
} , {} ),
96+
};
97+
const blockMetadataFiles = {
98+
'widgets/src/blocks/legacy-widget/block.json': 'wp-includes/blocks/legacy-widget/block.json',
99+
...blockFolders.reduce( ( files, blockName ) => {
100+
files[ `block-library/src/${ blockName }/block.json` ] = `wp-includes/blocks/${ blockName }/block.json`;
101+
return files;
102+
} , {} ),
103+
};
104+
105+
const blockPHPCopies = Object.keys( blockPHPFiles ).map( ( filename ) => ( {
106+
from: join( baseDir, `node_modules/@wordpress/${ filename }` ),
107+
to: join( baseDir, `src/${ blockPHPFiles[ filename ] }` ),
108+
} ) );
109+
110+
const blockMetadataCopies = Object.keys( blockMetadataFiles ).map( ( filename ) => ( {
111+
from: join( baseDir, `node_modules/@wordpress/${ filename }` ),
112+
to: join( baseDir, `src/${ blockMetadataFiles[ filename ] }` ),
113+
} ) );
114+
115+
const blockStylesheetCopies = blockFolders.map( ( blockName ) => ( {
116+
from: join( baseDir, `node_modules/@wordpress/block-library/build-style/${ blockName }/*.css` ),
117+
to: join( baseDir, `${ buildTarget }/blocks/${ blockName }/` ),
118+
flatten: true,
119+
transform: ( content ) => {
120+
if ( mode === 'production' ) {
121+
return postcss( [
122+
require( 'cssnano' )( {
123+
preset: 'default',
124+
} ),
125+
] )
126+
.process( content, { from: 'src/app.css', to: 'dest/app.css' } )
127+
.then( ( result ) => result.css );
128+
}
129+
130+
return content;
131+
},
132+
transformPath: ( targetPath, sourcePath ) => {
133+
if ( mode === 'production' ) {
134+
return targetPath.replace( /\.css$/, '.min.css' );
135+
}
136+
137+
return targetPath;
138+
}
139+
} ) );
140+
141+
const config = {
142+
mode,
143+
entry: {
144+
'file/view': join( baseDir, `node_modules/@wordpress/block-library/build-module/file/view` ),
145+
},
146+
output: {
147+
devtoolNamespace: 'wp',
148+
filename: `[name]${ suffix }.js`,
149+
path: join( baseDir, `${ buildTarget }/blocks` ),
150+
},
151+
resolve: {
152+
modules: [
153+
baseDir,
154+
'node_modules',
155+
],
156+
alias: {
157+
'lodash-es': 'lodash',
158+
},
159+
},
160+
module: {
161+
rules: [
162+
{
163+
test: /\.js$/,
164+
use: [ 'source-map-loader' ],
165+
enforce: 'pre',
166+
},
167+
],
168+
},
169+
optimization: {
170+
moduleIds: mode === 'production' ? 'hashed' : 'named',
171+
},
172+
plugins: [
173+
new DefinePlugin( {
174+
// Inject the `GUTENBERG_PHASE` global, used for feature flagging.
175+
'process.env.GUTENBERG_PHASE': 1,
176+
'process.env.FORCE_REDUCED_MOTION': JSON.stringify(
177+
process.env.FORCE_REDUCED_MOTION
178+
),
179+
} ),
180+
new DependencyExtractionPlugin( {
181+
injectPolyfill: true,
182+
} ),
183+
new CopyWebpackPlugin(
184+
[
185+
...blockPHPCopies,
186+
...blockMetadataCopies,
187+
...blockStylesheetCopies,
188+
],
189+
),
190+
],
191+
stats: {
192+
children: false,
193+
},
194+
195+
watch: env.watch,
196+
};
197+
198+
if ( config.mode !== 'production' ) {
199+
config.devtool = process.env.SOURCEMAP || 'source-map';
200+
}
201+
202+
if ( mode === 'development' && env.buildTarget === 'build/' ) {
203+
delete config.devtool;
204+
config.mode = 'production';
205+
config.optimization = {
206+
minimize: false,
207+
moduleIds: 'hashed',
208+
};
209+
}
210+
211+
return config;
212+
};

0 commit comments

Comments
 (0)