Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripts: Include variations paths in build #63098

Merged
merged 5 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 157 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Features

- Update webpack configuration for the `build` and `start` commands to automatically copy PHP files listed in the `variations` field of `block.json` files from the source to the build folder ([#63098](https://github.com/WordPress/gutenberg/pull/63098)).

## 28.3.0 (2024-07-10)

## 28.2.0 (2024-06-26)
Expand Down
4 changes: 2 additions & 2 deletions packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ This is how you execute the script with presented setup:

- `npm run build` - builds the code for production.
- `npm run build:custom` - builds the code for production with two entry points and a custom output directory. Paths for custom entry points are relative to the project root.
- `npm run build:copy-php` - builds the code for production and opts into copying all PHP files from the `src` directory and its subfolders to the output directory. By default, only PHP files listed in the `render` field in the detected `block.json` files get copied.
- `npm run build:copy-php` - builds the code for production and opts into copying all PHP files from the `src` directory and its subfolders to the output directory. By default, only PHP files listed in the `render` and `variations` fields in the detected `block.json` files get copied.
- `npm run build:custom-directory` - builds the code for production using the `custom-directory` as the source code directory.

This script automatically use the optimized config but sometimes you may want to specify some custom options:
Expand Down Expand Up @@ -382,7 +382,7 @@ This is how you execute the script with presented setup:
- `npm start` - starts the build for development.
- `npm run start:hot` - starts the build for development with "Fast Refresh". The page will automatically reload if you make changes to the files.
- `npm run start:custom` - starts the build for development which contains two entry points and a custom output directory. Paths for custom entry points are relative to the project root.
- `npm run start:copy-php` - starts the build for development and opts into copying all PHP files from the `src` directory and its subfolders to the output directory. By default, only PHP files listed in the `render` field in the detected `block.json` files get copied.
- `npm run start:copy-php` - starts the build for development and opts into copying all PHP files from the `src` directory and its subfolders to the output directory. By default, only PHP files listed in the `render` and `variations` fields in the detected `block.json` files get copied.
- `npm run start:custom-directory` - builds the code for production using the `custom-directory` as the source code directory.

This script automatically use the optimized config but sometimes you may want to specify some custom options:
Expand Down
38 changes: 30 additions & 8 deletions packages/scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const RtlCssPlugin = require( 'rtlcss-webpack-plugin' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const { realpathSync } = require( 'fs' );
const { sync: glob } = require( 'fast-glob' );
const { validate } = require( 'schema-utils' );

/**
* WordPress dependencies
Expand All @@ -31,7 +32,7 @@ const {
hasPostCSSConfig,
getWordPressSrcDirectory,
getWebpackEntryPoints,
getRenderPropPaths,
getPhpFilePaths,
getAsBooleanFromENV,
getBlockJsonModuleFields,
getBlockJsonScriptFields,
Expand All @@ -49,24 +50,45 @@ const hasExperimentalModulesFlag = getAsBooleanFromENV(
'WP_EXPERIMENTAL_MODULES'
);

const phpFilePathsPluginSchema = {
type: 'object',
properties: {
props: {
type: 'array',
items: {
type: 'string',
},
},
},
};

/**
* The plugin recomputes the render paths once on each compilation. It is necessary to avoid repeating processing
* The plugin recomputes PHP file paths once on each compilation. It is necessary to avoid repeating processing
* when filtering every discovered PHP file in the source folder. This is the most performant way to ensure that
* changes in `block.json` files are picked up in watch mode.
*/
class RenderPathsPlugin {
class PhpFilePathsPlugin {
/**
* Paths with the `render` props included in `block.json` files.
* PHP file paths from `render` and `variations` props found in `block.json` files.
*
* @type {string[]}
*/
static renderPaths;
static paths;

constructor( options = {} ) {
validate( phpFilePathsPluginSchema, options, {
name: 'PHP File Paths Plugin',
baseDataPath: 'options',
} );

this.options = options;
}

apply( compiler ) {
const pluginName = this.constructor.name;

compiler.hooks.thisCompilation.tap( pluginName, () => {
this.constructor.renderPaths = getRenderPropPaths();
this.constructor.paths = getPhpFilePaths( this.options.props );
} );
}
}
Expand Down Expand Up @@ -321,7 +343,7 @@ const scriptConfig = {
cleanStaleWebpackAssets: false,
} ),

new RenderPathsPlugin(),
new PhpFilePathsPlugin( { props: [ 'render', 'variations' ] } ),
ockham marked this conversation as resolved.
Show resolved Hide resolved
new CopyWebpackPlugin( {
patterns: [
{
Expand Down Expand Up @@ -371,7 +393,7 @@ const scriptConfig = {
filter: ( filepath ) => {
return (
process.env.WP_COPY_PHP_FILES_TO_DIST ||
RenderPathsPlugin.renderPaths.includes(
PhpFilePathsPlugin.paths.includes(
realpathSync( filepath ).replace( /\\/g, '/' )
)
);
Expand Down
Loading
Loading