自定义的咕task任务命名约定

有没有关于命名包含多个单词的自定义任务的任何约定? 例如: grunt-json-schema grunt插件有json_schema任务 。 一个名称包含破折号( - ),另一个名称包含下划线( _ )。

显然,dashed-name不能用作JavaScript对象键:

 grunt.initConfig({ json-schema: { // WON'T work 

他们必须用引号括起来:

 grunt.initConfig({ 'json-schema': { // will work 

我检查了所有官方插件( grunt-contrib-* ),但它们都只包含一个单词。 造成这个问题的动机很简单:我只是想遵循惯例。

简短的回答:插件/自定义任务名称不必关联到特定的configuration对象名称。

Grunt.js api允许使用grunt.config 方法访问configuration对象。 任务和插件可以访问整个对象,而不仅仅是与名称相关的子对象。

例如,我可以创build一个名为foo的任务,从bar访问configuration:

 grunt.initConfig({ bar: { baz: true } }); grunt.registerTask('foo', 'example custom task', function () { var config = grunt.config('bar'); grunt.log.ok(config); }); 

最佳实践:插件开发人员应该为他们的configuration对象命名,类似于插件名称本身。 这有助于缓解与其他可以引用相似的插件的冲突。

 grunt.initConfig({ foo: { baz: true } }); grunt.registerTask('foo', 'example custom task', function () { var config = grunt.config('foo'); grunt.log.ok(config); });