grunt合并压缩js、css文件
参考文章如下:
http://www.cnblogs.com/yexiaochai/p/3594561.html
http://www.cnblogs.com/yexiaochai/p/3602002.html
http://www.cnblogs.com/snandy/archive/2013/03/07/2946989.html
http://www.cnblogs.com/snandy/archive/2013/03/11/2949177.html
http://www.cnblogs.com/snandy/archive/2013/05/20/3088613.html
以上两个牛人的文章写得已经很棒了,我只奉上我的文件,package.json和Gruntfile.js
对js文件进行压缩并且合并操作
1.package.json文件
{ "name": "demo", "file": "zepto", "version": "0.1.0", "description": "demo", "license": "MIT", "devDependencies": { "grunt": "~0.4.1", "grunt-contrib-jshint": "~0.6.3", "grunt-contrib-concat": "~0.5.0", "grunt-contrib-uglify": "~0.2.1", "grunt-contrib-requirejs": "~0.4.1", "grunt-contrib-copy": "~0.4.1", "grunt-contrib-clean": "~0.5.0", "grunt-strip": "~0.2.1" }, "dependencies": { "express": "3.x" } }
2.Gruntfile.js
module.exports = function (grunt) { grunt.initConfig({ concat: { options: { }, dist: { src: ['src/**/*.js'],//src文件夹下包括子文件夹下的所有文件 dest: 'dist/built.js'//合并文件在dist下名为built.js的文件 } }, uglify: { build: { src: 'dist/built.js',//压缩源文件是之前合并的buildt.js文件 dest: 'dist/built.min.js'//压缩文件为built.min.js } } }); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-concat'); grunt.registerTask('default', ['concat','uglify']); }
对CSS进行合并压缩
如有没有下载cssmin的话,需要下载
在nodejs命令行中输入 npm install grunt-css
package.json跟上面一样
module.exports = function (grunt) { grunt.initConfig({ concat: {//css文件合并 css: { src: ['src/css/*.css'],//当前grunt项目中路径下的src/css目录下的所有css文件 dest: 'dist/all.css' //生成到grunt项目路径下的dist文件夹下为all.css } }, cssmin: { //css文件压缩 css: { src: 'dist/all.css',//将之前的all.css dest: 'dist/all.min.css' //压缩 } } }); grunt.loadNpmTasks('grunt-css'); grunt.loadNpmTasks('grunt-contrib-concat'); grunt.registerTask('default', ['concat','cssmin']); }
Gruntfile.js如下: