2015-04-01 100 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