gulpfile-build.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. module.exports = function(gulp, plugins) {
  2. var argv = require('yargs').argv,
  3. del = require('del'),
  4. moment = require('moment'),
  5. multiSprite = require('multi-sprite'),
  6. portfinder = require('portfinder'),
  7. browserSync = require('browser-sync'),
  8. log = console.log;
  9. var that = this;
  10. that.port = +argv.p || undefined;
  11. var pkg = require('../package.json');
  12. var banner = '/*!' + '\n * @project : ' + pkg.name + '\n * @version : ' + pkg.version + '\n * @author : ' + pkg.author + '\n * @update : ' + moment().format('YYYY-MM-DD h:mm:ss a') + '\n */\r';
  13. gulp.task('build_sass', function() {
  14. var config = {
  15. sourceComments: 'map',
  16. sourceMap: 'sass',
  17. style: 'compact',
  18. onError: function(err) { console.log('...err...: ' + err) }
  19. }
  20. return gulp.src('src/sass/*.scss')
  21. .pipe(plugins.sass(config))
  22. .pipe(plugins.autoprefixer("last 1 version", "> 1%", "ie 8", "ie 7"))
  23. .pipe(gulp.dest('src/css'))
  24. })
  25. gulp.task('build_css', ['build_sass'], function() {
  26. return gulp.src('src/css/**/*.css')
  27. .pipe(plugins.minifyCss({"compatibility":"ie7"}))
  28. .pipe(plugins.header(banner, { pkg : pkg } ))
  29. .pipe(gulp.dest('dest/css'))
  30. })
  31. gulp.task('build_slice', function() {
  32. return gulp.src('src/slice/**')
  33. .pipe(gulp.dest('dest/slice'))
  34. })
  35. gulp.task('build_sprite', ['build_slice', 'build_css'], function() {
  36. return multiSprite({
  37. srcCss: 'dest/css',
  38. srcImg: 'dest/slice',
  39. destCss: 'dest/css',
  40. destImg: 'dest/img/sprite',
  41. 'algorithm': 'binary-tree',
  42. 'padding': 4,
  43. 'exportOpts': {
  44. 'format': 'png',
  45. 'quality': 90
  46. },
  47. successCB: function(){
  48. del(['dest/slice/**'])
  49. // 给css文件的图片请求加上时间戳
  50. var timestamp = +new Date
  51. gulp.src(['dest/css/**'])
  52. .pipe(plugins.replace(/(\/[\w-]*\.(jpg|jpeg|gif|png|bmp|tiff|otf|ttf|woff|svg|webp|swf|htc))/ig, '$1?'+timestamp))
  53. .pipe(gulp.dest('dest/css'));
  54. }
  55. })
  56. })
  57. gulp.task('build_js', function() {
  58. return gulp.src('src/js/**')
  59. .pipe(plugins.uglify())
  60. .pipe(plugins.header(banner, { pkg : pkg } ))
  61. .pipe(gulp.dest('dest/js'))
  62. })
  63. gulp.task('build_img', function() {
  64. return gulp.src(['src/img/**', '!src/img/**/*.psd'])
  65. .pipe(plugins.imagemin({
  66. progressive: true
  67. }))
  68. .pipe(gulp.dest('dest/img'))
  69. })
  70. gulp.task('build_fonts', function() {
  71. return gulp.src('src/fonts/**')
  72. .pipe(gulp.dest('dest/fonts'))
  73. })
  74. gulp.task('build_html', ['build_ejs'], function() {
  75. return gulp.src(['src/*.html'])
  76. .pipe(gulp.dest('dest'))
  77. })
  78. gulp.task('build_ejs', function() {
  79. return gulp.src('src/tpl/*.ejs')
  80. .pipe(plugins.ejs().on('error', console.log))
  81. .pipe(gulp.dest('src/'))
  82. })
  83. gulp.task('build_clean', function() {
  84. del.sync(['dest/**'])
  85. })
  86. gulp.task('build', ['build_clean', 'build_html', 'build_sprite', 'build_js', 'build_img', 'build_fonts'], function(){
  87. portfinder.getPort(function (err, port) {
  88. browserSync({
  89. server: {
  90. baseDir: "dest",
  91. directory: true
  92. },
  93. notify: false,
  94. ghostMode:false,
  95. codeSync: false,
  96. port: that.port||port,
  97. open: "external",
  98. browser: "/Applications/Google\ Chrome.app/"
  99. })
  100. })
  101. })
  102. gulp.task('dest', function(){
  103. portfinder.getPort(function (err, port) {
  104. browserSync({
  105. server: {
  106. baseDir: "dest",
  107. directory: true
  108. },
  109. notify: false,
  110. ghostMode:false,
  111. codeSync: false,
  112. port: that.port||port,
  113. open: "external",
  114. browser: "/Applications/Google\ Chrome.app/"
  115. })
  116. })
  117. })
  118. }