Skip to content

Commit

Permalink
Initial and worst commit
Browse files Browse the repository at this point in the history
Try:
- move style engine into `/packages/style-engine`
- create webpack copy pattern for packages with classes
- switched class methods to static so we don't have to get the instance

Moving tests and breaking everything else.
  • Loading branch information
ramonjd committed Mar 29, 2022
1 parent c653bae commit e7487ca
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ node_modules
gutenberg.zip
coverage
*-performance-results.json
lib/packages

# Directories/files that may appear in your environment
*.log
Expand Down
2 changes: 1 addition & 1 deletion lib/block-supports/spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {
return $attributes;
}

$style_engine = WP_Style_Engine_Gutenberg::get_instance();
$style_engine = gutenberg_get_style_engine();
$skip_padding = gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
$skip_margin = gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
$spacing_block_styles = array();
Expand Down
8 changes: 4 additions & 4 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/global-styles.php';
require __DIR__ . '/pwa.php';

// TODO: Move this to be loaded from the style engine package, via the build directory.
// Part of the build process should be to copy the PHP file to the correct location,
// similar to the loading behaviour in `blocks.php`.
require __DIR__ . '/style-engine/class-wp-style-engine-gutenberg.php';
// Copied package PHP files.
if ( file_exists( __DIR__ . '/packages/class-wp-style-engine-gutenberg.php' ) ) {
require __DIR__ . '/packages/class-wp-style-engine-gutenberg.php';
}

require __DIR__ . '/block-supports/utils.php';
require __DIR__ . '/block-supports/elements.php';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
"api-docs:blocks": "node ./bin/api-docs/gen-block-lib-list.js",
"api-docs:ref": "node ./bin/api-docs/update-api-docs.js",
"api-docs:theme-ref": "node ./bin/api-docs/gen-theme-reference.js",
"clean:packages": "rimraf \"./packages/*/@(build|build-module|build-style)\"",
"clean:packages": "rimraf \"./packages/*/@(build|build-module|build-style)\" \"./lib/packages\"",
"clean:package-types": "tsc --build --clean",
"prebuild:packages": "npm run clean:packages && lerna run build",
"build:packages": "npm run build:package-types && node ./bin/packages/build.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
/**
* WP_Style_Engine_Gutenberg class
* WP_Style_Engine
*
* Generates classnames and block styles.
*
* @package Gutenberg
*/

if ( class_exists( 'WP_Style_Engine_Gutenberg' ) ) {
if ( class_exists( 'WP_Style_Engine' ) ) {
return;
}

Expand All @@ -19,11 +19,11 @@
*
* @since 6.0.0
*/
class WP_Style_Engine_Gutenberg {
class WP_Style_Engine {
/**
* Container for the main instance of the class.
*
* @var WP_Style_Engine_Gutenberg|null
* @var WP_Style_Engine|null
*/
private static $instance = null;

Expand Down Expand Up @@ -57,7 +57,7 @@ class WP_Style_Engine_Gutenberg {
*
* The instance will be created if it does not exist yet.
*
* @return WP_Style_Engine_Gutenberg The main instance.
* @return WP_Style_Engine The main instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
Expand Down Expand Up @@ -171,3 +171,14 @@ public static function get_css_box_rules( $style_value, $style_property ) {
return $rules;
}
}

/**
* This function returns the Style Engine instance.
*
* @return WP_Style_Engine
*/
function wp_get_style_engine() {
if ( class_exists( 'WP_Style_Engine' ) ) {
return WP_Style_Engine::get_instance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
* @subpackage style-engine
*/

require __DIR__ . '/../class-wp-style-engine.php';

/**
* Tests for registering, storing and generating styles.
*/
class WP_Style_Engine_Gutenberg_Test extends WP_UnitTestCase {
class WP_Style_Engine_Test extends WP_UnitTestCase {
/**
* Tests various manifestations of the $block_styles argument.
*
* @dataProvider data_block_styles_fixtures
*/
function test_generate_css( $block_styles, $options, $expected_output ) {
$style_engine = WP_Style_Engine_Gutenberg::get_instance();
$style_engine = WP_Style_Engine::get_instance();
$generated_styles = $style_engine->generate(
$block_styles,
$options
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<testsuite name="default">
<directory suffix="-test.php">./phpunit/</directory>
</testsuite>
<testsuite name="packages">
<directory suffix="-test.php">./packages/**/phpunit/</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
Expand Down
20 changes: 20 additions & 0 deletions phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
/**
* Manually load plugin dependencies.
*/
// HACK ******* WARNING ****** EXPERIMENTAL HACK
if ( ! file_exists(dirname( __DIR__ ) . '/lib/packages' ) ) {
mkdir( dirname( __DIR__ ) . '/lib/packages', 0777, true );
}

if ( ! file_exists( dirname( __DIR__ ) . '/lib/packages/class-wp-style-engine-gutenberg.php' ) ) {
copy( dirname( __DIR__ ) . '/packages/style-engine/class-wp-style-engine.php', dirname( __DIR__ ) . '/lib/packages/class-wp-style-engine-gutenberg.php' );
$contents = file_get_contents( dirname( __DIR__ ) . '/lib/packages/class-wp-style-engine-gutenberg.php' );
$patterns = array( '/WP_Style_Engine/', '/wp_get_style_engine/' );
$replace = array( 'WP_Style_Engine_Gutenberg', 'gutenberg_get_style_engine' );
$contents = preg_replace( $patterns, $replace, $contents );
file_put_contents( dirname( __DIR__ ) . '/lib/packages/class-wp-style-engine-gutenberg.php', $contents );
}

/**
* Manually load plugin.
*/
require dirname( __DIR__ ) . '/lib/load.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
Expand Down
56 changes: 50 additions & 6 deletions tools/webpack/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ const BUNDLED_PACKAGES = [
'@wordpress/interface',
'@wordpress/style-engine',
];

// PHP files in packages that have to be copied over to /lib.
const BUNDLED_PACKAGES_PHP = [
{
from: './packages/style-engine/',
to: 'lib/packages/',
replaceClasses: [ 'WP_Style_Engine' ],
},
];
const gutenbergPackages = Object.keys( dependencies )
.filter(
( packageName ) =>
Expand Down Expand Up @@ -99,15 +106,52 @@ module.exports = {
...plugins,
new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ),
new CopyWebpackPlugin( {
patterns: gutenbergPackages
.map( ( packageName ) => ( {
patterns: [].concat(
gutenbergPackages.map( ( packageName ) => ( {
from: '*.css',
context: `./packages/${ packageName }/build-style`,
to: `./build/${ packageName }`,
transform: stylesTransform,
noErrorOnMissing: true,
} ) )
.concat( vendorsCopyConfig ),
} ) ),
// Packages with PHP files to be parsed and copied to ./lib.
BUNDLED_PACKAGES_PHP.map(
( { from, to, replaceClasses } ) => ( {
from: `${ from }/*.php`,
to: ( { absoluteFilename } ) => {
const [ , filename ] = absoluteFilename.match(
/([\w-]+)(\.php){1,1}$/
);
return join( to, `${ filename }-gutenberg.php` );
},
transform: ( content ) => {
const classSuffix = '_Gutenberg';
const functionPrefix = 'gutenberg_';
content = content.toString();
// Replace class names.
content = content.replace(
new RegExp( replaceClasses.join( '|' ), 'g' ),
( match ) => `${ match }${ classSuffix }`
);
// Replace function names.
content = Array.from(
content.matchAll( /^\s*function ([^\(]+)/gm )
).reduce( ( result, [ , functionName ] ) => {
// Prepend the Gutenberg prefix, substituting any
// other core prefix (e.g. "wp_").
return result.replace(
new RegExp( functionName, 'g' ),
( match ) =>
functionPrefix +
match.replace( /^wp_/, '' )
);
}, content );
return content;
},
noErrorOnMissing: true,
} )
),
vendorsCopyConfig
),
} ),
].filter( Boolean ),
};

0 comments on commit e7487ca

Please sign in to comment.