gulpfile-dev.js 5.3 KB

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