Staple
Staple

Reputation: 712

Combining Gulp tasks into one gulp-rev manifest

Just starting out with Gulp - it's freaking great. This works but the rev.manifest over writes itself and doesn't have both the 'style' and 'scripts' in it. There must be a better way. Right? :-)

gulp.task('script', function() {
  var scripts = gulp.src('source-js/main.js')
                  .pipe(uglify())
                  .pipe(rev())
                  .pipe(gulp.dest());

  var manifest = gulp.src('./rev-manifest.json');

  return es.merge(scripts, manifest)
           .pipe(rev.manifest())
           .pipe(gulp.dest('.'))
});

gulp.task('style', function() {
  var styles = gulp.src('source-less/style.less')
                  .pipe(less({compress: true}))
                  .pipe(rev())
                  .pipe(gulp.dest());

  var manifest = gulp.src('./rev-manifest.json');

  return es.merge(styles, manifest)
           .pipe(rev.manifest())
           .pipe(gulp.dest('.'))
});

gulp.task('watch', function () {
    gulp.watch('source-less/**/*.less', ['style']);
    gulp.watch('source-js/**.js', ['script']);
});

Edit: trying with es is still lover writing the manifest:

gulp.task('script', function() {

  var scripts = gulp.src('source-js/main.js')
                  .pipe(uglify())
                  .pipe(rev())
                  .pipe(gulp.dest('assets/js'));

  var manifest = gulp.src('./rev-manifest.json');

  return es.merge(scripts, manifest)
           .pipe(rev.manifest())
           .pipe(gulp.dest('.'))
});

gulp.task('style', function() {

  var styles = gulp.src('source-less/style.less')
                  .pipe(less({compress: true}))
                  .pipe(rev())
                  .pipe(gulp.dest('assets/css'));

  var manifest = gulp.src('./rev-manifest.json');

  return es.merge(styles, manifest)
           .pipe(rev.manifest())
           .pipe(gulp.dest('.'))
});

Upvotes: 3

Views: 3495

Answers (2)

star
star

Reputation: 1

Here is my solution.

The folder structure is

└── src
    ├── images
    │   ├── skywalker.jpeg
    │   └── space.png
    ├── js
    │   ├── index.js
    │   ├── index2.js
    │   └── page.js
    ├── styles
    │   ├── index.css
    │   ├── index.styl
    │   ├── module
    │   │   ├── com.css
    │   │   └── com.styl
    │   └── page.css
    └── view
        ├── index.html
        └── page.html

My gulp task is

/**
 *  production
 */

var jsProductionPipe = lazypipe()
    .pipe(jshit)
    .pipe(jshit.reporter, 'default')
    .pipe(uglify, {
        compress: {
            dead_code: true,
            conditionals: true,
            booleans: true,
            unused: true,
            if_return: true,
            join_vars: true
        }
    }),
    cssProductionPipe = lazypipe()
        .pipe(stylus).pipe(minifyCss);

gulp.task("deploy_js_css", ["deploy_image"], function () {

    var manifest = gulp.src(path.join(__dirname, "dist/rev-manifest.json"));

    return gulp.src(["src/**/*.styl", "src/**/*.js"])
        .pipe(debug({title: '[deploy_js_css]:'}))
        .pipe(revReplace({manifest: manifest, replaceInExtensions: ['.js', '.css', '.html', '.hbs', '.styl']}))
        .pipe(gulpif('*.styl', cssProductionPipe()))
        .pipe(gulpif('*.js', jsProductionPipe()))
        .pipe(rev())
        .pipe(gulp.dest("dist"))
        .pipe(rev.manifest("dist/rev-manifest.json", {
            merge: true
        }))
        .pipe(revDel({ dest: 'dist', oldManifest: path.join(__dirname, "dist/rev-manifest.json") }))
        .pipe(gulp.dest("./"));
});

gulp.task("deploy_image", function() {
    return gulp.src("src/images/*", {
        base: "src/"
    })
        .pipe(debug({title: '[deploy_image]:'}))
        .pipe(imagemin({
            progressive: true
        }))
        .pipe(rev())
        .pipe(gulp.dest("dist"))
        .pipe(rev.manifest("dist/rev-manifest.json", {
            merge: true
        }))
        .pipe(revDel({ dest: 'dist', oldManifest: path.join(__dirname, "dist/rev-manifest.json") }))
        .pipe(gulp.dest("./"));
});

gulp.task('production', ['deploy_image', 'deploy_js_css']);

Upvotes: 0

Preview
Preview

Reputation: 35836

There is an example to add the manifest.json directly to the stream to prevent it to be overwritten but there is a currently a bug in gulp#396 related to vinyl-fs#25 that disallow it. For future readers when it's fixed:

gulp.task('scripts', function() {
  gulp.src('source-js/main.js')
    .pipe(uglify())
    .pipe(rev())
    .pipe(gulp.dest('assets/js'))
    .pipe(gulp.src('./rev-manifest.json'))
    .pipe(rev.manifest())
    .pipe(gulp.dest('assets'));
});

But for now, you should use event-stream for this. Note that until gulp-rev#59 is merged, it won't work.

var es = require('event-stream');

gulp.task('scripts', function() {
  var scripts = gulp.src('source-js/main.js')
                  .pipe(uglify())
                  .pipe(rev())
                  .pipe(gulp.dest('assets/js'));

  var manifest = gulp.src('./rev-manifest.json');

  return es.merge(scripts, manifest)
           .pipe(rev.manifest())
           .pipe(gulp.dest('.'))
});

Your style task will follow the same pattern. All of this assume that your manifest.json will be in the root directory.

Upvotes: 3

Related Questions