Skip to content

Instantly share code, notes, and snippets.

@danechitoaie
Forked from michalochman/gulpfile.js
Created May 19, 2017 09:52
Show Gist options
  • Save danechitoaie/cb20ea4bd92df207d3c53d51cd21ec8b to your computer and use it in GitHub Desktop.
Save danechitoaie/cb20ea4bd92df207d3c53d51cd21ec8b to your computer and use it in GitHub Desktop.
gulp.js: babelify + browserify + sourcemaps
/* jshint strict: false */
/* globals require, console */
var gulp = require('gulp');
var exit = require('gulp-exit');
var browserify = require('browserify');
var watchify = require('watchify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
function compile(watch) {
var bundler = watchify(browserify('./src/index.js', {debug: true}).transform(babelify, {
// Use all of the ES2015 spec
presets: ["es2015"],
sourceMaps: true
}));
function rebundle() {
return bundler
.bundle()
.on('error', function (err) {
console.error(err);
this.emit('end');
})
.pipe(source('build.js'))
.pipe(buffer())
.pipe(rename('index.min.js'))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./build'));
}
if (watch) {
bundler.on('update', function () {
console.log('-> bundling...');
rebundle();
});
rebundle();
} else {
rebundle().pipe(exit());
}
}
function watch() {
return compile(true);
}
gulp.task('build', function () {
return compile();
});
gulp.task('watch', function () {
return watch();
});
gulp.task('default', ['watch']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment