【问题标题】:How to use gulp-newer?如何使用 gulp-newer?
【发布时间】:2016-03-25 15:02:23
【问题描述】:

我是 gulp 的新手,我尝试按照 https://www.npmjs.com/package/gulp-newer 中的文档了解它的工作原理。但是,对于以下任务,它没有按预期工作。我想我遗漏了一些明显的东西。

这是文件夹结构,

    temp
      file1.js
      file2.js
    new
      file1.js
      file3.js
    change
      <empty initially>

我想比较 temp 文件夹和 new 文件夹以及 new 文件夹中是否有任何新文件(temp 中不存在较早)然后移动这些文件以更改文件夹。这只是我试图了解 gulp-newer 的工作原理。我做得对吗?

    gulp.task('newer', function() {
        return gulp.src('temp/*.js')
                .pipe(newer('new/*.js'))
                .pipe(gulp.dest('change')) 
        });

但是,当我运行此任务时,它只是将 temp 文件夹中的所有文件复制到 change 文件夹中。所以在任务运行后 change 文件夹有 file1.js 和 file2.js。我希望只有 file3.js 出现在更改中(因为那是一个新文件)。如果我对该方法的理解不正确,请纠正我。

【问题讨论】:

    标签: gulp gulp-newer


    【解决方案1】:

    来自gulp-newer:

    使用 newer 和 many:1 source:dest 映射 gulp-concat 等插件 获取许多源文件并生成一个目标文件。在这个 情况下,较新的流将通过所有源文件(如果有的话) 其中比目标文件更新。较新的插件是 配置了目标文件路径。

    以及示例代码:

    var gulp = require('gulp');
    var newer = require('gulp-newer');
    var concat = require('gulp-concat');
    
    // Concatenate all if any are newer 
    gulp.task('concat', function() {
    
      // Add the newer pipe to pass through all sources if any are newer 
      return gulp.src('lib/*.js')
          .pipe(newer('dist/all.js'))
          .pipe(concat('all.js'))
          .pipe(gulp.dest('dist'));
    
    });
    

    您似乎需要传入所有已连接到较新文件的文件。在你的情况下:

    gulp.task('newer', function() {
            return gulp.src('temp/*.js')
                    .pipe(newer('new/*.js'))
                    .pipe(concat('*.js'))
                    .pipe(gulp.dest('change')) 
            });
    

    此外,由于较新会检查文件的修改日期,因此请确保文件实际上是较新的。我知道这很明显,但我通常被困在“明显”的东西上。

    【讨论】:

      【解决方案2】:

      我还可以建议gulp-newy 在其中您可以在自己的函数中操作路径和文件名。然后,只需将该函数用作newy() 的回调。这使您可以完全控制要比较的文件。

      这将允许 1:1 或多对 1 比较。

      newy(function(projectDir, srcFile, absSrcFile) {
        // do whatever you want to here. 
        // construct your absolute path, change filename suffix, etc. 
        // then return /foo/bar/filename.suffix as the file to compare against
      }
      

      【讨论】:

        猜你喜欢
        • 2015-10-16
        • 1970-01-01
        • 2016-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-02
        • 1970-01-01
        相关资源
        最近更新 更多