From 906250e8deb260b113c61dda8f0b5be032f47a70 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 23 Apr 2019 16:00:32 +0200 Subject: [PATCH 1/8] Rework script registration to use generated deps --- lib/client-assets.php | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index d9fcc8105249b..8b4087404869f 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -177,17 +177,26 @@ function gutenberg_override_style( $handle, $src, $deps = array(), $ver = false, * @since 4.5.0 */ function gutenberg_register_packages_scripts() { - $packages_dependencies = include dirname( __FILE__ ) . '/packages-dependencies.php'; + foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) { + // Prefix `wp-` to package directory to get script handle. + // For example, `…/build/a11y/index.js` becomes `wp-a11y`. + $handle = 'wp-' . basename( dirname( $path ) ); + + // Replace `.js` extension with `.deps.json` to find the generated dependencies file. + $dependencies_file = substr( $path, 0, -3 ) . '.deps.json'; + + $dependencies = is_readable( $dependencies_file ) + ? json_decode( file_get_contents( $dependencies_file ) ) + : array(); + + // Get the path from Gutenberg directory as expected by `gutenberg_url`. + $gutenberg_path = substr( $path, strlen( gutenberg_dir_path() ) ); - foreach ( $packages_dependencies as $handle => $dependencies ) { - // Remove `wp-` prefix from the handle to get the package's name. - $package_name = strpos( $handle, 'wp-' ) === 0 ? substr( $handle, 3 ) : $handle; - $path = "build/$package_name/index.js"; gutenberg_override_script( $handle, - gutenberg_url( $path ), + gutenberg_url( $gutenberg_path ), array_merge( $dependencies, array( 'wp-polyfill' ) ), - filemtime( gutenberg_dir_path() . $path ), + filemtime( $path ), true ); } From 993c8a875ac173c545e11c77bb68dccc9bbc013a Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Apr 2019 09:23:14 +0200 Subject: [PATCH 2/8] Rework translation without packages-dependencies.php --- lib/client-assets.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 8b4087404869f..96f59424c3355 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -114,9 +114,14 @@ function gutenberg_override_translation_file( $file, $handle ) { return $file; } - // Only override script handles generated from the Gutenberg plugin. - $packages_dependencies = include dirname( __FILE__ ) . '/packages-dependencies.php'; - if ( ! isset( $packages_dependencies[ $handle ] ) ) { + // Ignore scripts whose handle does not have the "wp-" prefix. + if ( 'wp-' !== substr( $handle, 0, 3 ) ) { + return $file; + } + + // Ignore scripts that are not found in the expected `build/` location. + $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.js'; + if ( ! file_exists( $script_path ) ) { return $file; } From f540605f6e65ac2dd605f74ecb97e475c91e39c4 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 23 Apr 2019 16:01:36 +0200 Subject: [PATCH 3/8] Deprecate static list of dependencies --- lib/packages-dependencies.php | 262 +--------------------------------- 1 file changed, 1 insertion(+), 261 deletions(-) diff --git a/lib/packages-dependencies.php b/lib/packages-dependencies.php index 8a1f78fafe456..67d05cc3b6244 100644 --- a/lib/packages-dependencies.php +++ b/lib/packages-dependencies.php @@ -1,265 +1,5 @@ array( - 'wp-dom-ready', - ), - 'wp-annotations' => array( - 'wp-data', - 'wp-hooks', - 'wp-i18n', - 'wp-rich-text', - ), - 'wp-api-fetch' => array( - 'wp-i18n', - 'wp-url', - ), - 'wp-autop' => array(), - 'wp-blob' => array(), - 'wp-block-library' => array( - 'editor', - 'lodash', - 'wp-api-fetch', - 'wp-autop', - 'wp-blob', - 'wp-block-editor', - 'wp-blocks', - 'wp-components', - 'wp-compose', - 'wp-core-data', - 'wp-data', - 'wp-date', - 'wp-editor', - 'wp-element', - 'wp-html-entities', - 'wp-i18n', - 'wp-keycodes', - 'wp-rich-text', - 'wp-url', - 'wp-viewport', - ), - 'wp-block-serialization-default-parser' => array(), - 'wp-block-serialization-spec-parser' => array(), - 'wp-block-editor' => array( - 'lodash', - 'wp-a11y', - 'wp-blob', - 'wp-blocks', - 'wp-compose', - 'wp-components', - 'wp-data', - 'wp-dom', - 'wp-element', - 'wp-hooks', - 'wp-html-entities', - 'wp-i18n', - 'wp-is-shallow-equal', - 'wp-keycodes', - 'wp-rich-text', - 'wp-token-list', - 'wp-url', - 'wp-viewport', - 'wp-wordcount', - ), - 'wp-blocks' => array( - 'lodash', - 'wp-autop', - 'wp-blob', - 'wp-block-serialization-default-parser', - 'wp-data', - 'wp-dom', - 'wp-element', - 'wp-hooks', - 'wp-html-entities', - 'wp-i18n', - 'wp-is-shallow-equal', - 'wp-shortcode', - ), - 'wp-components' => array( - 'lodash', - 'moment', - 'wp-a11y', - 'wp-api-fetch', - 'wp-compose', - 'wp-dom', - 'wp-element', - 'wp-hooks', - 'wp-html-entities', - 'wp-i18n', - 'wp-is-shallow-equal', - 'wp-keycodes', - 'wp-rich-text', - 'wp-url', - ), - 'wp-compose' => array( - 'lodash', - 'wp-element', - 'wp-is-shallow-equal', - ), - 'wp-core-data' => array( - 'lodash', - 'wp-api-fetch', - 'wp-data', - 'wp-deprecated', - 'wp-url', - ), - 'wp-data' => array( - 'lodash', - 'wp-compose', - 'wp-deprecated', - 'wp-element', - 'wp-is-shallow-equal', - 'wp-priority-queue', - 'wp-redux-routine', - ), - 'wp-date' => array( - 'moment', - ), - 'wp-deprecated' => array( - 'wp-hooks', - ), - 'wp-dom' => array( - 'lodash', - ), - 'wp-dom-ready' => array(), - 'wp-edit-post' => array( - 'jquery', - 'lodash', - 'postbox', - 'media-models', - 'media-views', - 'wp-a11y', - 'wp-api-fetch', - 'wp-block-editor', - 'wp-block-library', - 'wp-blocks', - 'wp-components', - 'wp-compose', - 'wp-core-data', - 'wp-data', - 'wp-dom-ready', - 'wp-editor', - 'wp-element', - 'wp-embed', - 'wp-i18n', - 'wp-keycodes', - 'wp-notices', - 'wp-nux', - 'wp-plugins', - 'wp-url', - 'wp-viewport', - ), - 'wp-edit-widgets' => array( - 'wp-block-editor', - 'wp-block-library', - 'wp-components', - 'wp-element', - 'wp-i18n', - ), - 'wp-editor' => array( - 'lodash', - 'wp-a11y', - 'wp-api-fetch', - 'wp-blob', - 'wp-block-editor', - 'wp-blocks', - 'wp-components', - 'wp-compose', - 'wp-core-data', - 'wp-data', - 'wp-date', - 'wp-deprecated', - 'wp-dom', - 'wp-element', - 'wp-hooks', - 'wp-html-entities', - 'wp-i18n', - 'wp-is-shallow-equal', - 'wp-keycodes', - 'wp-notices', - 'wp-nux', - 'wp-rich-text', - 'wp-token-list', - 'wp-url', - 'wp-viewport', - 'wp-wordcount', - ), - 'wp-element' => array( - 'lodash', - 'react', - 'react-dom', - 'wp-escape-html', - ), - 'wp-escape-html' => array(), - 'wp-format-library' => array( - 'wp-block-editor', - 'wp-components', - 'wp-editor', - 'wp-element', - 'wp-i18n', - 'wp-keycodes', - 'wp-rich-text', - 'wp-url', - ), - 'wp-hooks' => array(), - 'wp-html-entities' => array(), - 'wp-i18n' => array(), - 'wp-is-shallow-equal' => array(), - 'wp-keycodes' => array( - 'lodash', - 'wp-i18n', - ), - 'wp-list-reusable-blocks' => array( - 'lodash', - 'wp-api-fetch', - 'wp-components', - 'wp-compose', - 'wp-element', - 'wp-i18n', - ), - 'wp-notices' => array( - 'lodash', - 'wp-a11y', - 'wp-data', - ), - 'wp-nux' => array( - 'lodash', - 'wp-components', - 'wp-compose', - 'wp-data', - 'wp-element', - 'wp-i18n', - ), - 'wp-plugins' => array( - 'lodash', - 'wp-compose', - 'wp-element', - 'wp-hooks', - ), - 'wp-priority-queue' => array(), - 'wp-redux-routine' => array(), - 'wp-rich-text' => array( - 'lodash', - 'wp-data', - 'wp-escape-html', - ), - 'wp-shortcode' => array( - 'lodash', - ), - 'wp-token-list' => array( - 'lodash', - ), - 'wp-url' => array(), - 'wp-viewport' => array( - 'lodash', - 'wp-compose', - 'wp-data', - 'wp-element', - ), - 'wp-wordcount' => array(), -); From 33991d633dfc33870919386b924466679eb54995 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Apr 2019 10:31:17 +0200 Subject: [PATCH 4/8] Include build/*/*.deps.json in plugin zip --- bin/build-plugin-zip.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/build-plugin-zip.sh b/bin/build-plugin-zip.sh index a518e83b0e311..7e76a2032228c 100755 --- a/bin/build-plugin-zip.sh +++ b/bin/build-plugin-zip.sh @@ -107,7 +107,7 @@ npm run build php bin/generate-gutenberg-php.php > gutenberg.tmp.php mv gutenberg.tmp.php gutenberg.php -build_files=$(ls build/*/*.{js,css} build/block-library/blocks/*.php) +build_files=$(ls build/*/*.{js,css,deps.json} build/block-library/blocks/*.php) # Generate the plugin zip file. status "Creating archive... 🎁" From 2c3b6546a1575f8ee2c9021b4e96a3cc600913df Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Apr 2019 11:04:08 +0200 Subject: [PATCH 5/8] Add script dependencies undetectable by build tools Some scripts have dependencies that are undetectable by the webpack plugin used to generate the dependency files. Add these dependencies to the generated dependencies. --- lib/client-assets.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/client-assets.php b/lib/client-assets.php index 96f59424c3355..96611de034916 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -194,6 +194,17 @@ function gutenberg_register_packages_scripts() { ? json_decode( file_get_contents( $dependencies_file ) ) : array(); + // Add dependencies that cannot be detected and generated by build tools. + switch ( $handle ) { + case 'wp-block-library': + array_push( $dependencies, 'editor' ); + break; + + case 'wp-edit-post': + array_push( $dependencies, 'media-models', 'media-views', 'postbox' ); + break; + } + // Get the path from Gutenberg directory as expected by `gutenberg_url`. $gutenberg_path = substr( $path, strlen( gutenberg_dir_path() ) ); From 27bccbeed80e55df7084f251ace83b55fb3c60d0 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Apr 2019 11:31:28 +0200 Subject: [PATCH 6/8] Add `wp-polyfill` dependency via webpack plugin Enable `injectPolyfill` option in the webpack plugin instead of manually injecting the dependency via PHP on script registration. This will enable the polyfill for consumers of wp-scripts default webpack config. --- lib/client-assets.php | 2 +- packages/scripts/config/webpack.config.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 96611de034916..47949cdccea4f 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -211,7 +211,7 @@ function gutenberg_register_packages_scripts() { gutenberg_override_script( $handle, gutenberg_url( $gutenberg_path ), - array_merge( $dependencies, array( 'wp-polyfill' ) ), + $dependencies, filemtime( $path ), true ); diff --git a/packages/scripts/config/webpack.config.js b/packages/scripts/config/webpack.config.js index 44f173d958e0f..5a3b30163a432 100644 --- a/packages/scripts/config/webpack.config.js +++ b/packages/scripts/config/webpack.config.js @@ -67,7 +67,7 @@ const config = { // WP_LIVE_RELOAD_PORT global variable changes port on which live reload works // when running watch mode. ! isProduction && new LiveReloadPlugin( { port: process.env.WP_LIVE_RELOAD_PORT || 35729 } ), - new DependencyExtractionWebpackPlugin(), + new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ), ].filter( Boolean ), stats: { children: false, From df21c64cb69f0f9050d92bf7ff1077d8d9a576cb Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 24 Apr 2019 11:51:33 +0200 Subject: [PATCH 7/8] Improve deprecated message --- lib/packages-dependencies.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/packages-dependencies.php b/lib/packages-dependencies.php index 67d05cc3b6244..07b29cc8d1ca6 100644 --- a/lib/packages-dependencies.php +++ b/lib/packages-dependencies.php @@ -1,5 +1,5 @@ Date: Wed, 24 Apr 2019 15:10:27 +0200 Subject: [PATCH 8/8] Remove obsolete lib/packages-dependencies.php --- lib/packages-dependencies.php | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 lib/packages-dependencies.php diff --git a/lib/packages-dependencies.php b/lib/packages-dependencies.php deleted file mode 100644 index 07b29cc8d1ca6..0000000000000 --- a/lib/packages-dependencies.php +++ /dev/null @@ -1,5 +0,0 @@ -