简体   繁体   English

使用自定义json配置的grunt任务

[英]grunt task with custom json config

i want to create some kind of task where i can read custom configs from different json files, and replace stuff inside my coffee-source files with contents of the json files, and concatenate the source-files. 我想创建某种任务,我可以从不同的json文件中读取自定义配置,并用我的咖啡源文件中的内容替换json文件的内容,并连接源文件。

my projekt-setup: 我的projekt设置:

  • ./src ./src

    • file1.coffee file1.coffee
    • file2.coffee file2.coffee
  • ./config ./config中

    • /folder1 /资料夹

      • development.json (contains: {"key": "value1"} development.json(包含:{“key”:“value1”}
      • production.json (contains: {"key": "value2"} production.json(包含:{“key”:“value2”}
    • /folder2 /资料夹

      • development.json (contains: {"key": "value3"} development.json(包含:{“key”:“value3”}
      • production.json (contains: {"key": "value4"} production.json(包含:{“key”:“value4”}
  • ./dist ./dist

    • package-name.coffee package-name.coffee
    • package-name.js 包name.js

file1.coffee contains file1.coffee包含

myVar = '@@putkeyhere'
version = '@@version'
...

i have the grunt concat task running for itself configured and working: 我有自己配置和工作的grunt concat任务:

concat: {
  dist: {
    src: ['<banner>', './src/*.coffee'],
    dest: './dist/<%= pkg.name %>.coffee'
  }
},

i have got the grunt-replace task (the replacement of version and so on is already working when i run "grunt replace" on already concatenated files) 我有grunt-replace任务(当我在已经连接的文件上运行“grunt replace”时,替换版本等已经工作了)

replace: {
  dist: {
    options: {
      variables: {
        'created': '<%= grunt.template.today("dd.mm.yyyy HH:MM:ss") %>',
        'environment': 'dev',
        'version': '<%= pkg.version %>'
      },
      prefix: '@@'
    },
    files: {
      'dist/': ['./dist/<%= pkg.name %>.coffee']
    }
  }
},

and finally the coffee compile task: 最后咖啡编译任务:

coffee: {
  compile: {
    files: {
      './dist/<%= pkg.name %>.js': ['./dist/*.coffee']        
    }
  }
}

all tasks work for themselves, but i need to read from the config-json files replace the contents into concatenated coffee-files, and then compile all files to js. 所有任务都适合自己,但我需要从config-json文件中读取内容替换为连接的咖啡文件,然后将所有文件编译为js。

i tried something like this, but that doesnt feel right: 我试过这样的事,但感觉不对:

grunt.registerTask('mytask', '', function (env) {

  env = env || 'development';
  if (env !== 'development' && env !== 'production') {
    grunt.log.error("'" + env + "' is not valid environment");
    return false;
  }

  var c = grunt.option('c');
  if(c) {

    // if i run the task "grunt mytask:production -c folder2 it should read
    // ./config/folder2/development.json
    // that works that way, but i dont think this is a good solution
    var config = grunt.file.readJSON('./config/' + c + '/' + env + '.json')


  } else {

     // here i need to iterate for all folders in ./config, and do stuff for all
  }

});

is the multiTask an option? multiTask是一个选项吗? but how do read dynamically from the config.json files? 但如何从config.json文件中动态读取?

appreciate your help! 感谢您的帮助!

我找到了一个可以帮助你实现这个目标的插件 - https://github.com/outaTiME/grunt-replace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM