Guest User

Untitled

a guest
Mar 18th, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var gulp = require('gulp');
  2. var autoprefixer = require('gulp-autoprefixer');
  3. var plumber = require('gulp-plumber');
  4. var gutil = require('gulp-util');
  5. var concat = require('gulp-concat');
  6. var cleanCSS = require('gulp-clean-css');
  7. var rename = require("gulp-rename");
  8. var sass = require('gulp-sass');
  9. var uglify = require('gulp-uglify');
  10. var sourcemaps  = require("gulp-sourcemaps");
  11. var browserSync = require('browser-sync').create();
  12. var nodemon = require('gulp-nodemon');
  13.  
  14. // Error Handling
  15. var gulp_src = gulp.src;
  16. gulp.src = function() {
  17.   return gulp_src.apply(gulp, arguments)
  18.     .pipe(plumber(function(error) {
  19.       // Output an error message
  20.       gutil.log(gutil.colors.red('Error (' + error.plugin + '): ' + error.message));
  21.       // emit the end event, to properly end the task
  22.       this.emit('end');
  23.     })
  24.   );
  25. };
  26.  
  27. // Scripts
  28. gulp.task('scripts', function() {
  29.   return gulp.src('./public/js/main.js')
  30.     .pipe(concat('main.min.js'))
  31.     .pipe(gulp.dest('./public/js/'))
  32.     .pipe(uglify())
  33.     .pipe(gulp.dest('./public/js/'))
  34. });
  35.  
  36. // Styles
  37. gulp.task('styles', function() {
  38.   return gulp.src('./public/sass/*.scss')
  39.   .pipe(sass())
  40.   .pipe(autoprefixer('last 2 versions'))
  41.   .pipe(sourcemaps.init())
  42.   .pipe(gulp.dest('./public/css/'))
  43.   .pipe(cleanCSS())
  44.   .pipe(sourcemaps.write())
  45.   .pipe(concat("main.css", {newLine: ""}))
  46.   .pipe(gulp.dest('./public/css/'))
  47. });
  48.  
  49. // BrowserSync
  50. gulp.task('browserSync', ['nodemon'], function() {
  51.   browserSync.init({
  52.     proxy: "http://localhost:3000",
  53.     port: 4000,
  54.     open: false,
  55.     notify: true,
  56.     // snippetOptions: {
  57.     //   rule: {
  58.     //     match: /<\/body>/i,
  59.     //     fn: function (snippet, match) {
  60.     //       return snippet + match;
  61.     //     }
  62.     //   }
  63.     // }
  64.   })
  65. })
  66.  
  67. // Default Tasks
  68. gulp.task('default', ['browserSync'], function () {
  69.     gulp.watch('./views/**/*.ejs', browserSync.reload);
  70.     gulp.watch('./public/**/*.js', browserSync.reload);
  71.     gulp.watch('./public/**/*.scss', browserSync.reload);
  72.     gulp.watch(['./routes/**/*.js', './app.js', './bin/www'], ['bs-delay']);
  73. });
  74.  
  75. // Give nodemon time to restart
  76. gulp.task('bs-delay', function () {
  77.   setTimeout(function () {
  78.     browserSync.reload({ stream: false });
  79.   }, 1000);
  80. });
  81.  
  82. // Nodemon
  83. gulp.task('nodemon', function (cb) {
  84.     var started = false;
  85.     return nodemon({
  86.         script: './bin/www',
  87.         ext: 'js',
  88.         ignore: ['public/**/*.js'],
  89.         env: {
  90.             'NODE_ENV': 'development',
  91.             'DEBUG': 'appname:*'
  92.      }
  93.     }).on('start', function () {
  94.         //avoid nodemon being started multiple times
  95.         if (!started) {
  96.             cb();
  97.             started = true;
  98.         }
  99.     })
  100.     .on('crash', function() {
  101.         // console.log('nodemon.crash');
  102.     })
  103.     .on('restart', function() {
  104.         // console.log('nodemon.restart');
  105.     })
  106.     .once('quit', function () {
  107.         // handle ctrl+c without a big weep
  108.         process.exit();
  109.     });
  110. });
Advertisement
Add Comment
Please, Sign In to add comment