Skip to content

Instantly share code, notes, and snippets.

@Sanyambansal76
Created January 19, 2017 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sanyambansal76/4f96ae88b06decff3e4a582277e992c8 to your computer and use it in GitHub Desktop.
Save Sanyambansal76/4f96ae88b06decff3e4a582277e992c8 to your computer and use it in GitHub Desktop.
GULP TASK
"use strict";
var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync');
var concat = require('gulp-concat');
var connect = require('gulp-connect');
var imagemin = require('gulp-imagemin');
var newer = require('gulp-newer');
var notify = require('gulp-notify');
var plumber = require('gulp-plumber');
var pngquant = require('imagemin-pngquant');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');
var webkitAssign = require('webkit-assign/gulp');
var gulpif = require('gulp-if');
var gutil = require('gulp-util');
var gulpHtml = require('gulp-htmlmin');
var decomment = require('gulp-decomment');
var inject = require('gulp-inject');
var createHash = require('hash-generator');
var clean = require('gulp-clean');
var runSequence = require('run-sequence');
var del = require('del');
var fs = require('fs');
var src = "./static-src";
var bower = "./static/bower_components";
var nodeModules = './node_modules';
var lib = './static/lib';
var dist = "./static";
var appFileName = 'beta-app-'+createHash(8)+'.js';
var vendorFileName = 'alpha-vendor-'+createHash(8)+'.js';
var cssFileName = 'style-'+createHash(8)+'.css';
var onLocal = gutil.env.local;
var errorHandlerFactory = function (taskName) {
return function (err) {
notify.onError({
title: "Gulp Error: " + taskName,
message: "<%= error.message %>",
sound: "Beep"
})(err);
this.emit('end');
};
};
gulp.task('clean-scripts', function () {
// return del(['./static/js/**/*.js'])
return gulp.src(['./static/js/temp/**/*.js', './static/css/**/style-*.css'], {read: false})
.pipe(clean());
});
gulp.task('html', function () {
return gulp.src('./templates/**/*.{htm,html}')
.pipe(browserSync.stream());
});
gulp.task('html-partials', function () {
return gulp.src(src + '/templates/*')
.pipe(gulpHtml({ collapseWhitespace: true }))
.pipe(decomment({ trim: true }))
.pipe(gulp.dest(dist + '/templates'));
});
gulp.task('injectFile', function () {
var injReq = '<!-- inject:css --> <!-- endinject --> <!-- inject:js --> <!-- endinject -->';
fs.writeFileSync('templates/inject.html', injReq);
});
gulp.task('index', function () {
var target = gulp.src('./templates/inject.html');
// It's not necessary to read the files (will speed up things), we're only after their paths:
var sources = gulp.src(['./static/js/temp/**/*.js', './static/css/'+ cssFileName], {read: false}, {relative : true});
return target
.pipe(inject(sources))
.pipe(gulp.dest('./templates'));
});
gulp.task('images', function () {
var onError = errorHandlerFactory('images');
return gulp.src(src + '/img/*')
.pipe(plumber({ errorHandler: onError }))
.pipe(newer(dist + '/img'))
.pipe(imagemin({
optimizationLevel: 7,
multipass: true,
use: [pngquant()]
}))
.pipe(gulp.dest(dist + '/img'));
});
gulp.task('npm-scripts' ,function() {
var onError = errorHandlerFactory('scripts');
return gulp.src([
nodeModules + '/jquery/dist/jquery.js',
nodeModules + '/angular-dragula/dist/angular-dragula.js',
nodeModules + '/angular/angular.js',
nodeModules + '/angular-ui-bootstrap/dist/ui-bootstrap.js',
nodeModules + '/angular-ui-bootstrap/dist/ui-bootstrap-tpls.js',
nodeModules + '/angular-drag-scroll/src/ng-drag-scroll.js',
nodeModules + '/angulartics/src/angulartics.js',
nodeModules + '/angulartics/dist/angulartics-debug.min.js',
nodeModules + '/angulartics-google-analytics/dist/angulartics-ga.min.js',
nodeModules + '/angular-moment/angular-moment.js',
nodeModules + '/angular-animate/angular-animate.min.js',
nodeModules + '/angular-aria/angular-aria.min.js',
nodeModules + '/angular-cookies/angular-cookies.min.js',
nodeModules + '/angular-scroll/angular-scroll.min.js',
nodeModules + '/angular-route/angular-route.min.js',
nodeModules + '/angular-sanitize/angular-sanitize.min.js',
nodeModules + '/angular-filter/dist/angular-filter.min.js',
nodeModules + '/angular-messages/angular-messages.min.js',
nodeModules + '/angular-touch/angular-touch.min.js',
nodeModules + '/ngstorage/ngStorage.min.js',
nodeModules + '/ng-file-upload/dist/ng-file-upload.min.js',
nodeModules + '/ui-select/dist/select.js',
lib + '/jquery.ui.draggable.min.js',
nodeModules + '/bootstrap-sass/assets/javascripts/bootstrap.min.js',
nodeModules + '/jquery.easing/jquery.easing.min.js',
nodeModules + '/moment/min/moment.min.js',
nodeModules + '/video.js/dist/video.min.js',
nodeModules + '/videojs-ga/dist/videojs-ga.min.js',
nodeModules + '/raven-js/dist/raven.js',
nodeModules + '/raven-js/dist/plugins/angular.js'
])
.pipe(plumber({ errorHandler: onError }))
.pipe(decomment({ trim: true }))
.pipe(concat('vendor.js'))
.pipe(gulp.dest(dist + '/js'))
.pipe(concat(vendorFileName))
.pipe(gulpif(!onLocal, uglify()))
.pipe(gulp.dest(dist + '/js/temp'));
});
gulp.task('scripts', function () {
var onError = errorHandlerFactory('scripts');
return gulp.src([
src + '/js/scripts.js',
// src + '/js/app.js',
src + '/js/app.module.js',
src + '/js/utilitiesApp/app.utilities.module.js',
src + '/js/utilitiesApp/config.js',
src + '/js/utilitiesApp/config.setCsrftoken.js',
src + '/js/utilitiesApp/*.directive.js',
src + '/js/utilitiesApp/*.service.js',
src + '/js/utilitiesApp/*.filter.js',
src + '/js/utilitiesApp/*.controller.js',
src + '/js/projectApp/config.js',
src + '/js/projectApp/run.js',
src + '/js/projectApp/*.directive.js',
src + '/js/projectApp/*.service.js',
src + '/js/projectApp/*.controller.js',
src + '/js/projectApp/filters.js',
src + '/js/registrationApp/app.registration.module.js',
src + '/js/registrationApp/*config.js',
src + '/js/registrationApp/*.directive.js',
src + '/js/registrationApp/*.service.js',
src + '/js/registrationApp/*.controller.js',
src + '/js/userApp/app.user.module.js',
src + '/js/userApp/*.config.js',
src + '/js/userApp/constants.js',
src + '/js/userApp/*.directive.js',
src + '/js/userApp/*.service.js',
src + '/js/userApp/ProjectList.controller.js',
src + '/js/userApp/Benefits.controller.js',
src + '/js/userApp/ExpandedProject.controller.js',
src + '/js/userApp/ManagerDashboard.controller.js',
src + '/js/userApp/VideoModal.controller.js',
src + '/js/wizardApp/app.wizard.module.js',
src + '/js/wizardApp/interpolate.config.js',
src + '/js/wizardApp/constants.js',
src + '/js/wizardApp/unsafe.filter.js',
src + '/js/wizardApp/compile.directive.js',
src + '/js/wizardApp/*.service.js',
src + '/js/wizardApp/routes.js',
src + '/js/wizardApp/run.js',
src + '/js/wizardApp/constants.js',
src + '/js/wizardApp/*.controller.js',
src + '/js/searchApp/app.search.module.js',
src + '/js/searchApp/*.config.js',
src + '/js/searchApp/constants.js',
src + '/js/searchApp/*.service.js',
src + '/js/searchApp/*.directive.js',
src + '/js/searchApp/*.filter.js',
src + '/js/searchApp/*.controller.js',
src + '/js/mezzanineApp/app.mezzanine.module.js'
])
.pipe(webkitAssign())
.pipe(plumber({ errorHandler: onError }))
.pipe(gulpif(!onLocal, sourcemaps.init()))
.pipe(decomment({ trim: true }))
.pipe(concat('app.js'))
.pipe(gulp.dest(dist + '/js'))
.pipe(concat(appFileName))
.pipe(gulpif(!onLocal, sourcemaps.write()))
.pipe(gulp.dest(dist + '/js/temp'));
});
gulp.task('styles', function () {
var onError = errorHandlerFactory('styles');
return gulp.src(src + '/scss/**/*.scss')
.pipe(plumber({ errorHandler: onError }))
.pipe(gulpif(!onLocal, sourcemaps.init()))
.pipe(sass({ outputStyle: 'compressed' }).on('error', sass.logError))
.pipe(autoprefixer())
.pipe(rename({ suffix: '.min' }))
.pipe(gulpif(!onLocal, sourcemaps.write()))
.pipe(gulp.dest(dist + '/css'))
.pipe(concat(cssFileName))
.pipe(gulp.dest(dist + '/css'))
.pipe(browserSync.stream({ match: '**/*.css' }));
});
gulp.task('vendor-styles', function() {
var onError = errorHandlerFactory('vendor-styles');
return gulp.src([
nodeModules + '/animate.css/animate.min.css',
nodeModules + '/ui-select/dist/select.min.css'
])
.pipe(plumber({ errorHandler: onError }))
.pipe(gulp.dest(dist + '/css/vendor'));
});
gulp.task('fonts', function () {
var onError = errorHandlerFactory('fonts');
return gulp.src(nodeModules + '/bootstrap-sass/assets/fonts/**/*.{eot,svg,ttf,woff,woff2}')
.pipe(plumber({ errorHandler: onError }))
.pipe(gulp.dest(dist + '/fonts'));
});
gulp.task('scripts-watch', ['scripts'], browserSync.reload);
gulp.task('image-watch', ['images'], browserSync.reload);
gulp.task('watch', ['browserSync'], function () {
gulp.watch(src + '/scss/*.scss', ['styles']);
gulp.watch(src + '/js/**/*.js', ['scripts-watch']);
gulp.watch('./templates/**/*.{htm,html}', ['html']);
gulp.watch(src + '/templates/*', ['html-partials']);
gulp.watch(src + '/img/*', ['image-watch']);
});
gulp.task('browserSync', function () {
browserSync({
proxy: onLocal ? '0.0.0.0:8000' : '192.168.99.100:8000'
})
});
gulp.task('build', function() {
runSequence('clean-scripts',
['npm-scripts', 'scripts', 'styles', 'vendor-styles', 'images', 'fonts', 'html-partials', 'injectFile'],
'index');
});
gulp.task('default', ['build', 'watch']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment