Skip to content

Instantly share code, notes, and snippets.

@ramasilveyra
Created December 16, 2015 00:20
Show Gist options
  • Save ramasilveyra/b4309edf809e55121385 to your computer and use it in GitHub Desktop.
Save ramasilveyra/b4309edf809e55121385 to your computer and use it in GitHub Desktop.
Multiple bundles with browserify and watchify on gulp. Plus ES2015 support with babelify and sourcemaps, linter with eslint
{
"extends": "airbnb",
"rules": {
"comma-dangle": 0
}
}
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import browserify from 'browserify';
import watchify from 'watchify';
import runSequence from 'run-sequence';
import babelify from 'babelify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import {assign} from 'lodash';
let isWatchify = false;
const $ = gulpLoadPlugins();
const bundles = [
{
entries: ['./src/app/components/menu.jsx'],
output: 'menu.js',
extensions: ['.jsx'],
destination: './dist/app/components/'
}, {
entries: ['./src/assets/scripts/main.js'],
output: 'main.min.js',
extensions: ['.js', '.json'],
destination: './src/assets/scripts'
}
];
/**
* Tasks for JS
*/
// browserify with babelify the JS code, and watchify
const createBundle = options => {
const opts = assign({}, watchify.args, {
entries: options.entries,
extensions: options.extensions,
debug: true
});
let b = browserify(opts);
b.transform(babelify.configure({
compact: false
}));
const rebundle = () =>
b.bundle()
// log errors if they happen
.on('error', $.util.log.bind($.util, 'Browserify Error'))
.pipe(source(options.output))
.pipe(buffer())
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.uglify())
.pipe($.sourcemaps.write('../maps'))
.pipe(gulp.dest(options.destination));
if (isWatchify) {
b = watchify(b);
b.on('update', rebundle);
b.on('log', $.util.log);
}
return rebundle();
};
gulp.task('scripts', ['scripts:lint'], () =>
bundles.forEach( bundle =>
createBundle({
entries: bundle.entries,
output: bundle.output,
extensions: bundle.extensions,
destination: bundle.destination
})
)
);
// Lint JavaScript
gulp.task('scripts:lint', () =>
gulp.src(['./src/assets/scripts/**/*.+(jsx|js)'])
.pipe($.eslint())
.pipe($.eslint.format())
);
/**
* Watch files for changes with watchify
*/
gulp.task('watch', () => {
isWatchify = true;
runSequence(['scripts']);
});
{
"name": "gulp-recipe-javascript-browserify",
"version": "1.0.0",
"author": {
"name": "Ramiro Silveyra d'Avila",
"url": "https://ramasilveyra.com.ar/"
},
"license": "MIT",
"private": true,
"devDependencies": {
"babel-core": "^6.1.4",
"babel-preset-es2015": "^6.0.11",
"babelify": "^7.0.2",
"browserify": "^12.0.1",
"eslint": "^1.7.3",
"eslint-config-airbnb": "^2.0.0",
"eslint-plugin-react": "^3.6.3",
"gulp": "^3.9.0",
"gulp-eslint": "^1.0.0",
"gulp-load-plugins": "^1.0.0",
"gulp-sourcemaps": "^1.6.0",
"gulp-uglify": "^1.4.2",
"gulp-util": "^3.0.7",
"lodash": "^3.10.1",
"run-sequence": "^1.1.4",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.5.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment