【问题标题】:Gulp-rev replacing files with generated hashGulp-rev 用生成的哈希替换文件
【发布时间】:2018-09-28 18:48:01
【问题描述】:

我开始使用 Gulp,我想创建一个任务,用哈希重命名文件(js,css),然后将它们注入到 html 中。

我实际上有两个函数可以做我想做的事,但我不知道如何删除旧文件并只保留新的 js 和 css 哈希文件。

有人可以给我提示吗?这是我现在的代码:

obs: 'config' 变量具有文件夹位置

gulp.task('prod:rev',['prod:revision'],function() { 
  var manifest = gulp.src(config.prod.manifest);
  return gulp
    .src(config.prod.header)
    .pipe(plugin.revReplace({manifest: manifest}))
    .pipe(gulp.dest('../'));
});

gulp.task('prod:revision',function(){
  var revJs = gulp
   .src(config.scripts.build+'*.js')
   .pipe(plugin.rev())
   .pipe(gulp.dest(config.scripts.build))
   .pipe(plugin.rev.manifest({merge: true}))
   .pipe(gulp.dest(config.prod.path));

  var revCss = gulp
   .src(config.styles.build+'*.css')
   .pipe(plugin.rev())
   .pipe(gulp.dest(config.styles.build))
   .pipe(plugin.rev.manifest({merge: true}))
   .pipe(gulp.dest(config.prod.path));

  return merge(revJs,revCss);

【问题讨论】:

    标签: hash gulp gulp-rename gulp-rev


    【解决方案1】:

    我目前在一个类似的问题上打开了question - 我得到了正确的文件,但未能使用 gulp-useref 将它们注入到 html 中(你可以在我的问题中检查应该工作的方法关于用户参考)。我怀疑我使用的目录存在一些问题,但现在我选择使用不同的缓存清除方案:

    var cb = Math.random();
    
    ....
    .pipe($.replace('dist/css/lib.css', 'dist/css/lib.css?cb=' + cb))
    ....
    

    它可能不是你想要的答案,但它可能是你需要的答案。 :)

    【讨论】:

      【解决方案2】:

      根据gulp-revreadme.md,听起来您可能还想使用gulp-rev-delete-original 删除未散列的原件。

      【讨论】:

        【解决方案3】:

        在调用 .pipe(rev()) 之后尝试把它放

        const revDelete = require('gulp-rev-delete-original');

        ... .pipe(revDelete()) ...

        这将删除“未散列的”文件。

        【讨论】:

          猜你喜欢
          • 2016-07-16
          • 1970-01-01
          • 2016-03-22
          • 1970-01-01
          • 2019-01-17
          • 2012-11-19
          • 1970-01-01
          • 2023-03-17
          • 2021-11-10
          相关资源
          最近更新 更多