我正在使用google-closure-compiler和gulp-watch来编译js文件,只要它们中的任何一个被更改。
这是代码;
var closureCompiler = require('google-closure-compiler').gulp();
var flatmap = require('gulp-flatmap');
var watch = require('gulp-watch');
gulp.task('js-closure', function () {
return watch(['app/js/*.js', 'dist/single.js'], function()
{
gulp.src(['app/js/*.js', 'dist/single.js'], {base: './'})
.pipe(flatmap(function(stream, file) {
return stream.pipe(closureCompiler({
compilation_level: 'SIMPLE_OPTIMIZATIONS',
warning_level: 'QUIET',
language_in: 'ECMASCRIPT6_STRICT',
language_out: 'ECMASCRIPT5_STRICT',
output_wrapper: '(function(){\n%output%\n}).call(this)',
js_output_file: path.basename(file.path).replace(/js$/, 'min.js')
}))
}))
.pipe(gulp.dest('./dist/js'));
});
});
工作正常。但是,当检测到js文件已更改并重新编译时,我想将新消息打印到终端。新消息应该提供这样的信息;
1201hrs - file XXX was changed and recompiled
任何形式的伐木都会受到欢迎。
答案 0 :(得分:2)
使用gulp-changed-in-place,它只传递已更改的源文件。 gulp-logger是基本的流记录器。
var gulp = require("gulp"),
changedInPlace = require("gulp-changed-in-place"),
logger = require("gulp-logger");
gulp.task("default", function () {
return gulp.src("app/js/**/*.js")
.pipe(changedInPlace())
// the pipe now contains the files
// that have changed since the last run
.pipe(logger({
before: "Closure compiler task",
after: "Compiling complete!",
showChange: true
}))
// your own gulp task goes here
// then pipe to the destination
.pipe(gulp.dest("./dist/js"));
});
答案 1 :(得分:0)
以下是问题中提供的源代码所需的修改。感谢Dan Nagle,他提供了使用gulp-changed-in-place
和gulp-logger
的提示。
var closureCompiler = require('google-closure-compiler').gulp();
var flatmap = require('gulp-flatmap');
var watch = require('gulp-watch');
var changedInPlace = require("gulp-changed-in-place");
var logger = require("gulp-logger");
gulp.task('js-closure', function () {
return watch(['app/js/*.js', 'dist/single.js'], function()
{
gulp.src(['app/js/*.js', 'dist/single.js'], {base: './'})
.pipe(changedInPlace())
.pipe(flatmap(function(stream, file) {
return stream.pipe(closureCompiler({
compilation_level: 'SIMPLE_OPTIMIZATIONS',
warning_level: 'QUIET',
language_in: 'ECMASCRIPT6_STRICT',
language_out: 'ECMASCRIPT5_STRICT',
output_wrapper: '(function(){\n%output%\n}).call(this)',
js_output_file: path.basename(file.path).replace(/js$/, 'min.js')
}))
}))
.pipe(logger({
before: "file was changed",
after: "file has been recompiled",
showChange: true
}))
.pipe(gulp.dest('./dist/js'));
});
});