22nds
8/5/2017 - 12:15 PM

Gulpfile.js - various

Gulpfile.js - various

'use strict';

var gulp = require( 'gulp' );
var env = require( 'babel-preset-env' );
var runSequence = require( 'run-sequence' );
var del = require( 'del' );

// loading in all plugins that start with 'gulp'
var plugins = require( 'gulp-load-plugins' )( {
  pattern: [ 'gulp-*', 'gulp.*', '@*/gulp{-,.}*', 'main-bower-files' ],
  replaceString: /^gulp(-|\.)/,
  camelize: true,
  rename: {
    'gulp-clean-css': 'cleanCSS'
  }
} );

// Define default destination folder
var src = 'src/';
var build = 'app/';

// TASK FOR BABEL
gulp.task( 'babel', function () {
  return gulp.src( src + 'js/getVersion.js' )
    .pipe( plugins.babel( {
      'presets': [
        [ 'env']
      ]
    } ) )
    .pipe( plugins.rename( {
      basename: 'transpiled'
    } ) )
    .pipe( gulp.dest( src + 'js/' ) )
    .pipe( plugins.notify( {
      message: 'Babel transpiled'
    } ) );
} );

// Delete CSS files - dependencies & build
gulp.task( 'clean', function () {
  return del( [
    build + 'index.html',
    build + 'css/*',
    build + 'js/*',
    build + 'lib/*',
    src + 'css/*',
    src + 'js/main.js',
    src + 'js/main.min.js',
    src + 'js/transpiled.js',
    src + 'lib/*'
  ] );
} );

// jQuery
gulp.task( 'copy-jquery', function () {
  return gulp.src( 'bower_components/jquery/dist/jquery.min.js' )
    .pipe( gulp.dest( build + 'lib/js' ) )
    .pipe( gulp.dest( src + 'lib/js' ) )
    .pipe( plugins.notify( {
      message: 'jQuery copied'
    } ) );
} );

// Bootstrap
gulp.task( 'copy-bootstrap', function () {
  return gulp.src( 'bower_components/bootstrap/dist/css/bootstrap.min.css' )
    .pipe( gulp.dest( src + 'lib/css' ) )
    .pipe( gulp.dest( build + 'lib/css' ) )
    .pipe( plugins.notify( {
      message: 'Bootstrap copied'
    } ) );
} );

// Ladda buttons
gulp.task( 'copy-ladda', function () {
  return gulp.src( 'node_modules/ladda/dist/**' )
    .pipe( gulp.dest( src + 'lib/ladda' ) )
    .pipe( gulp.dest( build + 'lib/ladda' ) )
    .pipe( plugins.notify( {
      message: 'Ladda copied'
    } ) );
} );

// Copy html
gulp.task( 'copy-html', function () {
  return gulp.src( src + 'index.html' )
    .pipe( gulp.dest( build ) )
    .pipe( plugins.notify( {
      message: 'HTML copied'
    } ) );
} );

// Concatenate & Minify JS
gulp.task( 'scripts', function () {
  // return gulp.src( src + 'js/transpiled.js' )
  return gulp.src( src + 'js/getVersion.js' )
    .pipe( plugins.concat( 'main.js' ) )
    .pipe( gulp.dest( build + './js/' ) )
    .pipe( gulp.dest( src + './js/' ) )
    .pipe( plugins.notify( {
      message: 'Scripts minified and copied'
    } ) )
    .pipe( plugins.livereload() );
} );

var swallowError = function ( err ) {
  // gulputil.log(err.toString());
  process.stdout.write( err );
  this.emit( 'end' );
};

// Build CSS and it's minified version from LESS
gulp.task( 'less', function () {
  return gulp.src( src + './less/main.less' )
    .pipe( plugins.less()
      // .on('error', sass.logError))
      .on( 'error', swallowError ) )
    .pipe( plugins.autoprefixer() )
    .pipe( plugins.rename( {
      basename: 'style'
    } ) )
    .pipe( gulp.dest( build + './css/' ) )
    .pipe( plugins.rename( {
      suffix: '.min'
    } ) )
    .pipe( plugins.cleanCSS( {
      keepBreaks: false,
      compatibility: 'ie9'
    } ) )
    .pipe( gulp.dest( build + './css/' ) )
    .pipe( gulp.dest( src + './css/' ) )
    .pipe( plugins.notify( {
      message: 'LESS/CSS minified and copied'
    } ) );
} );


// The default run action
gulp.task( 'default', function () {
  runSequence(
    [ 'clean' ], [ 'less' ], [ 'copy-html', 'copy-ladda' ,'copy-jquery', 'copy-bootstrap' ], // parallel
    //[ 'babel' ],
    [ 'scripts' ]
  );
} );

// Rerun the task when a file changes
gulp.task( 'watch', function () {
  plugins.livereload.listen();
  gulp.watch( [ src + 'js/*.js', src + 'less/*.less', src + 'index.html' ], [ 'default' ] );
} );
"use strict";

const plumber = require("gulp-plumber");
const through = require("through2");
const chalk = require("chalk");
const newer = require("gulp-newer");
const babel = require("gulp-babel");
const watch = require("gulp-watch");
const gutil = require("gulp-util");
const gulp = require("gulp");
const path = require("path");

const base = path.join(__dirname, "packages");
const scripts = "./packages/*/src/**/*.js";

function swapSrcWithLib(srcPath) {
  const parts = srcPath.split(path.sep);
  parts[1] = "lib";
  return parts.join(path.sep);
}

gulp.task("default", ["build"]);

gulp.task("build", function() {
  return gulp
    .src(scripts, { base: base })
    .pipe(
      plumber({
        errorHandler: function(err) {
          gutil.log(err.stack);
        },
      })
    )
    .pipe(
      newer({
        dest: base,
        map: swapSrcWithLib,
      })
    )
    .pipe(
      through.obj(function(file, enc, callback) {
        gutil.log("Compiling", "'" + chalk.cyan(file.relative) + "'...");
        callback(null, file);
      })
    )
    .pipe(babel())
    .pipe(
      through.obj(function(file, enc, callback) {
        // Passing 'file.relative' because newer() above uses a relative path and this keeps it consistent.
        file.path = path.resolve(file.base, swapSrcWithLib(file.relative));
        callback(null, file);
      })
    )
    .pipe(gulp.dest(base));
});

gulp.task("watch", ["build"], function() {
  watch(scripts, { debounceDelay: 200 }, function() {
    gulp.start("build");
  });
});
var gulp = require("gulp");
var babel = require("gulp-babel");
var sourcemaps = require("gulp-sourcemaps");
var concat = require("gulp-concat");

gulp.task("default", function () {
  return gulp.src("src/**/*.js")
    .pipe(sourcemaps.init())
    .pipe(babel())
    .pipe(concat("all.js"))
    .pipe(sourcemaps.write("."))
    .pipe(gulp.dest("dist"));
});