GRUNT安装与配置

Posted on 2016-08-19 18:13 听风吹来的种子 阅读(47) 评论(0) 编辑 收藏

安装 CLI

npm install -g grunt-cli//全局安装

npm init //初始化package.json

npm init   命令会创建一个基本的package.json文件。

1
2
3
npm install grunt --save-dev
 
npm install grunt-contrib-jshint --save-dev

  

.......//安装你需要的依赖

安装完以后在package.json的同级创建一个Gruntfile.js配置文件

配置如下

  1 module.exports = function(grunt) {
2 var timestamp = new Date().getTime();
3 grunt.initConfig({
4 pkg: grunt.file.readJSON('package.json'),
5 //文件合并
6 concat: {
7 options: {
8 //定义一个用于插入合并输出文件之间的字符
9 seperator: ';'
10 },
11 build: {
12 //将要合并的文件
13 // src:['src/**/*.js'],
14 //合并后的js文件的存放位置
15 // dest:['build/<%= pkg.name %>.js']
16 files: { // Dictionary of files
17 'dist/css/main.css': ['src/**/*.css', '!src/**/*.min.css'],
18 'dist/js/index.js': 'src/**/*.js'
19 //'src/sass/all.scss':'src/sass/*.scss'
20 }
21 },
22 concatsass: {
23 files: {
24 'src/sass/all.scss': 'src/sass/*.scss'
25 }
26 }
27 },
28 //压缩js
29 uglify: {
30 options: {
31 banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
32 },
33 /*dist:{
34 files:{
35 'dist/<%= pkg.name %>.min.js':['<%= concat.dist.dest %>']
36 }
37 }*/
38 build: {
39 src: 'dist/js/index.js',
40 dest: 'dist/js/index.min.js'
41 }
42 },
43 //压缩html
44 htmlmin: {
45
46 options: { // Target options
47 removeComments: true,
48 removeCommentsFromCDATA: true
49 // collapseWhitespace: true,
50 // minifyCSS:true
51 // collapseBooleanAttributes: true,
52 // removeAttributeQuotes: true,
53 // removeRedundantAttributes: true,
54 // useShortDoctype: true,
55 // removeEmptyAttributes: true,
56 // removeOptionalTags: true
57 },
58 html: {
59 files: [{ // Dictionary of files
60 expand: true,
61 cwd: 'dist',
62 src: ['**/*.html'],
63 dest: 'dist' //'destination':'source'
64 }]
65 }
66 },
67 //js的语法检测
68 jshint: {
69 files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js', '!src/**/*.min.js'],
70 options: {
71 //这里是覆盖JSHint默认配置的选项
72 globals: {
73 jQuery: true,
74 console: true,
75 module: true,
76 document: true
77 }
78 }
79 },
80 //css压缩
81 cssmin: {
82 options: {
83 //shorthandCompactiong:false,
84 roundingPercision: -1 //这个属性应该是将样式相同的都提取出来
85 },
86 build: {
87 files: {
88 // 'dist/css/main.css': 'dist/css/main.css'
89 }
90 }
91 },
92 //
93 // qunit:{
94 // files:['test/**/*.html']
95 // },
96 //监听事件
97 watch: {
98 build: {
99 files: ['<%= jshint.files %>', 'src/**/*.css'],
100 tasks: ['jshint', 'qunit'],
101 options: { spawn: false }
102 }
103
104 },
105 //处理html中css、js 引入合并问题
106 usemin: {
107 html: 'dist/**/*.html',
108 options: {
109 blockReplacements: {
110 js: function(block) {
111 return '<script type="text/javascript" src="' + block.dest + '?t=' + timestamp + '"></script>';
112 },
113 css: function(block) {
114 return '<link rel="stylesheet" type="text/css" href="' + block.dest + '?t=' + timestamp + '"/>';
115 }
116 }
117 }
118 },
119 //
120
121 //copy
122 copy: {
123 src: {
124 files: [
125 { expand: true, cwd: 'src', src: ['**/*.html'], dest: 'dist' }, //设置的相对于哪个路径找文件就是cwd的值,这里我写的html在test文件里,所以设置成test;所有的源文件路径,都是相对于cwd
126 //dist 目标路径
127 { expand: true, cwd: 'src/css', src: ['*.min.css'], dest: 'dist/css' },
128 { expand: true, cwd: 'src/js', src: ['*.min.js'], dest: 'dist/js' }
129 ]
130 },
131 image: {
132 files: [
133 { expand: true, cwd: 'src', src: ['images/**/*.{png,jpg,jpeg,gif}'], dest: 'dist' }
134 ]
135 },
136 robots: {
137 files: [
138 { expand: true, cwd: 'src', src: ['robots.txt'], dest: 'dist' }
139 ]
140 }
141 },
142 //图片压缩
143 imagemin: {
144 static: { // Target
145 options: { // Target options
146 optimizationLevel: 3,
147 svgoPlugins: [{ removeViewBox: false }],
148 use: []
149 },
150 files: { // Dictionary of files
151 // 'dist/img.png': 'src/img.png', // 'destination': 'source'
152 // 'dist/img.jpg': 'src/img.jpg',
153 // 'dist/img.gif': 'src/img.gif'
154 }
155 },
156 dynamic: { // Another target
157 files: [{
158 expand: true, // Enable dynamic expansion
159 cwd: 'src/', // Src matches are relative to this path
160 src: ['**/*.{png,jpg,gif}'], // Actual patterns to match
161 dest: 'dist' // Destination path prefix
162 }]
163 }
164 },
165 //requirejs 打包
166 requirejs: {
167 options: {
168 baseUrl: '',
169 paths: {
170 "$": 'src/zepto',
171 "_": 'src/underscore',
172 "B": 'src/backbone'
173 },
174 include: ['$', '_', 'B'],
175
176 out: 'dist/libs.js' //会将include里面的文件打包为out对应的文件,paths的本身意义不大,就是用于配置include里面的指向
177 }
178 },
179 //sprite 雪碧图 //grunt-spritesmith
180 sprite: {
181 options: {
182 //追加时间戳,默认不追加
183 spritestamp: true
184 },
185 //image-set 示例
186 all: {
187 src: 'src/images/*.jpg',
188 dest: 'dist/images/spritesheet.png',
189 destCss: 'dist/css/sprites.css'
190 }
191 },
192 sass: {
193 dist: {
194 // src:'src/**/*.scss',
195 // dest:'dist/css/compiled.css'
196 options: { // Target options
197 style: 'expanded'
198 },
199 files: {
200 'src/css/all.css': 'src/sass/all.scss'
201 }
202 }
203
204 },
205 //清理文件
206 clean: {
207 html: ['dist/**/*.html'],
208 sass: ['src/sass/all.scss'],
209 css: ['dist/css/*.css'],
210 js: ['dist/js/*.js'],
211 images: ['dist/images/**/*.{png,jpg,jpeg,gif}']
212 },
213 //合并 html
214 includereplace: {
215 dist: {
216 files: [
217 { src: ['**/*.html'], dest: 'dist', expand: true, cwd: 'src' }
218 ]
219 }
220 }
221
222 });
223
224 //加载包含"uglify" 任务的插件
225 grunt.loadNpmTasks('grunt-contrib-uglify');
226 grunt.loadNpmTasks('grunt-contrib-copy');
227 grunt.loadNpmTasks('grunt-contrib-jshint');
228 //grunt.loadNpmTasks('grunt-contrib-qunit');
229 grunt.loadNpmTasks('grunt-contrib-htmlmin');
230 grunt.loadNpmTasks('grunt-contrib-cssmin');
231 grunt.loadNpmTasks('grunt-contrib-watch');
232 grunt.loadNpmTasks('grunt-contrib-concat');
233 grunt.loadNpmTasks('grunt-contrib-imagemin');
234 grunt.loadNpmTasks('grunt-usemin');
235 grunt.loadNpmTasks('grunt-contrib-requirejs');
236 grunt.loadNpmTasks('grunt-spritesmith');
237 grunt.loadNpmTasks('grunt-contrib-sass');
238 grunt.loadNpmTasks('grunt-contrib-clean');
239 grunt.loadNpmTasks('grunt-include-replace');
240
241 //默认被执行的任务列表
242 //grunt.registerTask('dev',['clean','copy','concat','sass','uglify','copy','usemin','htmlmin']);
243 grunt.registerTask('dev', ['clean', 'jshint', 'copy', 'concat:concatsass', 'sass', 'concat:build', 'uglify', 'includereplace', 'usemin']);
244 grunt.registerTask('dist', ['clean', 'jshint', 'copy', 'concat:concatsass', 'sass', 'concat:build', 'uglify', 'cssmin', 'includereplace', 'usemin']);
245 };

Gruntfile.js文件配置项的更多相关文章

  1. 如何使用grunt压缩js文件

    jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位web前端开发人员,如果你现在还不知道grunt或者听说过 ...

  2. grunt压缩js文件

    grunt是node中很好的管理项目的工具,利用它可以实现对整个项目的管理,避免很多重复性的工作如合并.压缩,检查语法等. 使用grunt首先要安装node环境,nodejs官网http://node ...

  3. grunt压缩多个js文件和css文件

    压缩前的工程目录: 1.安装js,css需要的插件 使用npm安装:npm install grunt-contrib-uglify --save-dev  -------->安装js压缩插件 ...

  4. 吉特仓储管系统(开源)--使用Grunt压缩JS文件

    在吉特仓储管理系统开发的过程中大量使用到了JS,随着JS文件的增多我们需要对JS进行有效的管理,同时也要对JS文件进行一些压缩.文本用于记录一下使用grunt压缩JS的操作步骤,便于遗忘之后记录查找, ...

  5. 一路编程 -- Gruntfile.js

    <一路编程> Steven Foote 第四章构建工具 中的 Gruntfile.js 文件的 JSHint 部分,如果按照书中所写,run  grunt 的命令的时候会出错. 此处附上完 ...

  6. Javascript模块化开发2——Gruntfile.js详解

    一.grunt模块简介 grunt插件,是一种npm环境下的自动化工具.对于需要反复重复的任务,例如压缩.编译.单元测试.linting等,自动化工具可以减轻你的劳动,简化你的工作.grunt模块根据 ...

  7. 使用grunt-init自动创建gruntfile.js和package.json文件

    使用grunt-init可以自动创建gruntfile.js和package.json文件.下面说一下过程: 1.全局安装grunt-init npm install -g grunt-init 2. ...

  8. Gruntfile.js模板

    module.exports = function(grunt) { // 配置项 var AppConfig = { name: 'app', //源文件目录 src: 'app/src', //生 ...

  9. 编写可维护的 Gruntfile.js

    load-grunt-tasks 插件 首先介绍下 load-grunt-tasks 这个插件. 我们一般都会把所有用到的插件以及插件的配置写到 Gruntfile.js 里面,对于小项目来说这个文件 ...

随机推荐

  1. WebRTC开发基础(WebRTC入门系列2:RTCPeerConnection)

    RTCPeerConnection的作用是在浏览器之间建立数据的“点对点”(peer to peer)通信. 使用WebRTC的编解码器和协议做了大量的工作,方便了开发者,使实时通信成为可能,甚至在不 ...

  2. d3.js在vue项目中的安装及案例

    1. 安装: npm i d3 --save 2. 引入:main.js import * as d3 from "d3"; Vue.prototype.$d3 = d3; win ...

  3. (转)学会数据库读写分离、分表分库——用Mycat,这一篇就够了!

    原文:https://www.cnblogs.com/joylee/p/7513038.html 系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理 ...

  4. (转)Python3入门之线程threading常用方法

    原文:https://www.cnblogs.com/chengd/articles/7770898.html https://blog.csdn.net/sunhuaqiang1/article/d ...

  5. idea自己用得到的命令

    1.注释 Ctrl + / 单行注释 . 取消注释 Ctrl + Shift + / 多行注释 .取消注释 2.查找 Ctrl + N 通过输入类名打开类(标准说法是查找类文件) Ctrl + Shi ...

  6. 关于一点儿对仓储(Repository)的理解

    仓储(Repository) 内容来源于dudu的 关于Repository模式一文 Repository是一个独立的层,介于领域层与数据映射层(数据访问层)之间.它的存在让领域层感觉不到数据访问层的 ...

  7. pycharm格式化代码 常用快捷键

    ctrl+alt+L 一 常用快捷键 编辑类: Ctrl + D             复制选定的区域或行Ctrl + Y           删除选定的行Ctrl + Alt + L     代码 ...

  8. iOS Round Double value to 2 decimal digits and round 3rd decimal digit up

    I have I double value like this one 17.125. It should be rounded up to 17.13 If I use the simple met ...

  9. discuz 数据库文件密码修改

    网站系统需要修改的位置有两处 Discuz 和 UC-center ①路径:/wwwroot/config/config_global.php 这个根据你网站安装的路径而定. 打开 config_gl ...

  10. 关于oracle的缓冲区机制与HDFS中的edit logs的某些关联性的思考

    可能大家会问,oracle和HDFS属于不同场景的存储系统,它们之间为什么会有联系呢?确实,从技术本身来看,他们确实无关联,但利用“整体学习”的思想,跳出技术本身,可以发现Oracle的缓冲区和HDF ...