首先需要安装node.js, 可参考 http://www.gruntjs.net/getting-started
安装cli
npm install -g grunt-cli
安装grunt
npm install -g grunt
创建Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON(‘package.json’),
uglify: {
options: {
banner: ‘/! <%= pkg.name %> - v<%= pkg.version %> - <%= pkg.homepage %>- ‘ +
‘<%= grunt.template.today(“yyyy-mm-dd”) %> /‘
},
my_target: {
files: [{
expand: true,
cwd: ‘release/js’,
src: [‘/*.js’, ‘!/.min.js’],
dest: ‘release/js’,
ext: ‘.min.js’
}]
}
},
cssmin: {
target: {
files: [{
expand: true,
cwd: ‘release/css’,
src: [‘**/.css’, ‘!*/.min.css’],
dest: ‘release/css’,
ext: ‘.min.css’
}]
}
}
});
// Load the plugin that provides the “uglify” task.
grunt.loadNpmTasks(‘grunt-contrib-uglify’);
grunt.loadNpmTasks(‘grunt-contrib-cssmin’);
// Default task(s).
grunt.registerTask(‘default’, [‘uglify’,’cssmin’]);
};
创建package.json
{
“name”: “project-min”,
“version”: “1.0.0”,
“description”: “my site’s min file( js, css )”,
“homepage”: “http://www.yf2017.top/",
“keywords”: [
“leegtang”,
“project-min”
],
“author”: “Caigen Lee licaigen@yf2017.top“,
“devDependencies”: {
“grunt”: “~1.2.0”,
“grunt-contrib-cssmin”: “^1.0.1”,
“grunt-contrib-jshint”: “~0.10.0”,
“grunt-contrib-nodeunit”: “~0.4.1”,
“grunt-contrib-uglify”: “~2.0.0”
},
“repository”: {
“type”: “git”,
“url”: “git://github.com/leegtang/project-min.git”
},
“license”: [
{
“type”: “MIT”,
“url”: “https://github.com/leegtang/project-min/blob/master/LICENSE.md"
}
]
}
将需要压缩的文件放到release文件夹中
目录结构如下:
安装插件cssmin和uglify
npm install grunt-contrib-cssmin –save-dev
npm install grunt-contrib-uglify –save-dev
运行grunt
grunt
本文参考: