什么是Grunt
Grunt,简而言之,就是运行在Node.js上面的任务管理器(task runner),其可以在任何语言和项目中自动化指定的任务。我们可通过npm来安装Grunt和Grunt插件
为什么使用Grunt?
一词概括:自动化。
Grunt帮助你提高重复项目的性能,比如:
- Minification
- Compilation
- Unit testing
- Linting and more
安装 Grunt CLI:
为了获得Grunt的更多产品特性,你需要全局安装Grunt's 命令行接口(CLI),运行下面的代码:
- npm install -g grunt-cli
上述将你额grunt命令运行在你的系统路径上,这确保了其可以在任何的文件夹下面运行成功。
注意安装了grunt-cil并不意味这安装了Grunt 任务管理器(task runner)!
Grunt CLI是运行安装在邻近Gruntfile的Grunt版本。这允许了在同一台机器上同时安装不同版本的Grunt。
怎样在你的项目中使用Grunt
1.创建项目根目录
首先创建一个空的项目根目录且在目录中创建下面的文件:
- package.json :这是npm使用的文件,用来存储作为npm模块发布的项目的meta数据。如果我们想要列举Grunt或者Grunt插件,那么我们需要在package.json文件中将此项目作为devDependencies。 这个文件需要放在项目的根目录中,你也可以使用nmp init命令来生成这个文件
- cruntfile.js这个文件用来设置和定义任务以及加载Grunt插件。这个Gruntfile.js 文件同样需要放置在项目根目录中。 你如果需要在coffee脚本中配置任务,你可以同样命名此文件为 Gruntfile.coffee 。下文中将会讨论这个文件的具体内容。
2.增加Grutn 模块到你的项目中
我们接下来需要使用npm来增加Grunt 模块到你的项目中
运行如下语句
- > npm install grunt --save-dev
上述命令安装了Grutn同时也在package,json中创建了一个入口,此入口说明了这个模块将作为devDependency。 被标记为devDependency的模块只能是在开发环境中(development environment)安装的模块。在产品环境(producion environment)中安装它们将被忽略。
理解Gruntfile文件
Gruntfile是一个有效的js或者CooffeeScript文件,放置在项目更目录中,和package.json一起放置,并且需要和你的项目元一起提交。 一个Gruntfile文件包含下面三部分:
1.“包裹(wrapper)”函数
2.项目和任务配置
3.加载的Grunt插件和任务
4.典型任务(custom tasks)
让我们逐一讨论:
关于Gruntfile中的“包裹(wrapper)”函数
wrapper函数是指定到module exports上的封装所有Grunt代码的函数。这个函数被Grunt引擎调用,并且作为Grunt配置的入口点。因此,至此我们的Gruntfile看起来如下:
- module.exports = function(grunt) {
- // Do grunt-related things in here
- };
所有的Gruntfile和Grunt 插件使用上述基本形式,且你所有的Grunt代码必须写在函数中
Gruntfile中的项目和任务配置
大多数的Grunt任务依赖于赋值到grunt.initConfig方法上的对象中的配置数据。 这就是我门为什么需要为我们需要的插件指定配置参数。我们可以同样指定我们将要适用的配置参数。让我们定义一些随机的配置参数:
- module.exports = function(grunt) {
- // Project configuration.
- grunt.initConfig({
- //Lets read the project package.json
- pkg: grunt.file.readJSON('package.json'),
- //Some other configs
- someKey: 'Some Value',
- author: 'Modulus.io'
- });
- };
Grunt同样有一些帮助方法;其中的一个就是阅读文件。
所以我们使用grunt.file.readJson 来读取我们的package.json 文件并且存储转义好的对象到pkg key中。
我们同样可以将这些配置值设置成变量,动态化你的配置字符串。如下:
- module.exports = function (grunt) {
- // Project configuration.
- grunt.initConfig({
- //Lets read the project package.json
- pkg: grunt.file.readJSON('package.json'),
- //Some other configs
- someKey: 'Some Value',
- author: 'Modulus.io',
- filePostfix: '-<%= pkg.name %>-<%= pkg.version %>',
- greetingMessage: 'Running Grunt for project: <%= pkg.name %>, Version: <%= pkg.version %>, Author: <%= author %>'
- });
- };
上面我们使用 <%= varName %> 来确定string中的动态文本
加载的Grunt以及Grunt 配置
许多通常使用的任务比如concatenation,minificationhe linting对于Grunt插件而言是可用的,只要你在安装npm的时候在package.js中将插件定义成dependency,那么就可以在Gruntfile内部使用。
让我们安装一个简单的插件,grunt-clean到你的项目总共且尝试在Gruntfile中配置。使用下述命令通过vpm来安装这个模块
- > npm install grunt-contrib-clean --save-dev
现在我们可以通过grunt.loadNpmTask('pluginName') 方法在我们的Gruntfile中加载这个任务,且做一些所需的配置。现在我们的Gruntfil文件如下:
- module.exports = function (grunt) {
- // Project configuration.
- grunt.initConfig({
- //Lets read the project package.json
- pkg: grunt.file.readJSON('package.json'),
- //Some other configs
- someKey: 'Some Value',
- author: 'Modulus.io',
- filePostfix: '-<%= pkg.name %>-<%= pkg.version %>',
- greetingMessage: 'Running Grunt for project: <%= pkg.name %>, Version: <%= pkg.version %>, Author: <%= author %>',
- //Configure Clean Module
- clean: ['.tmp', 'dist', 'npm-debug.log']
- });
- // Load the plugin that provides the "clean" task.
- grunt.loadNpmTasks('grunt-contrib-clean');
- };
在上面的例子中,我们配置 clean 来删除 .tmp和dist文件夹以及项目根文件的npm-debug.log文件。你可指定任何数量的文件夹或者磁盘。每一个插件都有文档,用来指明必要的配置参数和形式(format)
注意:grunt --help 命令展示出所有可用的任务(tasks)
现在,在项目根目录下面尝试运行以下代码:
- > grunt clean
这个命令用来删除特定的文件和配置的文件
Gruntfile中特定的子任务
因为子任务(sub task)被很多的插件支持,所以你同样可以设置子任务。为了理解这个概念,让我们看看下面的例子:
- module.exports = function (grunt) {
- ***javascript***
- // Project configuration.
- grunt.initConfig({
- //Lets read the project package.json
- pkg: grunt.file.readJSON('package.json'),
- //Some other configs
- someKey: 'Some Value',
- author: 'Modulus.io',
- filePostfix: '-<%= pkg.name %>-<%= pkg.version %>',
- greetingMessage: 'Running Grunt for project: <%= pkg.name %>, Version: <%= pkg.version %>, Author: <%= author %>',
- //Configure Clean Module
- clean: {
- npm: 'npm-debug.log',
- temp: ['temp', '.tmp'],
- dist: ['dist', 'out/dist']
- }
- });
- // Load the plugin that provides the "clean" task.
- grunt.loadNpmTasks('grunt-contrib-clean');
- };
在参数配置中我们指明了一个有着不同健(keys)的对象,这些健包含npm,temp和dist。
为划分任务成为子任务,我们可以想出很多的随意的名字。 现在我们可以运行子任务(subtasks)来删除所有上述的文件和磁盘,或者来删除一个子集团(subgroup)。下面进行说明:
#删除所有的文件包括npm-debug.log, temp, .tmp, dist, out/dist : grunt clean
#只删除npm-debug.log 文件:grunt clean:npm
#只删除temp文件: grunt clean:temp
#只删除dist, out/dist文件:grunt clean:dist
在上面的例子中,我们特定化了在config中设置的key来运行子任务(subtask)。用来书写key的语法是:
- > grunt TaskName:SubTaskKeySpecifiedInConfig
用户自定义任务custom tasks
你可以使用自定义的名字来定义任务的名字。可以给已经存在的任务联合在一起命名亦可只是针对你自己的implementation来命名。如果你想implement自定义的任务,你必须在js中implement
让我们来给已经存在的任务的集合命名,且也给我们自己的implementment命名:
- module.exports = function (grunt) {
- // Project configuration.
- grunt.initConfig({
- //Lets read the project package.json
- pkg: grunt.file.readJSON('package.json'),
- //Some other configs
- someKey: 'Some Value',
- author: 'Modulus.io',
- filePostfix: '-<%= pkg.name %>-<%= pkg.version %>',
- greetingMessage: 'Running Grunt for project: <%= pkg.name %>, Version: <%= pkg.version %>, Author: <%= author %>. ',
- //Configure Clean Module
- clean: {
- npm: 'npm-debug.log',
- temp: ['temp', '.tmp'],
- dist: ['dist', 'out/dist']
- }
- });
- // Load the plugin that provides the "clean" task.
- grunt.loadNpmTasks('grunt-contrib-clean');
- //Lets register a basic task.
- grunt.registerTask('print-info', 'Lets print some info about the project.', function() {
- grunt.log.write(grunt.config('greetingMessage')).ok();
- });
- //Specify a custom task which is combination of tasks.
- grunt.registerTask('my-clean', ['print-info', 'clean:dist', 'clean:npm']);
- };
在上述例子中,我们创建了一个用户自定义项目print-info, 且在js中执行它。
我们使用grunt.registerTask()方法。
同时我们也定义了一个用户自定义的任务my-clean,这是其他任务的联合体。
因为你在任务中运行任何的js文件,所以命名有多重可能性。
使用下面的命令来看看哪些任务是可用的:
- > grunt --help
你应该可见两个你指定的用户自定义任务(custom tasks),现在你可以运行你的my-clean 任务:
- > grunt my-clean
默认任务:
当你木有特别指明任何的文件就在你的项目根目录下面运行grunt,那么代码将寻找默认的任务且运行它。
你可以通过简单的发布一项指令来指定默认的任务
- > grunt
指明一个默认的任务,我们简单的注册了一个名字为default的任务。
现在我们的Grutnfile看起来如下:
- module.exports = function (grunt) {
- ***javascript***
- // Project configuration.
- grunt.initConfig({
- //Lets read the project package.json
- pkg: grunt.file.readJSON('package.json'),
- //Some other configs
- someKey: 'Some Value',
- author: 'Modulus.io',
- filePostfix: '-<%= pkg.name %>-<%= pkg.version %>',
- greetingMessage: 'Running Grunt for project: <%= pkg.name %>, Version: <%= pkg.version %>, Author: <%= author %>. ',
- //Configure Clean Module
- clean: {
- npm: 'npm-debug.log',
- temp: ['temp', '.tmp'],
- dist: ['dist', 'out/dist']
- }
- });
- // Load the plugin that provides the "clean" task.
- grunt.loadNpmTasks('grunt-contrib-clean');
- //Lets register a basic task.
- grunt.registerTask('print-info', 'Lets print some info about the project.', function() {
- grunt.log.write(grunt.config('greetingMessage')).ok();
- });
- //Specify a custom task which is combination of tasks.
- grunt.registerTask('my-clean', ['print-info', 'clean:dist', 'clean:npm']);
- //Specify a default task
- grunt.registerTask('default', ['my-clean', 'clean:temp']);
- };
官网:https://gruntjs.com/api/grunt
来源:https://blog.xervo.io/introduction-to-grunt
什么是Grunt的更多相关文章
- 初学seaJs模块化开发,利用grunt打包,减少http请求
原文地址:初学seaJs模块化开发,利用grunt打包,减少http请求 未压缩合并的演示地址:demo2 学习seaJs的模块化开发,适合对seajs基础有所了解的同学看,目录结构 js — —di ...
- grunt配置任务
这个指南解释了如何使用 Gruntfile 来为你的项目配置task.如果你还不知道 Gruntfile 是什么,请先阅读 快速入门 指南并看看这个Gruntfile 实例. Grunt配置 Grun ...
- 快速开发Grunt插件----压缩js模板
前言 Grunt是一款前端构建工具,帮助我们自动化搭建前端工程.它可以实现自动对js.css.html文件的合并.压缩等一些列操作.Grunt有很多插件,每一款插件实现某个功能,你可以通过npm命名去 ...
- 是时候搁置Grunt,耍一耍gulp了
也算是用了半年Grunt,几个月前也写过一篇它的入门文章(点此查看),不得不说它是前端项目的一个得力助手.不过技术工具跟语言一样日新月异,总会有更好用的新的东西把旧的拍死在沙滩上(当然Grunt肯定没 ...
- 应用Grunt自动化地优化你的项目前端
在不久前我曾写了一篇 应用r.js来优化你的前端 的文章,为大家介绍了r.js这个实用工具,它可以很好地压缩.合并前端文件并打包整个项目.但是如果将r.js放到项目中,我们不得不顾及到一个问题——项目 ...
- Grunt(页面静态引入的文件地址的改变探究)-V2.0
相关插件的引用: grunt-usemin 对页面的操作 grunt-contrib-cssmin 压缩css load-grunt-tasks 瘦身gruntfile grunt-rev给md5 ...
- Grunt基本使用-V1.0
浅语:grunt中文网:http://www.gruntjs.net/ 第一步:Grunt 依赖 Node.js 所以在安装之前确保你安装了 Node.js.然后开始安装 Grunt. 实际上,安装的 ...
- nodejs、npm、grunt——名词解释
最近着手开发一个新项目,打算从工程化的角度整理一套自己的前端开发.发布体系. grunt这些工具,之前别人用我也用,并没有认真想过它们的前世今生,正好趁着这个机会,我来理一理目前业界比较流行这些工具的 ...
- grunt自定义任务——合并压缩css和js
npm文档:www.npmjs.com grunt基础教程:http://www.gruntjs.net/docs/getting-started/ http://www.w3cplus.com/to ...
- Grunt学习使用
原文地址:Grunt学习使用必看 grunt简介神马的不多说,到处一大堆. 我只说说我已经实现了的代码. 按照官方的教程 相信已经配置好了,接下来说 package.json 和 Gruntfile. ...
随机推荐
- vue 组件创建与销毁
vue 组件(如对话框组件)实时创建与销毁: 使用v-if <search-history :show="showSearchHistory" @close="sh ...
- c/c++:回调函数
1:函数名为指针 首先,在C语言中函数是一种function-to-pointer的方式,即对于一个函数,会将其自己主动转换成指针的类型.如: 1 #include<stdio.h> 2 ...
- SAS连接MYSQL的步骤及引用数据表
1.建立逻辑库 libname dz ’物理路径'; 2.逻辑库做为桥梁连接SAS与MYSQL libname dz MYSQL USER=***** PASSWORD=**** DATABA ...
- SQLSERVER聚集索引和主键(Primary Key)的误区认识
引用别人的,供以后学习使用,谢谢! 很多人会把Primary Key和聚集索引搞混起来,或者认为这是同一个东西.这个概念是非常错误的. 主键是一个约束(constraint),他依附在一个索引上,这个 ...
- log4j email EmailDailyRollingFileAppender
log4j发送日志邮件, 纠正非网上流传的"达到 BufferSize KB就会发送邮件", 另外重写了一个发送邮件的类DailyRollingFileAppender. 用于定期 ...
- 彻底解决zend studio 下 assignment in condition警告
最近在mac系统下安装zend studio作为php开发工具,把以前的代码导入,发现项目中有很多 “assignment in condition”的警告,造成原因是在条件判断的if.while中使 ...
- NGINX下如何自定义404页面
什么是404页面 如果碰巧网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是 ...
- jQuery Validate(三)
这里,我们再说说radio.checkbox.select的验证方式. 1.用新版的写法进行验证. <!DOCTYPE html> <html> <head> &l ...
- charles 4.x 破解版安装 以及使用
下载地址 https://pan.baidu.com/s/1dFvYM7B 破解方法 未破解的情况下,每30分钟会弹出一个提示,然后关闭软件 将压缩包内的 charles.jar 复制到安装目录下,替 ...
- Docker入门系列8
commit docker commit -m "Added json gem" -a "Docker Newbee" 0b2616b0e5a8 ouruser ...