Skip to content

Instantly share code, notes, and snippets.

@jorinvo
Created October 31, 2014 14:59
Show Gist options
  • Save jorinvo/3fdfccdb297d87be91f5 to your computer and use it in GitHub Desktop.
Save jorinvo/3fdfccdb297d87be91f5 to your computer and use it in GitHub Desktop.
use es6 transpiler and modules in gulp. also integrates fb-flo and caches builds
var fs = require('fs');
var path = require('path');
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var esnext = require('gulp-esnext');
var es6Modules = require('gulp-es6-module-transpiler');
var concat = require('gulp-concat');
var insert = require('gulp-insert');
var uglify = require('gulp-uglify');
var cache = require('gulp-cached');
var remember = require('gulp-remember');
var jshint = require('gulp-jshint');
var flo = require('fb-flo');
const scripts = 'src/**/*.js';
const amdLoader = fs.readFileSync('lib/amdLoader.js');
gulp.task('default', function() {
gulp.watch('src/', ['build']);
});
gulp.task('build', ['lint'], function() {
return gulp.src(scripts)
.pipe(es6Modules({
type: 'amd'
}))
.pipe(esnext())
.pipe(concat('createYourOwn.min.js'))
.pipe(insert.prepend(amdLoader))
.pipe(uglify())
.pipe(gulp.dest('.'));
});
gulp.task('watch', ['flo'], function() {
var watcher = gulp.watch(scripts, ['dev']);
watcher.on('change', function(event) {
if (event.type === 'deleted') { // if a file is deleted, forget about it
delete cache.caches.scripts[event.path];
remember.forget('scripts', event.path);
}
});
});
gulp.task('dev', function() {
return gulp.src(scripts)
.pipe(sourcemaps.init())
.pipe(cache('es6'))
.pipe(es6Modules({
type: 'amd'
}))
.pipe(esnext())
.pipe(remember('es6'))
.pipe(concat('createYourOwn.min.js'))
.pipe(insert.wrap(amdLoader, ';require("createYourOwn",function(){});'))
.pipe(sourcemaps.write())
.pipe(gulp.dest('.'));
});
gulp.task('lint', function() {
return gulp.src(scripts)
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'));
});
gulp.task('flo', function(done) {
var server = flo('src', {}, function resolver(filepath, callback) {
var file = 'src/'+filepath;
callback({
resourceURL: 'http://localhost/kumpelding/wp-content/themes/kumpelding/js/createYourOwn/createYourOwn.min.js',
contents: fs.readFileSync(file, 'utf8'),
update: function(_window, _resourceURL) {
console.log(_window.reload)
// _window.reload();
// get's executed on the client
// freeze state of app and re-initialize
}
});
});
server.once('ready', done);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment