duc*_*cin 9 javascript plugins node.js gruntjs
是否有关于命名包含多个单词的自定义grunt任务的约定?例如:grunt-json-schema grunt插件有json_schema任务.一个名称包括破折号(-),另一个名称包括下划线(_).
显然,dashed-name不能用作JavaScript对象键:
grunt.initConfig({
json-schema: { // WON'T work
Run Code Online (Sandbox Code Playgroud)
它们必须用引号括起来:
grunt.initConfig({
'json-schema': { // will work
Run Code Online (Sandbox Code Playgroud)
我检查了所有官方插件(grunt-contrib-*),但它们都只包含一个单词.这个问题的动机很简单:我只想遵循惯例.
the*_*nce -1
简短的回答: 插件/自定义任务名称不必与特定的配置对象名称相关。
Grunt.js api 允许使用方法 grunt.config访问配置对象。任务和插件可以访问整个对象,而不仅仅是与名称相关的子对象。
例如,我可以创建一个名为 的任务foo来访问配置bar:
grunt.initConfig({
bar: {
baz: true
}
});
grunt.registerTask('foo', 'example custom task', function () {
var config = grunt.config('bar');
grunt.log.ok(config);
});
Run Code Online (Sandbox Code Playgroud)
最佳实践:插件开发人员应为其配置对象命名类似于插件名称本身的键。这有助于减轻与可能引用类似内容的其他插件的冲突。
grunt.initConfig({
foo: {
baz: true
}
});
grunt.registerTask('foo', 'example custom task', function () {
var config = grunt.config('foo');
grunt.log.ok(config);
});
Run Code Online (Sandbox Code Playgroud)