Top Related Projects
Grunt: The JavaScript Task Runner
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
Next-generation ES module bundler
Next generation frontend tooling. It's fast!
Quick Overview
Gulp is a popular task runner and build system for JavaScript projects. It automates repetitive tasks in development workflows, such as minification, compilation, unit testing, and linting, using a streaming build system that's fast and efficient.
Pros
- Easy to learn and use, with a simple API and straightforward configuration
- Highly flexible and extensible through a vast ecosystem of plugins
- Efficient performance due to its streaming-based approach, which minimizes I/O operations
- Active community and regular updates, ensuring compatibility with modern web development practices
Cons
- Some users find the configuration process verbose compared to other build tools
- Debugging can be challenging, especially when dealing with complex task pipelines
- Occasional compatibility issues between different plugin versions
- Learning curve for creating custom plugins or complex task configurations
Code Examples
- Basic task definition:
const { src, dest } = require('gulp');
function copyHtml() {
return src('src/*.html')
.pipe(dest('dist'));
}
exports.copyHtml = copyHtml;
This example defines a simple task that copies HTML files from the src
directory to the dist
directory.
- Using plugins for file processing:
const { src, dest } = require('gulp');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
function minifyJs() {
return src('src/*.js')
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(dest('dist'));
}
exports.minifyJs = minifyJs;
This task minifies JavaScript files using the gulp-uglify
plugin and renames them with a .min
suffix.
- Watch task for automatic file processing:
const { watch, series } = require('gulp');
function watchFiles() {
watch('src/*.js', series(lintJs, minifyJs));
watch('src/*.css', compileCss);
}
exports.watch = watchFiles;
This example sets up a watch task that automatically runs linting and minification for JavaScript files, and compilation for CSS files when changes are detected.
Getting Started
To get started with Gulp, follow these steps:
-
Install Gulp globally:
npm install --global gulp-cli
-
Initialize a new project and install Gulp locally:
npm init -y npm install --save-dev gulp
-
Create a
gulpfile.js
in your project root:const { src, dest, watch, series } = require('gulp'); function defaultTask(cb) { console.log('Gulp is running!'); cb(); } exports.default = defaultTask;
-
Run Gulp:
gulp
This will execute the default task, printing "Gulp is running!" to the console.
Competitor Comparisons
Grunt: The JavaScript Task Runner
Pros of Grunt
- Configuration-based approach, which can be easier for beginners
- Extensive plugin ecosystem with a wide range of tasks available
- Well-established and mature project with a large community
Cons of Grunt
- Configuration files can become large and complex for bigger projects
- Slower execution compared to Gulp due to I/O operations
- Less flexibility in terms of task orchestration and file manipulation
Code Comparison
Grunt configuration:
grunt.initConfig({
concat: {
dist: {
src: ['src/*.js'],
dest: 'dist/built.js',
},
},
});
Gulp task:
gulp.task('concat', function() {
return gulp.src('src/*.js')
.pipe(concat('built.js'))
.pipe(gulp.dest('dist'));
});
Grunt uses a declarative configuration approach, while Gulp employs a more programmatic, stream-based method. Gulp's code is often more concise and easier to read, especially for complex tasks. Grunt's configuration can become verbose but provides a clear structure for defining tasks and their options.
Both tools are capable of handling most front-end build tasks, but Gulp generally offers better performance and flexibility. The choice between them often comes down to personal preference, project requirements, and team expertise.
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
Pros of webpack
- Built-in code splitting and module bundling
- Hot Module Replacement for faster development
- Extensive plugin ecosystem for advanced optimizations
Cons of webpack
- Steeper learning curve, especially for complex configurations
- Can be slower for simple projects compared to Gulp's streaming approach
- Configuration can become complex for large projects
Code Comparison
webpack (webpack.config.js):
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
};
Gulp (gulpfile.js):
gulp.task('scripts', function() {
return gulp.src('src/**/*.js')
.pipe(concat('all.js'))
.pipe(gulp.dest('dist'));
});
webpack focuses on module bundling and provides a more comprehensive build system out of the box. It excels at handling complex JavaScript applications with dependencies. Gulp, on the other hand, uses a simpler task-based approach and is more flexible for general-purpose build tasks.
webpack is better suited for modern JavaScript applications, especially those using frameworks like React or Vue. Gulp remains a solid choice for simpler projects or when you need more control over the build process.
Both tools have active communities and extensive documentation, making them viable options depending on your project requirements and complexity.
Next-generation ES module bundler
Pros of Rollup
- Specializes in bundling JavaScript modules, offering better tree-shaking and dead code elimination
- Produces smaller bundle sizes, especially for ES6 modules
- Supports code splitting and dynamic imports out of the box
Cons of Rollup
- Less flexible for non-JavaScript tasks compared to Gulp's streaming-based approach
- Steeper learning curve for complex configurations
- Limited plugin ecosystem compared to Gulp's extensive collection
Code Comparison
Gulp configuration:
const gulp = require('gulp');
const uglify = require('gulp-uglify');
gulp.task('minify', () => {
return gulp.src('src/*.js')
.pipe(uglify())
.pipe(gulp.dest('dist'));
});
Rollup configuration:
import { rollup } from 'rollup';
import uglify from 'rollup-plugin-uglify';
export default {
input: 'src/main.js',
output: { file: 'dist/bundle.js', format: 'iife' },
plugins: [uglify()]
};
Gulp excels at general-purpose task automation and handling various file types, while Rollup focuses on efficient JavaScript module bundling. Gulp's streaming approach allows for more flexibility in file processing, whereas Rollup offers superior tree-shaking and bundle optimization for modern JavaScript projects. The choice between the two depends on project requirements and the specific tasks needed in the build process.
Next generation frontend tooling. It's fast!
Pros of Vite
- Faster build times due to native ES modules and esbuild
- Built-in hot module replacement (HMR) for improved development experience
- Simpler configuration with sensible defaults
Cons of Vite
- Less mature ecosystem compared to Gulp's extensive plugin library
- Limited support for older browsers without additional configuration
- Primarily focused on modern web development, less versatile for non-web tasks
Code Comparison
Gulp task:
gulp.task('styles', () => {
return gulp.src('src/styles/*.scss')
.pipe(sass())
.pipe(autoprefixer())
.pipe(gulp.dest('dist/css'));
});
Vite configuration:
export default {
plugins: [
sass(),
autoprefixer()
]
}
Summary
Vite offers a more modern and faster development experience, particularly for JavaScript-heavy projects, while Gulp provides a more flexible and established ecosystem for various build tasks. Vite excels in quick startup times and HMR, making it ideal for modern web applications. Gulp, on the other hand, offers greater versatility and a wider range of plugins, making it suitable for diverse project types and legacy systems.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
The streaming build system
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.
Installation
Follow our Quick Start guide.
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 and 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.
var gulp = require('gulp');
var less = require('gulp-less');
var babel = require('gulp-babel');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var cleanCSS = require('gulp-clean-css');
var del = require('del');
var paths = {
styles: {
src: 'src/styles/**/*.less',
dest: 'assets/styles/'
},
scripts: {
src: 'src/scripts/**/*.js',
dest: 'assets/scripts/'
}
};
/* Not all tasks need to use streams, a gulpfile is just another node program
* and you can use all packages available on npm, but it must return either a
* Promise, a Stream or take a callback and call it
*/
function clean() {
// You can use multiple globbing patterns as you would with `gulp.src`,
// for example if you are using del 2.0 or above, return its promise
return del([ 'assets' ]);
}
/*
* Define our tasks using plain functions
*/
function styles() {
return gulp.src(paths.styles.src)
.pipe(less())
.pipe(cleanCSS())
// pass in options to the stream
.pipe(rename({
basename: 'main',
suffix: '.min'
}))
.pipe(gulp.dest(paths.styles.dest));
}
function scripts() {
return gulp.src(paths.scripts.src, { sourcemaps: true })
.pipe(babel())
.pipe(uglify())
.pipe(concat('main.min.js'))
.pipe(gulp.dest(paths.scripts.dest));
}
function watch() {
gulp.watch(paths.scripts.src, scripts);
gulp.watch(paths.styles.src, styles);
}
/*
* Specify if tasks run in series or parallel using `gulp.series` and `gulp.parallel`
*/
var build = gulp.series(clean, gulp.parallel(styles, scripts));
/*
* You can use CommonJS `exports` module notation to declare tasks
*/
exports.clean = clean;
exports.styles = styles;
exports.scripts = scripts;
exports.watch = watch;
exports.build = build;
/*
* Define default task that can be called by just running `gulp` from cli
*/
exports.default = build;
Use latest JavaScript version in your gulpfile
Gulp provides a wrapper that will be loaded in your ESM code, so you can name your gulpfile as gulpfile.mjs
or with "type": "module"
specified in your package.json
file.
And here's the same sample from above written in ESNext.
import { src, dest, watch } from 'gulp';
import less from 'gulp-less';
import babel from 'gulp-babel';
import concat from 'gulp-concat';
import uglify from 'gulp-uglify';
import rename from 'gulp-rename';
import cleanCSS from 'gulp-clean-css';
import del from 'del';
const paths = {
styles: {
src: 'src/styles/**/*.less',
dest: 'assets/styles/'
},
scripts: {
src: 'src/scripts/**/*.js',
dest: 'assets/scripts/'
}
};
/*
* For small tasks you can export arrow functions
*/
export const clean = () => del([ 'assets' ]);
/*
* You can also declare named functions and export them as tasks
*/
export function styles() {
return src(paths.styles.src)
.pipe(less())
.pipe(cleanCSS())
// pass in options to the stream
.pipe(rename({
basename: 'main',
suffix: '.min'
}))
.pipe(dest(paths.styles.dest));
}
export function scripts() {
return src(paths.scripts.src, { sourcemaps: true })
.pipe(babel())
.pipe(uglify())
.pipe(concat('main.min.js'))
.pipe(dest(paths.scripts.dest));
}
/*
* You could even use `export as` to rename exported tasks
*/
function watchFiles() {
watch(paths.scripts.src, scripts);
watch(paths.styles.src, styles);
}
export { watchFiles as watch };
const build = gulp.series(clean, gulp.parallel(styles, scripts));
/*
* Export a default task
*/
export default build;
Incremental Builds
You can filter out unchanged files between runs of a task using
the gulp.src
function's since
option and gulp.lastRun
:
const paths = {
...
images: {
src: 'src/images/**/*.{jpg,jpeg,png}',
dest: 'build/img/'
}
}
function images() {
return gulp.src(paths.images.src, {since: gulp.lastRun(images)})
.pipe(imagemin())
.pipe(gulp.dest(paths.images.dest));
}
function watch() {
gulp.watch(paths.images.src, images);
}
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!
Top Related Projects
Grunt: The JavaScript Task Runner
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
Next-generation ES module bundler
Next generation frontend tooling. It's fast!
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot