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. linux下时间问题---date

    显示日期 显示日期和时间

  2. sync.WaitGroup和sync.Once

    sync.WaitGroup,顾名思义,等待一组goroutinue运行完毕.sync.WaitGroup声明后即可使用,它有如下方法: func (wg *WaitGroup) Add(delta ...

  3. npm包发布记录

    下雪了,在家闲着,不如写一个npm 包发布.简单的 npm 包的发布网上有很多教程,我就不记录了.这里记录下,一个复杂的 npm 包发布,复杂指的构建环境复杂. 整个工程使用 rollup 来构建,其 ...

  4. Vue2.5开发去哪儿网App 首页开发

    主页划 5 个组件,即 header  icon  swiper recommend weekend 一. header区域开发 1. 安装 stylus npm install stylus --s ...

  5. odoo开发环境搭建(二):安装Ubuntu 17虚拟机

    odoo开发环境搭建(二):安装Ubuntu 17虚拟机 下载镜像文件: 配置网络: 安装vmware tools: 配置共享文件夹: 选中虚拟机,右键编辑设置里边配置共享文件夹,指定windows本 ...

  6. odoo 开发基础 -- 视图之widget

    Odoo 中的widget many2many_tags one2many_list selection progressbar selection statusbar handle monetary ...

  7. 课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 3、Python Basics with numpy (optional)

    Python Basics with numpy (optional)Welcome to your first (Optional) programming exercise of the deep ...

  8. Proxy代理模式(结构型模式)

    1.问题 在面向对象系统中,有些对象由于某种原因(比如创建对象的开销很大,或者某些操作需要安全控制,或者需要进程外的访问等),直接访问会给调用者带来麻烦,那么如何在不损失接口透明性的情况下,解决这些麻 ...

  9. c# winform as3相互调用

    C#主要代码: 首先要添加COM组件-Shockwave Flash Object //接收flash发送过来的信息        private void axShockwaveFlash1_Fla ...

  10. [Java初探07]__关于面向对象的简单认识

    前言 类和对象,在我们学习Java语言的过程中,它们无时无刻不存在着,在我们还远未详细弄明白它们的意义之前,在我们不知不觉的下意识里,我们都会直接或间接的用到它们,这是因为Java是一门面向对象的语言 ...