【问题标题】:Gulp - How to append to the dest instead of replaceGulp - 如何附加到目标而不是替换
【发布时间】:2015-10-30 21:13:42
【问题描述】:

我有这样的代码:

gulp.task('concat-uglify-js', function() {
  return gulp.src(src + 'js/*.js')
    .pipe(concat('angular-filemanager.min.js'))
    .pipe(uglify())
    .pipe(gulp.dest(dst))
});

gulp.task('cache-templates', function () {
  return gulp.src(tplPath + '/*.html')
    .pipe(templateCache('cached-templates.js', {
      module: 'FileManagerApp',
      base: function(file) {
        return tplPath + '/' + path.basename(file.history);
      }
    }))
    .pipe(uglify())
    .pipe(gulp.dest(dst));
});

我想将这两个任务输出合并到一个文件中...“angular-filemanager.min.js”

有一个选项可以做类似的事情

.pipe(gulp.dest(dst, {mode: "APPEND_INSTEAD_OF_REPLACE"}));

?

谢谢!

【问题讨论】:

    标签: javascript angularjs node.js gruntjs gulp


    【解决方案1】:

    我尚未对此进行测试,但请尝试使用gulp-filter

    var gulpFilter = require('gulp-filter');
    
    gulp.task('concat-uglify-js', function() {
    
      var filter = {
        html: gulpFilter('*.html'),
        js  : gulpFilter('*.js')
      };
    
      return gulp
        .src([src + 'js/*.js', tplPath + '/*.html'])
        .pipe(filter.html)
        .pipe(templateCache('cached-templates.js', {
          module: 'FileManagerApp',
          base: function(file) {
    
            return tplPath + '/' + path.basename(file.history);
          }
        }))
        .pipe(filter.html.restore)
        .pipe(filter.js)
        .pipe(concat('angular-filemanager.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest(dst));
    });
    

    【讨论】:

      【解决方案2】:

      现在可能没有帮助,因为它还不是 released,但是 4.0 中的 gulp.destadd an overwrite option 默认为 true 但可以设置为 false 以进行附加:

      .pipe(gulp.dest(path, { overwrite: false }))

      【讨论】:

        【解决方案3】:

        您可以将templateCache 的调用放在一个函数中,然后使用add-stream 将该函数的输出与concat-uglify-js 任务中的js 文件流结合起来。

        我不确定您使用的是什么角度模板缓存插件,但我更详细地写了这种情况here

        var addStream = require('add-stream');
        
        gulp.task('concat-uglify-js', function() {
          return gulp.src(src + 'js/*.js')
            // add js file containing cached templates to stream of js files
            .pipe(addStream.obj(cacheTemplates()))
            .pipe(concat('angular-filemanager.min.js'))
            .pipe(uglify())
            .pipe(gulp.dest(dst))
        });
        
        // is this task still needed?
        gulp.task('cache-templates', function () {
          return cacheTemplates()
            .pipe(uglify())
            .pipe(gulp.dest(dst));
        });
        
        // this returns a stream of one js file
        function cacheTemplates() {
          return gulp.src(tplPath + '/*.html')
            // (you may want to minify html here)
            .pipe(templateCache('cached-templates.js', {
              module: 'FileManagerApp',
              base: function(file) {
                return tplPath + '/' + path.basename(file.history);
              }
            }));
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-05-22
          • 1970-01-01
          • 1970-01-01
          • 2016-05-29
          • 2015-02-05
          • 1970-01-01
          • 2022-11-18
          相关资源
          最近更新 更多