将Gulp任务组合成一个gulp-rev清单

时间:2014-09-25 23:59:15

标签: node.js gulp gulp-less

刚开始与Gulp一起开始 - 它非常棒。这有效,但是rev.manifest会自行编写,并且不会同时包含“样式”和“脚本”。肯定有更好的办法。对? : - )

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

编辑:尝试使用es仍然是爱人写清单:

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('.'))
});

2 个答案:

答案 0 :(得分:3)

有一个示例可以将manifest.json直接添加到流中以防止它被覆盖但是gulp#396中当前存在与vinyl-fs#25相关的不允许它的错误。对于未来的读者来说,它是固定的:

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

但就目前而言,您应该使用 event-stream 。请注意,在合并gulp-rev#59之前,它将无效。

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('.'))
});

您的style任务将遵循相同的模式。所有这些都假设您的manifest.json将位于 root 目录中。

答案 1 :(得分:0)

这是我的解决方案。

文件夹结构

└── 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

我的gulp任务是

/**
 *  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']);