gulpfile.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. var browserify = require('browserify');
  2. var clean = require('gulp-clean');
  3. var gulp = require('gulp');
  4. var notify = require('gulp-notify');
  5. var path = require('path');
  6. var sass = require('gulp-dart-sass');
  7. var source = require('vinyl-source-stream');
  8. var taskListing = require('gulp-task-listing');
  9. var uglify = require('gulp-uglify');
  10. var sourcemaps = require('gulp-sourcemaps');
  11. var buffer = require('vinyl-buffer');
  12. var ROOT = 'static';
  13. var VENDOR_CONFIG = {
  14. 'src': [
  15. 'backbone',
  16. 'jquery',
  17. 'underscore',
  18. 'bootbox',
  19. ],
  20. 'target': 'vendor.js',
  21. 'targetDir': './static/build/'
  22. };
  23. function excludeVendor(b) {
  24. VENDOR_CONFIG.src.forEach(function(vendorLib) {
  25. b.exclude(vendorLib);
  26. });
  27. }
  28. function bytesToKB(bytes) { return Math.floor(+bytes/1024); }
  29. function logBundle(filename, watching) {
  30. return function (err, buf) {
  31. if (err) {
  32. console.error(err.toString());
  33. if (!watching) {
  34. process.exit(1);
  35. }
  36. }
  37. if (!watching) {
  38. console.log(filename + ' ' + bytesToKB(buf.length) + ' KB written');
  39. }
  40. }
  41. }
  42. function sassTask(root, inputFile) {
  43. return function sassing() {
  44. var onError = function(err) {
  45. notify({'title': 'Sass Compile Error'}).write(err);
  46. };
  47. return gulp.src(path.join(root, 'css', inputFile))
  48. .pipe(sass({
  49. 'sourceComments': 'map',
  50. }).on('error', onError))
  51. .pipe(gulp.dest(path.join(root, 'build/')));
  52. };
  53. }
  54. function browserifyTask(root, inputFile) {
  55. return function browserifying() {
  56. var onError = function() {
  57. var args = Array.prototype.slice.call(arguments);
  58. notify.onError({
  59. 'title': 'JS Compile Error',
  60. 'message': '<%= error.message %>'
  61. }).apply(this, args);
  62. // Keep gulp from hanging on this task
  63. this.emit('end');
  64. };
  65. // Browserify needs a node module to import as its arg, so we need to
  66. // force the leading "./" to be included.
  67. var b = browserify({
  68. entries: './' + path.join(root, 'js', inputFile),
  69. debug: true
  70. })
  71. excludeVendor(b);
  72. return b.bundle()
  73. .pipe(source(inputFile))
  74. .pipe(buffer())
  75. .pipe(sourcemaps.init({'loadMaps': true, 'debug': true}))
  76. // Add transformation tasks to the pipeline here.
  77. .pipe(uglify())
  78. .on('error', onError)
  79. .pipe(sourcemaps.write('./'))
  80. .pipe(gulp.dest(path.join(root, 'build/')));
  81. };
  82. }
  83. function watchTask(root) {
  84. return function watching() {
  85. gulp.watch(path.join(root, 'sass/**/*.scss'), gulp.series('sass'));
  86. gulp.watch([
  87. path.join(root, 'js/**/*.js'),
  88. path.join(root, 'js/**/*.hbs')
  89. ], gulp.series('browserify'));
  90. gulp.watch([
  91. path.join(root, 'html/**'),
  92. path.join(root, 'fonts/**')
  93. ], gulp.series('sync-static-assets'))
  94. };
  95. }
  96. function cleanTask() {
  97. var paths = Array.prototype.slice.apply(arguments);
  98. return function cleaning() {
  99. return gulp.src(paths, {allowEmpty: true}).pipe(clean());
  100. };
  101. }
  102. gulp.task('vendor-build-js', function() {
  103. var onError = function() {
  104. var args = Array.prototype.slice.call(arguments);
  105. notify.onError({
  106. 'title': 'JS Compile Error',
  107. 'message': '<%= error.message %>'
  108. }).apply(this, args);
  109. // Keep gulp from hanging on this task
  110. this.emit('end');
  111. };
  112. var b = browserify()
  113. .require(VENDOR_CONFIG.src);
  114. return b.bundle(logBundle(VENDOR_CONFIG.target))
  115. .pipe(source(VENDOR_CONFIG.target))
  116. .pipe(buffer())
  117. // Add transformation tasks to the pipeline here.
  118. .pipe(uglify())
  119. .on('error', onError)
  120. .pipe(gulp.dest(VENDOR_CONFIG.targetDir));
  121. });
  122. gulp.task('help', taskListing);
  123. gulp.task('sync-static-assets', function() {
  124. return gulp.src([
  125. path.join(ROOT, 'html/**'),
  126. path.join(ROOT, 'fonts/**'),
  127. path.join(ROOT, 'img/**')
  128. ]).pipe(gulp.dest(path.join(ROOT, 'build')));
  129. });
  130. gulp.task('sass', sassTask(ROOT, '*.*css'));
  131. gulp.task('browserify', browserifyTask(ROOT, 'main.js'));
  132. gulp.task('build', gulp.parallel('sass', 'browserify', 'sync-static-assets', 'vendor-build-js'));
  133. gulp.task('watch', gulp.series('build', watchTask(ROOT)));
  134. gulp.task('clean', gulp.series(cleanTask(path.join(ROOT, 'build'))));
  135. gulp.task('default', gulp.series('help'));