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

Add support for watching block.json files when running npm run dev #16150

Merged
merged 6 commits into from
Jun 26, 2019

Conversation

talldan
Copy link
Contributor

@talldan talldan commented Jun 13, 2019

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?

  1. Run npm run dev
  2. Load the editor and add an empty paragraph block, save the post
  3. In the paragraph block's block.json, change the content attributes default to something else like "Hello, world!"
  4. Observe in the console that a webpack rebuild is triggered
  5. Reload the editor and click on the paragraph block you added earlier

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:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.

@talldan talldan added [Type] Bug An existing feature does not function as intended [Type] Build Tooling Issues or PRs related to build tooling labels Jun 13, 2019
@talldan talldan self-assigned this Jun 13, 2019
gziolo
gziolo previously approved these changes Jun 13, 2019
Copy link
Member

@gziolo gziolo left a 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.

@aduth
Copy link
Member

aduth commented Jun 13, 2019

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 build.js and not in build-worker.js is to reserve the worker as minimally executing the computationally-intensive task (the transformation), and for all other filesystem management to occur in build.js (over time becoming to be clear to be a requirement, as evidenced by findings in #15967 (comment)).

@gziolo
Copy link
Member

gziolo commented Jun 13, 2019

In #15920, the reason I had chosen to implement this in build.js and not in build-worker.js is to reserve the worker as minimally executing the computationally-intensive task (the transformation), and for all other filesystem management to occur in build.js (over time becoming to be clear to be a requirement, as evidenced by findings in #15967 (comment)).

I see what you mean after checking source code:

function createStyleEntryTransform() {
const packages = new Set;
return new Transform( {
objectMode: true,
async transform( file, encoding, callback ) {
// Only stylesheets are subject to this transform.
if ( path.extname( file ) !== '.scss' ) {
this.push( file );
callback();
return;
}
// Only operate once per package, assuming entries are common.
const packageName = getPackageName( file );
if ( packages.has( packageName ) ) {
callback();
return;
}
packages.add( packageName );
const entries = await glob( path.resolve( PACKAGES_DIR, packageName, 'src/*.scss' ) );
entries.forEach( ( entry ) => this.push( entry ) );
callback();
},
} );
}

I wasn't aware it was architected this way. Now, I see it, it makes perfect sense to move it to build.js.

@gziolo
Copy link
Member

gziolo commented Jun 18, 2019

@talldan do you plan to move logic according to what @aduth suggested? This error is really annoying when you actively work on adding new attributes to the block definition :)

@talldan
Copy link
Contributor Author

talldan commented Jun 19, 2019

@gziolo Yep, I'll take a look at that when I get a chance.

@talldan talldan added the [Status] In Progress Tracking issues with work in progress label Jun 19, 2019
@talldan talldan removed the [Status] In Progress Tracking issues with work in progress label Jun 20, 2019
@talldan talldan requested a review from gziolo June 20, 2019 10:13
@talldan talldan dismissed gziolo’s stale review June 20, 2019 10:13

No longer relevant

@talldan
Copy link
Contributor Author

talldan commented Jun 20, 2019

This is ready for review again—reimplemented using the transform, similar to how scss stylesheets are handled.

Copy link
Member

@gziolo gziolo left a 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 :)

@gziolo
Copy link
Member

gziolo commented Jun 24, 2019

@aduth - would you mind doing a sanity check before we merge?

return new Transform( {
objectMode: true,
async transform( file, encoding, callback ) {
const matches = /block-library\/src\/(.*)\/block.json$/.exec( file );
Copy link
Member

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 ))

Copy link
Contributor Author

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() {
Copy link
Member

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.

Copy link
Contributor Author

@talldan talldan Jun 26, 2019

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.

Copy link
Contributor Author

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

@talldan talldan merged commit 3fc62bb into master Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended [Type] Build Tooling Issues or PRs related to build tooling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Block.json files aren't rebuilt when running npm run dev
3 participants