【问题标题】:How grunt command works?grunt 命令如何工作?
【发布时间】:2016-08-24 18:42:00
【问题描述】:

实际执行的是什么命令? 就像使用 npm start 并在 package.json 中包含以下内容

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

npm start 在幕后运行node index.js

同样,grunt 运行什么(在幕后)?

【问题讨论】:

    标签: node.js npm gruntjs


    【解决方案1】:

    当您调用 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 中找到更多信息

    【讨论】:

    • 只运行“grunt”会运行所有任务吗?以及在哪里提到运行服务器脚本让我们说 index.js,如上所述?
    • Running grunt 运行Gruntfile.js 中定义的默认任务。如果您安装了 cssmin Grunt 插件,您可以使用 grunt cssmin 单独调用它。通常你会在 Gruntfile 中定义你自己的任务来调用你需要以指定顺序运行的任何插件。
    • 例如,当我构建我的站点的新版本时,我运行grunt serve 来服务它,grunt deploy 来部署它。每个命令依次调用多个 Grunt 插件
    • 谢谢你的意思,但是我的服务器从哪里开始,比如“node index.js”
    • 这与 Grunt 无关。在package.json 中定义的任何命令都在与package.json 文件相同的目录中运行。
    【解决方案2】:

    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.
    });
    

    【讨论】:

      猜你喜欢
      • 2016-02-19
      • 2014-01-23
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多