gulpfile-dev.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 win32 = process.platform === 'win32';
  9. var that = this;
  10. that.port = +argv.p || 3000;
  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. browserSync({
  15. ui:false,
  16. server: {
  17. baseDir: "src",
  18. directory: true
  19. },
  20. notify: false,
  21. ghostMode:false,
  22. port: that.port,
  23. open: "external",
  24. browser: "/Applications/Google\ Chrome.app/"
  25. })
  26. })
  27. gulp.task('dev_sass', function() {
  28. function sassCompile4win(){
  29. function normalizeErr(err){
  30. var path = require('path')
  31. var cwd = path.join(__dirname, '..')
  32. var relativePath = path.relative(cwd, err.file)
  33. return relativePath+' @'+err.line+':'+err.column+'\n'+err.message
  34. }
  35. var config = {
  36. onError: function(err){
  37. return plugins.notify().write(normalizeErr(err))
  38. }
  39. }
  40. return plugins.sass(config)
  41. }
  42. function sassCompile4nix(){
  43. function handler(){
  44. return plugins.notify.onError({
  45. title:'sass编译错误',
  46. message:'<%=error.message%>'
  47. })
  48. }
  49. return plugins.sass().on('error', handler())
  50. }
  51. // `gulp -n` 不启用sourcemap
  52. if(argv.n){
  53. return gulp.src('src/sass/*.scss')
  54. .pipe(plugins.cached('sass', {optimizeMemory: true}))
  55. .pipe(win32? sassCompile4nix() : sassCompile4nix())
  56. .pipe(plugins.autoprefixer({browsers: ['> 0%']}))
  57. .pipe(plugins.remember('sass'))
  58. .pipe(gulp.dest('src/css'))
  59. .pipe(reload({stream:true}))
  60. }
  61. return gulp.src('src/sass/*.scss')
  62. .pipe(plugins.plumber())
  63. .pipe(plugins.sourcemaps.init())
  64. .pipe(win32? sassCompile4nix() : sassCompile4nix())
  65. .pipe(plugins.sourcemaps.write({includeContent: false, sourceRoot: '../sass/'}))
  66. .pipe(plugins.sourcemaps.init({loadMaps: true}))
  67. .pipe(plugins.autoprefixer( {browsers: ['> 0%']} ))
  68. .pipe(plugins.sourcemaps.write({includeContent: false, sourceRoot: '../sass/'}))
  69. .pipe(gulp.dest('src/css'))
  70. .pipe(reload({stream:true}))
  71. })
  72. gulp.task('dev_ejs', function() {
  73. return gulp.src('src/tpl/*.ejs')
  74. .pipe(plugins.ejs().on('error', console.log))
  75. .pipe(gulp.dest('src/'))
  76. .pipe(reload({stream:true}))
  77. })
  78. gulp.task('dev_svg', function() {
  79. function renameSvg(p){
  80. p.basename = 'symbols'
  81. }
  82. return gulp.src('src/svg/slice/*.svg')
  83. .pipe(plugins.svgSymbols({templates: ['default-svg']}))
  84. .pipe(plugins.rename(renameSvg))
  85. .pipe(gulp.dest('src/svg'))
  86. .pipe(reload({stream:true}))
  87. })
  88. // 检测 src/img/slice 文件夹,读取图片信息来生成css切片样式
  89. gulp.task('dev_slice2css', function(){
  90. var fs = require('fs')
  91. var path = require('path')
  92. var async = require('gulp-uglify/node_modules/uglify-js/node_modules/async')
  93. var gm = require('gm')
  94. var ejs = require('gulp-ejs/node_modules/ejs')
  95. var classnameRule = function(fileName, p){
  96. var relPath = path.relative('src/img/slice', path.dirname(p))
  97. var name = win32? path.join(relPath, fileName).replace(/\\/g, '-'):path.join(relPath, fileName).replace(/\//g, '-')
  98. return name
  99. }
  100. var files, data = {}
  101. async.series([
  102. // 1. 文件过滤
  103. function(next){
  104. var glob = require("glob")
  105. files = glob.sync("src/img/slice/**", {nodir:true})
  106. files = files.filter(function(f){
  107. return !~(path.basename(f).indexOf('@'))
  108. })
  109. next(null)
  110. },
  111. // 2. 生成切片数据
  112. function(next){
  113. var arr = data.slice = []
  114. async.eachSeries(files, iterator, callback)
  115. function iterator(f, _next){
  116. gm(f).size(function(err, size){
  117. if(err){return}
  118. arr.push({
  119. filepath: f,
  120. imageurl: path.relative('src/sass', f).split(path.sep).join('/'),
  121. classname: classnameRule.call({}, path.basename(f, path.extname(f)), f),
  122. width: size.width,
  123. height: size.height
  124. })
  125. _next(null)
  126. })
  127. }
  128. function callback(err, result){
  129. next(null)
  130. }
  131. },
  132. // 3. 生成css文件
  133. function(next){
  134. var tpl = (function(){/*
  135. // CSS Sprites切片样式
  136. <% slice.forEach(function(e){ %>
  137. %<%= e.classname%> {
  138. width: <%= e.width%>px;
  139. height: <%= e.height %>px;
  140. background-image: url(<%= e.imageurl%>);
  141. background-repeat: no-repeat;
  142. }
  143. <% }) %>
  144. */}).toString().split('\n').slice(1, -1).join('\n')
  145. var css = ejs.render(tpl, data).replace(/^\n/mg, '')
  146. fs.writeFileSync('src/sass/_slice.scss', css)
  147. }
  148. ])
  149. })
  150. gulp.task('default', ['dev_conn'], function(){
  151. gulp.watch('src/img/slice/**', ['dev_slice2css'])
  152. gulp.watch('src/svg/slice/**', ['dev_svg'])
  153. gulp.watch('src/tpl/**', ['dev_ejs'])
  154. gulp.watch('src/sass/**', ['dev_sass'])
  155. gulp.watch('src/img/**', reload)
  156. gulp.watch('src/svg/**', reload)
  157. gulp.watch('src/js/**', reload)
  158. gulp.watch('src/*.html', reload)
  159. })
  160. }