-
Notifications
You must be signed in to change notification settings - Fork 221
Expand Feature flags #2591
Expand Feature flags #2591
Changes from all commits
4869c9d
b4cc0c8
4761437
3579b7e
0b0a1f2
4b40833
ecaa53d
2c7db37
d222fa0
32a9b99
11546b1
01cd10b
2afdb14
e1ad5b4
43572e7
3dcb8f5
76ed128
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { registerBlockType } from '@wordpress/blocks'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One concern I have here is that we are now loading this api on the frontend for anything using block-settings right? is that problematic? I think I might have been the one who suggested putting this api in block-settings so that could be my fault here. It doesn't look like it's affected build sizes significantly, so maybe this would be okay (since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had similar conerns, but I'm assuming this is tree shakeable, bundle bot didn’t report any extra weight so It’s safe. |
||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { WOOCOMMERCE_BLOCKS_PHASE } from './constants'; | ||
|
||
/** | ||
* Registers a new experimental block provided a unique name and an object defining its | ||
* behavior. Once registered, the block is made available as an option to any | ||
* editor interface where blocks are implemented. | ||
* | ||
* @param {string} name Block name. | ||
* @param {Object} settings Block settings. | ||
* | ||
* @return {?Object} The block, if it has been successfully registered; | ||
* otherwise `undefined`. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for being so thorough with the comment blocks here 👏 ! |
||
export const registerExperimentalBlockType = ( name, settings ) => { | ||
if ( WOOCOMMERCE_BLOCKS_PHASE > 2 ) { | ||
return registerBlockType( name, settings ); | ||
} | ||
}; | ||
|
||
/** | ||
* Registers a new feature plugin block provided a unique name and an object | ||
* defining its behavior. Once registered, the block is made available as an | ||
* option to any editor interface where blocks are implemented. | ||
* | ||
* @param {string} name Block name. | ||
* @param {Object} settings Block settings. | ||
* | ||
* @return {?Object} The block, if it has been successfully registered; | ||
* otherwise `undefined`. | ||
*/ | ||
export const registerFeaturePluginBlockType = ( name, settings ) => { | ||
if ( WOOCOMMERCE_BLOCKS_PHASE > 1 ) { | ||
return registerBlockType( name, settings ); | ||
} | ||
}; | ||
|
||
/** | ||
* Checks if we're executing the code in an experimental build mode. | ||
* | ||
* @return {boolean} True if this is an experimental build, false otherwise. | ||
*/ | ||
export const isExperimentalBuild = () => WOOCOMMERCE_BLOCKS_PHASE > 2; | ||
|
||
/** | ||
* Checks if we're executing the code in an feature plugin or experimental build mode. | ||
* | ||
* @return {boolean} True if this is an experimental or feature plugin build, false otherwise. | ||
*/ | ||
export const isFeaturePluginBuild = () => WOOCOMMERCE_BLOCKS_PHASE > 1; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
* Internal dependencies | ||
*/ | ||
export * from './constants'; | ||
export * from './feature-flags'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extrac | |
const WebpackRTLPlugin = require( 'webpack-rtl-plugin' ); | ||
const chalk = require( 'chalk' ); | ||
const { omit } = require( 'lodash' ); | ||
const { DefinePlugin } = require( 'webpack' ); | ||
const NODE_ENV = process.env.NODE_ENV || 'development'; | ||
|
||
function findModuleMatch( module, match ) { | ||
|
@@ -149,16 +148,17 @@ const stableMainEntry = { | |
'active-filters': './assets/js/blocks/active-filters/index.js', | ||
'block-error-boundary': | ||
'./assets/js/base/components/block-error-boundary/style.scss', | ||
'single-product': './assets/js/blocks/single-product/index.js', | ||
cart: './assets/js/blocks/cart-checkout/cart/index.js', | ||
checkout: './assets/js/blocks/cart-checkout/checkout/index.js', | ||
}; | ||
|
||
const experimentalMainEntry = { | ||
cart: './assets/js/blocks/cart-checkout/cart/index.js', | ||
checkout: './assets/js/blocks/cart-checkout/checkout/index.js', | ||
'single-product': './assets/js/blocks/single-product/index.js', | ||
}; | ||
|
||
const mainEntry = | ||
process.env.WOOCOMMERCE_BLOCKS_PHASE === 'stable' | ||
// env variables are strings, so we compare against a string, so we need to parse it. | ||
parseInt( process.env.WOOCOMMERCE_BLOCKS_PHASE, 10 ) < 3 | ||
? stableMainEntry | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this means that we still have dead code removal on releases for anything marked experimental right? Might be a good idea to also check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It shouldn’t cause a Reference Error because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
: { ...stableMainEntry, ...experimentalMainEntry }; | ||
|
||
|
@@ -168,16 +168,17 @@ const stableFrontEndEntry = { | |
'price-filter': './assets/js/blocks/price-filter/frontend.js', | ||
'attribute-filter': './assets/js/blocks/attribute-filter/frontend.js', | ||
'active-filters': './assets/js/blocks/active-filters/frontend.js', | ||
'single-product': './assets/js/blocks/single-product/frontend.js', | ||
cart: './assets/js/blocks/cart-checkout/cart/frontend.js', | ||
checkout: './assets/js/blocks/cart-checkout/checkout/frontend.js', | ||
}; | ||
|
||
const experimentalFrontEndEntry = { | ||
cart: './assets/js/blocks/cart-checkout/cart/frontend.js', | ||
checkout: './assets/js/blocks/cart-checkout/checkout/frontend.js', | ||
'single-product': './assets/js/blocks/single-product/frontend.js', | ||
}; | ||
|
||
const frontEndEntry = | ||
process.env.WOOCOMMERCE_BLOCKS_PHASE === 'stable' | ||
// env variables are strings, so we compare against a string, so we need to parse it. | ||
parseInt( process.env.WOOCOMMERCE_BLOCKS_PHASE, 10 ) < 3 | ||
? stableFrontEndEntry | ||
: { ...stableFrontEndEntry, ...experimentalFrontEndEntry }; | ||
|
||
|
@@ -358,13 +359,6 @@ const getMainConfig = ( options = {} ) => { | |
requestToExternal, | ||
requestToHandle, | ||
} ), | ||
new DefinePlugin( { | ||
// Inject the `WOOCOMMERCE_BLOCKS_PHASE` global, used for feature flagging. | ||
'process.env.WOOCOMMERCE_BLOCKS_PHASE': JSON.stringify( | ||
// eslint-disable-next-line woocommerce/feature-flag | ||
process.env.WOOCOMMERCE_BLOCKS_PHASE || 'experimental' | ||
), | ||
} ), | ||
], | ||
resolve, | ||
}; | ||
|
@@ -460,13 +454,6 @@ const getFrontConfig = ( options = {} ) => { | |
requestToExternal, | ||
requestToHandle, | ||
} ), | ||
new DefinePlugin( { | ||
// Inject the `WOOCOMMERCE_BLOCKS_PHASE` global, used for feature flagging. | ||
'process.env.WOOCOMMERCE_BLOCKS_PHASE': JSON.stringify( | ||
// eslint-disable-next-line woocommerce/feature-flag | ||
process.env.WOOCOMMERCE_BLOCKS_PHASE || 'experimental' | ||
), | ||
} ), | ||
], | ||
resolve, | ||
}; | ||
|
@@ -599,13 +586,6 @@ const getPaymentMethodsExtensionConfig = ( options = {} ) => { | |
requestToExternal, | ||
requestToHandle, | ||
} ), | ||
new DefinePlugin( { | ||
// Inject the `WOOCOMMERCE_BLOCKS_PHASE` global, used for feature flagging. | ||
'process.env.WOOCOMMERCE_BLOCKS_PHASE': JSON.stringify( | ||
// eslint-disable-next-line woocommerce/feature-flag | ||
process.env.WOOCOMMERCE_BLOCKS_PHASE || 'experimental' | ||
), | ||
} ), | ||
], | ||
resolve, | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might want to get someone else to review webpack who knows more :) |
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,12 +81,15 @@ public static function register_assets() { | |
self::register_script( 'wc-price-filter', plugins_url( self::get_block_asset_build_path( 'price-filter' ), __DIR__ ), $block_dependencies ); | ||
self::register_script( 'wc-attribute-filter', plugins_url( self::get_block_asset_build_path( 'attribute-filter' ), __DIR__ ), $block_dependencies ); | ||
self::register_script( 'wc-active-filters', plugins_url( self::get_block_asset_build_path( 'active-filters' ), __DIR__ ), $block_dependencies ); | ||
self::register_script( 'wc-checkout-block', plugins_url( self::get_block_asset_build_path( 'checkout' ), __DIR__ ), $block_dependencies ); | ||
self::register_script( 'wc-cart-block', plugins_url( self::get_block_asset_build_path( 'cart' ), __DIR__ ), $block_dependencies ); | ||
|
||
if ( 'experimental' === WOOCOMMERCE_BLOCKS_PHASE ) { | ||
if ( Package::is_experimental_build() ) { | ||
self::register_script( 'wc-single-product-block', plugins_url( self::get_block_asset_build_path( 'single-product' ), __DIR__ ), $block_dependencies ); | ||
} | ||
|
||
if ( Package::is_feature_plugin_build() ) { | ||
self::register_script( 'wc-checkout-block', plugins_url( self::get_block_asset_build_path( 'checkout' ), __DIR__ ), $block_dependencies ); | ||
self::register_script( 'wc-cart-block', plugins_url( self::get_block_asset_build_path( 'cart' ), __DIR__ ), $block_dependencies ); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At some point we should move script registration to the block classes - I always forget to add them here :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, there's a bunch of things I'd like to do around that eventually (including incorporating using |
||
} | ||
|
||
/** | ||
|
@@ -178,6 +181,7 @@ public static function get_wc_block_data( $settings ) { | |
'checkoutAllowsGuest' => 'yes' === get_option( 'woocommerce_enable_guest_checkout' ), | ||
'checkoutAllowsSignup' => 'yes' === get_option( 'woocommerce_enable_signup_and_login_from_checkout' ), | ||
'baseLocation' => wc_get_base_location(), | ||
'woocommerceBlocksPhase' => WOOCOMMERCE_BLOCKS_PHASE, | ||
|
||
/* | ||
* translators: If your word count is based on single characters (e.g. East Asian characters), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ protected function init() { | |
if ( ! $this->has_core_dependencies() ) { | ||
return; | ||
} | ||
$this->define_feature_flag(); | ||
$this->register_dependencies(); | ||
$this->register_payment_methods(); | ||
|
||
|
@@ -72,7 +73,6 @@ protected function init() { | |
// Load assets in admin and on the frontend. | ||
if ( ! $is_rest ) { | ||
$this->add_build_notice(); | ||
$this->define_feature_flag(); | ||
$this->container->get( AssetDataRegistry::class ); | ||
$this->container->get( Installer::class ); | ||
BlockAssets::init(); | ||
|
@@ -130,17 +130,17 @@ function() { | |
* Define the global feature flag. | ||
*/ | ||
protected function define_feature_flag() { | ||
$allowed_flags = [ 'experimental', 'stable' ]; | ||
$flag = getenv( 'WOOCOMMERCE_BLOCKS_PHASE' ); | ||
if ( ! in_array( $flag, $allowed_flags, true ) ) { | ||
if ( file_exists( __DIR__ . '/../../blocks.ini' ) ) { | ||
$woo_options = parse_ini_file( __DIR__ . '/../../blocks.ini' ); | ||
$flag = is_array( $woo_options ) && 'experimental' === $woo_options['woocommerce_blocks_phase'] ? 'experimental' : 'stable'; | ||
} else { | ||
$flag = 'stable'; | ||
} | ||
$default_flag = defined( 'WC_BLOCKS_IS_FEATURE_PLUGIN' ) ? '2' : '1'; | ||
$allowed_flags = [ '1', '2', '3' ]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason we use strings here instead of numbers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Environment variables and ini values are treated as strings, to avoid casting and recasting things all over the place, I assume everything is a string and when I define the flag I turn it to an int. |
||
|
||
if ( file_exists( __DIR__ . '/../../blocks.ini' ) ) { | ||
$woo_options = parse_ini_file( __DIR__ . '/../../blocks.ini' ); | ||
$flag = is_array( $woo_options ) && in_array( $woo_options['woocommerce_blocks_phase'], $allowed_flags, true ) ? $woo_options['woocommerce_blocks_phase'] : $default_flag; | ||
} else { | ||
$flag = $default_flag; | ||
} | ||
define( 'WOOCOMMERCE_BLOCKS_PHASE', $flag ); | ||
|
||
define( 'WOOCOMMERCE_BLOCKS_PHASE', intval( $flag ) ); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 on putting these in their own file (and the name is great)!