Skip to content

Instantly share code, notes, and snippets.

@charlieroberts
Created April 24, 2018 14:11
Show Gist options
  • Select an option

  • Save charlieroberts/aa9049c0c38a968d3af08909e0aec562 to your computer and use it in GitHub Desktop.

Select an option

Save charlieroberts/aa9049c0c38a968d3af08909e0aec562 to your computer and use it in GitHub Desktop.
gulpfile running browserify, babel, and uglify
// import libraries installed via NPM
var gulp = require('gulp'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
notify = require('gulp-notify'),
browserify = require('browserify'),
transform = require('vinyl-transform'),
source = require('vinyl-source-stream'),
gutil = require( 'gulp-util' ),
babelify = require( 'babelify' ),
buffer = require( 'vinyl-buffer' )
// define a task named build that we can run from the command line
// or using gulp
gulp.task('build', function () {
// browserify our data
browserify()
// what file is our browserify entry point
.require( './js/index.js', { entry: true } )
// convert ES2015 to ES5 via babel
.transform( babelify, { presets:['es2015'] })
// create Node.js stream using browserify output
.bundle()
// add Vinyl meta-data to Node.js stream, in this case
// the name of the file we're streaming
.pipe( source('index.js') )
// rename stream to bundle.js for output
.pipe( rename ('bundle.js') )
// output file to dist folder
.pipe( gulp.dest('./dist') )
// collect chunks into a single buffer for
// buffer-based processing that operates on entire files
// instead of chunks
.pipe( buffer() )
// minimize our bufferr
.pipe( uglify() )
// rename our file for output
.pipe( rename ('bundle.min.js') )
// send minimized output to dist folder
.pipe( gulp.dest('./dist') )
// create notification that build is complete
.pipe( notify({
message: 'build complete.',
onLast:true
}) )
})
// watch our js folder and trigger build task
// whenever a .js file is changed
gulp.task( 'watch', function() {
gulp.watch( './js/**.js', function() {
gulp.run('build')
})
})
// define default task whenever 'gulp' is run
// in the terminal without explicitly naming another task
gulp.task( 'default', ['build'] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment