gulpfile.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. const { series, parallel, task, src, dest } = require('gulp');
  2. var gulp = require('gulp');
  3. var sass = require('gulp-sass');
  4. var header = require('gulp-header');
  5. var cleanCSS = require('gulp-clean-css');
  6. var rename = require('gulp-rename');
  7. var uglify = require('gulp-uglify');
  8. var beautify = require('gulp-html-beautify');
  9. var pkg = require('./package.json');
  10. var browserSync = require('browser-sync').create();
  11. var exec = require('child_process').exec;
  12. // Copy third party libraries from /node_modules into /app/static/app/static/vendor
  13. function vendor_export(cb) {
  14. // Bootstrap
  15. src([
  16. 'node_modules/bootstrap/dist/**/*',
  17. '!node_modules/bootstrap/dist/css/bootstrap-grid*',
  18. '!node_modules/bootstrap/dist/css/bootstrap-reboot*'
  19. ])
  20. .pipe(dest('app/static/vendor/bootstrap'));
  21. // DataTables
  22. src([
  23. 'node_modules/datatables.net/js/*.js',
  24. 'node_modules/datatables.net-bs4/js/*.js',
  25. 'node_modules/datatables.net-bs4/css/*.css',
  26. 'node_modules/datatables.net-responsive/js/*'
  27. ])
  28. .pipe(dest('app/static/vendor/datatables/'));
  29. // Font Awesome
  30. src([
  31. 'node_modules/font-awesome/**/*',
  32. '!node_modules/font-awesome/{less,less/*}',
  33. '!node_modules/font-awesome/{scss,scss/*}',
  34. '!node_modules/font-awesome/.*',
  35. '!node_modules/font-awesome/*.{txt,json,md}'
  36. ])
  37. .pipe(dest('app/static/vendor/font-awesome'));
  38. // jQuery
  39. src([
  40. 'node_modules/jquery/dist/*',
  41. '!node_modules/jquery/dist/core.js'
  42. ])
  43. .pipe(dest('app/static/vendor/jquery'));
  44. cb()
  45. }
  46. function vendor_minify_js(cb) {
  47. src([
  48. 'app/static/vendor/**/*.js',
  49. '!app/static/vendor/**/*.min.js'
  50. ])
  51. .pipe(uglify())
  52. .pipe(rename({
  53. suffix: '.min'
  54. }))
  55. .pipe(dest('app/static/vendor'))
  56. cb()
  57. }
  58. function vendor_minify_css(cb) {
  59. src([
  60. 'app/static/vendor/**/*.css',
  61. '!app/static/vendor/**/*.min.css'
  62. ])
  63. .pipe(cleanCSS())
  64. .pipe(rename({
  65. suffix: '.min'
  66. }))
  67. .pipe(dest('app/static/vendor'))
  68. cb()
  69. }
  70. // Compile SASS
  71. function css_compile(cb) {
  72. src('app/static/sass/**/*.sass')
  73. .pipe(sass.sync({
  74. outputStyle: 'expanded'
  75. }).on('error', sass.logError))
  76. .pipe(rename({
  77. suffix: '.compiled'
  78. }))
  79. .pipe(dest('app/static/css'));
  80. cb()
  81. }
  82. // Minify CSS
  83. function css_minify(cb) {
  84. src([
  85. 'app/static/css/**/*.css',
  86. '!app/static/css/**/*.min.css'
  87. ])
  88. .pipe(cleanCSS())
  89. .pipe(rename({
  90. suffix: '.min'
  91. }))
  92. .pipe(dest('app/static/css'))
  93. .pipe(browserSync.stream({match: 'app/static/css/**/*.css'}));
  94. cb()
  95. }
  96. // Minify JavaScript
  97. function js(cb) {
  98. src([
  99. 'app/static/js/**/*.js',
  100. '!app/static/js/**/*.min.js'
  101. ])
  102. .pipe(uglify())
  103. .pipe(rename({
  104. suffix: '.min'
  105. }))
  106. .pipe(dest('app/static/js'))
  107. .pipe(browserSync.stream());
  108. cb()
  109. }
  110. // Configure the browserSync task
  111. function browser_sync() {
  112. browserSync.init({
  113. notify: false,
  114. proxy: '127.0.0.1:5000',
  115. open: false
  116. });
  117. }
  118. //Run Flask server
  119. function run_server() {
  120. return exec('flask run');
  121. }
  122. // Dev task
  123. function dev() {
  124. watch([
  125. 'app/templates/**/*.html',
  126. 'app/**/*.py',
  127. ], browserSync.reload);
  128. watch([
  129. 'app/static/sass/**/*.sass',
  130. ], ['css:compile', browserSync.reload]);
  131. watch([
  132. 'app/static/css/**/*.css',
  133. '!app/static/css/**/*.min.css',
  134. ], ['css:minify', browserSync.reload]);
  135. watch([
  136. 'app/static/js/**/*.js',
  137. '!app/static/js/**/*.min.js'
  138. ], ['js', browserSync.reload]);
  139. }
  140. // Tasks
  141. task('vendor', series(vendor_export, parallel(vendor_minify_js, vendor_minify_css)))
  142. task("css", series(css_compile, css_minify))
  143. task('js', js)
  144. task('dev', series(parallel(run_server, browser_sync), dev))
  145. task('default', series('vendor', parallel('css', 'js')))