Gruntfile-dev.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. module.exports = function(grunt) {
  2. 'use strict';
  3. var path = require('path');
  4. var fs = require('fs');
  5. var ipAddress = require('network-address')();
  6. var middleware_directory = require('grunt-contrib-connect/node_modules/connect/lib/middleware/directory');
  7. var middleware_renderfile = require('./_middleware-render-ejs');
  8. grunt.initConfig({
  9. connect: {
  10. site_src: {
  11. options: {
  12. hostname: ipAddress,
  13. port: 9000,
  14. base: ['src/'],
  15. livereload: true,
  16. open: true, //打开默认浏览器
  17. middleware: [
  18. middleware_renderfile,
  19. middleware_directory(path.resolve('src/'))
  20. ]
  21. }
  22. }
  23. },
  24. autoprefixer: {
  25. options: {
  26. browsers: ['> 1%', 'last 2 versions', 'ff 17', 'opera 12.1', 'ie 8']
  27. },
  28. dist: {
  29. expand: true,
  30. flatten: true,
  31. src: 'src/css/*.css',
  32. dest: 'src/css/'
  33. }
  34. },
  35. watch: {
  36. css: {
  37. files: ['src/sass/{,*/}*.scss'],
  38. tasks:['sass','autoprefixer']
  39. },
  40. livereload: {
  41. options: {
  42. livereload: true
  43. },
  44. files: ['src/*.html', 'src/css/*.css', 'src/js/*.js', 'src/partial/*.ejs', 'src/data/*.json']
  45. }
  46. },
  47. sass: {
  48. dist: {
  49. options: {
  50. outputStyle: 'expanded',
  51. //nested, compact, compressed, expanded
  52. sourceComments: 'map',
  53. sourceMap: true
  54. },
  55. files: [{
  56. expand: true,
  57. cwd: 'src/sass',
  58. src: ['*.scss','!_*.scss','!*/_*.scss'],
  59. dest: 'src/css',
  60. ext: '.css'
  61. }]
  62. }
  63. }
  64. });
  65. // 默认任务
  66. grunt.registerTask('default', ['connect:site_src', 'watch']);
  67. // 自定义端口
  68. grunt.task.registerTask('port', 'multi port', function(arg) {
  69. if(arguments.length === 0){
  70. console.log('端口号不能为空!')
  71. }else{
  72. grunt.config.set('connect.port'+arg,{
  73. options: {
  74. hostname: ipAddress,
  75. port: arg,
  76. base: ['src/'],
  77. livereload: +arg+1,
  78. open: true,
  79. middleware: [
  80. middleware_renderfile,
  81. middleware_directory(path.resolve('src/'))
  82. ]
  83. }
  84. });
  85. grunt.config.set('watch.livereload',{
  86. options: {
  87. livereload: +arg+1
  88. },
  89. files: ['src/*.html', 'src/css/*.css', 'src/js/*.js', 'src/partial/*.ejs', 'src/data/*.json']
  90. })
  91. grunt.task.run(['connect:port'+arg, 'watch']);
  92. }
  93. });
  94. };