gulpfile-dev.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. module.exports = function(gulp, plugins) {
  2. var argv = require('yargs').argv,
  3. chalk = require('chalk'),
  4. moment = require('moment'),
  5. browserSync = require('browser-sync'),
  6. reload = browserSync.reload,
  7. log = console.log;
  8. var that = this;
  9. that.port = +argv.p || 3000;
  10. var pkg = require('../package.json');
  11. 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';
  12. gulp.task('dev_conn', function() {
  13. browserSync({
  14. server: {
  15. baseDir: "src",
  16. directory: true
  17. },
  18. notify: false,
  19. ghostMode:false,
  20. port: that.port,
  21. open: "external",
  22. browser: "/Applications/Google\ Chrome.app/"
  23. })
  24. })
  25. gulp.task('dev_sass', function() {
  26. var config = {
  27. onError: function(err){ log(chalk.bold.red('【sass compile error】>>> \n' + JSON.stringify(err) )) }
  28. }
  29. // `gulp -n` 不启用sourcemap
  30. if(argv.n){
  31. return gulp.src('src/sass/*.scss')
  32. .pipe(plugins.cached('sass', {optimizeMemory: true}))
  33. .pipe(plugins.sass(config))
  34. .pipe(plugins.autoprefixer({browser: ['> 0%']}))
  35. .pipe(plugins.remember('sass'))
  36. .pipe(gulp.dest('src/css'))
  37. .pipe(reload({stream:true}))
  38. }
  39. return gulp.src('src/sass/*.scss')
  40. .pipe(plugins.sourcemaps.init())
  41. .pipe(plugins.sass(config))
  42. .pipe(plugins.sourcemaps.write({includeContent: false, sourceRoot: '../sass/'}))
  43. .pipe(plugins.sourcemaps.init({loadMaps: true}))
  44. .pipe(plugins.autoprefixer( {browser: ['> 0%']} ))
  45. .pipe(plugins.sourcemaps.write({includeContent: false, sourceRoot: '../sass/'}))
  46. .pipe(gulp.dest('src/css'))
  47. .pipe(reload({stream:true}))
  48. })
  49. gulp.task('dev_ejs', function() {
  50. return gulp.src('src/tpl/*.ejs')
  51. .pipe(plugins.ejs().on('error', console.log))
  52. .pipe(gulp.dest('src/'))
  53. .pipe(reload({stream:true}))
  54. })
  55. // 检测 src/img/slice 文件夹,读取图片信息来生成css切片样式
  56. gulp.task('dev_slice2css', function(){
  57. var fs = require('fs')
  58. var path = require('path')
  59. var async = require('gulp-uglify/node_modules/uglify-js/node_modules/async')
  60. var gm = require('gm')
  61. var ejs = require('gulp-ejs/node_modules/ejs')
  62. var classnameRule = function(fileName, p){
  63. var relPath = path.relative('src/img/slice', path.dirname(p))
  64. var name = path.join(relPath, fileName).replace(/\//g, '-')
  65. return name
  66. }
  67. var files, data = {}
  68. async.series([
  69. // 1. 文件过滤
  70. function(next){
  71. var glob = require("glob")
  72. files = glob.sync("src/img/slice/**", {nodir:true})
  73. files = files.filter(function(f){
  74. return !~(path.basename(f).indexOf('@'))
  75. })
  76. next(null)
  77. },
  78. // 2. 生成切片数据
  79. function(next){
  80. var arr = data.slice = []
  81. async.eachSeries(files, iterator, callback)
  82. function iterator(f, _next){
  83. gm(f).size(function(err, size){
  84. if(err){return}
  85. arr.push({
  86. filepath: f,
  87. imageurl: path.relative('src/sass', f).split(path.sep).join('/'),
  88. classname: classnameRule.call({}, path.basename(f, path.extname(f)), f),
  89. width: size.width,
  90. height: size.height
  91. })
  92. _next(null)
  93. })
  94. }
  95. function callback(err, result){
  96. next(null)
  97. }
  98. },
  99. // 3. 生成css文件
  100. function(next){
  101. var tpl = (function(){/*
  102. // CSS Sprites切片样式
  103. <% slice.forEach(function(e){ %>
  104. .<%= e.classname%> {
  105. width: <%= e.width%>px;
  106. height: <%= e.height %>px;
  107. background-image: url(<%= e.imageurl%>);
  108. background-repeat: no-repeat;
  109. }
  110. <% }) %>
  111. */}).toString().split('\n').slice(1, -1).join('\n')
  112. var css = ejs.render(tpl, data).replace(/^\n/mg, '')
  113. fs.writeFileSync('src/sass/_slice.scss', css)
  114. }
  115. ])
  116. })
  117. gulp.task('default', ['dev_conn'], function(){
  118. if(argv.w){
  119. gulp.watch('src/img/slice/**', ['dev_slice2css'])
  120. }
  121. gulp.watch('src/tpl/**', ['dev_ejs'])
  122. gulp.watch('src/sass/**', ['dev_sass'])
  123. gulp.watch('src/img/**', reload)
  124. gulp.watch('src/js/**', reload)
  125. gulp.watch('src/*.html', reload)
  126. })
  127. }