grunt命令如何工作?

时间:2016-08-24 10:18:13

标签: node.js npm gruntjs

实际执行了什么命令? 就像使用包含在package.json

中的npm start一样
  "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
     "start": "node index.js"
  }

npm start在幕后运行node index.js

同样,grunt运行(幕后)?

2 个答案:

答案 0 :(得分:2)

当您调用Grunt任务运行程序时,它会按您指定的顺序运行您在Gruntfile中指定的任何Grunt插件。

Grunt插件由单个任务文件组成。该文件基本上只是执行相关任务的Node.js脚本。它可以访问传递给插件的设置,并可以使用Grunt的文件API来访问文件系统,但除此之外它只是一个Node.js脚本。

编写一个Grunt插件并不难,如果你有兴趣了解更多关于Grunt的信息,那么这是一个更熟悉它的好方法。我个人已经编写了几个静态站点生成器作为Grunt插件,它非常有用。 Grunt任务文件即。 gruntfile.js看起来像这样,

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      build: {
        src: 'src/<%= pkg.name %>.js',
        dest: 'build/<%= pkg.name %>.min.js'
      }
    }
  });

  // Load the plugin that provides the "uglify" task.
  grunt.loadNpmTasks('grunt-contrib-uglify');

  // Default task(s).
  grunt.registerTask('default', ['uglify']);

};

当您运行命令grunt uglify时,它基本上会运行uglify下定义的任务。您可以在其入门指南中找到更多内容here

答案 1 :(得分:1)

grunt命令执行Gruntfile.js,它通过节点执行此操作,但它将grunt配置和函数传递给Gruntfile中使用的插件。这就是为什么你写

module.exports = function(grunt) {
   // grunt parameter is passed from grunt-cli
   // which contains grunt functions and utilities
   // that you can use to configure the tasks etc.
});
相关问题