gulpfile-dev.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.sourcemaps.write({includeContent: false, sourceRoot: '../sass/'}))
  47. .pipe(plugins.sourcemaps.init({loadMaps: true}))
  48. .pipe(plugins.autoprefixer("last 1 version", "> 1%", "ie 8", "ie 7"))
  49. .pipe(plugins.sourcemaps.write({includeContent: false, sourceRoot: '../sass/'}))
  50. // .pipe(plugins.remember('sass'))
  51. .pipe(gulp.dest('src/css'))
  52. .pipe(reload({stream:true}))
  53. })
  54. gulp.task('dev_ejs', function() {
  55. return gulp.src('src/tpl/*.ejs')
  56. .pipe(plugins.ejs().on('error', console.log))
  57. .pipe(gulp.dest('src/'))
  58. .pipe(reload({stream:true}))
  59. })
  60. gulp.task('dev_slice2css', function(){
  61. var fs = require('fs')
  62. var path = require('path')
  63. var async = require('gulp-uglify/node_modules/uglify-js/node_modules/async')
  64. var gm = require('gm')
  65. var ejs = require('gulp-ejs/node_modules/ejs')
  66. var classnameRule = function(fileName, p){
  67. var relPath = path.relative('src/slice', path.dirname(p))
  68. var name = path.join(relPath, fileName).replace(/\//g, '-')
  69. return name
  70. }
  71. var files, data = {}
  72. async.series([
  73. // 列出图片
  74. function(next){
  75. var glob = require("glob")
  76. files = glob.sync("src/slice/**", {nodir:true})
  77. files = files.filter(function(f){
  78. return !~(path.basename(f).indexOf('@'))
  79. })
  80. next(null)
  81. },
  82. // 生成数据
  83. function(next){
  84. var arr = data.slice = []
  85. async.eachSeries(files, iterator, callback)
  86. function iterator(f, _next){
  87. gm(f).size(function(err, size){
  88. if(err){return}
  89. arr.push({
  90. filepath: f,
  91. imageurl: path.relative('src/sass', f).split(path.sep).join('/'),
  92. classname: classnameRule.call({}, path.basename(f, path.extname(f)), f),
  93. width: size.width,
  94. height: size.height
  95. })
  96. _next(null)
  97. })
  98. }
  99. function callback(err, result){
  100. next(null)
  101. }
  102. },
  103. // 生成css
  104. function(next){
  105. var tpl = (function(){/*
  106. // CSS Sprites切片样式
  107. <% slice.forEach(function(e){ %>
  108. .<%= e.classname%> {
  109. width: <%= e.width%>px;
  110. height: <%= e.height %>px;
  111. background-image: url(<%= e.imageurl%>);
  112. background-repeat: no-repeat;
  113. }
  114. <% }) %>
  115. */}).toString().split('\n').slice(1, -1).join('\n')
  116. var css = ejs.render(tpl, data).replace(/^\n/mg, '')
  117. fs.writeFileSync('src/sass/_slice.scss', css)
  118. }
  119. ])
  120. })
  121. gulp.task('default', ['dev_conn'], function(){
  122. if(argv.w){
  123. gulp.watch('src/slice/**', ['dev_slice2css'])
  124. }
  125. gulp.watch('src/tpl/**', ['dev_ejs'])
  126. gulp.watch('src/sass/**', ['dev_sass'])
  127. gulp.watch('src/img/**', reload)
  128. gulp.watch('src/js/**', reload)
  129. gulp.watch('src/*.html', reload)
  130. })
  131. }