From d49efd6d6d5273491d221f39bfc8414e18850786 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Thu, 13 Jun 2019 17:05:46 +0800 Subject: [PATCH 1/6] Add support for watching block.json files when running `npm run dev` --- bin/packages/build-worker.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/bin/packages/build-worker.js b/bin/packages/build-worker.js index 7e3e636c013a1d..994b6a3b9b9d39 100644 --- a/bin/packages/build-worker.js +++ b/bin/packages/build-worker.js @@ -140,6 +140,21 @@ const BUILD_TASK_BY_EXTENSION = { ] ); } }, + + async '.json'( file ) { + // Currently, only building block library block.json files is supported. + // See https://github.com/WordPress/gutenberg/issues/16104 for further info. + if ( ! /block-library.*block\.json$/.test( file ) ) { + return; + } + + // The block.json file is inlined into the block's index.js when building. + // The following code rebuilds the index.js relative to the block.json so + // that this inlining happens again. + const blockIndexFile = file.replace( 'block.json', 'index.js' ); + const task = BUILD_TASK_BY_EXTENSION[ '.js' ]; + await task( blockIndexFile ); + }, }; module.exports = async ( file, callback ) => { From 6f3cc6691b7fad43709f709aaa076dd1f034b2ad Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Thu, 20 Jun 2019 12:01:49 +0200 Subject: [PATCH 2/6] Revert "Add support for watching block.json files when running `npm run dev`" This reverts commit d49efd6d6d5273491d221f39bfc8414e18850786. --- bin/packages/build-worker.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/bin/packages/build-worker.js b/bin/packages/build-worker.js index 994b6a3b9b9d39..7e3e636c013a1d 100644 --- a/bin/packages/build-worker.js +++ b/bin/packages/build-worker.js @@ -140,21 +140,6 @@ const BUILD_TASK_BY_EXTENSION = { ] ); } }, - - async '.json'( file ) { - // Currently, only building block library block.json files is supported. - // See https://github.com/WordPress/gutenberg/issues/16104 for further info. - if ( ! /block-library.*block\.json$/.test( file ) ) { - return; - } - - // The block.json file is inlined into the block's index.js when building. - // The following code rebuilds the index.js relative to the block.json so - // that this inlining happens again. - const blockIndexFile = file.replace( 'block.json', 'index.js' ); - const task = BUILD_TASK_BY_EXTENSION[ '.js' ]; - await task( blockIndexFile ); - }, }; module.exports = async ( file, callback ) => { From 40360e6f0462e87571df4d92a3e89b220e2dcc64 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Thu, 20 Jun 2019 12:02:37 +0200 Subject: [PATCH 3/6] Handle block.json transform in the build.js script --- bin/packages/build.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/bin/packages/build.js b/bin/packages/build.js index 9639f8b232f70e..78f0e31c41dadf 100755 --- a/bin/packages/build.js +++ b/bin/packages/build.js @@ -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 ); + 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, From 84acb60c914541724b1aae3f3818ad8c4d03e74f Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 26 Jun 2019 11:55:08 +0100 Subject: [PATCH 4/6] Add jsdoc comment --- bin/packages/build.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bin/packages/build.js b/bin/packages/build.js index 78f0e31c41dadf..22630bfe6ec011 100755 --- a/bin/packages/build.js +++ b/bin/packages/build.js @@ -64,6 +64,14 @@ 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() { const blocks = new Set; From ae184342879d71d68a4357bea0f8919a5604f56b Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 26 Jun 2019 12:03:11 +0100 Subject: [PATCH 5/6] Fix path matching on Windows --- bin/packages/build.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/packages/build.js b/bin/packages/build.js index 22630bfe6ec011..725173eff2dc5b 100755 --- a/bin/packages/build.js +++ b/bin/packages/build.js @@ -78,7 +78,7 @@ function createBlockJsonEntryTransform() { return new Transform( { objectMode: true, async transform( file, encoding, callback ) { - const matches = /block-library\/src\/(.*)\/block.json$/.exec( file ); + 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. From 780350c7ae889a28e7b99977f890c79f5ba43322 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 26 Jun 2019 13:06:39 +0100 Subject: [PATCH 6/6] Simplify block.json build transform --- bin/packages/build.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/packages/build.js b/bin/packages/build.js index 725173eff2dc5b..e1a6bd7142f80d 100755 --- a/bin/packages/build.js +++ b/bin/packages/build.js @@ -95,8 +95,7 @@ function createBlockJsonEntryTransform() { } blocks.add( blockName ); - const entries = await glob( path.resolve( PACKAGES_DIR, 'block-library/src/', blockName, 'index.js' ) ); - entries.forEach( ( entry ) => this.push( entry ) ); + this.push( file.replace( 'block.json', 'index.js' ) ); callback(); }, } );