-
-
Save danechitoaie/cb20ea4bd92df207d3c53d51cd21ec8b to your computer and use it in GitHub Desktop.
gulp.js: babelify + browserify + sourcemaps
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
/* 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