压缩、编译、单元测试、代码检查等 咱们须要自动化,没必要重复劳动,减少工做量。为什么选择Grunt呢?好像是没有更好的选择了。css
Grunt基于Node.js,安装以前要先安装Node.js。node
shellbrew install node
shellsudo npm install npm -g
安装 grunt-cli 并不等于安装了 Grunt 任务运行器
Grunt CLI 的任务是运行 Gruntfile 指定的 Grunt 版本。 这样就能够在一台电脑上同时安装多个版本的 Grunt。(没有懂,俺直接实战)git
shellnpm install -g grunt-cli
建立文件必须建立文件夹,我创建了一个
test
的文件夹,一个标准的grunt
项目中必须有两个文件。githubpackage.json:用于保存项目元数据。
Gruntfile.js : 用于配置或定义任务、加载 Grunt 插件。shell在test1文件夹中建立这两个文件吧。那么文件中写什么内容呢。咱们首先来关注一下package.json写什么内容npm
json{ "name": "test", "version": "1.0.0" }
在项目的根目录下运行下面的命令json
shellnpm install
lognpm WARN package.json test1@1.0.0 No description npm WARN package.json test1@1.0.0 No repository field. npm WARN package.json test1@1.0.0 No README data
提示说没有描述信息没有README之类的
在根目录添加一个 README.md 文件数组
在 package.json 中添加下面内容以后sass
{ "name": "test", "version": "1.0.0", "description": "测试的例子", "repository": { "type": "git", "url": "https://github.com/JSLite/JSLite.git" } }
因而乎万事大吉了app
在此项目中安装 Grunt 插件
shellsudo npm install grunt --save-dev
package.json的文件内容发现多了 devDependencies
的信息
目录多了一个 node_modules
的文件夹
json{ "name": "test", "version": "1.0.0", "description": "测试的例子", "repository": { "type": "git", "url": "https://github.com/JSLite/JSLite.git" }, "devDependencies": { "grunt": "^0.4.5" } }
运行 npm install <module> --save-dev
,不只会安装指定的 模块,还会自动添加到 devDependencies
区域中,且包括 版本范围。
插件用途:压缩js,css文件
插件名称:grunt-contrib-uglify
shellsudo npm install grunt-contrib-uglify --save-dev
安装成功以后 在package.json的文件内容中的 devDependencies
的信息又增长了
json"devDependencies": { "grunt": "^0.4.5", "grunt-contrib-uglify": "^0.8.0" }
还记得咱们上面说一个Grunt项目要两个必须的文件一个
package.json
和Gruntfile.js
,注意大小写,Linux区分大小写的,建立Gruntfile.js
并写入以下内容
js// 包装函数 module.exports = function(grunt) { // 任务配置,全部插件的配置信息 grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), // uglify插件的配置信息 uglify: { options: { banner: '/*! This is uglify test - ' + '<%= grunt.template.today("yyyy-mm-dd") %> */', beautify: true,//是否压缩 mangle: false, //不混淆变量名 compress:true,//打开或关闭使用默认选项源压缩。 }, app_task: { files: { 'build/app.min.js': ['lib/index.js', 'lib/test.js'] } } } }); // 告诉grunt咱们将使用插件 grunt.loadNpmTasks('grunt-contrib-uglify'); // 告诉grunt当咱们在终端中输入grunt时须要作些什么 grunt.registerTask('default', ['uglify']); };
build/app.min.js
文件生成内容如日期什么的lib
目录中的 js
压缩合并生成到 build
目录中生成 app.min.js
输入命令下面命令,好了,个人没有错误正常的。若是有错误,会有错误日志,本身分析哦。
shellgrunt
插件用途:监控文件变化并自动运行grunt的watch插件
插件名称:grunt-contrib-watch
shellsudo npm install grunt-contrib-watch --save-dev
修改
Gruntfile.js
initConfig 中添加了
js// watch插件的配置信息 watch: { another: { files: ['lib/*.js'], tasks: ['uglify'], options: { // Start another live reload server on port 1337 livereload: 1337 } } }
增长了一行
jsgrunt.loadNpmTasks('grunt-contrib-watch');
数组中添加了watch
jsgrunt.registerTask('default', ['uglify','watch']);
添加了watch后,完整的 Gruntfile.js
js// 包装函数 module.exports = function(grunt) { // 任务配置,全部插件的配置信息 grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), // uglify插件的配置信息 uglify: { options: { banner: '/*! This is uglify test - ' + '<%= grunt.template.today("yyyy-mm-dd") %> */' }, app_task: { files: { 'build/app.min.js': ['lib/index.js', 'lib/test.js'] } } }, // watch插件的配置信息 watch: { another: { files: ['lib/*.js'], tasks: ['uglify'], options: { // Start another live reload server on port 1337 livereload: 1337 } } } }); // 告诉grunt咱们将使用插件 grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-watch'); // 告诉grunt当咱们在终端中输入grunt时须要作些什么 grunt.registerTask('default', ['uglify','watch']); };
stylus
shellsudo npm install grunt-contrib-watch --save-dev
Gruntfile.js
initConfig
中添加了
jsstylus:{ build: { options: { linenos: false, compress: false, banner: '\/** \n * <%= pkg.name %> - <%= pkg.description %>\n * version <%= pkg.version %> \n * author <%= pkg.author %> \n * date <%= grunt.template.today() %> \n**/\n' }, files: [{ 'css/historyDetail.css': 'styl/historyDetail.styl' }] } },
下面添加
jsgrunt.loadNpmTasks('grunt-contrib-stylus');
paths
将自动使用@import来引入一些Stylus
文件,好比一些Mixin
集合,放在一个Stylus文件中进行维护,写在paths中后,就能够在每一个Stylus
文件中调用它们。define
能够定义一些全局变量,而后在Stylus
中使用,但我不喜欢使用这个配置,而是更喜欢把全局变量放在一个单独的Stylus
文件中,而后将这个文件加入paths的数组中。一句话,把全部不会直接产出CSS的Stylus
代码分红若干个Stylus
文件,而后所有添加到paths
中,这样在全部Stylus
文件中均可以随时调用了,但要注意这些Stylus
文件的调用关系和使用前后顺序。
compress
及 linenos
是两个Boolean值,用来控制是否压缩处理后的CSS代码以及是否在CSS代码中保留注释。
banner
是一个字符串,会被放置在CSS文件的最前面,通常我用来写注释,好比
banner: '\/** \n * <%= pkg.name %> - <%= pkg.description %>\n * version <%= pkg.version %> \n * author <%= pkg.author %> \n * date <%= grunt.template.today() %> \n**/\n'
firebug
将控制是否使用一个Firebug
的Stylus
插件FireStylus for Firebug
,能够在Firefox中调试Stylus。
use
能够引入一些Stylus
的其余grunt
插件。
grunt-contrib-clean
:删除文件。npm>>grunt-contrib-compass
:使用compass编译sass文件。npm>>grunt-contrib-concat
:合并文件。npm>>grunt-contrib-copy
:复制文件。npm>>grunt-contrib-cssmin
:压缩以及合并CSS文件。npm>>grunt-contrib-imagemin
:图像压缩模块。npm>>grunt-contrib-jshint
:检查JavaScript语法。npm>>grunt-contrib-uglify
:压缩以及合并JavaScript文件。npm>>grunt-contrib-watch
:监视文件变更,作出相应动做。npm>>grunt-contrib-stylus
:stylus编写styl输出css npm>>