简体   繁体   中英

custom grunt task naming convention

Is there any convention regarding naming custom grunt tasks that include more than one word? For example: grunt-json-schema grunt plugin has json_schema task . One name includes dashes ( - ), the other includes underscores ( _ ).

Obviously, dashed-name can't be used as a JavaScript object key:

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

they have to be enclosed in quotes:

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

I checked all official plugins ( grunt-contrib-* ), but they all consist of only one word. The motivation foor this question is simple: I just want to follow conventions.

我认为一般约定是将camelCase用于包含多个单词的任务。

Short answer: Plugin/custom task names do not have to correlate to a specific config object name.

The Grunt.js api allows access to the config object using the method grunt.config . Tasks & Plugins have access to the entire object, not just the sub object correlating to the name.

For example, I could create a task called foo that accesses the config from bar :

grunt.initConfig({
    bar: {
        baz: true
    }
});

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

Best practice: Plugin developers should name the key for their config object similar to the plugin name itself. This helps mitigate conflicts with other plugins who could reference similar.

grunt.initConfig({
    foo: {
        baz: true
    }
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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