2015-04-01 126 views
1

我想写一个Grunt任务,在构建过程中,将复制我拥有的所有.html文件,并在/ dist中创建.asp版本。Grunt:更改文件扩展名

我一直在试图用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 
}, 

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

回答

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 
}, 

这应该工作。

+4

事实上'rename'方法可行,但通过仔细观察Grunt文档,我发现您还可以使用'ext'属性来完成一个简单的文件扩展名更改:'ext:'.asp'。 – GloryOfThe80s 2015-04-02 13:35:11

+0

请注意,您可能希望使用'path.sep()'加入dest和src参数,因为现在您加入的文件和文件夹不带'somefoldersomefile.asp'而不是'somefolder/somefile.asp'。 – 2016-09-05 11:19:04