gulpfile.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict'
  2. const gulp = require('gulp')
  3. const concat = require('gulp-concat')
  4. // const sass = require('gulp-sass')
  5. // const stylus = require('gulp-stylus')
  6. const rename = require('gulp-rename')
  7. const uglify = require('gulp-uglify')
  8. const cleanCSS = require('gulp-clean-css')
  9. const babel = require('gulp-babel')
  10. const gulpsync = require('gulp-sync')(gulp)
  11. const jshint = require('gulp-jshint')
  12. const stylish = require('jshint-stylish')
  13. const expect = require('gulp-expect-file')
  14. const plumber = require('gulp-plumber')
  15. const browserSync = require('browser-sync').create()
  16. const autoprefixer= require('gulp-autoprefixer')
  17. const rewriteCSS = require('gulp-rewrite-css')
  18. const _srcdir = 'tracim/tracim/public/assets/'
  19. const _tpldir = 'tracim/tracim/templates/'
  20. const listCssFiles = [
  21. _srcdir + 'css/default_skin.css',
  22. _srcdir + 'css/bootstrap.css',
  23. _srcdir + 'font-awesome-4.2.0/css/font-awesome.css',
  24. _srcdir + 'select2-4.0.3/css/select2.min.css',
  25. _srcdir + 'css/dashboard.css'
  26. ]
  27. const listJsFiles = [
  28. _srcdir + 'select2-4.0.3/js/select2.min.js',
  29. _srcdir + 'js/bootstrap.min.js',
  30. _srcdir + 'js/trad.js',
  31. _srcdir + 'js/main_es5.js'
  32. ]
  33. // CSS task
  34. gulp.task('css', function () {
  35. const dest = _srcdir + '/dist/'
  36. return gulp.src(listCssFiles)
  37. .pipe(expect({verbose: true}, listCssFiles))
  38. .pipe(rewriteCSS({
  39. destination: dest,
  40. // debug: true
  41. }))
  42. // .pipe(sass().on('error', sass.logError))
  43. // .pipe(stylus())
  44. .pipe(autoprefixer({
  45. browsers: ['last 2 versions'],
  46. cascade: false
  47. }))
  48. .pipe(concat('all.css'))
  49. .pipe(gulp.dest(dest))
  50. .pipe(browserSync.stream())
  51. })
  52. // JS tasks
  53. gulp.task('js_hint', function() {
  54. return gulp.src(_srcdir + '/js/main.js')
  55. .pipe(expect({verbose: true}, _srcdir + '/js/main.js'))
  56. .pipe(jshint())
  57. .pipe(jshint.reporter(stylish))
  58. })
  59. gulp.task('js_transpiling', function() {
  60. return gulp.src(_srcdir + '/js/main.js')
  61. .pipe(plumber())
  62. .pipe(babel({ presets: ['es2015'] }))
  63. .pipe(concat('main_es5.js'))
  64. .pipe(gulp.dest(_srcdir + '/js/'))
  65. })
  66. gulp.task('js_concat', function() {
  67. return gulp.src(listJsFiles)
  68. .pipe(expect({verbose: true}, listJsFiles))
  69. .pipe(concat('all.js'))
  70. .pipe(gulp.dest(_srcdir + '/dist/'))
  71. .pipe(browserSync.stream())
  72. })
  73. gulp.task('js_sync', gulpsync.sync(['js_transpiling', 'js_concat']))
  74. gulp.task('js', ['js_hint', 'js_sync'])
  75. // BUILD tasks
  76. gulp.task('prod_css', ['css'], function () {
  77. return gulp.src(_srcdir + '/dist/all.css')
  78. .pipe(expect({verbose: true}, _srcdir + '/dist/all.css'))
  79. .pipe(cleanCSS({keepSpecialComments:0}))
  80. .pipe(concat('all.min.css'))
  81. .pipe(gulp.dest(_srcdir + '/dist/'))
  82. })
  83. gulp.task('prod_js', ['js'], function () {
  84. return gulp.src(_srcdir + '/dist/all.js')
  85. .pipe(expect({verbose: true}, _srcdir + '/dist/all.js'))
  86. .pipe(uglify())
  87. .pipe(concat('all.min.js'))
  88. .pipe(gulp.dest(_srcdir + '/dist/'))
  89. })
  90. gulp.task('prod', ['prod_css', 'prod_js'])
  91. gulp.task('dev', ['css', 'js'])
  92. // WATCH task
  93. gulp.task('watch', function () {
  94. gulp.watch([
  95. _srcdir + '/js/*.js',
  96. '!'+_srcdir+'/js/scripts_es5.js',
  97. '!'+_srcdir+'/js/*.min.*'
  98. ], {verbose: true, debounceDelay: 2000}, ['js'])
  99. gulp.watch([
  100. _srcdir + '/css/*.css',
  101. '!'+_srcdir+'/css/*.min.*',
  102. '!'+_srcdir+'/css/*.map'
  103. ], ['css'])
  104. })
  105. // LIVERELOAD task
  106. gulp.task('livesync', function() {
  107. browserSync.init({
  108. ghostMode: false, // comment this line to mirror input click and scroll on all opened browsers
  109. proxy: "127.0.0.1:8080",
  110. browser: "chromium",
  111. port: 8081
  112. })
  113. gulp.watch(_tpldir + '**/*.html').on('change', browserSync.reload)
  114. })
  115. gulp.task('watchsync', ['watch', 'livesync'])
  116. // DEFAULT task
  117. gulp.task('default', function () {
  118. console.log(`
  119. Usable tasks : watchsync, watch, dev, prod, js, css
  120. Other available tasks : livesync, js_sync, js_concat, js_hint, prod_css, prod_js
  121. `)
  122. })