-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add support for watching block.json files when running npm run dev
#16150
Add support for watching block.json files when running npm run dev
#16150
Conversation
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.
I can confirm that it solves the issue 🎉
I'm aware it isn't perfect but this should cover all the use cases we have. I don't see any immediate plans to use JSON files for anything but block.json
files.
This seems to be a similar problem to one we encountered previously with the stylesheets (#15920), and as such I'd prefer to see a consistent approach used amongst the two of them. I'm not sure how far down the path of abstraction we want to go, but to me these appear to fall under a common behavior of a normalization process, where a file may serve as proxy to trigger another to be built instead. In #15920, the reason I had chosen to implement this in |
I see what you mean after checking source code: gutenberg/bin/packages/build.js Lines 39 to 65 in d49efd6
I wasn't aware it was architected this way. Now, I see it, it makes perfect sense to move it to |
@gziolo Yep, I'll take a look at that when I get a chance. |
This is ready for review again—reimplemented using the transform, similar to how scss stylesheets are handled. |
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.
I tested again, it still works properly :)
@aduth - would you mind doing a sanity check before we merge? |
bin/packages/build.js
Outdated
return new Transform( { | ||
objectMode: true, | ||
async transform( file, encoding, callback ) { | ||
const matches = /block-library\/src\/(.*)\/block.json$/.exec( file ); |
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.
I'd be curious if this works in Windows, where file
may have a different slash.
See also: https://nodejs.org/api/path.html#path_path_sep
I wonder instead if we:
- ... aren't too specific about whether the file is located, just that it's
block.json
(path.basename( file ) === 'block.json'
) - ... consider the block name as the directory name in which the file is located (
path.dirname( file )
)
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.
Good catch, I've improved the regex to handle both back and forward slash as the directory separator. I think that will cover all cases.
@@ -64,6 +64,36 @@ function createStyleEntryTransform() { | |||
} ); | |||
} | |||
|
|||
function createBlockJsonEntryTransform() { |
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.
I realize we might be aiming for the most direct solution, but:
- There's a huge amount of code duplication between this and the above function, which could be generalized.
- We should document every function; in this case, explaining the purpose of the transform.
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.
I've added some documentation, cheers for pointing that out.
I'll push up a separate PR for exploring generalization of these transforms. I had an initial attempt, but I feel like it could go through some iteration and I wouldn't want to hold up this main PR.
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.
Here's the separate PR: #16317
Description
Fixes #16104
This is quite a simplistic fix, but gets the job done!
The lowdown is that currently, block.json content is inlined into the index.js file its imported from when babel builds the files. The watch task currently doesn't work, firstly as the
build-worker
file had no task specified for building a json file. Secondly, such a task would have to rebuild the index.js relative to the block.json so that the inlined json is replaced when rebuilding.This PR adds a new task that checks whether the file is a block.json file, and if so rebuilds the relative index.js file. It'll only handle block.json files in the block-library package, but helps solve a problem for most devs working on core blocks.
How has this been tested?
On this branch:
The paragraph block contains the default defined in step 3, demonstrating that the watch task worked.
When running npm run dev against master:
The paragraph block isn't updated with its new default
Types of changes
Bug fix (non-breaking change which fixes an issue)
Checklist: