Programmatically set options for grunt task?

前端 未结 6 1195
旧时难觅i
旧时难觅i 2021-02-07 07:52

I have a grunt task that looks at options with grunt.option(\'foo\'). If I\'m calling this task from grunt.task.run(\'my-task\'), how can I change thos

6条回答
  •  闹比i
    闹比i (楼主)
    2021-02-07 08:57

    In addition to @Alessandro Pezzato

    Gruntfile.js:

    grunt.registerTask('build', ['clean:dist', 'assemble', 'compass:dist', 'cssmin', 'copy:main']);
    
        grunt.registerTask('build-prod', 'Build with production options', function () {
            grunt.config.set('assemble.options.production', true);
            grunt.task.run('build');
        });
    
        grunt.registerTask('build-live', 'Build with production options', function () {
            grunt.option('assemble.options.production', false);
            grunt.task.run('build');
        });
    

    Now you can run

    $ grunt build-prod

    -OR-

    $ grunt build-live

    They will both do the full task 'build' and respectively pass a value to one of the options of assemble, namely production 'true' or 'false'.


    In addition to illustrate the assemble example a bit more:

    In assemble you have the option to add a {{#if production}}do this on production{{else}}do this not non production{{/if}}

提交回复
热议问题