设为首页 加入收藏

TOP

grunt学习笔记(适用初学者)
2015-07-20 17:44:12 来源: 作者: 【 】 浏览:1
Tags:grunt 学习 笔记 适用 学者

?

1、在学习grunt之前,首先要对nodejs有一些简单的理解。安装nodejs的步骤很简单,根据官网的提示安装即可,在此文中将不再累述。

?

?

?

2. Grunt介绍

?

Grunt是一个自动化的项目构建工具. 如果你需要重复的执行像压缩, 编译, 单元测试, 代码检查以及打包发布的任务. 那么你可以使用Grunt来处理这些任务, 你所需要做的只是配置好Grunt, 这样能很大程度的简化你的工作.

如果在团队中使用Grunt, 你只需要与其他人员约定好使用Grunt应该规避的问题, 就能够很方便的自动化的处理大部分的常见工作任务, 你所付出的努力几乎为0.

3. Grunt安装

Grunt和Grunt插件都是通过npm, Node.js包管理器安装和管理的.

?

D:>cd nodeTest1
odejs-grunt

D:>cd nodeTest1
odejs-grunt
pm install -g grunt-cli

?

这是全局安装。

?

D:>cd nodeTest1
odejs-gruntgrunt

?

将会出现如下错误:

?

grunt-cli: The grunt command line interface. (v0.1.9)

Fatal error: Unable to find local grunt.

If you're seeing this message, either a Gruntfile wasn't found or grunt
hasn't been installed locally to your project. For more information about
installing and configuring grunt, please see the Getting Started guide:

http://gruntjs.com/getting-started

?

执行grunt命令,我们发现系统报错了,提示不能加载本地库。因为,grunt命令执行,是需要当前目录中包括package.json和Gruntfile.js两个文件。

package.json,是npm项目配置文件
Gruntfile.js,是专门用来配置grunt的配置文件

为了做验证,本文做了一个例子:

?

~ D:>express -e nodejs-grunt
~ D:>cd nodejs-grunt && npm install	 
~ D:
odejs-grunt>npm install grunt --save-dev
package.json文件中输入如下量:

?

?

{
  name: nodejs-grunt,
  version: 0.0.1,
  private: true,
  scripts: {
    start: node app.js
  },
  dependencies: {
    express: 3.2.2,
    ejs: *
  },
  devDependencies: {
    grunt: ~0.4.1,
  }
}

?

并在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) %> */
'
      },
      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']);
};

再次编辑package.json, 在devDependencies中增加grunt-contrib-uglify的依赖库

?

?

{
  name: nodejs-grunt,
  version: 0.0.1,
  private: true,
  scripts: {
    start: node app.js
  },
  dependencies: {
    express: 3.2.2,
    ejs: *
  },
  devDependencies: {
    grunt: ~0.4.1,
    grunt-contrib-uglify: ~0.1.1
  }
}
继而,再在 nodejs-grunt目录下创建 src和build,和nodejs-grunt.js的文件,其中nodejs-grunt.js

?

?

var sayHello=function(name) {
	return 你好 + name;
}
为确保依赖库被加载,需要 再次执行npm install 命令

?

接着执行:

D:
odejs-grunt>grunt
控制台输入如下:

?

?

Running uglify:build (uglify) task
File build/nodejs-grunt.min.js created.
Uncompressed size: 59 bytes.
Compressed size: 40 bytes gzipped (43 bytes minified).

Done, without errors.
打开nodejs-gruntuild odejs-grunt.min.js

?

?

/*! nodejs-grunt 2014-09-08 */
var sayHello=function(l){returnhello +l};
这是一个压缩实例。

?

?

Grunt常用插件

  • grunt-contrib-uglify:压缩js代码
  • grunt-contrib-concat:合并js文件
  • grunt-contrib-qunit:单元测试
  • grunt-contrib-jshint:js代码检查
  • grunt-contrib-watch:监控文件修改并重新执行注册的任务 上述已经讲述一个压缩实例。后面几个插件就不再一一介绍。

    ?

    下面将demo放到资源中供大家免费下载

    http://download.csdn.net/detail/zz410675629/7881187

    参考文章:http://blog.fens.me/nodejs-grunt-intro/





    ?

    ?

    ?




    ?

    ?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Leetcode 细节实现 Length of Las.. 下一篇浅谈C++ 异常处理的语义和性能

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)
·SQL CREATE INDEX 语 (2025-12-25 01:21:45)
·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)