Gulp 监视文件的增删改查
Gulp watch for file additions and deletions
我有一个 Gulp 任务,wire
将应用程序 JavaScript 注入 index.html。
我想要另一个 Gulp 任务,watch
,监视 JavaScript 文件的变化,并且 运行s wire
如果添加了新文件或删除了现有文件。
以下任务完成此过程:
gulp.task('wire', function () {
var injectScripts = gulp.src('src/app/**/*.js');
return gulp.src(path.join(conf.paths.base, 'index.html'))
.pipe($.inject(injectScripts, {}))
.pipe(gulp.dest(path.join(conf.paths.base)));
});
gulp.task('watch', function () {
var watcher = gulp.watch(path.join(conf.paths.src, '/app/**/*.js'));
watcher.on('change', function (ev) {
if (ev.type === 'added' || ev.type === 'deleted') {
gulp.run('wire');
}
});
});
不幸的是,这种方法似乎已弃用。当 运行 启用它时,我在控制台中收到弃用通知:
gulp.run() has been deprecated. Use task dependencies or gulp.watch
task triggering instead.
当我用 gulp.start()
换出 gulp.run()
时(这似乎几乎没有文档),文件更改为 index.html 是滞后的(他们需要几分钟才能完成,这很奇怪)。
显然,只要 JavaScript 文件发生变化,我就可以将我的 watch
任务更改为 运行 wire
,而不是根据事件类型进行过滤,但看起来当现有文件更改时,对 运行 wire
的不必要处理造成巨大浪费:
gulp.watch('src/app/**/*.js', ['wire']);
推荐的 Gulp 策略是什么?
看看 this link
中对此的长期 运行 讨论
var wire = function() {
var injectScripts = gulp.src('src/app/**/*.js');
return gulp.src(path.join(conf.paths.base, 'index.html'))
.pipe($.inject(injectScripts, {}))
.pipe(gulp.dest(path.join(conf.paths.base)));
};
gulp.task('wire', function () {
wire();
});
gulp.task('watch', function () {
var watcher = gulp.watch(path.join(conf.paths.src, '/app/**/*.js'));
watcher.on('change', function (ev) {
if (ev.type === 'added' || ev.type === 'deleted') {
wire();
}
});
});
我有一个 Gulp 任务,wire
将应用程序 JavaScript 注入 index.html。
我想要另一个 Gulp 任务,watch
,监视 JavaScript 文件的变化,并且 运行s wire
如果添加了新文件或删除了现有文件。
以下任务完成此过程:
gulp.task('wire', function () {
var injectScripts = gulp.src('src/app/**/*.js');
return gulp.src(path.join(conf.paths.base, 'index.html'))
.pipe($.inject(injectScripts, {}))
.pipe(gulp.dest(path.join(conf.paths.base)));
});
gulp.task('watch', function () {
var watcher = gulp.watch(path.join(conf.paths.src, '/app/**/*.js'));
watcher.on('change', function (ev) {
if (ev.type === 'added' || ev.type === 'deleted') {
gulp.run('wire');
}
});
});
不幸的是,这种方法似乎已弃用。当 运行 启用它时,我在控制台中收到弃用通知:
gulp.run() has been deprecated. Use task dependencies or gulp.watch task triggering instead.
当我用 gulp.start()
换出 gulp.run()
时(这似乎几乎没有文档),文件更改为 index.html 是滞后的(他们需要几分钟才能完成,这很奇怪)。
显然,只要 JavaScript 文件发生变化,我就可以将我的 watch
任务更改为 运行 wire
,而不是根据事件类型进行过滤,但看起来当现有文件更改时,对 运行 wire
的不必要处理造成巨大浪费:
gulp.watch('src/app/**/*.js', ['wire']);
推荐的 Gulp 策略是什么?
看看 this link
中对此的长期 运行 讨论var wire = function() {
var injectScripts = gulp.src('src/app/**/*.js');
return gulp.src(path.join(conf.paths.base, 'index.html'))
.pipe($.inject(injectScripts, {}))
.pipe(gulp.dest(path.join(conf.paths.base)));
};
gulp.task('wire', function () {
wire();
});
gulp.task('watch', function () {
var watcher = gulp.watch(path.join(conf.paths.src, '/app/**/*.js'));
watcher.on('change', function (ev) {
if (ev.type === 'added' || ev.type === 'deleted') {
wire();
}
});
});