-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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: Include Core blocks' render
and variations
files
#63311
Changes from all commits
4487adf
7dee693
8dc7a58
f1d7830
8f7b0c2
1edc3b0
5a635e9
aaa23d3
030c3c0
8a4fdaf
916ab9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const { validate } = require( 'schema-utils' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { getPhpFilePaths } = require( './config' ); | ||
|
||
const phpFilePathsPluginSchema = { | ||
type: 'object', | ||
properties: { | ||
context: { | ||
type: 'string', | ||
}, | ||
props: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
/** | ||
* 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 PhpFilePathsPlugin { | ||
/** | ||
* PHP file paths from `render` and `variations` props found in `block.json` files. | ||
* | ||
* @type {string[]} | ||
*/ | ||
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.paths = getPhpFilePaths( | ||
this.options.context, | ||
this.options.props | ||
); | ||
} ); | ||
} | ||
} | ||
|
||
module.exports = { PhpFilePathsPlugin }; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,11 +4,13 @@ | |||||||||||||||||
const CopyWebpackPlugin = require( 'copy-webpack-plugin' ); | ||||||||||||||||||
const { join, sep } = 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 | ||||||||||||||||||
|
@@ -90,6 +92,10 @@ module.exports = [ | |||||||||||||||||
plugins: [ | ||||||||||||||||||
...plugins, | ||||||||||||||||||
new DependencyExtractionWebpackPlugin( { injectPolyfill: false } ), | ||||||||||||||||||
new PhpFilePathsPlugin( { | ||||||||||||||||||
context: './packages/block-library/src/', | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably add the other paths used as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's an internal plugin that will never be used outside so I don't think we need to spend more time on it if it works as is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see what you did. It's also imported to use within the Gutenberg plugin. Hmm, that complicates it a little bit. In general, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I meant with my comment is that the gutenberg/tools/webpack/blocks.js Lines 121 to 128 in 18164f0
AFAICS, it's just three blocks total across those two directories, but in theory, they could at some point include a On the downside, this would make the logic in
Hmm, are you saying we shouldn't altogether be doing what we're doing in this PR? AFAICS, we have some instances where we're doing "deep" imports from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deep imports work perfectly fine. It’s manageable inside the Gutenberg monorepo as breaking changes have a high chance of bubbling up during the development. However it should be discouraged for usage through npm. In effect, we should never support requests to make it backward compatible on that level. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Got it! Initially, I didn’t think about it because I assumed these changes apply exclusively to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I'd be more than happy to land this without further ado. If anything, I was trying to gauge if this has the potential of breaking things, but I guess that chance is pretty minimal. I'll set the PR as ready for review. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll try to give it a round of testing this week before approving it. |
||||||||||||||||||
props: [ 'render', 'variations' ], | ||||||||||||||||||
} ), | ||||||||||||||||||
new CopyWebpackPlugin( { | ||||||||||||||||||
patterns: [].concat( | ||||||||||||||||||
[ | ||||||||||||||||||
|
@@ -127,17 +133,32 @@ module.exports = [ | |||||||||||||||||
'build/widgets/blocks/', | ||||||||||||||||||
} ).flatMap( ( [ from, to ] ) => [ | ||||||||||||||||||
{ | ||||||||||||||||||
from: `${ from }/**/index.php`, | ||||||||||||||||||
from: `${ from }/**/*.php`, | ||||||||||||||||||
to( { absoluteFilename } ) { | ||||||||||||||||||
const [ , dirname ] = absoluteFilename.match( | ||||||||||||||||||
new RegExp( | ||||||||||||||||||
`([\\w-]+)${ escapeRegExp( | ||||||||||||||||||
sep | ||||||||||||||||||
) }index\\.php$` | ||||||||||||||||||
const [ , dirname, basename ] = | ||||||||||||||||||
absoluteFilename.match( | ||||||||||||||||||
new RegExp( | ||||||||||||||||||
`([\\w-]+)${ escapeRegExp( | ||||||||||||||||||
sep | ||||||||||||||||||
) }([\\w-]+)\\.php$` | ||||||||||||||||||
) | ||||||||||||||||||
); | ||||||||||||||||||
|
||||||||||||||||||
if ( basename === 'index' ) { | ||||||||||||||||||
return join( to, `${ dirname }.php` ); | ||||||||||||||||||
} | ||||||||||||||||||
return join( to, dirname, `${ basename }.php` ); | ||||||||||||||||||
}, | ||||||||||||||||||
filter: ( filepath ) => { | ||||||||||||||||||
return ( | ||||||||||||||||||
filepath.endsWith( sep + 'index.php' ) || | ||||||||||||||||||
PhpFilePathsPlugin.paths.includes( | ||||||||||||||||||
realpathSync( filepath ).replace( | ||||||||||||||||||
/\\/g, | ||||||||||||||||||
'/' | ||||||||||||||||||
) | ||||||||||||||||||
) | ||||||||||||||||||
); | ||||||||||||||||||
|
||||||||||||||||||
return join( to, `${ dirname }.php` ); | ||||||||||||||||||
}, | ||||||||||||||||||
transform: ( content ) => { | ||||||||||||||||||
const prefix = 'gutenberg_'; | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably need to change this so there won't be any collisions between class instances.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm being overly cautious here. My concern is basically that if these paths are stored as a
static
class variable, they might get polluted. However, I'm not sure if the latter is realistic. While I can think ofnpm run dev
running in parallel with another job that builds some 3rd-party package, that might not be enough to pollute class-level vars.(OTOH, making it non-static -- in the context of a Webpack plugin -- seems to be non-trivial.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It worked exactly the same before and no issues were reported. As far as I understand it, the webpack config is executed only once when you run
wp-scripts start
. It's when the plugin get instatiated, all subsequent changes to paths happen through th webpack lifecycle events.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't used for Core blocks -- i.e. Gutenberg code itself -- before, so my concern was vaguely that e.g. running the GB build in watch mode might collide with running a wp-scripts command to build a third-party block.
Yeah, I guess we should be fine 🤔