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... 🎁" diff --git a/lib/client-assets.php b/lib/client-assets.php index d9fcc8105249b..47949cdccea4f 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; } @@ -177,17 +182,37 @@ 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(); + + // 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() ) ); - 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 ), - array_merge( $dependencies, array( 'wp-polyfill' ) ), - filemtime( gutenberg_dir_path() . $path ), + gutenberg_url( $gutenberg_path ), + $dependencies, + filemtime( $path ), true ); } diff --git a/lib/packages-dependencies.php b/lib/packages-dependencies.php deleted file mode 100644 index 8a1f78fafe456..0000000000000 --- a/lib/packages-dependencies.php +++ /dev/null @@ -1,265 +0,0 @@ - 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(), -); 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,