【问题标题】:Exclude files from Gulp stream if contents contain a string如果内容包含字符串,则从 Gulp 流中排除文件
【发布时间】:2020-04-21 12:45:39
【问题描述】:

我有一个看起来像这样的 gulp 任务:

    return src(['storage/framework/views/*.php'])
        .pipe(htmlmin({
            collapseWhitespace: true,
        }))
        .pipe(dest('storage/framework/views'));

它从 Laravel 获取编译后的视图,并通过 htmlmin 管道它们。

这适用于 HTML 视图,但它会破坏 Markdown 视图的内容。

很遗憾,我无法在 src([...]) 中添加排除项,因为文件名都是哈希值。如果文件包含mail::message,我需要能够检查文件的内容并排除该文件。

尝试自己解决这个问题,有gulp-contains,但似乎只有在文件包含给定字符串时才能抛出错误。我想不出一种方法来使用它的回调来排除文件。

还有gulp-ignore,但似乎无法将单个文件排除在流之外。

假设有三个模板文件,其中y.php 是一个 Markdown 模板。理想的解决方案是这样的:

    return src(['storage/framework/views/*.php']) // [x.php, y.php, z.php]
        .pipe(exclude_files_containing('mail::message')) // [x.php, z.php]
        .pipe(htmlmin({
            collapseWhitespace: true,
        }))
        .pipe(dest('storage/framework/views'));

【问题讨论】:

    标签: laravel gulp


    【解决方案1】:

    使用gulp-filter

    const filter = require('gulp-filter');
    
    gulp.task("TaskExample", function () {
    
      // return true if want the file in the stream
      // return file to exclude the file
    
      const excludeMessageFilter = filter(function (file) {
    
        let contents = file.contents.toString();
        return !contents.match('mail::message');
      });
    
      return gulp.src('storage/framework/views/*.php')
    
        .pipe(excludeMessageFilter)
    
        // .pipe(htmlmin(...
        // .pipe(gulp.dest('''''''));
    });
    

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      • 1970-01-01
      • 2019-09-28
      • 2020-02-14
      相关资源
      最近更新 更多