2016-05-30 2 views
1

私はグラント用にincludereplaceプラグインを使用しようとしています。私は以下のコードを使用してJSONをフェッチすることによって、それはglobalsプロパティの値与えることをしようとしている:私はproperties.var_aを行う際グランントでグローバルオブジェクトを作成

grunt.registerTask('fetchProperties', 'Fetches the properties.json file', function() { 
    properties = grunt.file.readJSON('properties.json'); 
    grunt.log.writeln(['Properties file loaded']); 
}); 

は、今では正しくvar_aの値を返します。 だから、私はこのでした:ここ

grunt.registerTask('fetchProperties', 'Fetches the properties.json file', function() { 
    properties = grunt.file.readJSON('properties.json'); 
    grunt.task.run('includereplace'); 
}) 

を私includereplaceタスクです:

includereplace: { 
     dist: { 
      options: { 
       globals: { 
        VAR_A: properties.var_a, 
        VAR_B: properties.var_b 
       }, 
      }, 
      files: [{ 
       src: '../myFiles/path/to/some/file/main.txt', 
       dest: '../myOtherFiles/path/to/some/file/mainrep.txt' 
      }] 
     } 
    } 

includereplaceタスクによって置き換えられている値がundefinedです。 この問題を緩和するにはどうすればよいですか? また、私はgrunt.configを使って変数を設定しようとしましたが、実際にはロード時に単一の値ではなくオブジェクトを返すJSONファイルがあります。実行時のすべてのタスクがそのパラメータを設定するために使用できるグローバルオブジェクトを設定するには? gruntファイルの先頭にJSONファイルをロードしています。それは私の中の最初の行ですmodule.exports = function(grunt) { ...

答えて

1

これは機能しました!

grunt.initConfig({ 
    properties: grunt.file.readJSON('properties.json'), 
    includereplace: { 
    dist: { 
     options: { 
      globals: { 
       VAR_A: '<%= properties.var_a %>', 
       VAR_B: '<%= properties.var_b %>' 
      }, 
     }, 
     files: [{ 
      src: '../myFiles/path/to/some/file/main.txt', 
      dest: '../myOtherFiles/path/to/some/file/mainrep.txt' 
     }] 
    } 
    } 
}); 

これにはhttps://stackoverflow.com/a/16792592/2459789が使用されました。

関連する問題