-
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
Changes from 3 commits
d49efd6
6f3cc66
40360e6
84acb60
ae18434
780350c
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 |
---|---|---|
|
@@ -64,6 +64,36 @@ function createStyleEntryTransform() { | |
} ); | ||
} | ||
|
||
function createBlockJsonEntryTransform() { | ||
const blocks = new Set; | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. I'd be curious if this works in Windows, where See also: https://nodejs.org/api/path.html#path_path_sep I wonder instead if we:
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. 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. |
||
const blockName = matches ? matches[ 1 ] : undefined; | ||
|
||
// Only block.json files in the block-library folder are subject to this transform. | ||
if ( ! blockName ) { | ||
this.push( file ); | ||
callback(); | ||
return; | ||
} | ||
|
||
// Only operate once per block, assuming entries are common. | ||
if ( blockName && blocks.has( blockName ) ) { | ||
callback(); | ||
return; | ||
} | ||
|
||
blocks.add( blockName ); | ||
const entries = await glob( path.resolve( PACKAGES_DIR, 'block-library/src/', blockName, 'index.js' ) ); | ||
entries.forEach( ( entry ) => this.push( entry ) ); | ||
callback(); | ||
}, | ||
} ); | ||
} | ||
|
||
let onFileComplete = () => {}; | ||
|
||
let stream; | ||
|
@@ -72,7 +102,9 @@ if ( files.length ) { | |
stream = new Readable( { encoding: 'utf8' } ); | ||
files.forEach( ( file ) => stream.push( file ) ); | ||
stream.push( null ); | ||
stream = stream.pipe( createStyleEntryTransform() ); | ||
stream = stream | ||
.pipe( createStyleEntryTransform() ) | ||
.pipe( createBlockJsonEntryTransform() ); | ||
} else { | ||
const bar = new ProgressBar( 'Build Progress: [:bar] :percent', { | ||
width: 30, | ||
|
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 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