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
Merged
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
41 changes: 40 additions & 1 deletion bin/packages/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,43 @@ function createStyleEntryTransform() {
} );
}

/**
* Returns a stream transform which maps an individual block.json to the
* index.js that imports it. Presently, babel resolves the import of json
* files by inlining them as a JavaScript primitive in the importing file.
* This transform ensures the importing file is rebuilt.
*
* @return {Transform} Stream transform instance.
*/
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

const blocks = new Set;

return new Transform( {
objectMode: true,
async transform( file, encoding, callback ) {
const matches = /block-library[\/\\]src[\/\\](.*)[\/\\]block.json$/.exec( file );
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 );
this.push( file.replace( 'block.json', 'index.js' ) );
callback();
},
} );
}

let onFileComplete = () => {};

let stream;
Expand All @@ -72,7 +109,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,
Expand Down