Skip to content

Commit e2e879c

Browse files
authored
Chore/cleanup (#41)
1 parent d5fe96c commit e2e879c

File tree

16 files changed

+215
-172
lines changed

16 files changed

+215
-172
lines changed

index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* Gulp WP
2+
* Gulp WP.
33
*/
44

5-
// Load .env file
5+
// Load .env file.
66
const env = require( 'dotenv' ).config();
77
if ( process.env.NOTIFY === 'false' ) {
88
process.env.DISABLE_NOTIFIER = true;
@@ -26,20 +26,20 @@ function init( gulp, config = {} ) {
2626
const localConfig = loadConfig();
2727
config = Object.assign( {}, localConfig, config );
2828

29-
// Add env explicitly so that it can't be set via config
29+
// Add env explicitly so that it can't be set via config.
3030
config.env = {
3131
DEV_URL: 'http://localhost',
3232
...env.parsed,
3333
};
3434

35-
// If `plugin` not specified in config, check for style.css and then for a plugin entry point
35+
// If `plugin` not specified in config, check for style.css and then for a plugin entry point.
3636
if ( ! config.plugin ) {
3737
if ( ! isTheme() ) {
3838
config.plugin = getPluginFile();
3939
}
4040
}
4141

42-
// Load local tasks first so we can ignore tasks meant to be overridden
42+
// Load local tasks first so we can ignore tasks meant to be overridden.
4343
const localTaskFolder = config.taskFolder || 'gulp-wp';
4444
log.debug( 'Loading local tasks from', c.blue( localTaskFolder ) );
4545
const localTasks = loadTasks( localTaskFolder );
@@ -48,7 +48,7 @@ function init( gulp, config = {} ) {
4848
c.cyan( Object.keys( localTasks ).join( ' ' ) )
4949
);
5050

51-
// Load Gulp WP default tasks
51+
// Load Gulp WP core tasks.
5252
const gulpWPTasks = loadTasks(
5353
resolve( __dirname, 'tasks' ),
5454
Object.keys( localTasks )
@@ -60,14 +60,14 @@ function init( gulp, config = {} ) {
6060

6161
const tasks = Object.assign( {}, gulpWPTasks, localTasks );
6262

63-
// Register our custom registry
63+
// Register our custom registry.
6464
const gulpWP = new GulpWPRegistry( gulp, tasks, config );
6565
gulp.registry( gulpWP );
6666

6767
return gulpWP;
6868
}
6969

70-
// Expose GulpWPRegistry for advanced use
70+
// Expose GulpWPRegistry for advanced use.
7171
init.GulpWPRegistry = GulpWPRegistry;
7272

7373
module.exports = init;

lib/gulpfile.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/**
2-
* Default Gulpfile
2+
* Default Gulpfile.
33
*
44
* Used when running `gulp-wp` binary.
55
*/
66

7-
const gulp = require( 'gulp' );
8-
const gulpWP = require( '../' )( gulp );
7+
require( '../' )( require( 'gulp' ) );

lib/registry.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ const { DepGraph } = require( 'dependency-graph' );
1111
const { c, log } = require( '../util' );
1212
const { version } = require( '../package.json' );
1313

14-
const overwriteArrayMerge = ( destinationArray, sourceArray, options ) =>
15-
sourceArray;
14+
const overwriteArrayMerge = ( destinationArray, sourceArray ) => sourceArray;
1615

1716
/**
1817
* Custom Gulp task registry.
1918
*
2019
* @class
21-
* @param {Gulp} gulp Main gulp object
22-
* @param {object} tasks Tasks data
23-
* @param {object} config Configuration
20+
* @param {Object} gulp Main gulp object
21+
* @param {Object} tasks Tasks data
22+
* @param {Object} config Configuration
2423
*/
2524
class GulpWPRegistry extends DefaultRegistry {
2625
constructor( gulp, tasks = {}, config = {} ) {
@@ -50,8 +49,8 @@ class GulpWPRegistry extends DefaultRegistry {
5049
for ( const [ taskName, taskInfo ] of Object.entries( taskData ) ) {
5150
graph.addNode( taskName );
5251

53-
// Handle merging config early in case dependencies is a function
54-
// NOTE: array properties are overwritten, not concatenated
52+
// Handle merging config early in case `dependencies` is a function.
53+
// NOTE: array properties are overwritten, not concatenated!
5554
const taskConfig = ( config.tasks[ taskName ] = merge(
5655
taskInfo?.config || {},
5756
config.tasks[ taskName ] || {},
@@ -63,12 +62,12 @@ class GulpWPRegistry extends DefaultRegistry {
6362
log.debug( taskConfig );
6463

6564
const { dependencies = [] } = taskInfo;
66-
// `dependencies` can be a function that builds a dependency array dynamically
65+
// `dependencies` can be a function that builds a dependency array dynamically.
6766
const deps =
6867
typeof dependencies === 'function'
6968
? dependencies( taskConfig )
7069
: dependencies;
71-
// for each dependency, add as a node and mark the dependency link
70+
// For each dependency, add as a node and mark the dependency link.
7271
if ( Array.isArray( deps ) && deps.length ) {
7372
for ( const dep of deps ) {
7473
graph.addNode( dep );
@@ -80,9 +79,9 @@ class GulpWPRegistry extends DefaultRegistry {
8079
const taskOrder = graph.overallOrder();
8180
log.debug( 'taskOrder:', taskOrder );
8281

83-
// import default tasks
82+
// Import core tasks.
8483
for ( const taskName of taskOrder ) {
85-
// Check if task exists or if this is an errant dependency
84+
// Check if task exists or if this is an errant dependency.
8685
if ( ! taskData.hasOwnProperty( taskName ) ) {
8786
throw new Error(
8887
`Task "${ taskName }" does not exist, though marked as a dependency by these tasks: ${ graph.dependantsOf(
@@ -105,20 +104,18 @@ class GulpWPRegistry extends DefaultRegistry {
105104
task( taskName, taskFn( gulp, taskConfig, this ) );
106105
}
107106

108-
// Register default task
107+
// Register default task.
109108
task( 'default', this.get( 'dev' ) );
110109
}
111110

112-
//get(taskName) {}
111+
// get(taskName) {}
113112

114-
/*
115-
set( taskName, fn ) {
116-
const task = this._tasks[taskName] = fn.bind(this.context);
117-
return task;
118-
}
119-
*/
113+
// set( taskName, fn ) {
114+
// const task = this._tasks[taskName] = fn.bind(this.context);
115+
// return task;
116+
// }
120117

121-
//tasks() {}
118+
// tasks() {}
122119
}
123120

124121
module.exports = GulpWPRegistry;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"autoprefixer": "^10.4.14",
5050
"browser-sync": "^2.29.1",
5151
"color-support": "^1.1.3",
52+
"copy-webpack-plugin": "^11.0.0",
5253
"cross-spawn": "^7.0.3",
5354
"deepmerge": "^4.3.1",
5455
"del": "^6.1.1",
@@ -72,6 +73,7 @@
7273
"gulp-wp-pot": "^2.5.0",
7374
"gulp-zip": "^5.1.0",
7475
"gulplog": "^2.0.1",
76+
"merge-stream": "^2.0.0",
7577
"micromatch": "^4.0.5",
7678
"node-sass-json-importer": "^4.3.0",
7779
"resolve-bin": "^1.0.1",

tasks/blocks.js

Lines changed: 70 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Task: blocks
2+
* Task: blocks.
33
*/
44

55
// Node
@@ -18,18 +18,21 @@ const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
1818
const webpackStream = require( 'webpack-stream' );
1919
const wpWebpackConfig = require( '@wordpress/scripts/config/webpack.config' );
2020

21-
// GulpWP
21+
// Internal
2222
const { 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

2632
module.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 ) ) )

tasks/build.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Task: build
2+
* Task: build.
33
*/
44

55
module.exports = {
@@ -9,8 +9,7 @@ module.exports = {
99
preBuild: preBuildTasks,
1010
build: buildTasks,
1111
postBuild: postBuildTasks,
12-
},
13-
registry
12+
}
1413
) => {
1514
function maybeParallel( tasks ) {
1615
if ( ! Array.isArray( tasks ) || tasks.length === 0 ) {
@@ -20,7 +19,7 @@ module.exports = {
2019
return [ gulp.parallel( ...tasks ) ];
2120
}
2221

23-
// Build main series array conditionally
22+
// Build main series array conditionally.
2423
const buildSeries = [
2524
...maybeParallel( preBuildTasks ),
2625
...maybeParallel( buildTasks ),

tasks/clean.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* Task: clean
2+
* Task: clean.
33
*
4-
* Clean build folders
4+
* Cleans build folders.
55
*/
66

77
// External

tasks/dev.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
2-
* Task: dev
2+
* Task: dev.
33
*/
44

55
module.exports = {
6-
task: ( gulp, {}, registry ) => {
6+
task: ( gulp ) => {
77
const dev = gulp.series( 'build', 'serve', 'watch' );
88

99
return dev;

0 commit comments

Comments
 (0)