javascript 获取 Gulp 手表以仅对更改的文件执行功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28545843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Gulp watch to perform function only on changed file
提问by bmdeveloper
I am new to Gulp and have the following Gulpfile
我是 Gulp 的新手并且有以下 Gulpfile
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
gulp.task('compress', function () {
return gulp.src('js/*.js') // read all of the files that are in js with a .js extension
.pipe(uglify()) // run uglify (for minification)
.pipe(gulp.dest('dist/js')); // write to the dist/js file
});
// default gulp task
gulp.task('default', function () {
// watch for JS changes
gulp.watch('js/*.js', function () {
gulp.run('compress');
});
});
I would like to configure this to rename, minify and save only my changed file to the dist folder. What is the best way to do this?
我想将其配置为重命名、缩小并将仅我更改的文件保存到 dist 文件夹。做这个的最好方式是什么?
回答by Leon Gaban
This is how:
这是如何:
// Watch for file updates
gulp.task('watch', function () {
livereload.listen();
// Javascript change + prints log in console
gulp.watch('js/*.js').on('change', function(file) {
livereload.changed(file.path);
gutil.log(gutil.colors.yellow('JS changed' + ' (' + file.path + ')'));
});
// SASS/CSS change + prints log in console
// On SASS change, call and run task 'sass'
gulp.watch('sass/*.scss', ['sass']).on('change', function(file) {
livereload.changed(file.path);
gutil.log(gutil.colors.yellow('CSS changed' + ' (' + file.path + ')'));
});
});
Also great to use gulp-livereloadwith it, you need to install the Chrome pluginfor it to work btw.
与它一起使用gulp-livereload也很棒,顺便说一句,您需要安装Chrome 插件才能正常工作。
回答by James
See incremental builds on the Gulp docs.
请参阅Gulp 文档中的增量构建。
You can filter out unchanged files between runs of a task using the gulp.src function's since option and gulp.lastRun
您可以使用 gulp.src 函数的 since 选项和 gulp.lastRun 过滤掉任务运行之间未更改的文件