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

Scan block.json to generate a list of files to be copied via CopyWebpackPlugin #39653

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/scripts/CHANGELOG.md
@@ -1,7 +1,7 @@
<!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->

## Unreleased

- Parse block.json files to look for a `files` property and generate a list of files to be copied from `src` to the output directory (`build` by default)([39653](https://github.com/WordPress/gutenberg/pull/39653)
## 22.2.0 (2022-03-11)

### Enhancement
Expand Down
5 changes: 2 additions & 3 deletions packages/scripts/config/webpack.config.js
Expand Up @@ -26,6 +26,7 @@ const {
hasCssnanoConfig,
hasPostCSSConfig,
getWebpackEntryPoints,
getFilesToCopy,
} = require( '../utils' );

const isProduction = process.env.NODE_ENV === 'production';
Expand All @@ -36,9 +37,7 @@ if ( ! browserslist.findConfig( '.' ) ) {
}
const hasReactFastRefresh = hasArgInCLI( '--hot' ) && ! isProduction;

const copyWebPackPattens = process.env.WP_COPY_PHP_FILES_TO_DIST
? '**/{block.json,*.php}'
: '**/block.json';
const copyWebPackPattens = getFilesToCopy();

const cssLoaders = [
{
Expand Down
39 changes: 39 additions & 0 deletions packages/scripts/utils/config.js
Expand Up @@ -284,7 +284,46 @@ function getWebpackEntryPoints() {
};
}

/**
* Generates a list of files to be copied from the src directory to the build directory based on a "files"
* property in block.json
*
* @return {string} The copy pattern passed to the CopyWebpackPlugin
*/
function getFilesToCopy() {
// Continue only if the `src` directory exists.
if ( ! hasProjectFile( 'src' ) ) {
return {};
}

// 2. Checks whether any block metadata files can be detected in the `src` directory.
// It scans all discovered files looking for JavaScript assets and converts them to entry points.
const blockMetadataFiles = glob( 'src/**/block.json', {
absolute: true,
} );

const filesArray = process.env.WP_COPY_PHP_FILES_TO_DIST
? [ 'block.json', '*.php' ]
: [ 'block.json' ];

if ( blockMetadataFiles.length > 0 ) {
const fileList = blockMetadataFiles.reduce(
( array, blockMetadataFile ) => {
const { files } = require( blockMetadataFile );
return files ? [ ...array, ...files.split( ',' ) ] : array;
},
filesArray
);

// Create the pattern to be used.
return `**/{${ fileList.join( ',' ) }}`;
}
// Return the default pattern
return `**/{${ filesArray.join( ',' ) }}`;
}

module.exports = {
getFilesToCopy,
getJestOverrideConfigFile,
getWebpackArgs,
getWebpackEntryPoints,
Expand Down
2 changes: 2 additions & 0 deletions packages/scripts/utils/index.js
Expand Up @@ -11,6 +11,7 @@ const {
spawnScript,
} = require( './cli' );
const {
getFilesToCopy,
getJestOverrideConfigFile,
getWebpackArgs,
getWebpackEntryPoints,
Expand All @@ -29,6 +30,7 @@ module.exports = {
getArgFromCLI,
getArgsFromCLI,
getFileArgsFromCLI,
getFilesToCopy,
getJestOverrideConfigFile,
getNodeArgsFromCLI,
getPackageProp,
Expand Down