var gulp         = require('gulp'),
    sass         = require('gulp-sass'),
    sourcemaps   = require('gulp-sourcemaps'),
    autoprefixer = require('gulp-autoprefixer'),
    changed      = require('gulp-changed'),
    imagemin     = require('gulp-imagemin'),
    minifyCSS    = require('gulp-minify-css'),
    size         = require('gulp-filesize'),
    uglify       = require('gulp-uglify'),
    browserify   = require('browserify'),
    transform    = require('vinyl-transform'),
    rename       = require('gulp-rename'),
    del          = require('del'),
    vinylPaths   = require('vinyl-paths'),
    iconfont     = require('gulp-iconfont'),
    swig         = require('gulp-swig');

var dest = "./web/static",
    config = {
      sass: {
        src: "./web/css/**/*.{sass,scss}",
        dest: dest,
        settings: {
          indentedSyntax: true, // Enable .sass syntax!
          imagePath: 'images' // Used by the image-url helper
        }
      },
      images: {
        src: "./web/images/**",
        dest: dest + "/images"
      },
      javascript: {
        src: "./web/javascript",
        dest: dest
      },
      production: {
        cssSrc: dest + '/*.css',
        jsSrc: dest + '/*.js',
        dest: dest
      },
      iconfont: {
        src: './web/icons/*.svg',
        dest: dest + '/fonts',
        sassDest: './web/css',
        template: './web/css/template/template.sass.swig',
        sassOutputName: '_icons.sass',
        fontPath: '../static/fonts',
        className: 'icon',
        options: {
          fontName: 'Boilerplate',
          appendCodepoints: true,
          normalize: false
        }
      }
    };

function generateIconSass(codepoints, options) {
  gulp.src(config.iconfont.template)
    .pipe(swig({
      data: {
        icons: codepoints.map(function(icon) {
          return {
            name: icon.name,
            code: icon.codepoint.toString(16)
          }
        }),
        fontName: config.iconfont.options.fontName,
        fontPath: config.iconfont.fontPath,
        className: config.iconfont.className,
      }
    }))
    .pipe(rename(config.iconfont.sassOutputName))
    .pipe(gulp.dest(config.iconfont.sassDest));
}

gulp.task('iconfont', function() {
  return gulp.src(config.iconfont.src)
    .pipe(iconfont(config.iconfont.options))
    .on('codepoints', generateIconSass)
    .pipe(gulp.dest(config.iconfont.dest));
});

gulp.task('sass', ['iconfont'], function () {
  return gulp.src(config.sass.src)
    // .pipe(sourcemaps.init())
    .pipe(sass(config.sass.settings))
    //.pipe(sourcemaps.write())
    .pipe(autoprefixer({ browsers: ['last 2 version'] }))
    .pipe(gulp.dest(config.sass.dest));
});

gulp.task('clean:css', function () {
  return gulp.src(dest + '/index.min.css')
    .pipe(vinylPaths(del));
});

gulp.task('minify', ['sass', 'clean:css'], function() {
  return gulp.src(config.production.cssSrc)
    .pipe(rename('index.min.css'))
    .pipe(minifyCSS({keepBreaks:true}))
    .pipe(gulp.dest(config.production.dest))
    .pipe(size());
});

gulp.task('images', function() {
  return gulp.src(config.images.src)
    .pipe(changed(config.images.dest)) // Ignore unchanged files
    .pipe(imagemin()) // Optimize
    .pipe(gulp.dest(config.images.dest));
});

gulp.task('browserify', function () {
  // transform regular node stream to gulp (buffered vinyl) stream
  var browserified = transform(function(filename) {
    var b = browserify(filename);
    return b.bundle();
  });
  gulp.src(config.javascript.src + "/index.js")
    .pipe(browserified)
    .pipe(sourcemaps.init({loadMaps: true})) // loads map from browserify file
    .pipe(sourcemaps.write()) // writes .map file
    .pipe(gulp.dest(config.javascript.dest))
});

gulp.task('clean:js', function () {
  return gulp.src(dest + '/index.min.js')
    .pipe(vinylPaths(del));
});

gulp.task('uglify', ['browserify', 'clean:js'], function() {
  return gulp.src(config.production.jsSrc)
    .pipe(rename('index.min.js'))
    .pipe(uglify())
    .pipe(gulp.dest(config.production.dest))
    .pipe(size());
});

gulp.task('watch', function(callback) {
  gulp.watch(config.sass.src, ['sass']);
  gulp.watch(config.images.src, ['images']);
  gulp.watch(config.javascript.src + '/*.js', ['browserify']);
});

gulp.task('default', ['sass', 'browserify', 'images']);
gulp.task('prod', ['minify', 'images', 'uglify']);

@@ -436,25 +436,25 @@
 '),%0A    
-transform
+source   
     = re
@@ -466,25 +466,73 @@
 ('vinyl-
-transform
+source-stream'),%0A    buffer       = require('vinyl-buffer
 '),%0A    
@@ -3361,199 +3361,27 @@
 %7B%0A  
-// transform regular node stream to gulp (buffered vinyl) stream%0A  var browserified = transform(function(filename) %7B%0A    var b = browserify(filename);%0A    return b.bundle();%0A  %7D);%0A  gulp.src(
+return browserify(%5B
 conf
@@ -3400,17 +3400,17 @@
 t.src + 
-%22
+'
 /index.j
@@ -3414,163 +3414,93 @@
 x.js
-%22)%0A    .pipe(browserified)%0A    .pipe(sourcemaps.init(%7BloadMaps: true%7D)) // loads map from browserify file%0A    .pipe(sourcemaps.write()) // writes .map file
+'%5D).bundle()%0A    .pipe(source('index.js'))%0A    .pipe(buffer())%0A    // .pipe(uglify())
 %0A   
@@ -3540,16 +3540,17 @@
 t.dest))
+;
 %0A%7D);%0A%0Agu

update gulp use vinyl-source-stream