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

Build: Stop generating unused legacy scripts for core blocks #65268

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
43 changes: 1 addition & 42 deletions tools/webpack/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,18 @@
*/
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const { join, sep, basename } = require( 'path' );
const fastGlob = require( 'fast-glob' );
const { realpathSync } = require( 'fs' );

/**
* WordPress dependencies
*/
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
const { PhpFilePathsPlugin } = require( '@wordpress/scripts/utils' );

/**
* Internal dependencies
*/
const { baseConfig, plugins, stylesTransform } = require( './shared' );

/*
* Matches a block's filepaths in the form build-module/<filename>.js
*/
const blockViewRegex = new RegExp(
/build-module\/(?<filename>.*\/view.*).js$/
);

/**
* We need to automatically rename some functions when they are called inside block files,
* but have been declared elsewhere. This way we can call Gutenberg override functions, but
Expand All @@ -50,48 +41,16 @@ function escapeRegExp( string ) {
return string.replace( /[\\^$.*+?()[\]{}|]/g, '\\$&' );
}

const createEntrypoints = () => {
/*
* Returns an array of paths to block view files within the `@wordpress/block-library` package.
* These paths can be matched by the regex `blockViewRegex` in order to extract
* the block's filename.
*
* Returns an empty array if no files were found.
*/
const blockViewScriptPaths = fastGlob.sync(
'./packages/block-library/build-module/**/view*.js'
);

/*
* Go through the paths found above, in order to define webpack entry points for
* each block's view.js file.
*/
return blockViewScriptPaths.reduce( ( entries, scriptPath ) => {
const result = scriptPath.match( blockViewRegex );
if ( ! result?.groups?.filename ) {
return entries;
}

return {
...entries,
[ result.groups.filename ]: scriptPath,
};
}, {} );
};

module.exports = [
{
...baseConfig,
name: 'blocks',
entry: createEntrypoints(),
entry: {},
output: {
devtoolNamespace: 'wp',
filename: './build/block-library/blocks/[name].min.js',
path: join( __dirname, '..', '..' ),
},
plugins: [
...plugins,
new DependencyExtractionWebpackPlugin( { injectPolyfill: false } ),
new PhpFilePathsPlugin( {
context: './packages/block-library/src/',
props: [ 'render', 'variations' ],
Expand Down
Loading