Running grunt task with api, without command line

前端 未结 2 1566
不知归路
不知归路 2020-12-25 08:00

I want to create and run grunt task in node.js code for test use.

var foo = function() {
    var grunt = require(\"grunt\");

    var options = {\"blahblah\"         


        
2条回答
  •  無奈伤痛
    2020-12-25 08:37

    You can. I don't know why anyone would need to do this as currently Grunt is a command line tool. WARNING: I don't recommend running Grunt in this way. But here it is:

    var grunt = require('grunt');
    
    // hack to avoid loading a Gruntfile
    // You can skip this and just use a Gruntfile instead
    grunt.task.init = function() {};
    
    // Init config
    grunt.initConfig({
      jshint: {
        all: ['index.js']
      }
    });
    
    // Register your own tasks
    grunt.registerTask('mytask', function() {
      grunt.log.write('Ran my task.');
    });
    
    // Load tasks from npm
    grunt.loadNpmTasks('grunt-contrib-jshint');
    
    // Finally run the tasks, with options and a callback when we're done
    grunt.tasks(['mytask', 'jshint'], {}, function() {
      grunt.log.ok('Done running tasks.');
    });
    

提交回复
热议问题