gulp-task-maker
A small wrapper for gulp, which helps you separate task configuration (objects) and implementation (functions).
Some advantages:
- Creates
watch
tasks automatically. - Helps you write tasks with sourcemaps, and better logging and system notifications!
- Reuse the same task with different configuration objects, to create multiple builds.
⚠ Requires Node.js 6.5 or later.
Install
Install gulp
and gulp-task-maker
as devDependencies
:
npm install --save-dev gulp@4 gulp-task-maker@2
Configure
Then in your gulpfile.js
, use gulp-task-maker’s add
method:
const gtm = const test = { console tools} gtm
With this configuration, gulp-task-maker will create four gulp tasks:
build
(runs allbuild_*
tasks)watch
(runs allwatch_*
tasks)build_test
watch_test
We can run them on the command line, for example using npx
(which comes with recent versions of Node and/or npm
):
# run the "parent" build task $ npx gulp build# run a specific task $ npx gulp build_test
Let’s go back to our gulpfile.js
. We could also:
- tell
gulp-task-maker
to load theminifyJS
function from a node script, - provide an array of config objects, instead of just one,
- and add any arbitrary config values we want on config objects.
Here is an updated example:
gtm
Writing tasks
With the previous gulpfile.js
, gulp-task-maker will load the module at ./tasks/minifyJS.js
(or ./tasks/minifyJS/index.js
). This module should export a function (and that function must be named, because we’re using its name for the gulp tasks’ names).
Here is a function that concatenates JS files, minifies the result, and writes it (plus a source map) to a destination folder.
const concat = const uglify = module { return tools}
If you’re used to gulp, you can see that we’re not using gulp.src
and gulp.dest
. Instead, we’re using tools.simpleStream
which does this work for us, supports source maps, and logs file sizes. If we want the same result with gulp only, we have to write:
const gulp = const concat = const plumber = const sourcemaps = const size = const uglify = module { // take some source files return gulp // use gulp-plumber to log errors (to console + notifications) // start source maps // concatenate files // minify JS // log resulting file names and size // generate sourcemaps // write resulting files to a directory }
This is a bit longer, as you can see.
Running tasks
Finally we can run the gulp command, and get a console output that looks like this:
$ npx gulp build[13:37:21] Using gulpfile ~/Code/my-project/gulpfile.js[13:37:21] Starting 'default'...[13:37:21] Starting 'build_minifyJS'...[13:37:22] ./public/ main.js 88.97 kB[13:37:22] Finished 'build_minifyJS' after 1.12 s[13:37:22] Finished 'build' after 1.13 s
You could also run a specific build task, which can be useful when you have many:
$ npx gulp build_minifyJS...
Or start the main watch
task:
$ npx gulp watch[13:37:49] Using gulpfile ~/Code/my-project/gulpfile.js[13:37:49] Starting 'watch'...[13:37:49] Starting 'watch_minifyJS'...
Full API doc, debugging and more
For a complete guide on using gulp-task-maker
’s API, see the API docs.