Gulp

A toolkit to automate & enhance your workflow

README

The streaming build system

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Azure Pipelines Build Status][azure-pipelines-image]][azure-pipelines-url] [![Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![OpenCollective Backers][backer-badge]][backer-url] [![OpenCollective Sponsors][sponsor-badge]][sponsor-url] [![Gitter chat][gitter-image]][gitter-url]


What is gulp?


- Automation - gulp is a toolkit that helps you automate painful or time-consuming tasks in your development workflow.
- Platform-agnostic - Integrations are built into all major IDEs and people are using gulp with PHP, .NET, Node.js, Java, and other platforms.
- Strong Ecosystem - Use npm modules to do anything you want + over 3000 curated plugins for streaming file transformations.
- Simple - By providing only a minimal API surface, gulp is easy to learn and simple to use.

What's new in 4.0?!


The task system was rewritten from the ground-up, allowing task composition using series() and parallel() methods.
The watcher was updated, now using chokidar (no more need for gulp-watch!), with feature parity to our task system.
First-class support was added for incremental builds using lastRun().
A symlink() method was exposed to create symlinks instead of copying files.
Built-in support for sourcemaps was added - the gulp-sourcemaps plugin is no longer necessary!
Task registration of exported functions - using node or ES exports - is now recommended.
Custom registries were designed, allowing for shared tasks or augmented functionality.
Stream implementations were improved, allowing for better conditional and phased builds.


gulp for enterprise


Available as part of the Tidelift Subscription

The maintainers of gulp and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.


Installation


Follow our [Quick Start guide][quick-start].

Roadmap


Find out about all our work-in-progress and outstanding issues at https://github.com/orgs/gulpjs/projects.

Documentation


Check out the [Getting Started guide][getting-started-guide] and [API docs][api-docs] on our website!

__Excuse our dust! All other docs will be behind until we get everything updated. Please open an issue if something isn't working.__

Sample gulpfile.js


This file will give you a taste of what gulp does.

  1. ``` js
  2. var gulp = require('gulp');
  3. var less = require('gulp-less');
  4. var babel = require('gulp-babel');
  5. var concat = require('gulp-concat');
  6. var uglify = require('gulp-uglify');
  7. var rename = require('gulp-rename');
  8. var cleanCSS = require('gulp-clean-css');
  9. var del = require('del');

  10. var paths = {
  11.   styles: {
  12.     src: 'src/styles/**/*.less',
  13.     dest: 'assets/styles/'
  14.   },
  15.   scripts: {
  16.     src: 'src/scripts/**/*.js',
  17.     dest: 'assets/scripts/'
  18.   }
  19. };

  20. /* Not all tasks need to use streams, a gulpfile is just another node program
  21. * and you can use all packages available on npm, but it must return either a
  22. * Promise, a Stream or take a callback and call it
  23. */
  24. function clean() {
  25.   // You can use multiple globbing patterns as you would with `gulp.src`,
  26.   // for example if you are using del 2.0 or above, return its promise
  27.   return del([ 'assets' ]);
  28. }

  29. /*
  30. * Define our tasks using plain functions
  31. */
  32. function styles() {
  33.   return gulp.src(paths.styles.src)
  34.     .pipe(less())
  35.     .pipe(cleanCSS())
  36.     // pass in options to the stream
  37.     .pipe(rename({
  38.       basename: 'main',
  39.       suffix: '.min'
  40.     }))
  41.     .pipe(gulp.dest(paths.styles.dest));
  42. }

  43. function scripts() {
  44.   return gulp.src(paths.scripts.src, { sourcemaps: true })
  45.     .pipe(babel())
  46.     .pipe(uglify())
  47.     .pipe(concat('main.min.js'))
  48.     .pipe(gulp.dest(paths.scripts.dest));
  49. }

  50. function watch() {
  51.   gulp.watch(paths.scripts.src, scripts);
  52.   gulp.watch(paths.styles.src, styles);
  53. }

  54. /*
  55. * Specify if tasks run in series or parallel using `gulp.series` and `gulp.parallel`
  56. */
  57. var build = gulp.series(clean, gulp.parallel(styles, scripts));

  58. /*
  59. * You can use CommonJS `exports` module notation to declare tasks
  60. */
  61. exports.clean = clean;
  62. exports.styles = styles;
  63. exports.scripts = scripts;
  64. exports.watch = watch;
  65. exports.build = build;
  66. /*
  67. * Define default task that can be called by just running `gulp` from cli
  68. */
  69. exports.default = build;
  70. ```

Use latest JavaScript version in your gulpfile


__Most new versions of node support most features that Babel provides, except the import/export syntax. When only that syntax is desired, rename to gulpfile.esm.js, install the [esm][esm-module] module, and skip the Babel portion below.__

Node already supports a lot of __ES2015+__ features, but to avoid compatibility problems we suggest to install Babel and rename your gulpfile.js to gulpfile.babel.js.

  1. ```sh
  2. npm install --save-dev @babel/register @babel/core @babel/preset-env
  3. ```

Then create a .babelrc file with the preset configuration.

  1. ``` js
  2. {
  3.   "presets": [ "@babel/preset-env" ]
  4. }
  5. ```

And here's the same sample from above written in ES2015+.

  1. ``` js
  2. import gulp from 'gulp';
  3. import less from 'gulp-less';
  4. import babel from 'gulp-babel';
  5. import concat from 'gulp-concat';
  6. import uglify from 'gulp-uglify';
  7. import rename from 'gulp-rename';
  8. import cleanCSS from 'gulp-clean-css';
  9. import del from 'del';

  10. const paths = {
  11.   styles: {
  12.     src: 'src/styles/**/*.less',
  13.     dest: 'assets/styles/'
  14.   },
  15.   scripts: {
  16.     src: 'src/scripts/**/*.js',
  17.     dest: 'assets/scripts/'
  18.   }
  19. };

  20. /*
  21. * For small tasks you can export arrow functions
  22. */
  23. export const clean = () => del([ 'assets' ]);

  24. /*
  25. * You can also declare named functions and export them as tasks
  26. */
  27. export function styles() {
  28.   return gulp.src(paths.styles.src)
  29.     .pipe(less())
  30.     .pipe(cleanCSS())
  31.     // pass in options to the stream
  32.     .pipe(rename({
  33.       basename: 'main',
  34.       suffix: '.min'
  35.     }))
  36.     .pipe(gulp.dest(paths.styles.dest));
  37. }

  38. export function scripts() {
  39.   return gulp.src(paths.scripts.src, { sourcemaps: true })
  40.     .pipe(babel())
  41.     .pipe(uglify())
  42.     .pipe(concat('main.min.js'))
  43.     .pipe(gulp.dest(paths.scripts.dest));
  44. }

  45. /*
  46.   * You could even use `export as` to rename exported tasks
  47.   */
  48. function watchFiles() {
  49.   gulp.watch(paths.scripts.src, scripts);
  50.   gulp.watch(paths.styles.src, styles);
  51. }
  52. export { watchFiles as watch };

  53. const build = gulp.series(clean, gulp.parallel(styles, scripts));
  54. /*
  55. * Export a default task
  56. */
  57. export default build;
  58. ```

Incremental Builds


You can filter out unchanged files between runs of a task using
the gulp.src function's since option and gulp.lastRun:
  1. ``` js
  2. const paths = {
  3.   ...
  4.   images: {
  5.     src: 'src/images/**/*.{jpg,jpeg,png}',
  6.     dest: 'build/img/'
  7.   }
  8. }

  9. function images() {
  10.   return gulp.src(paths.images.src, {since: gulp.lastRun(images)})
  11.     .pipe(imagemin())
  12.     .pipe(gulp.dest(paths.images.dest));
  13. }

  14. function watch() {
  15.   gulp.watch(paths.images.src, images);
  16. }
  17. ```
Task run times are saved in memory and are lost when gulp exits. It will only
save time during the watch task when running the images task
for a second time.

Want to contribute?


Anyone can help make this project better - check out our Contributing guide!

Backers


Support us with a monthly donation and help us continue our activities.
[![Backers][backers-image]][support-url]

Sponsors


Become a sponsor to get your logo on our README on Github.
[![Sponsors][sponsors-image]][support-url]

[downloads-image]: https://img.shields.io/npm/dm/gulp.svg
[npm-url]: https://www.npmjs.com/package/gulp
[npm-image]: https://img.shields.io/npm/v/gulp.svg

[azure-pipelines-url]: https://dev.azure.com/gulpjs/gulp/_build/latest?definitionId=1&branchName=master
[azure-pipelines-image]: https://dev.azure.com/gulpjs/gulp/_apis/build/status/gulp?branchName=master

[travis-url]: https://travis-ci.org/gulpjs/gulp
[travis-image]: https://img.shields.io/travis/gulpjs/gulp.svg?label=travis-ci

[appveyor-url]: https://ci.appveyor.com/project/gulpjs/gulp
[appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/gulp.svg?label=appveyor

[coveralls-url]: https://coveralls.io/r/gulpjs/gulp
[coveralls-image]: https://img.shields.io/coveralls/gulpjs/gulp/master.svg

[gitter-url]: https://gitter.im/gulpjs/gulp
[gitter-image]: https://badges.gitter.im/gulpjs/gulp.svg

[backer-url]: #backers
[backer-badge]: https://opencollective.com/gulpjs/backers/badge.svg?color=blue
[sponsor-url]: #sponsors
[sponsor-badge]: https://opencollective.com/gulpjs/sponsors/badge.svg?color=blue

[support-url]: https://opencollective.com/gulpjs#support

[backers-image]: https://opencollective.com/gulpjs/backers.svg
[sponsors-image]: https://opencollective.com/gulpjs/sponsors.svg

[quick-start]: https://gulpjs.com/docs/en/getting-started/quick-start
[getting-started-guide]: https://gulpjs.com/docs/en/getting-started/quick-start
[api-docs]: https://gulpjs.com/docs/en/api/concepts
[esm-module]: https://github.com/standard-things/esm