Skip to content

Instantly share code, notes, and snippets.

@scottcorgan
Last active August 29, 2015 14:10
Show Gist options
  • Save scottcorgan/4242ce4ae07112e2c411 to your computer and use it in GitHub Desktop.
Save scottcorgan/4242ce4ae07112e2c411 to your computer and use it in GitHub Desktop.
Divshot local with LiveReload and Gulp
var livereload = require('gulp-livereload'),
superstatic = require('superstatic'),
dest = 'build';
gulp.task('server', function(next) {
var server = superstatic();
next();
});
gulp.task('watch', ['server'], function() {
var server = livereload();
gulp.watch(dest + '/**').on('change', function(file) {
server.changed(file.path);
});
});
@daanaerts
Copy link

'use strict';

// USAGE
// Development (serve, superstatic, watch) : gulp watch
// Build dist package (minify, version)    : gulp build
// Build and start divshot server          : gulp build --serve

var gulp = require('gulp');
var $ = require('gulp-load-plugins')();

var argvServe = require('yargs').argv.serve;

// Config
var dev = '.tmp',
  dist = 'dist',
  devApiHost = ***
  distApiHost = ***
  apiHost = devApiHost;

gulp.task('styles', function() {
  return gulp.src('app/styles/main.scss')
    .pipe($.sass({
      style: 'expanded',
      precision: 10
    }))
    .pipe($.autoprefixer('last 1 version'))
    .pipe(gulp.dest(dev + '/styles'));
});

gulp.task('bower-components', function() {
  return $.bowerFiles()
    .pipe(gulp.dest(dev + '/bower_components'));
});

gulp.task('scripts', function() {
  return gulp.src('app/scripts/**/*.js')
    .pipe($.jshint())
    .pipe($.jshint.reporter(require('jshint-stylish')))
    .pipe($.replaceTask({
      patterns: [{
        match: "apiHost",
        replacement: devApiHost
      }]
    }))
    .pipe(gulp.dest(dev + '/scripts'));
});

gulp.task('templates', function() {
  return gulp.src('./app/views/partials/**/*.html')
    .pipe($.angularTemplatecache({
      module: 'templatescache',
      standalone: true,
      root: 'partials/'
    }))
    .pipe(gulp.dest(dev + '/scripts'));
});

gulp.task('images', function() {
  return gulp.src('app/images/**/*')
    .pipe(gulp.dest(dev + '/images'));
});

gulp.task('imagesmin', function() {
  return gulp.src('app/images/**/*')
    .pipe(gulp.dest(dist + '/images'))
    .pipe($.cache($.imagemin({
      optimizationLevel: 3,
      progressive: true,
      interlaced: true
    })));
});

gulp.task('html', ['bower-components', 'scripts', 'styles', 'templates', 'images'], function() {
  return gulp.src('app/views/*.html')
    .pipe(gulp.dest(dev));
});

gulp.task('htmlmin', ['html'], function() {
  apiHost = distApiHost;
  var assets = $.useref.assets({
    searchPath: dev
  });
  return gulp.src(dev + '/*.html')
    .pipe(assets) // Get assets from index.html
    .pipe($.replaceTask({
      patterns: [{
        match: devApiHost,
        replacement: distApiHost
      }],
      usePrefix: false
    }))
    .pipe($.rev()) // Apply version names to built files
    .pipe($.if('*.js', $.uglify({ // concat and uglify js
      mangle: false // Do not mangle variable names for Angular
    })))
    .pipe($.if('*.css', $.minifyCss())) // Minify css
    .pipe(assets.restore()) // Rewrite unversioned filerefs in index.html
    .pipe($.revReplace()) // Replace filerefs with version
    .pipe($.useref())
    .pipe(gulp.dest(dist)) // Write versioned files to dist
    .pipe($.size({
      showFiles: true
    }));
});

gulp.task('clean', function() {
  return gulp.src(['.tmp', 'dist'], {
    read: false
  }).pipe($.clean());
});

gulp.task('superstatic', function(next) {
  var superstatic = require('superstatic');
  var server = superstatic({
    logger: {
      info: function(msg) {
        console.log('Info:', msg);
      },
      error: function(msg) {
        console.error('Error:', msg);
      }
    },
    port: 9010,
    config: 'divshot-localdev.json'
  });
  server.listen(function() {
    console.log('Server running on port ' + server.port);
  });
  next();
});

gulp.task('wiredep', function() {
  var wiredep = require('wiredep').stream;

  gulp.src('app/styles/*.scss')
    .pipe(wiredep({
      directory: 'app/bower_components'
    }))
    .pipe(gulp.dest(dev + '/styles'));

  gulp.src('app/*.html')
    .pipe(wiredep({
      directory: 'app/bower_components',
      exclude: ['bootstrap-sass-official']
    }))
    .pipe(gulp.dest(dev));
});

gulp.task('serve', [
  'html',
  'images',
  'superstatic'
], function() {
  require('opn')('http://localhost:9010');
});

gulp.task('watch', ['serve'], function() {
  var server = $.livereload();
  gulp.watch(dev + '/**').on('change', function(file) {
    server.changed(file.path)
  });

  gulp.watch('app/styles/**/*.scss', ['styles']);
  gulp.watch('app/scripts/**/*.js', ['scripts']);
  gulp.watch('app/images/**/*', ['images']);
  gulp.watch('bower.json', ['wiredep']);
});

gulp.task('build', [
  'htmlmin',
  'imagesmin',
], function() {
  if (argvServe) {
    var spawn = require('child_process').spawn;
    var log = function(data) {
      console.log("[Divshot] " + data.toString().trim());
    };
    var divshotConfig = 'divshot.json';
    var server = spawn('divshot', ['server', '--port', '9010', '-c', divshotConfig]);
    server.on('error', function(error) {
      console.log(error.stack);
    });
    server.stdout.on('data', log);
    server.stderr.on('data', log);
  }
});

gulp.task('default', function() {
  gulp.start('watch');
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment