如何使用gulp-rev进行更新的工作?

时间:2015-07-26 14:48:44

标签: gulp gulp-changed gulp-newer gulp-rev

设置就像这样简单:

gulp.task('rev-js',  function() {
  return gulp.src('/js/main.js, {base: '.'})
      .pipe(newer('_build'))
      .pipe(rev())
      .pipe(gulp.dest('_build'))
      .pipe(rev.manifest())
      .pipe(gulp.dest('_build/rev/js'));
});
gulp-newer显然不在这里工作,因为目标文件获得了不同的名称。在这种情况下,任何使用gulp-newer(或gulp-changed)工作的解决方法?

2 个答案:

答案 0 :(得分:1)

在gulp-newer options文档中,我读到它支持传入配置对象而不是目标。在该配置对象中,您可以指定从旧文件到新文件的映射功能。而不是

newer('_build')

你可以写

newer({dest: '_build', map: mappingFn})

映射函数获取文件的相对名称,并期望它返回已翻译的名称 - 请参阅index.js文件。您可以定义一个函数,该函数使用先前生成的rev-manifest.json清单来查找正确的文件名。我在你的构建脚本(未经测试)中添加了这些内容:

gulp.task('rev-js',  function() {

  // get the existing manifest
  // todo: add logic to skip this if file doesn't exist
  var currentManifest = JSON.parse(fs.readFileSync('rev-manifest.json', 'utf8'));

  // mapping function for gulp-newer
  function mapToRevisions(relativeName) {
    return currentManifest[relativeName]
  }

  return gulp.src('/js/main.js, {base: '.'})
      .pipe(newer({dest: '_build', map: mapToRevisions}))
      .pipe(rev())
      .pipe(gulp.dest('_build'))
      .pipe(rev.manifest())
      .pipe(gulp.dest('_build/rev/js'));
});

答案 1 :(得分:1)

我建议您gulp-newy在其中操作自己函数中的路径和文件名。然后,只需使用该函数作为newy()的回调。这使您可以完全控制要比较的文件。

这将允许1:1或多对1比较。

newy(function(projectDir, srcFile, absSrcFile) {
  // do whatever you want to here. 
  // construct your absolute path, change filename suffix, etc. 
  // then return /foo/bar/filename.suffix as the file to compare against
}

enter image description here

相关问题