npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-task-maker

v2.0.0

Published

Helper for writing and running configurable gulp tasks. Separates task logic and configuration, allows multiple builds using the same task, and uses system notifications for configuration and plugin errors.

Downloads

47

Readme

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 = require('gulp-task-maker')
const test = (config, tools) => {
  console.log(config)
  tools.done()
}

gtm.add(test, {
  src: './src/*.js',
  watch: true,
  dest: './public',
})

With this configuration, gulp-task-maker will create four gulp tasks:

  • build (runs all build_* tasks)
  • watch (runs all watch_* 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 the minifyJS 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.add('./tasks/minifyJS', [
  {
    src: [
      'node_modules/jquery/dist/jquery.js',
      'node_modules/some-other-lib/dist/some-other-lib.js'
    ],
    dest: 'public',
    bundle: 'vendor.js',
    sourcemaps: '.'
  },
  {
    src: 'src/*.js',
    watch: true,
    dest: 'public',
    bundle: 'bundle.js',
    sourcemaps: '.'
  }
])

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 = require('gulp-concat')
const uglify = require('gulp-uglify')

module.exports = function minifyJS(config, tools) {
  return tools.simpleStream(config, [
    concat(config.bundle), // concatenate files
    uglify() // minify JS
  ])
}

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 = require('gulp')
const concat = require('gulp-concat')
const plumber = require('gulp-plumber')
const sourcemaps = require('gulp-sourcemaps')
const size = require('gulp-size')
const uglify = require('gulp-uglify')

module.exports = function minjs(config, tools) {
  // take some source files
  return gulp.src(config.src)
    // use gulp-plumber to log errors (to console + notifications)
    .pipe(tools.catchErrors())
    // start source maps
    .pipe(sourcemaps.init())
    // concatenate files
    .pipe(concat(config.bundle))
    // minify JS
    .pipe(uglify())
    // log resulting file names and size
    .pipe(size())
    // generate sourcemaps
    .pipe(sourcemaps.write(config.sourcemaps))
    // write resulting files to a directory
    .pipe(gulp.dest(config.dest))
}

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.