From 138afe39dcadff3a85f624262af75f518ab13a99 Mon Sep 17 00:00:00 2001
From: Kevin Jahns <kevin.jahns@rwth-aachen.de>
Date: Fri, 6 Nov 2015 16:16:38 +0100
Subject: [PATCH] improving.. breaking.. the gulpfile

---
 gulpfile.helper.js | 138 +++++++++++++++++++++++++++------------------
 gulpfile.js        |   7 +--
 package.json       |  10 +++-
 src/y.js           |   4 ++
 4 files changed, 98 insertions(+), 61 deletions(-)

diff --git a/gulpfile.helper.js b/gulpfile.helper.js
index 4dacee80..df6e2213 100644
--- a/gulpfile.helper.js
+++ b/gulpfile.helper.js
@@ -5,28 +5,47 @@ var minimist = require('minimist')
 module.exports = function (gulp, helperOptions) {
   var runSequence = require('run-sequence').use(gulp)
   var options = minimist(process.argv.slice(2), {
-    string: ['modulename', 'export', 'name', 'testport', 'testfiles', 'regenerator'],
+    string: ['modulename', 'export', 'name', 'testport', 'testfiles'],
     default: {
       modulename: helperOptions.moduleName,
       targetName: helperOptions.targetName,
       export: 'ignore',
       testport: '8888',
-      testfiles: 'src/**/*.js',
-      regenerator: process.version < 'v0.12'
+      testfiles: '**/*.spec.js',
+      browserify: helperOptions.browserify != null ? helperOptions.browserify : false,
+      regenerator: true,
+      debug: false
     }
   })
-
+  if (options.regenerator === 'false') {
+    options.regenerator = false
+    // TODO: include './node_modules/gulp-babel/node_modules/babel-core/node_modules/regenerator/runtime.js'
+  }
+  var concatOrder = [
+    'y.js',
+    'Connector.js',
+    'Database.js',
+    'Transaction.js',
+    'Struct.js',
+    'Utils.js',
+    'Databases/RedBlackTree.js',
+    'Databases/Memory.js',
+    'Databases/IndexedDB.js',
+    'Connectors/Test.js',
+    'Types/Array.js',
+    'Types/Map.js',
+    'Types/TextBind.js'
+  ]
+  var yjsfiles = concatOrder.map(function (f) {
+    return '../yjs/src/' + f
+  })
   var files = {
-    src: helperOptions.polyfills.concat(helperOptions.concatOrder.map(function (f) {
+    dist: helperOptions.polyfills.concat(helperOptions.files.map(function (f) {
       return 'src/' + f
     })),
-    test: ['build/Helper.spec.js'].concat(helperOptions.concatOrder.map(function (f) {
-      return 'build/' + f
-    }).concat(['build/**/*.spec.js']))
-  }
-
-  if (options.regenerator) {
-    files.test = helperOptions.polyfills.concat(files.test)
+    test: ['../yjs/src/Helper.spec.js'].concat(yjsfiles).concat(helperOptions.files.map(function (f) {
+      return 'src/' + f
+    }).concat(['src/' + options.testfiles]))
   }
 
   var babelOptions = {
@@ -34,54 +53,64 @@ module.exports = function (gulp, helperOptions) {
     modules: 'ignore',
     experimental: true
   }
-  if (!options.regenerator) {
+  if (options.regenerator) {
+    files.test = helperOptions.polyfills.concat(files.test)
+  } else {
     babelOptions.blacklist = 'regenerator'
   }
+  // babelOptions.blacklist = 'regenerator'
 
-  gulp.task('dist', function () {
-    return gulp.src(files.src)
-      .pipe($.sourcemaps.init())
-      .pipe($.concat(options.targetName))
-      .pipe($.babel({
-        loose: 'all',
-        modules: 'ignore',
-        experimental: true
-      }))
-      .pipe($.uglify())
-      .pipe($.sourcemaps.write('.'))
-      .pipe(gulp.dest('./dist/'))
+  gulp.task('dist', ['build:dist'], function () {
+    function createDist (pipe) {
+      return pipe
+        .pipe($.if(options.debug, $.sourcemaps.init({loadMaps: true})))
+        .pipe($.concat(options.targetName))
+        .pipe($.if(!options.debug && options.regenerator, $.uglify()))
+        .pipe($.if(options.debug, $.sourcemaps.write('.')))
+        .pipe(gulp.dest('./dist/'))
+    }
+    var pipe
+    if (options.browserify || true) {
+      var browserify = require('browserify')
+      var source = require('vinyl-source-stream')
+      var buffer = require('vinyl-buffer')
+
+      pipe = browserify({
+        entries: 'build/' + options.targetName,
+        debug: options.debug
+      }).bundle()
+        .pipe(source(options.targetName))
+        .pipe(buffer())
+    } else {
+      pipe = gulp.src('build/' + options.targetName)
+    }
+    return createDist(pipe)
   })
 
-  gulp.task('watch:dist', function () {
-    gulp.src(files.src)
-      .pipe($.watch(files.src))
-      .pipe($.sourcemaps.init())
+  gulp.task('dist', function () {
+    var browserify = require('browserify')
+    var source = require('vinyl-source-stream')
+    var buffer = require('vinyl-buffer')
+
+    return browserify({
+      entries: files.dist,
+      debug: options.debug
+    }).bundle()
+      .pipe(source(options.targetName))
+      .pipe(buffer())
+      .pipe($.if(options.debug, $.sourcemaps.init({loadMaps: true})))
       .pipe($.concat(options.targetName))
-      .pipe($.babel({
-        loose: 'all',
-        modules: 'ignore',
-        experimental: true
-      }))
-      // .pipe($.uglify())
-      .pipe($.sourcemaps.write('.'))
+      .pipe($.if(!options.debug && options.regenerator, $.uglify()))
+      .pipe($.if(options.debug, $.sourcemaps.write('.')))
       .pipe(gulp.dest('./dist/'))
   })
 
-  gulp.task('build', function () {
-    return gulp.src('src/**/*.js')
-      .pipe($.sourcemaps.init())
-      .pipe($.babel(babelOptions))
-      .pipe($.sourcemaps.write())
-      .pipe(gulp.dest('build'))
-  })
-
-  gulp.task('watch:build', function () {
-    gulp.src('src/**/*.js')
-      .pipe($.watch('src/**/*.js'))
-      .pipe($.sourcemaps.init())
-      .pipe($.babel(babelOptions))
-      .pipe($.sourcemaps.write())
-      .pipe(gulp.dest('build'))
+  gulp.task('watch:dist', function (cb) {
+    options.debug = true
+    runSequence('dist', function () {
+      gulp.watch(files.dist, ['dist'])
+      cb()
+    })
   })
 
   gulp.task('updateSubmodule', function () {
@@ -141,7 +170,7 @@ module.exports = function (gulp, helperOptions) {
   })
 
   gulp.task('dev:node', ['test'], function () {
-    gulp.watch('src/**/*.js', ['test'])
+    gulp.watch(files.dist, ['test'])
   })
 
   gulp.task('dev:browser', ['watch:build'], function () {
@@ -151,8 +180,9 @@ module.exports = function (gulp, helperOptions) {
       .pipe($.jasmineBrowser.server({port: options.testport}))
   })
 
-  gulp.task('test', ['build'], function () {
-    return gulp.src(files.test)
+  gulp.task('test', function () {
+    console.log(files.test)
+    return gulp.src('./dist/y.js')
       .pipe($.jasmine({
         verbose: true,
         includeStuckTrace: true
diff --git a/gulpfile.js b/gulpfile.js
index 68d93f31..5813a05c 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -48,8 +48,8 @@ var $ = require('gulp-load-plugins')()
 var runSequence = require('run-sequence').use(gulp)
 
 require('./gulpfile.helper.js')(gulp, {
-  polyfills: ['./node_modules/gulp-babel/node_modules/babel-core/node_modules/regenerator/runtime.js'],
-  concatOrder: [
+  polyfills: [],
+  files: [
     'y.js',
     'Connector.js',
     'Database.js',
@@ -60,7 +60,6 @@ require('./gulpfile.helper.js')(gulp, {
     'Databases/Memory.js',
     'Databases/IndexedDB.js',
     'Connectors/Test.js',
-    'Connectors/WebRTC.js',
     'Types/Array.js',
     'Types/Map.js',
     'Types/TextBind.js'
@@ -84,7 +83,7 @@ gulp.task('dev:examples', ['updateSubmodule', 'watch:dist'], function () {
   return $.serve('dist/Examples/')()
 })
 
-gulp.task('default', function (cb) {
+gulp.task('default', ['updateSubmodule'], function (cb) {
   gulp.src('package.json')
     .pipe($.prompt.prompt({
       type: 'checkbox',
diff --git a/package.json b/package.json
index 137134ad..6cc53b78 100644
--- a/package.json
+++ b/package.json
@@ -43,12 +43,14 @@
   "homepage": "http://y-js.org",
   "devDependencies": {
     "babel-eslint": "^4.1.2",
+    "browserify": "^12.0.1",
     "gulp": "^3.9.0",
     "gulp-babel": "^5.2.1",
     "gulp-bump": "^1.0.0",
     "gulp-concat": "^2.6.0",
     "gulp-filter": "^3.0.1",
     "gulp-git": "^1.6.0",
+    "gulp-if": "^2.0.0",
     "gulp-jasmine": "^2.0.1",
     "gulp-jasmine-browser": "^0.2.3",
     "gulp-load-plugins": "^1.0.0",
@@ -58,12 +60,14 @@
     "gulp-shell": "^0.5.1",
     "gulp-sourcemaps": "^1.5.2",
     "gulp-tag-version": "^1.3.0",
-    "gulp-uglify": "^1.4.1",
+    "gulp-uglify": "^1.4.2",
     "gulp-util": "^3.0.6",
     "gulp-watch": "^4.3.5",
     "minimist": "^1.2.0",
     "pre-commit": "^1.1.1",
-    "promise-polyfill": "^2.1.0",
-    "standard": "^5.2.2"
+    "run-sequence": "^1.1.4",
+    "standard": "^5.2.2",
+    "vinyl-buffer": "^1.0.0",
+    "vinyl-source-stream": "^1.1.0"
   }
 }
diff --git a/src/y.js b/src/y.js
index 5f5266d1..a599db18 100644
--- a/src/y.js
+++ b/src/y.js
@@ -46,6 +46,10 @@ class YConfig {
   }
 }
 
+if (typeof window !== 'undefined') {
+  window.Y = Y
+}
+
 if (typeof YConcurrency_TestingMode !== 'undefined') {
   g.Y = Y //eslint-disable-line
   // debugger //eslint-disable-line
-- 
GitLab