Core points
gulpfile.js
process.argv
to create an object containing parameter values. gulpfile.js
Gulp's simplicity is one of its most attractive features. You can write task functions in
: gulpfile.js
gulp.task('doSomething', () => { // 执行某些操作 });
to execute the task from the command line. Tasks can be as simple or complex as you like, and can contain further subtasks. gulp doSomething
gulp doSomething --option1 "my string" --option2 123 --option3
equals option3
)true
and cannot be checked or used in your task functions. gulpfile.js
You can also analyze environment variables, such as
. For example, you can check if the value is set to production or similar on the live server. This setting can then be used to determine whether to shrink the JavaScript source file while the task is running, for example: NODE_ENV
// 这是开发版本吗? const devBuild = ((process.env.NODE_ENV || 'development').trim().toLowerCase() === 'development'); // Gulp 插件 const stripdebug = require('gulp-strip-debug'); // 删除调试代码 const uglify = require('gulp-uglify'); // 压缩 // 构建 JavaScript gulp.task('js', () => { let jsbuild = gulp.src('src/js/*') .pipe(some-plugin1()) .pipe(some-plugin2()); // 生产服务器任务 if (!devBuild) { jsbuild = jsbuild .pipe(stripdebug()) .pipe(uglify()); } return jsbuild.pipe(gulp.dest('build/js/')); });
on Linux/Mac, or gulp js
on Windows before running the export NODE_ENV=production
task. It then deletes the NODE_ENV=production
and console.log
statements before compressing the JavaScript file. debugger
gulp.task('doSomething', () => { // 执行某些操作 });
Run gulp doSomething1
The first task will be performed. Running gulp doSomething2
will perform both tasks in order, because doSomething1
is defined as a dependency in an optional array after the task name.
Avoid using parameters when there are better alternatives. Your --option1
parameter may become a valid command-line option in the next Gulp version with adverse consequences.
In other words, there are always some special circumstances...
You should generally avoid hard-code credentials such as IDs and passwords into gulpfile.js
. Consider the following task that uses the vinyl-ftp plugin to deploy files to the server:
gulp doSomething --option1 "my string" --option2 123 --option3
(Authentic, FTP is not a good way to deploy, but many developers are still using it, and it may be the only option on some hosts.)
There are several problems with this method:
gulp deploy
. This is not ideal for larger teams who want to control deployment time. gulpfile.js
to ensure that the deployment task is still valid. Gulp can be used for purposes other than typical website tasks. For example, you might have some common tasks to clear folders, create databases, transfer files, and more. Hard-coded content such as database or folder names will reduce the practicality of these tasks.
Imagine a complex task involving dozens of plugins. If it is not practical to split it into multiple subtasks, it can become difficult to edit gulpfile.js
directly to add configuration options before running the task.
You may think of more special circumstances (comments are welcome!)
property in process.argv
Node.js returns an array containing the process, script, and all command line parameters. For example, gulp task1 --a 123 --b "my string" --c
returns the following array (values may vary depending on your operating system and settings):
// 这是开发版本吗? const devBuild = ((process.env.NODE_ENV || 'development').trim().toLowerCase() === 'development'); // Gulp 插件 const stripdebug = require('gulp-strip-debug'); // 删除调试代码 const uglify = require('gulp-uglify'); // 压缩 // 构建 JavaScript gulp.task('js', () => { let jsbuild = gulp.src('src/js/*') .pipe(some-plugin1()) .pipe(some-plugin2()); // 生产服务器任务 if (!devBuild) { jsbuild = jsbuild .pipe(stripdebug()) .pipe(uglify()); } return jsbuild.pipe(gulp.dest('build/js/')); });
This array can be parsed in gulpfile.js
. The following code creates an object named arg
with parameter values:
gulp.task('doSomething1', () => { return gulp.src('src/*') .pipe(some-plugin1()) .pipe(gulp.dest('build/')); }); // 首先运行 doSomething1 gulp.task('doSomething2', ['doSomething1'], () => { // 执行其他操作 return gulp.src('src/*') .pipe(some-plugin2()) .pipe(gulp.dest('build/')); });
This function loops through the process.argv
array. When it encounters a value starting with one or more dashes, it creates a new named value in the arg
object, which is set to true
. When it encounters a value without a dash, it sets the previous named value (if available) to the string.
When we run gulp task1 --a 123 --b "my string" --c
, the arg
object is set to:
gulp.task('doSomething', () => { // 执行某些操作 });
Therefore, we can check and use these values as needed.
Suppose arg
is set at the top of gulpfile.js
, we can rewrite our FTP deployment task so that we can pass:
--user
or --u
parameter --password
or --p
parameter gulp doSomething --option1 "my string" --option2 123 --option3
Deployment only occurs when we run the task with the appropriate FTP credentials, for example:
// 这是开发版本吗? const devBuild = ((process.env.NODE_ENV || 'development').trim().toLowerCase() === 'development'); // Gulp 插件 const stripdebug = require('gulp-strip-debug'); // 删除调试代码 const uglify = require('gulp-uglify'); // 压缩 // 构建 JavaScript gulp.task('js', () => { let jsbuild = gulp.src('src/js/*') .pipe(some-plugin1()) .pipe(some-plugin2()); // 生产服务器任务 if (!devBuild) { jsbuild = jsbuild .pipe(stripdebug()) .pipe(uglify()); } return jsbuild.pipe(gulp.dest('build/js/')); });
As we have seen, using a small amount of custom code, parameters can be passed to Gulp tasks. While your task usually does not require receiving parameters, we see that it is very useful in some cases. This is definitely a good technique you should master in your toolbox.
Parameter parsing code can be used in any Node.js command line process. However, if you need it in a non-Gulp project, the commander module provides more powerful features.
I hope you find this useful. Of course, just because you can pass parameters to Gulp tasks doesn't mean you should do so! If you come up with more good use cases for this approach, let me know in the comments.
This article was reviewed by Tim Severien. Thanks to all SitePoint peer reviewers for getting SitePoint content to its best!
Arrays can be used to pass parameters to Gulp tasks from the command line. This array contains command line parameters passed when starting the Node.js process. The first two elements of this array are the path to the Node.js executable and the path to the Gulp file. Any other parameters will be added to this array starting from the third position. You can access these parameters in the Gulp task by indexing the process.argv
array. process.argv
object as a command line parameter. yargs.argv
. In your Gulp task, you can check if this flag is set using a package like gulp build --production
or yargs or minimist. process.argv
Yes, you can pass multiple parameters to Gulp tasks from the command line. These parameters are added to the process.argv
array in the order they are passed. You can access these parameters in the Gulp task by indexing the process.argv
array.
Can use command line parameters in Gulp tasks by accessing process.argv
arrays or packages such as yargs or minimist. You can use these parameters to control the behavior of Gulp tasks. For example, you can use the "production" flag to compress code in a build task.
Yes, you can use environment variables in Gulp tasks. These variables can be accessed through the process.env
object. This is useful if you want to pass sensitive information such as API keys to Gulp tasks without exposing them in command line parameters.
The .on('error')
can be used to handle errors in Gulp tasks. This method takes a callback function that will be called when an error occurs. In this callback function, you can log errors and end the task to prevent Gulp from crashing.
Yes, you can run multiple Gulp tasks in order using the gulp.series()
method. This method takes an array of task names and runs them in the defined order.
Yes, you can run multiple Gulp tasks in parallel using the gulp.parallel()
method. This method takes an array of task names and runs all tasks at the same time.
The default Gulp task can be defined using the gulp.task()
method using "default" as the task name. This task is run when you execute the gulp command without any task name. You can use this task to run a series of other tasks by default.
The above is the detailed content of How to Pass Command Line Parameters to Gulp Tasks. For more information, please follow other related articles on the PHP Chinese website!