Gruntfile-release.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. module.exports = function(grunt) {
  2. 'use strict';
  3. var path = require('path');
  4. var fs = require('fs');
  5. var pkg = require('../package');
  6. var ipAddress = require('network-address')();
  7. var checkAssets = require('./_check-assets');
  8. var configObj = {
  9. // 全局变量
  10. banner: '/*!'+'\n * @project : '+pkg.name+'\n * @version : '+pkg.version+'\n * @author : '+pkg.author.name+'\n * @update : <%= grunt.template.today("yyyy-mm-dd hh:MM:ss TT") %>\n */\r',
  11. connect: {
  12. site_dest: {
  13. options: {
  14. hostname: ipAddress,
  15. port: 9001,
  16. base: ['dest/'],
  17. keepalive: true, //保持sever不退出
  18. open: true //打开默认浏览器
  19. }
  20. }
  21. },
  22. cssmin: {
  23. options: {
  24. banner: '<%= banner %>'
  25. },
  26. minify: {
  27. expand: true,
  28. cwd: 'dest/css',
  29. src: ['*.css', '!*.min.css'],
  30. dest: 'dest/css',
  31. ext: '.css'
  32. }
  33. },
  34. uglify: {
  35. options: {
  36. banner: '<%= banner %>',
  37. preserveComments: 'some',
  38. mangle: false
  39. },
  40. dist: {
  41. files: [{
  42. expand: true,
  43. cwd: 'dest/js',
  44. src: '**/*.js',
  45. dest: 'dest/js'
  46. }]
  47. }
  48. },
  49. clean: {
  50. build: ["dest"],
  51. release: ["dest/slice", "dest/data", "dest/partial"]
  52. },
  53. copy: {
  54. release: {
  55. expand: true,
  56. cwd: 'src/',
  57. src: ['**', '!sass', '!sass/{,*/}*', '!css/*.map', '!img/psd','!img/psd/{,*/}*'],
  58. dest: 'dest/'
  59. }
  60. },
  61. autoprefixer: {
  62. options: {
  63. browsers: ['> 1%', 'last 2 versions', 'ff 17', 'opera 12.1', 'ie 8']
  64. },
  65. dist: {
  66. expand: true,
  67. flatten: true,
  68. src: 'src/css/*.css',
  69. dest: 'src/css/'
  70. }
  71. },
  72. imagemin: {
  73. options: {
  74. pngquant: true
  75. },
  76. dist: {
  77. files: [{
  78. expand: true,
  79. cwd: 'dest/img/',
  80. src: ['**/*.{png,jpg,jpeg}'], // 优化 img 目录下所有 png/jpg/jpeg 图片
  81. dest: 'dest/img/' // 优化后的图片保存位置,覆盖旧图片,并且不作提示
  82. }]
  83. }
  84. },
  85. adisprite: {
  86. all: {
  87. srcCss: 'dest/css',
  88. srcImg: 'dest/slice',
  89. destCss: 'dest/css',
  90. destImg: 'dest/img/sprite',
  91. 'padding': 5,
  92. 'algorithm': 'binary-tree',
  93. 'engine': 'gm',
  94. 'exportOpts': {
  95. 'format': 'png',
  96. 'quality': 90
  97. }
  98. }
  99. },
  100. sass: {
  101. dist: {
  102. options: {
  103. outputStyle: 'expanded',
  104. //nested, compact, compressed, expanded
  105. sourceComments: 'map',
  106. sourceMap: true
  107. },
  108. files: [{
  109. expand: true,
  110. cwd: 'src/sass',
  111. src: ['*.scss','!_*.scss','!*/_*.scss'],
  112. dest: 'src/css',
  113. ext: '.css'
  114. }]
  115. }
  116. },
  117. ejs_static: {
  118. release:{
  119. options: {
  120. dest: 'dest/',
  121. path_to_data: 'src/data/config.json',
  122. path_to_layouts: 'src/',
  123. underscores_to_dashes: false,
  124. file_extension: '.html',
  125. underscore: true
  126. }
  127. }
  128. }
  129. };
  130. // webserver 查看发布目录
  131. grunt.registerTask('dest', function(arg){
  132. grunt.config.merge(configObj)
  133. grunt.task.run(['connect:site_dest'])
  134. })
  135. // 发布任务
  136. grunt.registerTask('release', function(){
  137. grunt.config.merge(configObj)
  138. grunt.task.run(['sass', '__checkAssets', 'autoprefixer', 'clean:build', 'copy:release', 'adisprite', 'cssmin', 'uglify', 'imagemin', 'ejs_static:release', 'clean:release', 'connect:site_dest'])
  139. })
  140. // 坚持样式表是否存在无效的本地资源
  141. grunt.registerTask('__checkAssets', function(){
  142. var isError = checkAssets()
  143. if(isError){
  144. grunt.log.error(isError)
  145. process.abort();
  146. }
  147. })
  148. };