//包裝函數 module.exports = function(grunt){ //任務配置,全部插件的配置信息 grunt.initConfig({ //獲取package.json的信息 pkg:grunt.file.readJSON('package.json') }) //告訴grunt當咱們在終端中輸入grunt時須要作些什麼(注意前後順序) grunt.registerTask('default',[]); }
uglify:{ option:{ stripBanners:true, banner:'/*!<%=pkg.name%>-<%=pkg.version%>.js <%- grunt.template.today("yyyy-mm-dd") %> */\n' }, build:{ src:'src/test.js', dest:'build/<%=pkg.name%>-<%=pkg.version%>.js.min.js' } }
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.registerTask('default',['uglify']);
jshint:{ options:{ jshintrc:'.jshintrc' }, build:[ 'Gruntfile.js','src/*.js'] }
{ "boss":false, "curly":true, "eqeqeq":true, "eqnull":true, "expr":true, "immed":true, "newcap":true, "noempty":true }
cssmin: { options : { compatibility : 'ie8', //設置兼容模式 noAdvanced : true //取消高級特性 }, minify: { expand: true, cwd: 'web/css/', src: ['*.css', '!*.min.css'], dest: 'dist/resource/web/css', ext: '.css' } }
htmlmin: { options: { removeComments: true, collapseWhitespace: true }, dist: { files: [ {expand: true,cwd: 'web',src: '*.html',dest: 'web/dist/partials'}, {expand: true,cwd: 'web/partials',src: '**/*.html',dest: 'web/dist/partials'} ] }
copy:{ build:{ cwd:'web', //cwd只想源文件的目錄都是相對的,和src制定源文件相似 src:['**'], //**是一個通配符,用來匹配Grunt任何文件 dest:'resource', //dest是Grunt用來輸出結果任務的 expand:true } }
clean:{ build:{ src:['resource'] } },
----grunt_test1項目目錄及Gruntfile.js配置-------------------javascript
/** * Created by liuhuanli on 2017/4/1. */ module.exports = function(grunt){ //項目配置: grunt.initConfig({ pkg:grunt.file.readJSON('package.json'), clean: { all: ['resource/web/*',"!dist/resource/web/.git"], productAll:['resource/**'], js: ["resource/web/js/*","resource/web/js/*/*"] }, copy:{ //運行文件 build1:{ expand: true, src: [ 'web/**', ], dest: 'resource' }, //源碼 product:{ expand: true, src: [ 'web/js/**', 'web/css/**', 'web/images/**', 'web/partials/**', 'web/bower_components/**', 'web/*.html' ], dest: 'resource' } }, watch:{ css:{ files:[ 'resource/web/css/*.css' ] }, js:{ files:['resource/web/js/*.js', 'resource/Gruntfile.js'], tasks:['jshint'] } }, jshint:{ options:{ jshintrc:'.jshintrc' }, all:['resource/Gruntfile.js','resource/web/js/*.js'] }, uglify:{ options:{ banner:'/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n' }, js0: { files: {'resource/web/js/jquery.ztree.all.js':['web/js/jquery.ztree.all.js']} }, js1: { files: {'resource/web/js/jquery.ztree.core.js':['web/js/jquery.ztree.core.js']} }, js2: { files: {'resource/web/js/jquery.ztree.excheck.js':['web/js/jquery.ztree.excheck.js']} }, js3: { files: {'resource/web/js/jquery.ztree.exedit.js':['web/js/jquery.ztree.exedit.js']} }, js4: { files: {'resource/web/js/jquery.ztree.exhide.js':['web/js/jquery.ztree.exhide.js']} } }, //壓縮css cssmin: { options : { compatibility : 'ie8', //設置兼容模式 noAdvanced : true //取消高級特性 }, minify: { expand: true, cwd: 'web/css', src: ['*.css', '!*.min.css'], dest: 'resource/web/css', ext: '.css' }, minifyMetro: { expand: true, cwd: 'web/css/metroStyle', src: ['*.css', '!*.min.css'], dest: 'resource/web/css/metroStyle', ext: '.css' } }, // 壓縮html htmlmin: { options: { removeComments: true, collapseWhitespace: true }, dist: { files: [ {expand: true,cwd: 'web',src: '*.html',dest: 'dist/resource/web'}, {expand: true,cwd: 'web/partials',src: '**/*.html',dest: 'resource/web/partials'} ] } } }); //加載插件 grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-cssmin'); grunt.loadNpmTasks('grunt-contrib-htmlmin'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-contrib-clean'); grunt.registerTask('cleanAll', ['clean:all']); grunt.registerTask('default',[ 'clean:productAll', 'clean:all', 'copy:product', 'cssmin', 'htmlmin', 'uglify', 'watch' //'jshint', ]); }
當你安裝以上插件成功後,觀察你的package.json文件 css
devDependencies 下有內容以下所示:
若是是團隊開發用同樣的插件版本,只須要把package.json文件拷到項目目錄下,npm install 就能自動安裝package.json文件中devDependencies下
的插件