Gulp.js
include : gulpfile.js
: The configuration file where developers define their tasks and workflows using Gulp APIs. gulpfile.js
. These tasks can encompass various actions like file concatenation, minification, compilation, etc.gulp-sass
for compiling Sass to CSS, gulp-uglify
for JavaScript minification, etc.gulpfile.js
.gulp.src()
is used to define the source files or file patterns to be processed.gulp.dest()
specifies the destination folder for the processed files..pipe()
connect tasks together.gulp.src('src/*.js')
.pipe(concat('bundle.js'))
.pipe(uglify())
.pipe(gulp.dest('dist'));​
dist
directory.
gulp taskName
) or set to run automatically when file changes are detected (gulp.watch()
).gulp.series()
and gulp.parallel()
to execute tasks sequentially or concurrently, as per the defined workflow requirements.gulp-plumber
can be used to manage errors and keep the workflow intact. npm install --global gulp-cli​
npm install --save-dev gulp​
gulp.task('default', function() {
// place code for your default task here
});​
Gulp​
Node.js
and npm. Then, globally install Gulp using the command ‘npm install gulp -g
’. Create a new directory for your project and navigate into it. Initialize a new npm project with ‘npm init
’ and follow the prompts to create a package.json
file. Install Gulp locally in your project using ‘npm install gulp –save-dev
’, which adds Gulp as a devDependency in your package.json
file.‘const gulp = require(‘gulp’);
’. Now, you can start defining tasks. For example, to define a default task that logs ‘Hello, World!
’, write:gulp.task('default', function() {
console.log('Hello, World!');
});​
gulp
’ in your terminal. gulp.task()
` method. gulp.task('taskName', function() {
// Task code here
});​
npm uninstall gulp​
This removes Gulp from your project's node_modules
folder and updates your package.json
file to remove the Gulp dependency.node_modules
folder entirely if you no longer need any of the project dependencies:rm -rf node_modules # For Unix-based systems like macOS or Linux
rmdir /s /q node_modules # For Windows (Command Prompt)​
npm uninstall -g gulp​
prune
command to remove packages not listed in your package.json
:npm prune​
node_modules
that are not listed as dependencies in your package.json
.package.json
file and that you can no longer access Gulp commands in your project or system. gulp
’ module for basic functionality, ‘uglify
’ to minify JS files, or ‘sass
’ to compile SASS files.src(), dest(), watch()
, and series()
or parallel()
.gulp.watch()
method which takes two arguments – file path pattern and tasks to run.gulp.default()
method with an array of tasks as argument. gulp-plumber
’ plugin is commonly used to prevent pipe breaking caused by errors from gulp plugins. It replaces pipe method and removes standard onerror handler on ‘error
’ event, outputting errors directly to console.error
’ event listener to each stream in your pipeline. This allows you to handle errors at a granular level, providing more control over how they’re handled. var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task('sass', function () {
return gulp.src('./scss/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./css'));
});​
error
’ event listener to the Sass compiler. If an error occurs during compilation, it’s logged to the console but doesn’t break the entire build process. gulp.src()
and gulp.dest()
are methods used to handle file input and output within tasks. They are essential for defining the source files to process and specifying the destination for the processed files.gulp.src()
gulp.src()
is used to define the source files or file patterns that need to be processed by Gulp.gulp.src('source/*.js')​
paths/patterns
as an argument.gulp.dest()
gulp.dest()
is used to specify the destination folder where the processed files will be outputted.gulp.dest('dist/js')​
gulp.dest()
receives these modified files and writes them to the specified destination directory.const gulp = require('gulp');
const uglify = require('gulp-uglify');
gulp.task('minify-js', function() {
return gulp.src('src/*.js') // Selecting source JS files
.pipe(uglify()) // Minifying the files
.pipe(gulp.dest('dist/js')); // Saving the minified files to the dist/js folder
});​
gulp.src('src/*.js')
specifies the source folder and file pattern for JavaScript files to be processed..pipe(uglify())
applies the uglify()
plugin to minify the JavaScript files..pipe(gulp.dest('dist/js'))
writes the minified files to the dist/js
folder.gulp.src()
and gulp.dest()
form the core of Gulp's file manipulation process, allowing you to define input files, apply transformations, and output the processed files to specified destinations within your Gulp tasks. gulpfile.js
’ at the root of your project where you define tasks. gulp.task('task-name', function() { // Task actions here });
. You can automate tasks like minification, compilation, unit testing, linting etc., by including respective plugins such as gulp-uglify for JS minification or gulp-sass for SASS compilation.<task-name>
in your terminal. Tasks can be chained together using gulp.series()
or gulp.parallel()
methods allowing them to run sequentially or simultaneously.gulp.task('sass', function() {
return gulp.src('app/scss/**/*.scss') // Gets all files ending with .scss
.pipe(sass())
.pipe(gulp.dest('app/css'))
});
gulp.task('minify', function(){
return gulp.src('app/css/*.css')
.pipe(uglify())
.pipe(gulp.dest('dist'));
});
gulp.task('default', gulp.series('sass', 'minify'));​
gulpfile.js
’ at the root of your project.gulp-uglify
’ plugin via npm. In ‘gulpfile.js
’, require gulp and gulp-uglify. Define a task named ‘minify-js
’ that sources all JS files, pipes them through uglify(
), and outputs minified versions into a destination folder.gulp-clean-css
’ plugin. Similar to JS, define a ‘minify-css
’ task that sources all CSS files, pipes them through cleanCSS()
, and outputs minified versions.minify-js
’ and ‘minify-css
’. Run ‘gulp
’ command in terminal to execute these tasks. This process reduces file sizes, improving load times and performance. packages/libraries/dependencies
into your application. Gulp requires Node and its package manager, npm (Node Package Manager) which is responsible for installing the gulp plugins.
src()
and dest()
methods for interacting with files on your computer. When src()
is given a glob to read from the file system, it returns a Node stream. It finds all matched files and reads them into memory so that they can be passed over the stream.const { src, dest } = require('gulp');
exports.default = function() {
return src('src/*.js')
.pipe(dest('output/'));
}​
Gulp.js
offers several advantages that distinguish it from other task runners like Grunt
or npm
scripts :gulp.series()
and gulp.parallel()
.Using gulp.series()
: gulp.series()
allows you to run tasks sequentially, one after the other. Tasks defined within gulp.series()
will execute in the order they are listed.const gulp = require('gulp');
gulp.task('taskA', function() {
// Task A code here
});
gulp.task('taskB', function() {
// Task B code here
});
gulp.task('taskC', gulp.series('taskA', 'taskB', function() {
// Task C code here
}));​
Using gulp.parallel()
: gulp.parallel()
allows tasks to run concurrently, simultaneously executing multiple tasks.const gulp = require('gulp');
gulp.task('taskA', function() {
// Task A code here
});
gulp.task('taskB', function() {
// Task B code here
});
gulp.task('taskC', gulp.parallel('taskA', 'taskB', function() {
// Task C code here
}));​
gulp.series()
and gulp.parallel()
to create complex dependency trees, allowing for more intricate task execution orders.const gulp = require('gulp');
gulp.task('taskA', function() {
// Task A code here
});
gulp.task('taskB', function() {
// Task B code here
});
gulp.task('taskC', gulp.series('taskA', gulp.parallel('taskB', function() {
// Task C code here
})));​
gulp.series()
and gulp.parallel()
effectively, you can establish clear dependencies between tasks, ensuring that they run in the required sequence or concurrently as needed within your Gulp workflow. gulp-sass
for SASS files and gulp-less
for LESS files.gulp.src()
, pipe it through sass()
and then through gulp.dest()
.var gulp = require('gulp');
var sass = require('gulp-sass');
function styleSass() {
return gulp.src('./sass/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./css'));
}
exports.style = styleSass;​
npm install –save-dev gulp @babel/core @babel/preset-env gulp-babel
’. Create a ‘gulpfile.js
’ in your project root directory. In the file, require both Gulp and Babel.const { src, dest } = require('gulp');
const babel = require('gulp-babel');
function transpile() {
return src('src/**/*.js')
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(dest('dist'));
}
exports.default = transpile;​
.js
’ files from the ‘src
’ directory, transpile them using Babel, and output the results into the ‘dist
’ directory. Run the task using ‘gulp
’ command in terminal. gulp.series()
.gulp.parallel()
.gulp.series()
or gulp.parallel()
.const gulp = require('gulp');
gulp.task('taskA', function() {
// Task A code here
});
gulp.task('taskB', function() {
// Task B code here
});
gulp.task('taskC', gulp.series('taskA', 'taskB', function() {
// Task C code here
}));​
gulp.task('taskC', gulp.series('taskA', gulp.parallel('taskB', function() {
// Task C code here
})));​
gulp.series()
and gulp.parallel()
to establish complex relationships between tasks.gulp.series()
and gulp.parallel()
are methods in Gulp that help manage task execution and dependencies within the Gulp workflow.gulp.series()
is used to define a sequence of tasks that should run one after another, ensuring a sequential execution order.const gulp = require('gulp');
gulp.task('taskA', function() {
// Task A code here
});
gulp.task('taskB', function() {
// Task B code here
});
gulp.task('taskC', gulp.series('taskA', 'taskB', function() {
// Task C code here
}));​
taskC
depends on both taskA
and taskB
. When gulp.task('taskC')
is run, Gulp will execute taskA first, followed by taskB
, and finally, the code within the function defined for taskC
.
gulp.parallel()
is used to define tasks that can run simultaneously, allowing for concurrent execution.taskD
will initiate both taskA and taskB simultaneously and execute its own function code after both taskA
and taskB
have completed.gulp.series()
and gulp.parallel()
to create more complex task execution sequences:gulp.task('taskE', gulp.series('taskA', gulp.parallel('taskB', 'taskC'), function() {
// Task E code here
}));​
taskE
will first execute taskA
, then concurrently run taskB
and taskC
, and finally execute its own function code.gulp.series()
ensures tasks execute in a specific order, while gulp.parallel()
enables concurrent execution when tasks don't have explicit dependencies.gulp.series()
and gulp.parallel()
are crucial for managing task dependencies, enabling developers to create organized, efficient, and controlled workflows within their Gulp tasks. npm install --save-dev gulp gulp-imagemin​
gulpfile.js
’ at your project root. Inside this file, require both Gulp and gulp-imagemin:var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
gulp.task('images', function(){
return gulp.src('src/images/*')
.pipe(imagemin())
.pipe(gulp.dest('dist/images'))
});​​
images
’ task from the command line with gulp images. This will start the image optimization process. gulp-clean-css
’ plugin using the command npm install gulp-clean-css --save-dev
.gulpfile.js
like so: var cleanCSS = require('gulp-clean-css');
. Then, you can pipe it into a task. Here’s an example :var gulp = require('gulp');
var cleanCSS = require('gulp-clean-css');
gulp.task('minify-css', function() {
return gulp.src('styles/*.css')
.pipe(cleanCSS({compatibility: 'ie8'}))
.pipe(gulp.dest('dist'));
});​
minify-css
’. It takes any CSS file in the ‘styles
’ directory, pipes it through the ‘cleanCSS
’ plugin to minify it, then outputs the result to the ‘dist
’ directory. const gulp = require('gulp');
const sass = require('gulp-sass');
gulp.task('styles', function() {
return gulp.src('src/scss/*.scss')
.pipe(sass())
.pipe(gulp.dest('dist/css'));
});
gulp.task('scripts', function() {
return gulp.src('src/js/*.js')
.pipe(concat('bundle.js'))
.pipe(uglify())
.pipe(gulp.dest('dist/js'));
});
// More task definitions...
// Default task
gulp.task('default', gulp.series('styles', 'scripts'));​
gulpfile.js
defines tasks like styles
and scripts
that compile Sass
to CSS
, concatenate and minify JavaScript
files, and a default task that runs both styles
and scripts
in sequence. This demonstrates how tasks are defined and organized within the gulpfile.js
. Gulp
and its plugins
. Create a gulpfile.js
at the root of your project directory. Inside this file, require Gulp and any necessary plugins. Define tasks that perform actions on your files such as compiling or minifying.gulp.watch
’ method. This takes two arguments: the path to the files to monitor, and the tasks to run when those files change. For example:var gulp = require('gulp');
gulp.task('watch', function() {
gulp.watch('src/js/*.js', ['jshint']);
});​
src/js
’ directory. When a change is detected, it runs the ‘jshint
’ task.gulp watch
’. BrowserSync
for live reloading to enhance development efficiency. To achieve this, first install both Gulp and BrowserSync
using npm (Node Package Manager).gulpfile.js
’ at the root of your project directory. This file will contain tasks that Gulp will execute. Define a task named ‘serve
’ in gulpfile.js
. In this task, initialize BrowserSync
with server setup pointing to the base directory of your project.reload
’. This task should call the reload function of BrowserSync
whenever changes are detected in your files. Use Gulp’s watch method to monitor changes in your HTML, CSS,
or JS files
. When any change is detected, it triggers the ‘reload
’ task causing BrowserSync
to refresh all connected browsers.var gulp = require('gulp');
var browserSync = require('browser-sync').create();
gulp.task('serve', function(done) {
browserSync.init({
server: "./"
});
done();
});
gulp.task('reload', function (done) {
browserSync.reload();
done();
});
gulp.task('watch', gulp.series('serve', function () {
gulp.watch("./*.html", gulp.series('reload'));
}));​
gulp-debug
: This plugin logs the files going through the pipeline.gulp-plumber
’: It prevents pipe breaking caused by errors from gulp plugins.Node.js
built-in debugger or third-party tools like WebStorm or Visual Studio Code for debugging tasks.gulp-notify
’ and ‘gulp-util
’ to get error notifications in your operating system.console.log()
to understand what’s happening inside your tasks. Gulp 3.x
used an implicit dependency system where the order of execution wasn’t guaranteed.Gulp 4.x
which introduced explicit task functions and series/parallel methods for controlling task execution order. For instance, if ‘task1
’ should complete before ‘task2
’, it would be written as gulp.series(‘task1
’, ‘task2
’). This ensured that tasks ran in the correct sequence, resolving the issue. Continuous Integration (CI)
process involves automating tasks and ensuring they run seamlessly within your CI environment. Here's how you can integrate Gulp.js into a CI workflow:Jenkins, Travis CI, CircleCI, GitHub Actions, etc
., based on your project requirements.e.g., .travis.yml, circleci/config.yml, etc.
) within your project repository.gulp <taskName>
) to run specific tasks required for building, testing, or preparing your project..travis.yml
example:language: node_js
node_js:
- "14"
install:
- npm install
script:
- gulp build # Execute Gulp tasks for building
# Other configurations for deployment, notifications, etc.​
gulpfile.js
. Then, create a task that uses the gulp.src method to get the test files and pipe them into the mocha function.child_process
module to spawn a new process running Jest. In your gulpfile.js
, create a task that calls child_process.spawnSync(‘jest’)
, passing any necessary arguments. gulp.task()
. For instance, if ‘js
’ depends on ‘clean
’, we write: gulp.task(‘js’, [‘clean’], function() {…})
. This ensures ‘clean
’ runs before ‘js
’.series()
and parallel()
methods. The series()
method executes tasks sequentially, while parallel()
runs them concurrently. An example would be: gulp.series(‘task1’, ‘task2’)
or gulp.parallel(‘task1’, ‘task2’)
.src()
method to specify the entry point file. Use Browserify’s bundle()
method followed by Gulp’s dest()
method to output the bundled file.webpack()
, passing it your configuration object. Again, use Gulp’s src()
and dest()
methods for input and output respectively. Both these setups allow you to run ‘gulp’ in your terminal to execute the bundling task. process.env
’ object. To set an environment variable, use ‘export VARNAME=value
’ in Unix or ‘set VARNAME=value
’ in Windows before running Gulp. In your Gulpfile.js, access this with ‘process.env.VARNAME
’.gulp.task('taskname', function() {
if(process.env.NODE_ENV === 'production') {
// Production behavior
} else {
// Development behavior
}
});​
NODE_ENV=production gulp taskname
’ for production and simply ‘gulp taskname
’ for development. This allows you to customize tasks based on the environment. gulp-environments
’ plugin. This allows you to define tasks specific to each environment like development, testing, and production.npm: npm install --save-dev gulp-environments
.var gulp = require('gulp');
var environments = require('gulp-environments');
var development = environments.development;
var production = environments.production;​
gulp.task('scripts', function() {
gulp.src('src/js/*.js')
.pipe(production(uglify()))
.pipe(gulp.dest('dist/js'));
});​
NODE_ENV
variable before running Gulp: NODE_ENV=production gulp scripts
.