'use strict';

const browserify = require('browserify');
const gulp = require('gulp');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const uglify = require('gulp-uglify-es').default;
const sourcemaps = require('gulp-sourcemaps');
const gutil = require('gulp-util');
const rename = require('gulp-rename');
const stripDebug = require('gulp-strip-debug');
var bump = require('gulp-bump');

gulp.task('bundleBackbone', function () {
  // set up the browserify instance on a task basis
  const b = browserify({
    'debug': true,
    'entries': './src/v1/js/app.js'
  });

  return b.bundle()
    .pipe(source('app.js'))
    .pipe(buffer())
    // .pipe(stripDebug())
    .pipe(rename('bundle.js'))

    .pipe(sourcemaps.init({ 'loadMaps': true }))
  // Add transformation tasks to the pipeline here.
    // .pipe(uglify())
    .on('error', gutil.log)
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('./live/js'));
});

gulp.task('bump', function() {
  gulp.src('src/service-worker.js')
    .pipe(bump({ 'key': 'version' }))
    .pipe(gulp.dest('src'));
});

gulp.task('buildBackbone', ['bump', 'bundleBackbone'], function() {
  gulp.watch('src/v1/js/**/*.js', ['bump', 'bundleBackbone']);
});

