11/**
2- * Task: blocks
2+ * Task: blocks.
33 */
44
55// Node
@@ -18,18 +18,21 @@ const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
1818const webpackStream = require ( 'webpack-stream' ) ;
1919const wpWebpackConfig = require ( '@wordpress/scripts/config/webpack.config' ) ;
2020
21- // GulpWP
21+ // Internal
2222const { c, changed, handleStreamError, log, logFiles } = require ( '../util' ) ;
2323
24- const blockAssets = [ 'script' , 'style' , 'editorScript' , 'editorStyle' , 'viewScript' ] ;
24+ const blockAssets = [
25+ 'script' ,
26+ 'style' ,
27+ 'editorScript' ,
28+ 'editorStyle' ,
29+ 'viewScript' ,
30+ ] ;
2531
2632module . exports = {
2733 task : ( gulp , { src, entries, dest, includePaths } ) => {
28- return function blocks ( done ) {
29- const filterOthers = filter ( [
30- '*.{php,json}' ,
31- '!*.asset.php' ,
32- ] ) ;
34+ return function blocks ( ) {
35+ const filterOthers = filter ( [ '*.{php,json}' , '!*.asset.php' ] ) ;
3336
3437 /**
3538 * Overwrites pieces of the default webpack config.
@@ -70,7 +73,7 @@ module.exports = {
7073 use : use . map ( ( u ) => {
7174 const { loader, options } = u ;
7275 if (
73- loader ==
76+ loader ===
7477 require . resolve ( 'sass-loader' )
7578 ) {
7679 return {
@@ -101,65 +104,83 @@ module.exports = {
101104 } ,
102105 plugins : [
103106 new RemoveEmptyScriptsPlugin ( ) ,
104- ...( ( wpWebpackConfig ?. plugins || [ ] ) . filter ( plugin => ! ( plugin instanceof CopyWebpackPlugin ) ) ) ,
107+ ...( wpWebpackConfig ?. plugins || [ ] ) . filter (
108+ ( plugin ) => ! ( plugin instanceof CopyWebpackPlugin )
109+ ) ,
105110 ] ,
106111 devtool : 'source-map' ,
107112 } ;
108113 // Remove config props that may interfere with webpackStream
109- delete webpackConfig [ ' entry' ] ;
114+ delete webpackConfig . entry ;
110115
111116 /**
112117 * Reads block.json files for block assets.
113118 *
114119 * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#assets
115120 */
116- const streamBlockAssets = through2 . obj ( function filterBlockAssetEntries ( file , enc , cb ) {
117- log . debug ( file . path ) ;
118- // Parse block.json for asset files.
119- const data = JSON . parse ( file . contents . toString ( ) ) ;
120- if ( data ) {
121- // Check each potential blockAsset prop.
122- for ( const key of blockAssets ) {
123- if ( data . hasOwnProperty ( key ) ) {
124- // Value can be string or array, ensure array for consistency.
125- if ( ! Array . isArray ( data [ key ] ) ) {
126- data [ key ] = [ data [ key ] ] ;
127- }
121+ const streamBlockAssets = through2 . obj (
122+ function filterBlockAssetEntries ( file , enc , cb ) {
123+ log . debug ( file . path ) ;
124+ // Parse block.json for asset files.
125+ const data = JSON . parse ( file . contents . toString ( ) ) ;
126+ if ( data ) {
127+ // Check each potential blockAsset prop.
128+ for ( const key of blockAssets ) {
129+ if ( data . hasOwnProperty ( key ) ) {
130+ // Value can be string or array, ensure array for consistency.
131+ if ( ! Array . isArray ( data [ key ] ) ) {
132+ data [ key ] = [ data [ key ] ] ;
133+ }
128134
129- for ( const asset of data [ key ] ) {
130- if ( asset . startsWith ( 'file:' ) ) {
131- const assetFile = asset . substring ( 5 ) ;
132- log . debug ( 'block asset:' , c . blue ( assetFile ) ) ;
133- let assetPath = dirname ( file . path ) ;
134- // If asset is js, glob for js, ts, jsx, or tsx.
135- if ( assetFile . endsWith ( 'js' ) ) {
136- assetPath += `/${ assetFile . slice ( 0 , - 2 ) } {j,t}{s,sx}` ;
137- }
138- // If asset is css, glob for sass, scss, or css.
139- else if ( assetFile . endsWith ( 'css' ) ) {
140- assetPath += `/${ assetFile . slice ( 0 , - 3 ) } {sa,sc,c}ss` ;
141- }
142- // If neither, just pass the file through, consequences be damned.
143- else {
144- assetPath += `/${ assetFile } ` ;
135+ for ( const asset of data [ key ] ) {
136+ if ( asset . startsWith ( 'file:' ) ) {
137+ const assetFile = asset . substring ( 5 ) ;
138+ log . debug (
139+ 'block asset:' ,
140+ c . blue ( assetFile )
141+ ) ;
142+ let assetPath = dirname ( file . path ) ;
143+ // If asset is js, glob for js, ts, jsx, or tsx.
144+ if ( assetFile . endsWith ( 'js' ) ) {
145+ assetPath += `/${ assetFile . slice (
146+ 0 ,
147+ - 2
148+ ) } {j,t}{s,sx}`;
149+ }
150+ // If asset is css, glob for sass, scss, or css.
151+ else if (
152+ assetFile . endsWith ( 'css' )
153+ ) {
154+ assetPath += `/${ assetFile . slice (
155+ 0 ,
156+ - 3
157+ ) } {sa,sc,c}ss`;
158+ }
159+ // If neither, just pass the file through, consequences be damned.
160+ else {
161+ assetPath += `/${ assetFile } ` ;
162+ }
163+ vinylRead
164+ . sync ( assetPath )
165+ . map ( ( f ) => this . push ( f ) ) ;
145166 }
146- vinylRead . sync ( assetPath ) . map ( f => this . push ( f ) ) ;
147167 }
148168 }
149169 }
170+
171+ // End processing and remove this file from the stream.
172+ return cb ( ) ;
150173 }
151174
152- // End processing and remove this file from the stream .
153- return cb ( ) ;
175+ // Pass the file through .
176+ return cb ( null , file ) ;
154177 }
155-
156- // Pass the file through.
157- return cb ( null , file ) ;
158- } ) ;
178+ ) ;
159179
160180 return merge (
161181 // Handle block assets.
162- gulp . src ( entries )
182+ gulp
183+ . src ( entries )
163184 . pipe ( handleStreamError ( 'blocks' ) )
164185 // Filter out all but entrypoints.
165186 . pipe ( streamBlockAssets )
@@ -178,7 +199,8 @@ module.exports = {
178199 . pipe ( webpackStream ( webpackConfig , webpack ) )
179200 . pipe ( gulp . dest ( dest ) ) ,
180201 // Handle moving other files (php, json)
181- gulp . src ( src )
202+ gulp
203+ . src ( src )
182204 . pipe ( handleStreamError ( 'blocks' ) )
183205 . pipe ( filterOthers )
184206 . pipe ( changed ( gulp . lastRun ( blocks ) ) )
0 commit comments