Skip to content

Instantly share code, notes, and snippets.

@rusco
Created February 24, 2016 08:44
Show Gist options
  • Save rusco/bb0fe6358ce2edcf7f99 to your computer and use it in GitHub Desktop.
Save rusco/bb0fe6358ce2edcf7f99 to your computer and use it in GitHub Desktop.
babel starter
//gulpfile template:
'use strict';
const gulp = require('gulp'),
babelify = require('babelify'),
browserify = require('browserify'),
buffer = require('vinyl-buffer'),
connect = require('gulp-connect'),
del = require('del'),
gulpif = require('gulp-if'),
jshint = require('gulp-jshint'),
source = require('vinyl-source-stream'),
uglify = require('gulp-uglify'),
watchify = require('watchify');
const browserifyConfig = {
debug: true,
entries: './src/main.js',
packageCache: {},
};
const browserifyLibConfig = {
debug: false,
packageCache: {},
};
const libs = [
'lodash',
'randomcolor'
];
let watch, production;
gulp.task('build', ['clean'], () => {
production = true;
gulp.start('start');
});
gulp.task('buildDev', ['clean'], () => {
gulp.start('start');
});
gulp.task('watch', ['clean'], () => {
watch = true;
gulp.start('start');
gulp.start('connect');
gulp.watch('src/**/*', ['reload']);
});
gulp.task('start', ['js', 'vendor', 'css', 'index']);
gulp.task('clean', () => {
return del([
'build/**/*',
]);
});
gulp.task('connect', () => {
connect.server({
root: 'build/',
livereload: true
});
});
gulp.task('css', () => gulp.src('./src/style.css').pipe(gulp.dest('build/')));
gulp.task('index', () => gulp.src('./src/index.html').pipe(gulp.dest('build/')));
gulp.task('js', () => {
let b = browserify(browserifyConfig)
.transform(babelify, {
presets: ['es2015']
})
.external(libs);
if (watch) b.plugin(watchify);
bundle(b, 'bundle.js');
if (watch) b.on('update', bundle.bind(null, b, 'bundle.js'));
});
gulp.task('lint', () => gulp.src(['src/**/*.js'])
.pipe(jshint({
elision: true,
esnext: true
}))
.pipe(jshint.reporter('default')));
gulp.task('reload', ['index', 'CSS'], () => gulp.src('src/**/*')
.pipe(connect.reload()));
gulp.task('vendor', () => {
let b = browserify(browserifyLibConfig)
.transform(babelify, {
presets: ['es2015']
})
.require(libs);
bundle(b, 'vendor.js');
});
function bundle(b, name) {
gulp.start('lint');
b.bundle().on('error', handleError)
.pipe(source(name))
.pipe(buffer())
.pipe(gulpif(production, uglify()))
.pipe(gulp.dest('build'));
console.log(`Bundling of ${name} finished!`);
return b;
}
function handleError(err) {
console.log(err.toString());
this.emit('end');
}
{
"name": "planets",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/rusco/project.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/rusco/project/issues"
},
"homepage": "https://github.com/rusco/project#readme",
"devDependencies": {
"babel-preset-es2015": "^6.3.13",
"babelify": "^7.2.0",
"browserify": "^13.0.0",
"del": "^2.2.0",
"gulp": "^3.9.0",
"gulp-connect": "^2.2.0",
"gulp-if": "^2.0.0",
"gulp-jshint": "^1.11.2",
"gulp-uglify": "^1.5.1",
"lodash": "^4.1.0",
"randomcolor": "^0.4.3",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.7.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment