grunt介绍

前端不能承受之痛

1、这是我们的生活

  • 文件压缩:YUI Compressor、Google Closure
  • 文件合并:fiddler + qzmin
  • 文件校验:jshint
  • 雪碧图:cssGaga
  • sass编译:sass/compass
  • 文件打包:require + r.js / seajs + wpm
  • 。。。

2、究竟痛在哪里

下载难 /(版本)管理难

YUI Compressor:https://github.com/yui/yuicompressor

Google Closure:https://code.google.com/p/closure-compiler/downloads/list

jshint:http://www.jshint.com/

其他:。。。

环境依赖、平台依赖

YUI Compressor:JDK

fiddler/qzmin:win平台

sass/compass:ruby

配置使用难:

系统参数设置

工具自己的命令、参数

3、谁能拯救我们

grunt

问题一:grunt是什么

  • 官方定义:The JavaScript Task Runner
  • 民间版本:基于任务的JavaScript项目构建工具
  • 关键词:JavaScript、Task、Runner

问题二:grunt是什么

曾经grunt是: 命令行工具+构建工具+脚手架工具+预定义任务

  • 命令行工具(grunt-cli)
  • 构建工具(grunt)
  • 脚手架工具(grunt-init)
  • 预定义任务(concat、uglify、jshint等)

grunt-cli:

The Grunt command line interface.

Note: The job of the grunt command is to load and run the version of Grunt you have installed locally to your project, irrespective of its version.

grunt:

The JavaScript Task Runner

grunt-init:

Grunt-init is a scaffolding tool used to automate project creation.

问题三:为什么使用grunt

哪些优势

  1. 环境/平台依赖小(node环境、grunt-cli)

  2. 便捷的下载/版本管理(npm)

  3. 插件丰富,极易扩展(目前300++)http://gruntjs.com/plugins

  4. 活跃的社区

demo演示:运行任务

步骤一:安装package

npm install

步骤二:运行任务

文件合并

grunt dist

js文件校验

grunt jshint

grunt项目的要素

Gruntfile.js:必要

Grunt任务的主入口文件,主要作用在于任务的定义与配置

package.json

项目组件依赖的描述文件,非必要

grunt我们需知道什么

  • 基于nodejs(npm)
  • 核心是任务、任务配置(配置即任务)
  • 大部分是文件操作 (基于blob、minmath的文件匹配)
  • 一系列API:file、config、log、task、option等
  • 自定义插件

grunt任务配置

方式一:grunt.initConfig

grunt.initConfig({
clean: {
dev: [ 'dev/' ],
},
jshint: {
all: ['dist/js/**/*.js']
}
});

方式二:grunt.config 接口

grunt.config.set('jshint', {
all: ['dist/js/**/*.js']
});
grunt.task.run('jshint');

grunt Task类型

根据任务类型:

  • 普通任务
  • 插件任务

根据任务位置:

  • 内部任务:Gruntfile.js里定义
  • 外部任务:Gruntfile.js之外定义

grunt Task类型:根据任务类型

普通任务

任务定义

grunt.task.registerTask('hello', '一个无聊的demo', function() {
console.log( '大家好,我是grunt任务!');
});

运行任务

grunt hello

插件任务

任务内部

grunt.registerMultiTask('inline', "同样是很无聊的demo", function() {

    var files = this.filesSrc;  // 用户

    files.forEach(function(filepath){
console.log( '输出文件路径:'+ filepath );
};
});

任务配置

grunt.initConfig({
'inline': {
test: {
src: [$config.distPath+'**/*.html']
}
}
});

运行任务

grunt inline

grunt Task类型:根据任务位置

内部任务

最常见,Gruntfile.js里定义,可满足绝大部分项目的需求

grunt.task.registerTask('hello', '一个无聊的demo', function() {
console.log( '大家好,我是grunt任务!');
});

外部任务

定义方式跟内部任务基本没区别,在Grungfile.js之外定义,用到的时候显式加载即可

加载插件:

grunt.loadNpmTasks('grunt-cdn');

加载自定义任务

grunt.task.loadTasks('proj-task/core');

grunt-inline:一个自定义的grunt插件

grunt-inline作用:将html页面里的声明了__inline标记的<script><link><img>等变成内联资源,即:

  • script:内联脚本
  • link:内联样式
  • img:base64

例子:下面这段script标签,声明了__inline,构建阶段会被行内脚本替换

构建前

<script type="text/javascript" src="modules/common/js/nohost.js?__inline"></script>

构建后

<script>
void function(){setTimeout(function(){var b=document.cookie.match(/(^| )nohost_guid=([^;]*)(;|$)/);if(!b?0:decodeURIComponent(b[2])){var b="/nohost_htdocs/js/SwitchHost.js?random="+Math.random(),c=function(a){try{eval(a)}catch(b){}window.SwitchHost&&window.SwitchHost.init&&window.SwitchHost.init()},a=window.ActiveXObject?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest;a.open("GET",b);a.onreadystatechange=function(){4==a.readyState&&((200<=a.status&&300>a.status||304===a.status||1223===a.status||
0===a.status)&&c(a.responseText),a=null)};a.send(null)}},1500)}();
</script>

grunt-inline:插件创建实战

参见 开源一小步,前端一大步 第二部分《插件编写及发布》

进入实战

@TODO

Yeoman的好基友:Grunt的更多相关文章

  1. 前端工程化开发之yeoman、bower、grunt

    上两遍文章介绍了前端模块化开发(以seaJs为例)和前端自动化开发(以grunt为例)的流程,参见: http://www.cnblogs.com/luozhihao/p/4818782.html ( ...

  2. 前端自动化学习笔记(一)——Yeoman,bower,Grunt的安装

    最近看视频学习了前端自动化的一些知识,确实让我大开眼界.感觉前端越来越神器了.同时跟着视频自己也尝试运用了一些工具去构建前端项目,但是中间遇见了很多坑,磕磕绊绊的才实现了一点功能,所以打算记录一下学习 ...

  3. 2015 前端[JS]工程师必知必会

    2015 前端[JS]工程师必知必会 本文摘自:http://zhuanlan.zhihu.com/FrontendMagazine/20002850 ,因为好东东西暂时没看懂,所以暂时保留下来,供以 ...

  4. [ 学习路线 ] 2015 前端(JS)工程师必知必会 (2)

    http://segmentfault.com/a/1190000002678515?utm_source=Weibo&utm_medium=shareLink&utm_campaig ...

  5. Visual Studio 2015 开发 ASP.NET 5 有何变化?

    本篇博文目录: ASP.NET 5 模版 ASP.NET 5 目录结构 前端管理工具 无编译开发 Microsoft Git Provider 智能感知和错误信息 Smart Unit Testing ...

  6. 2014 Visual Studio Contact(); 直播笔记

    昨天微软干了几件了不起的事:.NET开发环境将开源.跨平台支持(Mac OS X和Linux).多设备支持(WP.Android和iOS)和Visual Studio免费(Visual Studio ...

  7. AngularJS+NodeJS环境搭建

    需要安装的软件: node-v0.12.7-x64.msi python-2.7.10.amd64.msi Git-2.5.1-64-bit.exe (注意:Git安装时,需要选择的步骤)  安装位置 ...

  8. Visual Studio Contact

    Visual Studio Contact(); 直播笔记   昨天微软干了几件了不起的事:.NET开发环境将开源.跨平台支持(Mac OS X和Linux).多设备支持(WP.Android和iOS ...

  9. angularjs框架及其生态环境 --待续

    angular的MVVM框架结构:     1. app,   2.routes, config,   3.module,   4.Controller, $scope,controller参数,事件 ...

随机推荐

  1. sqlserver sql优化案例及思路

    始sql: SELECT TOP 100 PERCENT ZZ.CREW_NAME AS 机组, ZZ.CREW_ID, AA.年度时间, CC.当月时间, DD.连续七天时间 AS 最近七天 FRO ...

  2. orcl 如何快速删除表中百万或千万数据

    orcl 数据库表中数据达到上千万时,已经变的特别慢了,所以时不时需要清掉一部分数据. bqh8表中目前有10000000条数据,需要保留19条数据,其余全部清除掉. 以下为个人方法: 1.首先把需要 ...

  3. selenium - pycharm三种案例运行模式

    1.unittest 运行单个用例 (1)将鼠标放到对应的用例,右键运行即可 2.unittest运行整个脚本案例 将鼠标放到if __name__ == "__main__": ...

  4. android下载 sdk 的两个代理 ,解决下载sdk慢的问题

    mirrors.opencas.cn mirrors.neusoft.edu.cn   设置教程:http://blog.csdn.net/mociml/article/details/1633125 ...

  5. PyQt5--EventHandler

    # -*- coding:utf-8 -*- ''' Created on Sep 14, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...

  6. Maven配置setting.xml值Mirror与Repository区别

    1 Repository(仓库) 1.1 Maven仓库主要有2种: remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问 local repository ...

  7. metamask-iframe-stream,没成功

    https://github.com/kumavis/iframe-stream/blob/master/test/rebundle.js iframe-stream-其实就是将iframe打包成流 ...

  8. day4-课堂笔记

    变量 成员变量-构造方法里定义 self.xxx 使用:类内部: self.xxx 类外部: 先创建实例 实例.xxx 类变量 类定义下面直接定义 使用:类方法内 cls.xxx 类名.xxx sel ...

  9. 在openresty或nginx编译nginx-upsync-module&nginx_upstream_check_module

    针对我在编译在两个模块的过程中遇到的一系列问题,特此记录编译流程的一些细节 1.下载 install git git clone https://github.com/weibocom/nginx-u ...

  10. Docker学习4-Containers - 容器

    用Docker方式构建应用程序,从这个应用程序层次结构的底层容器开始.高于此级别的是一项服务,它定义了容器在生产中的行为方式.在顶层是堆栈,它定义了所有服务的交互. Stack  堆栈 Service ...