gulpfile.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var gulp = require('gulp');
  2. var gutil = require('gulp-util');
  3. var source = require('vinyl-source-stream');
  4. var babelify = require('babelify');
  5. var watchify = require('watchify');
  6. var exorcist = require('exorcist');
  7. var browserify = require('browserify');
  8. var browserSync = require('browser-sync').create();
  9. // Watchify args contains necessary cache options to achieve fast incremental bundles.
  10. // See watchify readme for details. Adding debug true for source-map generation.
  11. watchify.args.debug = true;
  12. // Input file.
  13. var bundler = watchify(browserify('./app/js/app.js', watchify.args));
  14. // Babel transform
  15. bundler.transform(babelify.configure({
  16. sourceMapRelative: 'app/js'
  17. }));
  18. // On updates recompile
  19. bundler.on('update', bundle);
  20. function bundle() {
  21. gutil.log('Compiling JS...');
  22. return bundler.bundle()
  23. .on('error', function (err) {
  24. gutil.log(err.message);
  25. browserSync.notify("Browserify Error!");
  26. this.emit("end");
  27. })
  28. .pipe(exorcist('app/js/dist/bundle.js.map'))
  29. .pipe(source('bundle.js'))
  30. .pipe(gulp.dest('./app/js/dist'))
  31. .pipe(browserSync.stream({once: true}));
  32. }
  33. /**
  34. * Gulp task alias
  35. */
  36. gulp.task('bundle', function () {
  37. return bundle();
  38. });
  39. /**
  40. * First bundle, then serve from the ./app directory
  41. */
  42. gulp.task('default', ['bundle'], function () {
  43. browserSync.init({
  44. server: "./app"
  45. });
  46. });