【问题标题】:Launch Gulp Watch for JS and detect current folder has changed启动 Gulp Watch for JS 并检测当前文件夹已更改
【发布时间】:2015-12-10 15:26:39
【问题描述】:

我需要从我的 gulpfile 中最小化 gulp 命令的数量。

这是我的 JS 文件夹

js/
   templates/
              t-01/
              t-02/
              [...]
              t-xxx/

我对 JS 的 gulp 任务(使用 livereload)

gulp.task('da-js', function() {
    gulp.src([
        'js/templates/**/*.js',
        '!js/templates/**/*.min.js'
        ])
        .pipe(concat('app.min.js'))
        .pipe(gulp.dest('js/templates'))
        .pipe(livereload());
});

此任务是全局的,目标文件夹是templates,但我想检测 js 文件的当前文件夹,例如:

  1. 我在/templates/t-01/改js
  2. gulp.watch 正在启动
  3. app.min.js 仅在此文件夹中生成 t-01

我知道gulp.dest 不适合定位当前文件夹,但我不知道该怎么做。

感谢您的帮助:)

【问题讨论】:

  • 您是否监视templates 文件夹下的所有文件以进行更新,以便此文件夹下任何JS 文件的任何更改都会再次生成您的app.min.js

标签: javascript gulp gulp-watch gulp-concat gulp-livereload


【解决方案1】:

您可以使用 gulp 的 watch 方法来监视包含 JS 文件的文件夹的更新,并运行一系列任务以响应某些更改。

gulp.task('watch-files', function() {
    gulp.watch('js/templates/**/*.js', ['da-js']);
}); 

在这里,我们正在查看模板中的所有 JS 文件及其所有子文件夹,并为每次更新运行文件连接任务 (da-js)。现在我们正在执行相同的任务,即使您更改了templates/t-01/some.js,您的app.min.js 文件夹也会在您的templates 文件夹中生成。

gulpfile.js 中定义watch-files 任务后,您只需运行gulp watch-files 命令即可开始监控您的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    相关资源
    最近更新 更多