Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. var gulp = require('gulp');
  2. var browserSync = require('browser-sync').create();
  3. var path = require('path');
  4. var fs = require('fs');
  5. var sourcemaps = require('gulp-sourcemaps');
  6. var source = require('vinyl-source-stream');
  7. var buffer = require('vinyl-buffer');
  8. var browserify = require('browserify');
  9. var watchify = require('watchify');
  10. var babel = require('babelify');
  11. var exit = require('gulp-exit');
  12.  
  13. //
  14. // Vars
  15.  
  16. var _publicDir = './public';
  17. var _sourceDir = './source';
  18.  
  19. var _scriptSource = _sourceDir + '/js';
  20. var _scriptPublic = _publicDir + '/js';
  21.  
  22. //
  23. // Babel
  24.  
  25. function compile(watch) {
  26.  
  27. var bundler = bundler = watchify(
  28. browserify({
  29. // Define the entry point for our application
  30. entries: [_scriptSource + '/main.js'],
  31. // Debugging is nice
  32. debug: true,
  33. // Allow importing from the following extensions
  34. extensions: [' ', 'js']
  35. }).transform(babel.configure({
  36. // Use all of the ES2015 spec
  37. presets: ["es2015"]
  38. }))
  39. );
  40.  
  41. function rebundle() {
  42. return bundler.bundle()
  43. .on('error', function(err) { console.error(err); this.emit('end'); })
  44. .pipe(source('main.js'))
  45. .pipe(buffer())
  46. .pipe(sourcemaps.init({ loadMaps: true }))
  47. .pipe(sourcemaps.write('./'))
  48. .pipe(gulp.dest(_scriptPublic))
  49. .pipe(browserSync.stream());
  50. }
  51.  
  52. if (watch) {
  53.  
  54. bundler.on('update', function() {
  55. console.log('-> re-bundling...');
  56. rebundle();
  57.  
  58. });
  59.  
  60. rebundle()
  61.  
  62. }else {
  63. rebundle().pipe(exit());
  64. }
  65.  
  66. }
  67.  
  68. function watch() {
  69. return compile(true);
  70. }
  71.  
  72. gulp.task('build',function () {
  73.  
  74. // compile JS
  75. compile(false);
  76.  
  77. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement