Skip to content

Instantly share code, notes, and snippets.

@megamaddu
Last active September 15, 2015 13:52
Show Gist options
  • Save megamaddu/bbd581094f96b3db0861 to your computer and use it in GitHub Desktop.
Save megamaddu/bbd581094f96b3db0861 to your computer and use it in GitHub Desktop.
gulp + browserify + iojs + es6 + isomorphic
---
parser: babel-eslint
env:
node: true
browser: true
globals:
fetch: true
noop: true
React: true
rules:
strict: 0
quotes: single
no-multi-spaces: 0
key-spacing: 0
comma-spacing: 0
curly: 0
no-use-before-define: 0
no-cond-assignment: 0
no-debugger: 0
no-extra-boolean-cast: 0
no-unused-expressions: 0
no-shadow: 0
no-underscore-dangle: 0
no-unused-vars:
- 0
- vars: all
- args: after-used
'use strict';
require('babel/register')({
ignore: /node_modules/,
// blacklist features iojs fully supports: https://iojs.org/en/es6.html
// in the future this will probably be whitelist instead
blacklist: [
'es6.blockScoping',
'es6.constants',
'es6.forOf',
'es6.templateLiterals',
'es5.properties.mutators',
'es3.memberExpressionLiterals',
'es3.propertyLiterals',
'regenerator',
'runtime'
]
});
require('./app.server');
import './globals';
import koa from 'koa';
import body from 'koa-better-body';
import router from 'koa-router';
import staticCache from 'koa-static-cache';
import prettyHrtime from 'pretty-hrtime';
global.isServer = true;
const app = koa();
app.use(staticCache(`${__dirname}/../static`, {buffer: true, gzip: true}));
if (process.env.NODE_ENV !== 'production') {
app.use(function* responseTime(next){
const start = process.hrtime();
yield* next;
const reqTime = process.hrtime(start);
const prettyReqTime = prettyHrtime(reqTime);
console.log(prettyReqTime);
});
}
app.use(router(app));
// routes
const ips = '0.0.0.0';
const port = 4010;
app.listen(port, ips);
console.log(` -- koa listening at ${ips}:${port}`);
import gulp from 'gulp';
import del from 'del';
import transform from 'vinyl-transform';
import browserify from 'browserify';
import watchify from 'watchify';
import babelify from 'babelify';
import nodemon from 'gulp-nodemon';
import eslint from 'gulp-eslint';
import notify from 'gulp-notify';
import uglify from 'gulp-uglify';
import rename from 'gulp-rename';
import gutil from 'gulp-util';
import prettyHrtime from 'pretty-hrtime';
import fs from 'fs';
process.env.NODE_ENV = 'production';
let conf = {
isProduction: false,
isWatching: false,
src: './src/**/*.js',
browserify: {
entry: './src/app.js',
dist: './static'
},
server: {
entry: './src/app.server.bootstrap.js'
}
};
gulp.task('default', ['dev']);
gulp.task('build', ['lint'], _browserify);
gulp.task('dev', ['watch', 'nodemon']);
gulp.task('watch', ['setWatching'], _browserify);
gulp.task('clean', cb => del([conf.browserify.dist], cb));
gulp.task('reset', cb => del([conf.dist, 'node_modules'], cb));
gulp.task('setWatching', () => {
conf.isWatching = true;
process.env.NODE_ENV = 'development';
});
gulp.task('lint', () =>
gulp.src(conf.src)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failOnError()));
gulp.task('nodemon', () =>
nodemon({
script: conf.server.entry,
watch: 'src/*',
ext: 'js'
// nodeArgs: ['--debug']
}));
gulp.task('browserify', _browserify);
function _browserify() {
const src = conf.browserify.entry;
const logger = getLogger(src);
let bundler = browserify(src, {
debug: true,
cache: {}, packageCache: {}, fullPaths: true
}).transform(babelify.configure({
sourceMap: 'inline',
ignore: /node_modules/
}));
const bundle = () => {
logger.start();
var bundle = transform(filename =>
bundler.bundle().on('error', handleErrors));
return gulp.src(src)
.pipe(bundle)
.pipe(gulp.dest(conf.browserify.dist))
.pipe(uglify())
.pipe(rename({extname: '.min.js'}))
.pipe(gulp.dest(conf.browserify.dist))
.on('end', logger.end);
};
if (conf.isWatching) {
bundler = watchify(bundler).on('update', bundle);
}
return bundle();
}
var handleErrors = function () {
const args = Array.prototype.slice.call(arguments);
notify.onError({
title: 'Compile Error',
message: '<%= error.message %>'
}).apply(this, args);
return this.emit('end');
};
var getLogger = src => {
let startTime = null;
return {
start: () => {
startTime = process.hrtime();
return gutil.log('Running', gutil.colors.green("'bundle'"), 'for path', gutil.colors.yellow("'"+src+"'"), '...');
},
end: () => {
const taskTime = process.hrtime(startTime);
const prettyTime = prettyHrtime(taskTime);
return gutil.log('Finished', gutil.colors.green("'bundle'"), 'for path', gutil.colors.yellow("'"+src+"'"), 'in', gutil.colors.magenta(prettyTime));
}
};
};
'use strict';
require('babel/register');
require('./gulpfile.es6');
{
"engines": {
"node": ">=1.4.3"
},
"engineStrict": true,
"devDependencies": {
"babel-eslint": "^2.0.0",
"babel-runtime": "^4.7.8",
"babelify": "^5.0.4",
"browserify": "^9.0.3",
"del": "^1.1.1",
"eslint": "^0.16.2",
"gulp": "^3.8.11",
"gulp-compass": "^2.0.3",
"gulp-eslint": "^0.6.0",
"gulp-nodemon": "^1.0.5",
"gulp-notify": "^2.2.0",
"gulp-rename": "^1.2.0",
"gulp-uglify": "^1.1.0",
"gulp-util": "^3.0.3",
"vinyl-transform": "^1.0.0",
"watchify": "^2.4.0"
},
"dependencies": {
"babel": "^4.7.8",
"koa": "^0.18.1",
"koa-better-body": "^1.0.17",
"koa-router": "^4.2.0",
"koa-static-cache": "^3.0.1",
"pretty-hrtime": "^1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment