Advertisement
Guest User

gulp4

a guest
Jan 9th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. var autoPrefixer = require("gulp-autoprefixer"),
  4.     browserSync = require("browser-sync"),
  5.     csscomb = require("gulp-csscomb"),
  6.     gulp = require("gulp"),
  7.     sass = require("gulp-sass"),
  8.     wait = require('gulp-wait'),
  9.     gutil = require("gutil"),
  10.     ftp = require("vinyl-ftp"),
  11.     fileinclude = require('gulp-file-include'),
  12.     changed = require('gulp-changed'),
  13.     del = require('del'),
  14.     plumber = require('gulp-plumber'),
  15.     notify = require('gulp-notify'),
  16.     imagemin = require('gulp-imagemin'),
  17.     sourcemaps = require('gulp-sourcemaps'),
  18.     imageminJpegRecompress = require('imagemin-jpeg-recompress');
  19.  
  20.     /**
  21.      * Browser selection for Autoprefixer
  22.      * @type {Array}
  23.      */
  24.  
  25.     var AUTOPREFIXER_BROWSERS = [
  26.         "last 2 version",
  27.         "> 1%",
  28.         "ie >= 9",
  29.         "ie_mob >= 10",
  30.         "ff >= 30",
  31.         "chrome >= 34",
  32.         "safari >= 7",
  33.         "opera >= 23",
  34.         "ios >= 7",
  35.         "android >= 4",
  36.         "bb >= 10"
  37.     ];
  38.  
  39.      /* file locations */
  40.  
  41.      var styleSass = "./src/assets/scss/style.scss",
  42.      sassPartialFiles = "src/assets/scss/**/*.scss",
  43.      vendorJs = "./src/assets/js/vendors.js",
  44.      activeJs = "./src/assets/js/active.js";
  45.  
  46.  
  47.  
  48.     /* browser sync */
  49.  
  50.     function browser_sync() {
  51.         browserSync.init({
  52.             server: {
  53.                 baseDir: "./tmp",
  54.                 index: "index.html",
  55.             },
  56.             port: 5566,
  57.             notify: false
  58.         });
  59.     };
  60.  
  61.  
  62.     /* SCSS compile */
  63.    
  64.     function css(done){
  65.        
  66.         gulp.src(styleSass)
  67.         .pipe(customPlumber('Error Running Sass'))
  68.         .pipe(sourcemaps.init())
  69.         .pipe(wait(500))
  70.         .pipe(sass())
  71.         .pipe(autoPrefixer(AUTOPREFIXER_BROWSERS))
  72.         .pipe(csscomb())
  73.         .pipe(sourcemaps.write('.'))
  74.         .pipe(gulp.dest("./src/assets/css"))
  75.         .pipe(browserSync.stream());
  76.  
  77.         done();
  78.     };
  79.  
  80.      /* JS tasks */
  81.    
  82.      function js(done) {
  83.         gulp.src([vendorJs, activeJs])
  84.         .pipe(customPlumber('Error Running JS'));
  85.         done();
  86.     };
  87.  
  88.     /* Include Html Pertials */
  89.  
  90.     function compile_html(done) {
  91.         gulp.src( 'src/*.html' )
  92.             .pipe(customPlumber('Error Running html-include'))
  93.             .pipe(fileinclude({ basepath: "src/_partials/" }))
  94.             .pipe(gulp.dest('./tmp'))
  95.             .pipe(browserSync.reload({
  96.                 stream: true
  97.                 })
  98.             );
  99.         done();
  100.     };
  101.  
  102.     /* clean tmp folder */
  103.  
  104.     function clean_tmp(done) {
  105.         del(['tmp/**/*']);
  106.         done();
  107.     };
  108.  
  109.     /* copy all folder to specific location */
  110.  
  111.     function copy_assets(done) {
  112.         gulp.src('./src/assets/**/*')
  113.         .pipe(changed('./tmp/assets'))
  114.         .pipe(gulp.dest('./tmp/assets'));
  115.         done();
  116.     };
  117.  
  118.     /* watch files */
  119.    
  120.     function watch_files(){
  121.         gulp.watch(sassPartialFiles, css);
  122.         gulp.watch([vendorJs, activeJs]).on("change", browserSync.reload);
  123.         gulp.watch('src/**/*', copy_assets);
  124.         gulp.watch(["src/*.html" , "src/_partials/**/*.htm"], compile_html);
  125.         gulp.watch(["src/*.html" , "src/_partials/**/*.htm"]).on("change", browserSync.reload);
  126.     }
  127.  
  128.     /* Gulp Ftp Server Upload */
  129.  
  130.     var d = new Date(),
  131.     month = d.getMonth(),
  132.     todayDate = d.getFullYear()+'.'+(month + 1)+'.'+d.getDate();
  133.  
  134.     var localFiles = ['tmp/**/*'];
  135.  
  136.     function getFtpConnection() {
  137.          return ftp.create({
  138.              host: 'ftp.whizthemes.com',
  139.              port: 21,
  140.              user: 'rashed@whizthemes.com',
  141.              password: 'agP8[;4dT.Ax',
  142.              parallel: 5,
  143.              log: gutil.log
  144.          });
  145.     }
  146.  
  147.     var remoteLogLocation = 'log/' + todayDate;
  148.     var remoteProjectLocation = 'project_name';
  149.  
  150.      
  151.      /* upload log */
  152.      
  153.     function upload_log(done) {
  154.          var conn = getFtpConnection();
  155.          gulp.src(localFiles, {
  156.              base: './tmp/',
  157.              buffer: false
  158.          }).pipe(conn.dest(remoteLogLocation));
  159.          done();
  160.     };  
  161.  
  162.      /* upload project */
  163.      
  164.     function upload_project(done) {
  165.          var conn = getFtpConnection();
  166.          gulp.src(localFiles, {
  167.              base: './tmp/',
  168.              buffer: false
  169.          }).pipe(conn.dest(remoteProjectLocation));
  170.          done();
  171.     };  
  172.  
  173.     /*  custom plumber */
  174.  
  175.     function customPlumber(errTitle) {
  176.         return plumber({
  177.             errorHandler: notify.onError({
  178.             title: errTitle || "Error running Gulp",
  179.             message: "Error: <%= error.message %>",
  180.             sound: "Glass"
  181.             })
  182.         });
  183.     }
  184.  
  185.  
  186.     /* Minify images */
  187.    
  188.     function minify_image(done) {
  189.         gulp.src('./tmp/assets/img/**/*')
  190.           .pipe(imagemin([
  191.             imagemin.gifsicle(),
  192.             imageminJpegRecompress({
  193.               loops:4,
  194.               min: 50,
  195.               max: 95,
  196.               quality:'high'
  197.             }),
  198.             imagemin.optipng({optimizationLevel: 7}),
  199.             imagemin.svgo({
  200.                 plugins: [
  201.                     {removeViewBox: true},
  202.                     {cleanupIDs: false}
  203.                 ]
  204.             })
  205.           ],
  206.  
  207.           {
  208.             verbose: true
  209.           }))
  210.           .pipe(gulp.dest('./build/assets/img'));
  211.  
  212.           done();
  213.     };
  214.  
  215.  
  216.     gulp.task('browser_sync', browser_sync);
  217.     gulp.task('css', css);
  218.     gulp.task('js', js);
  219.     gulp.task('compile_html', compile_html);
  220.     gulp.task('copy_assets', copy_assets);
  221.     gulp.task('clean_tmp', clean_tmp);
  222.     gulp.task('upload_log', upload_log);
  223.     gulp.task('upload_project', upload_project);
  224.     gulp.task('minify_image', minify_image);
  225.  
  226.     const build = gulp.series(copy_assets, compile_html, css, js);
  227.     const buildWatch = gulp.series(build, gulp.parallel(browser_sync, watch_files));
  228.  
  229.     gulp.task('default', buildWatch);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement