Last active
March 22, 2016 07:12
-
-
Save bicubic/3ef4f7cfecc8ba29f6f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var gulp = require('gulp'); | |
var browserify = require('browserify'); | |
var watchify = require('watchify'); | |
var path = require('path'); | |
var source = require('vinyl-source-stream'); | |
var buffer = require('vinyl-buffer'); | |
var merge = require('merge-stream'); | |
var gutil = require('gulp-util'); | |
var uglify = require('gulp-uglify'); | |
var sourcemaps = require('gulp-sourcemaps'); | |
var derequire = require('gulp-derequire'); | |
var changed = require('gulp-changed'); | |
var glob = require('glob'); | |
var memoize = require('lodash.memoize'); | |
var assign = require('lodash.assign'); | |
var browserSync = require('browser-sync').create(); | |
var gulpWatch = require('gulp-watch'); | |
var clean = require('gulp-clean'); | |
var merge = require('merge-stream'); | |
// Contains library files, meant only for consuming. | |
var SRC = "src/"; | |
// Used for building the demo, otherwise not released to anyone. | |
var DIST = "dist/"; | |
// Build directory containing development and production versions | |
// of the app | |
var BUILD = "build/"; | |
// Contains demo files only | |
var DEMO = "demo/"; | |
var pjson = require('./package.json'); | |
var build; | |
var watch = false; | |
// Builds development and production versions. This is called | |
// either manually (`gulp js`) or on change of one of the source | |
// library files | |
gulp.task('js', function() { | |
if (!build) { | |
build = browserify(assign({}, watchify.args, { | |
entries: SRC + pjson.name + '.js', | |
standalone: 'Three2D', | |
debug: true | |
})); | |
build.on('log', gutil.log); | |
if (watch) { | |
build = watchify(build); | |
} | |
} | |
function bundle(name, errs) { | |
return build.bundle() | |
.on('error', function(err){ | |
if (errs) { | |
gutil.log( | |
gutil.colors.red("Browserify compile error:"), | |
err.message, | |
err.stack, | |
"\n\t" | |
); | |
} | |
this.emit('end'); | |
}) | |
.pipe(source(name)) | |
.pipe(buffer()) | |
.pipe(sourcemaps.init({ loadMaps: true })) | |
.pipe(derequire()); | |
} | |
var development = bundle(pjson.name + '.js', true) | |
.pipe(sourcemaps.write('./')) | |
.pipe(gulp.dest(BUILD)); | |
if (watch) { | |
return development; | |
} | |
var production = bundle(pjson.name + '.min.js', false) | |
.pipe(uglify()) | |
.pipe(sourcemaps.write('./')) | |
.pipe(gulp.dest(BUILD)); | |
return merge(development, production); | |
}); | |
gulp.task('html', function() { | |
return gulp.src(DEMO + '*.html') | |
.pipe(changed(DIST)) | |
.pipe(gulp.dest(DIST)); | |
}); | |
gulp.task('html-watch', ['html'], function() { | |
return browserSync.reload(); | |
}); | |
gulp.task('copy-assets', function() { | |
gulp.src('./assets/**/*') | |
.pipe(gulp.dest('./dist/assets/')); | |
}); | |
var asset_source = './assets'; | |
var asset_dest = './dist/assets/'; | |
gulp.task('watch-assets', ['clean'], function() { | |
var watch = gulpWatch(asset_source, {base: asset_source}); | |
watch.on("add", ()=> gutil.log(gutil.colors.yellow('updating assets'))); | |
gulp.src(asset_source + '/*/', {base: asset_source}) | |
.pipe(watch) | |
.pipe(gulp.dest(asset_dest)); | |
}); | |
gulp.task('start-watch', function() { | |
watch = true; | |
}); | |
gulp.task('clean', function () { | |
return merge(gulp.src('./dist/*', {read: false}).pipe(clean()), | |
gulp.src('./build/*', {read: false}).pipe(clean())); | |
}); | |
gulp.task('watch', ['clean'], function(){ | |
gulp.start('watch-core'); | |
}); | |
gulp.task('watch-core', ['clean', 'start-watch', 'html', 'js', 'copy-assets', 'watch-assets'], function () { | |
browserSync.init({ | |
server: "./" + DIST, | |
open: false | |
}); | |
var demo = memoize(function(entry) { | |
return watchify(browserify(assign({}, watchify.args, { | |
entries: entry, | |
debug: true, | |
cache: {}, | |
packageCache: {} | |
}))); | |
}); | |
function bundle(entry) { | |
return demo(entry).bundle() | |
.on('error', function(err){ | |
if (err) { | |
gutil.log( | |
gutil.colors.red("Browserify compile error:"), | |
err.message, | |
err.stack, | |
"\n\t" | |
); | |
} | |
}) | |
.on('log', gutil.log) | |
.pipe(source(path.basename(entry))) | |
.pipe(buffer()) | |
.pipe(sourcemaps.init({ loadMaps: true })) | |
.pipe(derequire()) | |
.on('error', gutil.log) | |
.pipe(sourcemaps.write('./')) | |
.pipe(gulp.dest(DIST)); | |
} | |
// demo.on('update', function() { | |
// bundle().on('end', browserSync.reload); | |
// }); | |
var globs = glob.sync(DEMO + '*.js'); | |
globs.forEach(function(g) { | |
var entryBundle = bundle(g); | |
entryBundle.on('update', function() { | |
bundle(g).on('end', browserSync.reload); | |
}); | |
}); | |
gulp.watch(DEMO + '*.html', ['html-watch']); | |
gulp.watch(SRC + '**/*.js', ['js']); | |
return bundle() | |
.pipe(browserSync.stream({ once: true })); | |
}); | |
gulp.task('default', ['watch']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment