ToDoDoDo

WEBとクリエイティブな何かについて

Gruntでファイル内のテキストを一括で置換する

http://www.flickr.com/photos/8395214@N06/8696962276
photo by "Cowboy" Ben Alman

grunt deploy --revision=hogehoge

これがやりたかった

まずは、 Grunt Option API で、コマンドのオプションを設定、取得する

grunt.option('revision') // hogehoge

参考:
http://js.studio-kingdom.com/grunt/api/grunt_option


テキストの置換には grunt-replace を使う

$ npm install grunt-replace --save-dev

Gruntfileの設定は下記のような感じで、置換対象のテキストには'@@'をつけておく。

options: {
  patterns: [
    {
      match: 'foo',
      replacement: 'bar', // replaces "@@foo" to "bar"
      expression: false   // simple variable lookup
    }
  ]
}

Grunt Replace
https://github.com/outaTiME/grunt-replace