Jonas Sciangula Street
Jonas Sciangula Street

Reputation: 1892

Gulp - How to append to the dest instead of replace

I have a code like:

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));
});

And I want to merge this two tasks output in one file... "angular-filemanager.min.js"

There are an option to do something like

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

?

Thanks!

Upvotes: 1

Views: 1964

Answers (3)

Trevor Robinson
Trevor Robinson

Reputation: 16564

Maybe not helpful for now, since it is not yet released, but gulp.dest in 4.0 will add an overwrite option that defaults to true but can be set to false for appending:

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

Upvotes: 1

coma
coma

Reputation: 16649

I haven't tested this but try using 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));
});

Upvotes: 2

psalaets
psalaets

Reputation: 717

You can put the call to templateCache in a function and then use add-stream to combine that function's output with the stream of js files in the concat-uglify-js task.

I'm not sure what angular template cache plugin you're using but I wrote about this situation in more detail 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);
      }
    }));
}

Upvotes: 0

Related Questions