Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Grunt替换:参数化json文件_Javascript_Json_Gruntjs - Fatal编程技术网

Javascript Grunt替换:参数化json文件

Javascript Grunt替换:参数化json文件,javascript,json,gruntjs,Javascript,Json,Gruntjs,早上好 我在gruntfile中使用grunt replace()通过从json文件加载json对象来替换html文件中的一些字符串 我想为这种方法增加一些灵活性,我定制了另一个名为“setopts”的任务,它只需向grunt.option添加一些属性,我使用“替换”任务的方式如下: replace: { common: { options: { patterns: [ { json: '<%=grunt.op

早上好

我在gruntfile中使用grunt replace()通过从json文件加载json对象来替换html文件中的一些字符串

我想为这种方法增加一些灵活性,我定制了另一个名为“setopts”的任务,它只需向grunt.option添加一些属性,我使用“替换”任务的方式如下:

replace: {
    common: {
      options: {
        patterns: [
          {
            json: '<%=grunt.option("locales")%>'
          }
        ]
      },
      files: [
        {expand: true, flatten: true, src: ['public/sites/<%=grunt.option("domain")%>/index.html'], dest: 'public/sites/<%=grunt.option("domain")%>/'},
      ]
    }
}    
我运行以下任务:

grunt.registerTask('maintask',    [ 'setopts:mydomain', 'replace:common']);
经过一些尝试后,我发现“替换”任务中的“文件”属性工作正常,但“模式”属性中出现错误:

正在处理源…错误 警告:处理“public/sites/xxxxx/index.html”文件时出错。使用--force继续

这怎么了


谢谢你的评论

我知道我迟到了1.5年,但也许其他人需要答案

我的工作方式是不使用
grunt.option
。相反,我使用了
grunt.config.set

replace: {
    common: {
      options: {
        patterns: [
          {
            json: '<%= locales %>'
          }
        ]
      },
      files: [
        {expand: true, flatten: true, src: ['public/sites/<%= domain %>/index.html'], dest: 'public/sites/<%= domain %>/'},
      ]
    }
}   
希望它能帮助某人:)

这个问题帮助我找到了答案

replace: {
    common: {
      options: {
        patterns: [
          {
            json: '<%= locales %>'
          }
        ]
      },
      files: [
        {expand: true, flatten: true, src: ['public/sites/<%= domain %>/index.html'], dest: 'public/sites/<%= domain %>/'},
      ]
    }
}   
grunt.registerTask('setopts', function (domain) {

  locales = grunt.file.readJSON('src/locales/domain/myfile.json');
  grunt.config.set('locales', locales);

  grunt.config.set('domain', domain);

}