How to Pass Command Line Parameters to Gulp Tasks
I’m a big fan of Gulp.js and recent surveys indicate almost 44% of front-end developers are using Gulp today.
Gulp’s simplicity is one of its most attractive features. You write a task function in your gulpfile.js
:
gulp.task('doSomething', () => {
// do something
});
then execute that task from the command line with gulp doSomething
. Tasks can be as basic or complex as you like and include further sub-tasks.
However, it’s not possible to pass arguments on the command line which can be used by that task, e.g.
gulp doSomething --option1 "my string" --option2 123 --option3
(where option3
would equate to true
)
The arguments would be passed to the Gulp application itself – not your task. Gulp knows nothing about these values so they’re not made available within gulpfile.js
and cannot be examined or used within your task functions.
Do Gulp Tasks Need Arguments?
Generally not – otherwise, a facility to pass arguments to tasks would have been added many years ago! Gulp tasks are written in JavaScript so you can set value defaults within your code.
You can also analyze environment variables such as NODE_ENV. For example, you can check whether the value is set to production
or similar on a live server. The setting can then be used to determine whether JavaScript source files are minified when the task runs, e.g.
// is this a development build?
const devBuild = ((process.env.NODE_ENV || 'development').trim().toLowerCase() === 'development');
// Gulp plugins
const
stripdebug = require('gulp-strip-debug'), // remove debugging code
uglify = require('gulp-uglify'); // minify
// build JavaScript
gulp.task('js', () => {
let jsbuild = gulp.src('src/js/*')
.pipe(some-plugin1())
.pipe(some-plugin2());
// production server tasks
if (!devBuild) {
jsbuild = jsbuild
.pipe(stripdebug())
.pipe(uglify());
}
return jsbuild.pipe(gulp.dest('build/js/'));
});
You can now set export NODE_ENV=production
on Linux/Mac or set NODE_ENV=production
on Windows prior to running the gulp js
task. It will then remove console.log
and debugger
statements before minifying your JavaScript files.
Finally, if you want a task to do something slightly different, you can create a new task. Tasks can be chained together to run in sequence as necessary, for example:
gulp.task('doSomething1', () => {
return gulp.src('src/*')
.pipe(some-plugin1())
.pipe(gulp.dest('build/'));
});
// run doSomething1 first
gulp.task('doSomething2', [doSomething1], () => {
// do something else
return gulp.src('src/*')
.pipe(some-plugin2())
.pipe(gulp.dest('build/'));
});
Running gulp doSomething1
will execute the first task. Running gulp doSomething2
will execute both tasks in order because doSomething1
is defined as a dependency in the optional array following the task name.
Should We Ever Consider Arguments?
Arguments should be avoided when there are better alternative options. Your --option1
argument could become a valid command-line option in the next release of Gulp and have undesired consequences.
That said, there are always edge cases…
1. Passwords and security
You should normally avoid hard-coding credentials such as IDs and passwords into gulpfile.js
. Consider the following task which deploys files to a server via FTP using the vinyl-ftp plug-in:
Continue reading %How to Pass Command Line Parameters to Gulp Tasks%
LEAVE A REPLY
You must be logged in to post a comment.