gruntjs 是一个基于nodejs的自动化工具,只要熟悉nodejs或者又必定js经验就能够熟练应用。
最近很火的前端自动化小工具,基于任务的命令行构建工具 http://gruntjs.comjavascript
咱们须要一个自动化的工做流程,让咱们更专一于coding,而不是coding外的繁琐工做。因而Grunt应运而生。能够想像,若是在node环境下,一行命令搞定一个场景,So Cool…css
package.json //项目自动化所依赖的相关插件。
Gruntfile.js //项目自动化工做流配置文件,重要html
建立gruntjs工程文件
gruntjs支持两种语言建立工程文件: gruntfile.js或者gruntfile.coffee
grunt工程文件遵循node模块定义方式,主题结构为:前端
module.exports = function(grunt){ grunt.initConfig({ clean:{ a:['<%=temp%>'] }, b:['<%=temp%>'] }); //获取工程中须要的插件 grunt.loadNpmTasks('grunt-contrib-clean'); grunt.loadNpmTasks('grunt-contrib-cssmin'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-contrib-includes'); //注册任务 grunt.registerTask('build', ['clean','uglify','cssmin','includes','copy','clean']); //在命令行中执行grunt,就会使用默认任务 grunt.registerTask('default',['build']);
//一个完整的gruntjs工程文件 module.exports = function(grunt){ var rt = 'src-dev/', indexDir = rt + 'index/', tempDir = rt + 'temp/'; console.log(grunt.option('keys')); grunt.file.exists(tempDir) && grunt.file.delete(tempDir); grunt.file.markdir(tempDir); grunt.initConfi({ pkg: grunt.file.readJSON('package.json'); rt: rt, tempDir: tempDir, indexDir = indexDir, clear: { build:['<%=temp%>']}, jsdoc:{ dist:{ src:'<%=index%>doc.js', options:{ destination:'<%=rt%>../out/' } } }, cssmin:{ combine:{ files:{ '<%=temp%>c.min.css':['<%=rt%>common/reset.css', '<%=index%>c.css'] } } }, includes:{ files:{ sr:['<%=rt%>**/*.html'], dest:'<%=temp%>', cwd:'.', flatten:true, options{ banner:'' } } }, watch:{ files:['<%=index%>j.js','<%=index%>*.html', '<%=index%>c.css'] tasks:['clean','uglify','cssmin','includes','copy','clearn'], options:{ livereload:true} }, uglify:{ dist:{ files:{ '<%=temp%>j.js', ['<%=index%>*.js'] } } }, copy:{ main:{ files:[{ flatten: true, expand: true, filter:'isFile', src:['<%=temp%>index.html'], dest='<%=rt%>../src/' }] } } //获取工程中须要的插件 grunt.loadNpmTasks('grunt-contrib-clean'); grunt.loadNpmTasks('grunt-contrib-cssmin'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-contrib-includes'); //注册任务 grunt.registerTask('build', ['clean','uglify','cssmin','includes','copy','clean']); //在命令行中执行grunt,就会使用默认任务 grunt.registerTask('default',['build']);