我在gulp定义了sass和js等task,然后又定义了一个watch的task。现在想在watch的时候获得改动文件的path,然后将path传递给前面的sass和js对应的task,这样就可以实现只编译我改动的文件。
用插件
npm install minimist
var argv = require('minimist')(process.argv.slice(2));
console.dir(argv);
CLI
gulp taskname --a value
你可以尝试用插件 gulp-foal 来运行一个带参数的自定义 task
不是来自命令行的参数
'use strict';
var gulp = require('gulp');
var clean = require('gulp-clean');
var foal = require('gulp-foal');
//使用 foal.task(...) 来定义一个 foal 任务
foal.task('clean_some', function(cleanPath) {
//为了顺序执行 foal 任务,请别省略 return 语句
return gulp.src(cleanPath)
.pipe(clean());
});
gulp.task('default', function(cb) {
//使用 foal.run(...) 来执行带参数的 foal 任务
foal.run(clean_some('test_path'), cb);
});
var filePath = '';
gulp.watch('js/*/.js', function(event) {
console.log('event.path + ' : ' + event.type);
filePath = event.path;
});
通过一个外部变量filePath,在下次要用时传给要用的地方,行不?
推荐 gulp-changed 插件
更多内容 https://github.com/Platform-CUF/use-gulp
另一种方式,按照楼主所说(下面代码我没有试过,只是一个想法)
gulp.watch('js/**/*.js', function(event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
uglify(event.path);
});
function uglify(path){
gulp.src(path)
.pipe(uglify())
.pipe(gulp.dest('./dest'));
}