javascript 在 initConfig() 中访问 Grunt 配置数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22996343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:10:06  来源:igfitidea点击:

Access Grunt config data within initConfig()

javascriptgruntjs

提问by cantera

How can I access the Grunt config property siteto read the project.jsonfile at the path specified by the config property value?

如何访问 Grunt 配置属性site以读取project.json配置属性值指定的路径中的文件?

grunt.registerTask('build', function(target) {
  grunt.config('site', target);
  grunt.task.run('foo:dist', 'bar:dist');
});

grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'),
  site: grunt.file.readJSON('./sites/' + grunt.config('site') + '/project.json')
});

grunt-cli:

咕噜-cli:

grunt build:sitename

>> Error: Unable to read "./sites/undefined/project.json"


Using an examplefrom the docs, I also tried this:

使用文档中的示例,我也尝试过:

grunt.registerTask('global', 'site', function(prop, value) {
  global[prop] = val;
});

grunt.registerTask('build', ['foo:dist', 'bar:dist']);

grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'),
  site: grunt.file.readJSON('./sites/' + global.site + '/project.json')
});

grunt-cli:

咕噜-cli:

grunt global:site:sitename

>> Error: Unable to read "./sites/undefined/project.json"


Update:

更新

Using the @FelixKling answer as a guide, I've made some progress:

使用@FelixKling 的答案作为指导,我取得了一些进展:

grunt.registerTask('build', function(target) {
  grunt.config.set('target', target);
  grunt.config.set('site', grunt.file.readJSON('./sites/' + grunt.config.get('target') + '/project.json'));
  grunt.task.run('watch');
});

grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'),
  site: grunt.config.get('site'),

  watch: {
    sass: {
      files: ['<%= site.dev %>/scss/*.scss'],
      tasks: ['sass:dist']
    }
  },

  sass: {
    dist: {
      files: {
        '<%= site.dist %>/style.css': '<%= site.dev %>/scss/style.scss'
      }
    }
  },
});

Now I'm able to read in the project.jsonfile successfully, and (somehow) it's even able to recognize when edits are made to watched files. But for some reason when it runs the sass:disttask, I get this error: Warning: An error occurred while processing a template (Cannot read property 'dev' of undefined).

现在我能够project.json成功读取文件,并且(不知何故)它甚至能够识别何时对观看的文件进行了编辑。但是由于某种原因,当它运行sass:dist任务时,我收到此错误:Warning: An error occurred while processing a template (Cannot read property 'dev' of undefined).

I'm not clear on how the watchtask is able to get the correct value for site, but more importantly, I need to figure out a way to get that same value to the sasstask.

我不清楚watch任务如何能够为 获得正确的值site,但更重要的是,我需要找出一种方法来为sass任务获得相同的值。

回答by Felix Kling

initConfigand grunt.file.readJSONrun before your task runs. It seems like what you need are template stringsand you can only call grunt.file.readJSONwhen you actually have the target name.

initConfiggrunt.file.readJSON在您的任务运行之前运行。看起来您需要的是模板字符串,并且您只能grunt.file.readJSON在实际拥有目标名称时调用。

For example:

例如:

grunt.registerTask('build', function(target) {
  grunt.config.set('target', target);
  grunt.config.set('site', grunt.file.readJSON(grunt.config.get('path'));
  grunt.task.run('foo:dist', 'bar:dist');
});

grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'),
  path: './sites/<%= target %>/project.json'
});

More info: http://gruntjs.com/api/grunt.config

更多信息:http: //gruntjs.com/api/grunt.config



Regarding your update: You are basically making the same mistake again as you did in your first example. You are trying to access the config sitebefore it was set.

关于您的更新:您基本上又犯了与第一个示例中相同的错误。您正在尝试site在设置之前访问配置。

You have to understand that the initialization step, i.e. grunt.initConfigtakes place beforeany task related code runs:

您必须了解初始化步骤,即任何与任务相关的代码运行之前grunt.initConfig发生:

Initialize config -> Run task

Lets look at grunt.initConfigin isolation:

让我们单独来看grunt.initConfig

grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'),
  site: grunt.config.get('site'),
});

This is the initialization step, which happens before everything else. The argument passed to initConfig, the configuration object, is evaluated first. What you are trying to do here is access the config options site, before the config was even created. I hope you recognize that this doesn't make sense.

这是初始化步骤,它发生在其他一切之前。传递给initConfig配置对象的参数首先被评估。您在这里尝试做的是访问配置选项site,甚至在创建配置之前。我希望你认识到这没有意义。

Maybe it helps you to understand the process if you put grunt.initConfigat the very top, before you register any tasks.

如果你grunt.initConfig在注册任何任务之前放在最上面,也许它会帮助你理解这个过程。



The solution:

解决方案:

I think what you actually might be after are command-line arguments, with which you can control which site to build. See grunt.optionfor more information.

我认为您实际上可能追求的是命令行参数,您可以使用它来控制要构建的站点。请参阅grunt.option以获取更多信息。

For example:

例如:

grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'),

  watch: {
    sass: {
      files: ['<%= site.dev %>/scss/*.scss'],
      tasks: ['sass:dist']
    }
  }
});

grunt.config.set('site', grunt.file.readJSON('./sites/' + grunt.option('site') + '/project.json'));

And then you run the task with

然后你运行任务

grunt watch --site=somesite