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. Spring学习笔记2——表单数据验证、文件上传

    在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...

  2. 全屏使用swiper.js过程中遇到的坑

    概述 swiper.js确实是一个很好用的插件,下面记录下我在全屏使用过程中遇到的一些坑和解决办法,供以后开发时参考,相信对其他人也有用. 通用方案 一般来说,swiper需要放在body的下一层,虽 ...

  3. 【ElasticSearch】:Mapping相关

    Mapping 类似数据库中的表结构定义,主要作用如下: 定义Index下的字段名(Field Name). 定义字段类型,例如数值型.字符串型.布尔型等. 定义倒排索引相关配置,比如是否索引.记录p ...

  4. js基于json的级联下拉框

    级联下拉列表是项目中常用到的.比如省市县,比如企业性质等,做成一个js通用组件, 在静态页出来后可以直接插入,将数据和html静态页做一个解耦. 贴出来抛砖引玉吧. /** * @author sun ...

  5. Oracle日期格式化以及extract函数的使用

    由于业务需要,这两天在学习Oracle,发现Oracle里面的日期它会给你转成一种很不习惯的格式,于是想着怎么样把它弄成年.月.日的格式来显示,查资料.看文档,最终找到解决办法了,其实是用到了to_c ...

  6. 【sping揭秘】3、Spring容器中bean默认是保持一个实例

    Spring容器中bean默认是保持一个实例 这里做一个测试,基础代码 package cn.cutter.start.provider; import org.springframework.con ...

  7. odoo开发笔记 -- odoo和postgresql数据库导入相关

    odoo数据库 导入.导出 首先odoo框架下postgresql数据库中,表结构的存储方式: 存在id(小写),并没有所谓的外部ID 例如数据库中的国家表:模块名_tb_country   (注意: ...

  8. SQL Server 2016 需要单独安装 SSMS

    默认安装完 MSSQL 后,不自带 SSMS 的管理工具了,需要的话可以单独安装,貌似更专业了一些. https://msdn.microsoft.com/library/mt238290.aspx ...

  9. 如何用R来处理数据表的长宽转换(图文详解)

    不多说,直接上干货! 很多地方都需用到这个知识点,比如Tableau里.   通常可以采取如python 和 r来作为数据处理的前期. Tableau学习系列之Tableau如何通过数据透视表方式读取 ...

  10. CentOS7 下安装 iSCSI Target(tgt) ,使用 Ceph rbd

    目录 一.iSCSI 介绍 1. iSCSI 定义 2. 几种常见的 iSCSI Target 3. 优缺点比较 二.安装步骤 1. 关闭防火墙 2. 关闭selinux 3. 通过 yum 安装 t ...