gulp-newer vs gulp-changed

前端 未结 3 1092
孤独总比滥情好
孤独总比滥情好 2020-12-08 04:42

What\'re the differences between them?

gulp-newer:

gulp.src(imgSrc)
  .pipe(newer(imgDest))
  .pipe(imagemin())
  .pipe(gulp.dest(imgDest));
<         


        
3条回答
  •  無奈伤痛
    2020-12-08 04:51

    I hope it's not too late to answer this question. I have had to evaluated both of them at a source-code level for a recent project, and here is my take.

    gulp-newer

    At the core, this plugin compares the source and dest file's modified time (see node API) to decide whether the source file is newer than the dest file or if there is no dest file at all. Here is the related code in the plugin:

    var newer = !destFileStats || srcFile.stat.mtime > destFileStats.mtime;
    

    gulp-changed

    This plugin by default also uses a file's modified time to decide which to pass through the stream

    function compareLastModifiedTime(stream, cb, sourceFile, targetPath) {}
    

    but it goes one step further by offering an option to compare the file's content SHA1 hash:

    function compareSha1Digest(stream, cb, sourceFile, targetPath) {}
    

    This information is nicely documented.

    Conclusion

    So theoretically speaking, if you use gulp-changed's default hasChanged: changed.compareLastModifiedTime, each plugin is relatively as fast as the other. If you use gulp-changed's hasChanged: changed.compareSha1Digest, it's reasonable to expect gulp-changed to be a bit slower because it does create a SHA1 hash of the file content. I didn't benchmark but I'm also interested in seeing some number.

    Which to choose

    gulp-changed, purely because of the developer behind it (sindresorhus). If one day this awesome man decides that he will stop supporting his gulp plugins, I think I will stop using gulp altogether.

    Joking aside, though, gulp-changed's source code is gulp-y, while gulp-newer's source reads pretty much like just another node module's source with lots of promises. So another +1 for gulp-changed :)

    HUGE EDIT

    Gulp-changed only works with 1:1 source:dest mapping. If you need many:1, e.g. when using with gulp concat, choose gulp-newer instead.

提交回复
热议问题