woocommerce/plugins/woocommerce-blocks/bin/webpack-entries.js

140 lines
3.8 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
const { omit } = require( 'lodash' );
const glob = require( 'glob' );
// List of blocks that should be used as webpack entry points. They are expected
// to be in `/assets/js/blocks/[BLOCK_NAME]`. If they are not, their relative
// path should be defined in the `customDir` property. The scripts below will
// take care of looking for `index.js`, `frontend.js` and `*.scss` files in each
// block directory.
// If a block is experimental, it should be marked with the `isExperimental`
// property.
const blocks = {
'handpicked-products': {},
'product-best-sellers': {},
'product-category': {},
'product-categories': {},
'product-new': {},
'product-on-sale': {},
'product-top-rated': {},
'products-by-attribute': {},
'featured-product': {},
'all-reviews': {
customDir: 'reviews/all-reviews',
},
'reviews-by-product': {
customDir: 'reviews/reviews-by-product',
},
'reviews-by-category': {
customDir: 'reviews/reviews-by-category',
},
'product-search': {},
'product-tag': {},
'featured-category': {},
'all-products': {
customDir: 'products/all-products',
},
'price-filter': {},
'attribute-filter': {},
'active-filters': {},
cart: {
customDir: 'cart-checkout/cart',
},
checkout: {
customDir: 'cart-checkout/checkout',
},
'single-product': {
isExperimental: true,
},
};
// Returns the entries for each block given a relative path (ie: `index.js`,
// `**/*.scss`...).
// It also filters out elements with undefined props and experimental blocks.
const getBlockEntries = ( relativePath ) => {
const experimental =
! parseInt( process.env.WOOCOMMERCE_BLOCKS_PHASE, 10 ) < 3;
return Object.fromEntries(
Object.entries( blocks )
.filter(
( [ , config ] ) =>
! config.isExperimental ||
config.isExperimental === experimental
)
.map( ( [ blockCode, config ] ) => {
const filePaths = glob.sync(
`./assets/js/blocks/${ config.customDir || blockCode }/` +
relativePath
);
if ( filePaths.length > 0 ) {
return [ blockCode, filePaths ];
}
return null;
} )
.filter( Boolean )
);
};
const entries = {
styling: {
// @wordpress/components styles
'custom-select-control-style':
'./node_modules/wordpress-components/src/custom-select-control/style.scss',
'spinner-style':
'./node_modules/wordpress-components/src/spinner/style.scss',
'snackbar-notice-style':
'./node_modules/wordpress-components/src/snackbar/style.scss',
'general-style': glob.sync( './assets/**/*.scss', {
ignore: [
// Block styles are added below.
'./assets/js/blocks/*/*.scss',
],
} ),
...getBlockEntries( '**/*.scss' ),
},
core: {
wcBlocksRegistry: './assets/js/blocks-registry/index.js',
wcSettings: './assets/js/settings/shared/index.js',
wcBlocksData: './assets/js/data/index.js',
wcBlocksMiddleware: './assets/js/middleware/index.js',
wcSharedContext: './assets/js/shared/context/index.js',
wcSharedHocs: './assets/js/shared/hocs/index.js',
},
main: {
// Shared blocks code
blocks: './assets/js/index.js',
// Blocks
...getBlockEntries( 'index.js' ),
},
frontend: {
reviews: './assets/js/blocks/reviews/frontend.js',
...getBlockEntries( 'frontend.js' ),
},
payments: {
'wc-payment-method-stripe':
'./assets/js/payment-method-extensions/payment-methods/stripe/index.js',
'wc-payment-method-cheque':
'./assets/js/payment-method-extensions/payment-methods/cheque/index.js',
'wc-payment-method-paypal':
'./assets/js/payment-method-extensions/payment-methods/paypal/index.js',
'wc-payment-method-bacs':
'./assets/js/payment-method-extensions/payment-methods/bacs/index.js',
'wc-payment-method-cod':
'./assets/js/payment-method-extensions/payment-methods/cod/index.js',
},
};
const getEntryConfig = ( type = 'main', exclude = [] ) => {
return omit( entries[ type ], exclude );
};
module.exports = {
getEntryConfig,
};