如何将参数传递给Gulp任务?

时间:2016-01-22 11:52:04

标签: node.js gulp

说我的任务看起来像这样:

settings.localLESS = ["../css/*.less"];

gulp.task('compile-less-files', function(){
    gulp.src(settings.localLESS)
    .pipe(less())
    .pipe(minifyCSS())
    .pipe(gulp.dest(someDest));
})

gulp.watch(settings.localLESS, ['compile-less-files']).on('change', function(event) {
        ...
});

但我不想全部编译它们。我只想编译我作为参数传递给该任务的文件。也许是这样的?

gulp.task('compile-less-file', function(fileName){
    gulp.src(fileName)
    .pipe(less())
    .pipe(minifyCSS())
    .pipe(gulp.dest(someDest));
})

gulp.watch(settings.localLESS, ['compile-less-file']).on('change', function(event) {
    ...
});

更新:我想为所有源项目文件设置观察程序,并在任何文件更改后运行特定任务。

2 个答案:

答案 0 :(得分:0)

尝试将项目分解为小的部分文件,然后创建一个新文件,该文件将充当“主”文件,您可以在其中导入其他文件。这样,您可以在导入时选择要编译的文件。

答案 1 :(得分:0)

您可以使用Log file created at: 2016/01/22 13:23:01 Running on machine: 10-18-89-11 Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg W0122 13:23:01.585664 23030 authenticator.cpp:505] No credentials provided, authentication requests will be refused - https://www.npmjs.com/package/yargs

yargs

您可以使用节点npm install --save-dev yargs

process.argv

然后你可以将参数传递给你的任务,如下所示

gulp.task('taskname', function() {
    console.log(process.argv);
});