Grunt:在构建时更改文件扩展名

Glo*_*80s 1 gruntjs grunt-contrib-copy

我想编写一个Grunt任务,在构建期间,将复制我拥有的所有.html文件,并在/ dist中创建它的.asp版本.

我一直在尝试使用grunt-contrib-copy来实现这一点,这就是我所拥有的:

copy: {
  //some other tasks that work...

  //copy an .asp version of all .html files
  asp: {
    files: [{
      expand: true,
      dot: true,
      cwd: '<%= config.app %>',
      src: ['{,*/}*.html'],
      dest: '<%= config.dist %>',
      option: {
        process: function (content, srcpath) {
          return srcpath.replace(".asp");
        }
      }
    }]
  } //end asp task
},
Run Code Online (Sandbox Code Playgroud)

我知道这个process功能实际上并不正确......我已经尝试了一些不同的正则表达式,使其工作无济于事.当我运行asp任务时,Grunt CLI说我已经复制了2个文件,但它们无处可寻.任何帮助表示赞赏.

Mar*_*que 6

你可以使用rename函数来做到这一点.

例如:

copy: {
  //some other tasks that work...

  //copy an .asp version of all .html files
  asp: {
    files: [{
      expand: true,
      dot: true,
      cwd: '<%= config.app %>',
      src: ['{,*/}*.html'],
      dest: '<%= config.dist %>',
      rename: function(dest, src) {
         return dest + src.replace(/\.html$/, ".asp");
      }
    }]
  } //end asp task
},
Run Code Online (Sandbox Code Playgroud)

这应该有效.

  • 确实`rename`方法有效,但是通过仔细查看Grunt文档,我发现你也可以使用`ext`属性完成一个简单的文件扩展名更改:`ext:'.asp'`. (5认同)