module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), //源码 src: "asserts", //输出 dist: "dist", transport: { options: { paths:["<%=src%>/sea-modules"], alias: { "jquery": "lib/jquery.js" } }, modules: { files : [ { cwd: '<%=src%>/sea-modules', expand: true, src: '**/*.js', dest: '<%=dist%>/sea-modules' } ] }, dwstatic: { options:{ }, files : [ { cwd: '<%=src%>', expand: true, src: 'dwstatic/**/*.js', dest: '<%=dist%>' } ] } }, copy:{ moduleRes:{ files:[ {expand: true, cwd: '<%=src%>/sea-modules/css', src:['**'],dest: '<%=dist%>/sea-modules/css'}, {expand: true, cwd: '<%=src%>/sea-modules/img', src:['**'],dest: '<%=dist%>/sea-modules/img'} ] }, staticRes:{ files:[ {expand: true, cwd: '<%=src%>/dwstatic/css', src:['**'],dest: '<%=dist%>/dwstatic/css'}, {expand: true, cwd: '<%=src%>/dwstatic/images', src:['**'],dest: '<%=dist%>/dwstatic/images'} ] } }, concat: { //合并一个公用的基础js,并且利用 preload插件,让其前置加载。 aio: { options: { paths:["<%=dist%>/sea-modules"], include: "all" }, files: { "<%=dist%>/sea-modules/aio.js": ["<%=dist%>/sea-modules/aio.js"] } } }, uglify:{ allJs:{ files:[{ expand: true, cwd: '<%=dist%>', src: ['**/*.js', '!**/*-debug.js'], dest: '<%=dist%>' }] } }, cssmin: { allCss: { expand: true, cwd: '<%=dist%>', src: ['**/*.css'], dest: '<%=dist%>' } } }); grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-cssmin'); grunt.loadNpmTasks('grunt-cmd-transport'); grunt.loadNpmTasks('grunt-cmd-concat'); grunt.registerTask('min', ['uglify','cssmin']); grunt.registerTask('default', ['transport', 'concat','copy','uglify','cssmin']); };
页面调用:
Image preview example npm 安装的包: npm install -g grunt-cli npm install grunt --save-dev npm install grunt-cmd-transport --save-dev npm install grunt-cmd-concat --save-dev npm install grunt-contrib-uglify --save-dev npm install grunt-contrib-cssmin --save-dev
工程目录:
代码: