* introduce feature flags

* move config to webpack-helper

* add flag to deploy command

* remove package default

* add cross-env

* add gating to frontendConfig and coreConfig

* exclude entries from being built on stable mode

* add feature gating to PHP

* add flag to start command

* add flags to travis

* add endpoints

* add better defaults for php

* move code to Bootstrap.php

* no need to spread single object

* ignore blocks.ini

* type check feature flag

* remove blocks.ini

* sanitize flag

* remove flag from npm start

* format condition

* keep spaces for package-lock.json

* check for env before going to block ini

* add env vars to travis

* whitelist env var
This commit is contained in:
Seghir Nadir 2020-01-31 21:04:37 +01:00 committed by GitHub
parent bfb2f33bfd
commit 8e3ac17585
12 changed files with 2450 additions and 9230 deletions

View File

@ -50,3 +50,4 @@ tests/cli/vendor
bin/languages
woocommerce-gutenberg-products-block.zip
storybook-static/
blocks.ini

View File

@ -47,25 +47,32 @@ jobs:
php: 7.1
env:
- WP_VERSION=latest
- WOOCOMMERCE_BLOCKS_PHASE=experimental
script:
- phpunit
- name: PHP 5.6/unit-tests/Latest WP
php: 5.6
env:
- WP_VERSION=latest
- WOOCOMMERCE_BLOCKS_PHASE=experimental
script:
- phpunit
- name: PHP Linting Check
php: 7.1
env:
- WP_TRAVISCI=phpcs
- WOOCOMMERCE_BLOCKS_PHASE=experimental
script:
- npm run lint:php
- name: Javascript Tests
script:
- npm install
- npm run test
env:
- WOOCOMMERCE_BLOCKS_PHASE=experimental
- name: Javascript/CSS Lint and Bundle Size Check
script:
- npm install
- npm run build:ci
env:
- WOOCOMMERCE_BLOCKS_PHASE=experimental

View File

@ -16,7 +16,7 @@ import './style.scss';
/**
* Register and run the Cart block.
*/
registerBlockType( 'woocommerce/cart', {
const settings = {
title: __( 'Cart', 'woo-gutenberg-products-block' ),
icon: {
src: <Icon srcElement={ cart } />,
@ -52,4 +52,8 @@ registerBlockType( 'woocommerce/cart', {
</div>
);
},
} );
};
if ( process.env.WOOCOMMERCE_BLOCKS_PHASE === 'experimental' ) {
registerBlockType( 'woocommerce/cart', settings );
}

View File

@ -12,7 +12,7 @@ import edit from './edit';
import { example } from './example';
import './editor.scss';
registerBlockType( 'woocommerce/checkout', {
const settings = {
title: __( 'Checkout', 'woo-gutenberg-products-block' ),
icon: {
src: <Icon srcElement={ card } />,
@ -51,4 +51,8 @@ registerBlockType( 'woocommerce/checkout', {
</div>
);
},
} );
};
if ( process.env.WOOCOMMERCE_BLOCKS_PHASE === 'experimental' ) {
registerBlockType( 'woocommerce/checkout', settings );
}

View File

@ -9,7 +9,7 @@ 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 ) {
@ -96,7 +96,7 @@ const getAlias = ( options = {} ) => {
};
};
const mainEntry = {
const stableMainEntry = {
// Shared blocks code
blocks: './assets/js/index.js',
@ -128,22 +128,36 @@ const mainEntry = {
'panel-style': './node_modules/@wordpress/components/src/panel/style.scss',
'custom-select-control-style':
'./node_modules/@wordpress/components/src/custom-select-control/style.scss',
};
// cart & checkout blocks
const experimentalMainEntry = {
cart: './assets/js/blocks/cart-checkout/cart/index.js',
checkout: './assets/js/blocks/cart-checkout/checkout/index.js',
};
const frontEndEntry = {
const mainEntry =
process.env.WOOCOMMERCE_BLOCKS_PHASE === 'experimental'
? { ...stableMainEntry, ...experimentalMainEntry }
: stableMainEntry;
const stableFrontEndEntry = {
reviews: './assets/js/blocks/reviews/frontend.js',
'all-products': './assets/js/blocks/products/all-products/frontend.js',
'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',
};
const experimentalFrontEndEntry = {
checkout: './assets/js/blocks/cart-checkout/checkout/frontend.js',
cart: './assets/js/blocks/cart-checkout/cart/frontend.js',
};
const frontEndEntry =
process.env.WOOCOMMERCE_BLOCKS_PHASE === 'experimental'
? { ...stableFrontEndEntry, ...experimentalFrontEndEntry }
: stableFrontEndEntry;
const getEntryConfig = ( main = true, exclude = [] ) => {
const entryConfig = main ? mainEntry : frontEndEntry;
return omit( entryConfig, exclude );
@ -306,6 +320,12 @@ const getMainConfig = ( options = {} ) => {
requestToExternal,
requestToHandle,
} ),
new DefinePlugin( {
// Inject the `WOOCOMMERCE_BLOCKS_PHASE` global, used for feature flagging.
'process.env.WOOCOMMERCE_BLOCKS_PHASE': JSON.stringify(
process.env.WOOCOMMERCE_BLOCKS_PHASE || 'experimental'
),
} ),
],
resolve,
};
@ -401,6 +421,12 @@ const getFrontConfig = ( options = {} ) => {
requestToExternal,
requestToHandle,
} ),
new DefinePlugin( {
// Inject the `WOOCOMMERCE_BLOCKS_PHASE` global, used for feature flagging.
'process.env.WOOCOMMERCE_BLOCKS_PHASE': JSON.stringify(
process.env.WOOCOMMERCE_BLOCKS_PHASE || 'experimental'
),
} ),
],
resolve,
};

File diff suppressed because it is too large Load Diff

View File

@ -22,7 +22,7 @@
},
"license": "GPL-3.0+",
"scripts": {
"deploy": "composer install --no-dev && npm run build --loglevel error && sh ./bin/github-deploy.sh",
"deploy": "cross-env WOOCOMMERCE_BLOCKS_PHASE=stable composer install --no-dev && npm run build --loglevel error && sh ./bin/github-deploy.sh",
"release": "sh ./bin/wordpress-deploy.sh",
"build": "cross-env BABEL_ENV=default NODE_ENV=production webpack",
"build:ci": "npm run build && npm run size-check && npm run lint:js && npm run lint:css",
@ -86,6 +86,7 @@
"bundlewatch": "0.2.5",
"chalk": "3.0.0",
"clean-webpack-plugin": "3.0.0",
"create-file-webpack": "^1.0.2",
"cross-env": "6.0.3",
"css-loader": "3.4.2",
"cssnano": "4.1.10",

View File

@ -70,8 +70,11 @@ class 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 ( WOOCOMMERCE_BLOCKS_PHASE === 'experimental' ) {
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 );
}
}
/**

View File

@ -65,6 +65,8 @@ class Bootstrap {
$this->add_build_notice();
}
$this->define_feature_flag();
// register core dependencies with the container.
$this->container->register(
AssetApi::class,
@ -148,4 +150,17 @@ class Bootstrap {
remove_action( 'admin_print_footer_scripts', array( 'WC_Block_Library', 'print_script_settings' ), 1 );
remove_action( 'init', array( 'WGPB_Block_Library', 'init' ) );
}
/**
* 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 ) ) {
$woo_options = parse_ini_file( __DIR__ . '/../../blocks.ini' );
$flag = is_array( $woo_options ) && 'experimental' === $woo_options['woocommerce_blocks_phase'] ? 'experimental' : 'stable';
}
define( 'WOOCOMMERCE_BLOCKS_PHASE', $flag );
}
}

View File

@ -107,8 +107,11 @@ class Library {
$blocks[] = 'PriceFilter';
$blocks[] = 'AttributeFilter';
$blocks[] = 'ActiveFilters';
$blocks[] = 'Checkout';
$blocks[] = 'Cart';
if ( WOOCOMMERCE_BLOCKS_PHASE === 'experimental' ) {
$blocks[] = 'Checkout';
$blocks[] = 'Cart';
}
}
foreach ( $blocks as $class ) {
$class = __NAMESPACE__ . '\\BlockTypes\\' . $class;

View File

@ -84,7 +84,7 @@ class RestApi {
* @return array
*/
protected static function get_controllers() {
return [
$controllers = [
'product-attributes' => __NAMESPACE__ . '\RestApi\Controllers\ProductAttributes',
'product-attribute-terms' => __NAMESPACE__ . '\RestApi\Controllers\ProductAttributeTerms',
'product-categories' => __NAMESPACE__ . '\RestApi\Controllers\ProductCategories',
@ -92,16 +92,25 @@ class RestApi {
'products' => __NAMESPACE__ . '\RestApi\Controllers\Products',
'variations' => __NAMESPACE__ . '\RestApi\Controllers\Variations',
'product-reviews' => __NAMESPACE__ . '\RestApi\Controllers\ProductReviews',
'store-cart' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\Cart',
'store-cart-items' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\CartItems',
'store-cart-coupons' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\CartCoupons',
'store-cart-shipping-rates' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\CartShippingRates',
'store-cart-order' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\CartOrder',
'store-customer' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\Customer',
'store-products' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\Products',
'store-product-collection-data' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\ProductCollectionData',
'store-product-attributes' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\ProductAttributes',
'store-product-attribute-terms' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\ProductAttributeTerms',
];
$experimental_controllers = [
'store-cart' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\Cart',
'store-cart-order' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\CartOrder',
'store-cart-items' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\CartItems',
'store-cart-coupons' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\CartCoupons',
'store-cart-shipping-rates' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\CartShippingRates',
'store-customer' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\Customer',
];
if ( WOOCOMMERCE_BLOCKS_PHASE === 'experimental' ) {
$controllers = array_merge( $controllers, $experimental_controllers );
}
return $controllers;
}
}

View File

@ -3,7 +3,9 @@
*/
const path = require( 'path' );
const { kebabCase } = require( 'lodash' );
const { DefinePlugin } = require( 'webpack' );
const { CleanWebpackPlugin } = require( 'clean-webpack-plugin' );
const CreateFileWebpack = require( 'create-file-webpack' );
const ProgressBarPlugin = require( 'progress-bar-webpack-plugin' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
const chalk = require( 'chalk' );
@ -83,6 +85,20 @@ const CoreConfig = {
' :msg (:elapsed seconds)',
} ),
new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ),
new DefinePlugin( {
// Inject the `WOOCOMMERCE_BLOCKS_PHASE` global, used for feature flagging.
'process.env.WOOCOMMERCE_BLOCKS_PHASE': JSON.stringify(
process.env.WOOCOMMERCE_BLOCKS_PHASE || 'experimental'
),
} ),
new CreateFileWebpack( {
path: './',
// file name
fileName: 'blocks.ini',
// content of the file
content: `woocommerce_blocks_phase = ${ process.env
.WOOCOMMERCE_BLOCKS_PHASE || 'experimental' }`,
} ),
],
};