Advertisement
nazares

gulpfile.js

Nov 19th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const   gulp          = require('gulp'),
  4.         connect       = require('gulp-connect-php'),
  5.         browserSync   = require('browser-sync').create(),
  6.         ngrok         = require('ngrok'),
  7.         gutil         = require('gulp-util'),
  8.         pug           = require('gulp-pug'),
  9.         sass          = require('gulp-sass'),
  10.         sourcemaps    = require('gulp-sourcemaps'),
  11.         babel         = require('gulp-babel'),
  12.         concat        = require('gulp-concat'),
  13.         uglify        = require('gulp-uglify'),
  14.         rename        = require('gulp-rename'),
  15.         notify        = require('gulp-notify'),
  16.         autoprefixer  = require('gulp-autoprefixer'),
  17.  
  18.     path = {
  19.         root: './app',
  20.         maps: './_maps',
  21.         src: {
  22.             pug:     'app/**/*.pug',
  23.             html:    'app/**/*.html',
  24.             php:     'app/**/*.php',
  25.             sass:    'app/source/styles/**/*.scss',
  26.             js:      'app/source/scripts/*.js',
  27.             jquery:  'bower_components/jquery/dist/jquery.min.js'
  28.         },
  29.         dest: {
  30.             css:     'app/css',
  31.             js:      'app/js',
  32.             jslibs:  'app/js/libs'
  33.         }
  34.     },
  35.     jslibs = [path.src.jquery];
  36.  
  37. gulp.task('connect-sync', function() {
  38.   connect.server({hostname:'0.0.0.0', port:'8000', base: path.root}, function (){
  39.     browserSync.init({
  40.       proxy: 'localhost:8000',
  41.       notify: false
  42.     });
  43.   }, ngrok.connect({
  44.                 proto: 'http',
  45.                 addr: 8000,
  46.                 console_ui: true
  47.             }, function(err, url){
  48.                 console.log(gutil.colors.blue('-------------------------'));
  49.                 console.log(gutil.colors.bgBlue(url));
  50.                 console.log(gutil.colors.blue('-------------------------'));
  51.                 })
  52.             );
  53. });
  54.  
  55. gulp.task('pug2html', function(){
  56.     return gulp.src(path.src.pug, {base: './'})
  57.         .pipe(sourcemaps.init())
  58.         .pipe(pug().on('error', notify.onError(function(error){
  59.             return 'An error occured while compiling pug file.\nLook in the console for details.\n' + error;
  60.         })))
  61.         .pipe(sourcemaps.write(path.src.maps))
  62.         .pipe(gulp.dest('.'))
  63. });
  64.  
  65. gulp.task('sass', function(){
  66.     return gulp.src(path.src.sass)
  67.         .pipe(sourcemaps.init())
  68.         .pipe(sass.sync({outputStyle: 'compressed'}).on('error', sass.logError))
  69.         .pipe(autoprefixer({browsers:['last 2 versions'],
  70.             cascade: false
  71.         }))
  72.         .pipe(sourcemaps.write(path.maps))
  73.         .pipe(gulp.dest(path.dest.css))
  74.         .pipe(browserSync.stream())
  75. });
  76.  
  77. gulp.task('jslibs', function(){
  78.     return gulp.src(jslibs)
  79.         .pipe(sourcemaps.init())
  80.         .pipe(concat('libs.min.js'))
  81.         .pipe(sourcemaps.write('../_maps'))
  82.         .pipe(gulp.dest(path.dest.jslibs))
  83. });
  84.  
  85. gulp.task('scripts', ['jslibs'], function(){
  86.     return gulp.src(path.src.js)
  87.         .pipe(sourcemaps.init())
  88.         .pipe(babel({presets:['latest']}))
  89.         .pipe(concat('source.js'))
  90.         .pipe(uglify())
  91.         .pipe(rename({suffix:'.min'}))
  92.         .pipe(sourcemaps.write(path.maps))
  93.         .pipe(gulp.dest(path.dest.js))
  94.         .pipe(browserSync.stream())
  95. });
  96.  
  97. gulp.task('watch',['connect-sync', 'scripts', 'sass', 'pug2html'], function(){
  98.     gulp.watch(path.src.pug, ['pug2html']);
  99.     gulp.watch([path.src.html, path.src.php]).on('change', browserSync.reload);
  100.     gulp.watch(path.src.sass, ['sass']);
  101.     gulp.watch(path.src.js, ['scripts']);
  102. });
  103.  
  104. gulp.task('default', ['watch']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement