* revert changes to webpack file

* fix loading of chunks

* revert changes to assets api as well

* include chunks from other blocks

* bail early if there is no build folder
This commit is contained in:
Seghir Nadir 2022-05-25 16:59:51 +01:00 committed by GitHub
parent 8a702ab50f
commit d3c2f638fa
6 changed files with 35 additions and 51 deletions

View File

@ -122,7 +122,9 @@ woocommerce_blocks_env = ${ NODE_ENV }
// Only concatenate modules in production, when not analyzing bundles.
concatenateModules:
isProduction && ! process.env.WP_BUNDLE_ANALYZER,
splitChunks: false,
splitChunks: {
automaticNameDelimiter: '--',
},
minimizer: [
new TerserPlugin( {
cache: true,
@ -220,7 +222,7 @@ const getMainConfig = ( options = {} ) => {
concatenateModules:
isProduction && ! process.env.WP_BUNDLE_ANALYZER,
splitChunks: {
minSize: 10000, // makes the smallest chunk file 10kbs, this doesn't affect lazy loaded chunks.
minSize: 0,
automaticNameDelimiter: '--',
cacheGroups: {
commons: {
@ -259,31 +261,31 @@ const getMainConfig = ( options = {} ) => {
patterns: [
{
from: './assets/js/blocks/checkout/block.json',
to: './checkout.block.json',
to: './checkout/block.json',
},
{
from:
'./assets/js/blocks/featured-items/featured-category/block.json',
to: './featured-category.block.json',
to: './featured-category/block.json',
},
{
from:
'./assets/js/blocks/featured-items/featured-product/block.json',
to: './featured-product.block.json',
to: './featured-product/block.json',
},
{
from:
'./assets/js/blocks/handpicked-products/block.json',
to: './handpicked-products.block.json',
to: './handpicked-products/block.json',
},
{
from: './assets/js/blocks/product-tag/block.json',
to: './product-tag.block.json',
to: './product-tag/block.json',
},
{
from:
'./assets/js/blocks/products-by-attribute/block.json',
to: './products-by-attribute.block.json',
to: './products-by-attribute/block.json',
},
],
} ),
@ -376,39 +378,7 @@ const getFrontConfig = ( options = {} ) => {
concatenateModules:
isProduction && ! process.env.WP_BUNDLE_ANALYZER,
splitChunks: {
cacheGroups: {
default: false,
vendors: {
test: /[\\/]node_modules[\\/]/,
name( module ) {
const moduleFileName = module
.identifier()
.split( '/' )
.reduceRight( ( item ) => item );
return `vendor/${ moduleFileName }`;
},
automaticNameDelimiter: '--',
minSize: 20000,
priority: -20,
},
'wp-components': {
test: ( module ) => {
if (
module?.resource?.match(
/wordpress-components/
)
) {
return true;
}
},
name: 'wp/wp-components.js',
automaticNameDelimiter: '--',
reuseExistingChunk: true,
enforce: true,
minSize: 20000,
priority: -10,
},
},
automaticNameDelimiter: '--',
},
minimizer: [
new TerserPlugin( {

View File

@ -68,7 +68,7 @@ class Api {
* @return string|boolean False if metadata file is not found for the block.
*/
public function get_block_metadata_path( $block_name ) {
$path_to_metadata_from_plugin_root = $this->package->get_path( 'build/' . $block_name . '.block.json' );
$path_to_metadata_from_plugin_root = $this->package->get_path( 'build/' . $block_name . '/block.json' );
if ( ! file_exists( $path_to_metadata_from_plugin_root ) ) {
return false;
}

View File

@ -173,10 +173,18 @@ abstract class AbstractBlock {
/**
* Generate an array of chunks paths for loading translation.
*
* @param string $chunks_folder The folder to iterate over.
* @return string[] $chunks list of chunks to load.
*/
protected function get_chunks_paths() {
foreach ( glob( \Automattic\WooCommerce\Blocks\Package::get_path() . "build/{$this->chunks_folder}/*-frontend.js" ) as $block_name ) {
$blocks[] = "{$this->chunks_folder}/" . basename( $block_name );
protected function get_chunks_paths( $chunks_folder ) {
$build_path = \Automattic\WooCommerce\Blocks\Package::get_path() . 'build/';
$blocks = [];
if ( ! is_dir( $build_path ) ) {
return [];
}
foreach ( new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $build_path . $chunks_folder ) ) as $block_name ) {
$blocks[] = str_replace( $build_path, '', $block_name );
}
$chunks = preg_filter( '/.js/', '', $blocks );

View File

@ -241,7 +241,9 @@ class Cart extends AbstractBlock {
*/
protected function register_block_type_assets() {
parent::register_block_type_assets();
$chunks = $this->get_chunks_paths();
$this->register_chunk_translations( $chunks );
$chunks = $this->get_chunks_paths( $this->chunks_folder );
$vendor_chunks = $this->get_chunks_paths( 'vendors--cart-blocks' );
$this->register_chunk_translations( array_merge( $chunks, $vendor_chunks ) );
}
}

View File

@ -406,7 +406,9 @@ class Checkout extends AbstractBlock {
*/
protected function register_block_type_assets() {
parent::register_block_type_assets();
$chunks = $this->get_chunks_paths();
$this->register_chunk_translations( $chunks );
$chunks = $this->get_chunks_paths( $this->chunks_folder );
$vendor_chunks = $this->get_chunks_paths( 'vendors--cart-blocks' );
$shared_chunks = [ 'cart-blocks/order-summary-shipping--checkout-blocks/order-summary-shipping-frontend' ];
$this->register_chunk_translations( array_merge( $chunks, $vendor_chunks, $shared_chunks ) );
}
}

View File

@ -488,8 +488,10 @@ class MiniCart extends AbstractBlock {
*/
protected function register_block_type_assets() {
parent::register_block_type_assets();
$chunks = $this->get_chunks_paths();
$this->register_chunk_translations( $chunks );
$chunks = $this->get_chunks_paths( $this->chunks_folder );
$vendor_chunks = $this->get_chunks_paths( 'vendors--mini-cart-contents-block' );
$shared_chunks = [ 'cart-blocks/cart-line-items--mini-cart-contents-block/products-table-frontend' ];
$this->register_chunk_translations( array_merge( $chunks, $vendor_chunks, $shared_chunks ) );
}
/**