Last active
December 27, 2015 13:29
-
-
Save sorl/7333617 to your computer and use it in GitHub Desktop.
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
var assets = require('./assets.json') | |
, path = require('path') | |
var baseDir = __dirname | |
var Component = function(opts) { | |
var self = this | |
self.name = opts.name | |
self.src = path.join(baseDir, opts.src) | |
self.dest = path.join(baseDir, opts.dest) | |
self.url = opts.url || '/' + opts.dest | |
} | |
var Lib = function(opts) { | |
var self = this | |
self.name = opts.name | |
self.src = path.join(baseDir, opts.src) | |
self.noParse = opts.noParse | |
self.opts = opts.opts | |
} | |
var components = [] | |
assets.components.forEach(function(c) { | |
components.push(new Component(c)) | |
}) | |
var libs = [] | |
assets.libs.forEach(function(l) { | |
libs.push(new Lib(l)) | |
}) | |
var libsMeta = { | |
name: assets.libsMeta.name | |
, dest: path.join(baseDir, assets.libsMeta.dest) | |
, url: assets.libs.url || '/' + assets.libsMeta.dest | |
} | |
exports.components = components | |
exports.libs = libs | |
exports.libsMeta = libsMeta |
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
{ | |
"components": [ | |
{"name": "admin", "src": "components/admin.js", "dest": "static/build/admin.bundle.js"} | |
], | |
"libs": [ | |
{"name": "jquery", "src": "bower_components/jquery/jquery.js", "noParse": true, "opts": {"expose": "jquery"}} | |
, {"name": "showdown", "src": "bower_components/showdown/src/showdown.js", "opts": {"expose": "showdown"}} | |
, {"name": "backbone", "src": "bower_components/backbone/backbone.js", "opts": {"expose": "backbone"}} | |
, {"name": "react", "src": "bower_components/react/react.js", "noParse": true, "opts": {"expose": "react"}} | |
], | |
"libsMeta": { | |
"name": "libs" | |
, "dest": "static/build/libs.bundle.js" | |
} | |
} |
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
#!/usr/bin/env node | |
var fs = require('fs') | |
, browserify = require('browserify') | |
, watchify = require('watchify') | |
, reactify = require('reactify').process | |
, UglifyJS = require('uglify-js') | |
, db = require('../db') | |
, crypto = require('crypto') | |
, async = require('async') | |
, components = require('../assets').components | |
, libs = require('../assets').libs | |
, libsMeta = require('../assets').libsMeta | |
var bundleQue = async.queue(function(opts, callback) { | |
opts.br.bundle({debug: opts.debug}, function(err, code) { | |
if (err) { | |
if (opts.debug) { | |
console.error(String(err)) | |
return | |
} else { | |
throw err | |
} | |
} | |
var sum = crypto.createHash('md5') | |
, msg = ' with source maps.' | |
if (!opts.debug) { | |
code = UglifyJS.minify(code, {fromString: true}).code | |
var msg = '' | |
} | |
fs.writeFile(opts.dest, code, function(err) { | |
if (err) throw err | |
console.log('Wrote ' + opts.dest + msg) | |
sum.update(code) | |
db.rediscli.hset('bundles', opts.name, sum.digest('hex'), function(err) { | |
if (err) throw err | |
callback() | |
}) | |
}) | |
}) | |
}, 4) | |
var bundleLibs = function(debug) { | |
var noParse = [] | |
libs.forEach(function(lib) { | |
if (lib.noParse) { | |
noParse.push(lib.src) | |
} | |
}) | |
var br = browserify({noParse: noParse}) | |
libs.forEach(function(lib) { | |
br.require(lib.src, lib.opts) | |
}) | |
bundleQue.push({br: br, name: libsMeta.name, dest: libsMeta.dest, debug: debug}) | |
} | |
var bundleComponent = function(c, debug) { | |
var br = browserify(c.src) | |
libs.forEach(function(lib) { | |
br.external(lib.name) | |
}) | |
br.transform(reactify) | |
bundleQue.push({br: br, name: c.name, dest: c.dest, debug: debug}) | |
} | |
var listenComponent = function(c, debug) { | |
var w = watchify(c.src) | |
libs.forEach(function(lib) { | |
w.external(lib.name) | |
}) | |
w.transform(reactify) | |
w.on('update', function() { | |
bundleQue.push({br: w, name: c.name, dest: c.dest, debug: debug}) | |
}) | |
bundleQue.push({br: w, name: c.name, dest: c.dest, debug: debug}) | |
} | |
var bundle = function(all, debug) { | |
if (all) { | |
bundleLibs(debug) | |
} | |
components.forEach(function(c) { | |
bundleComponent(c, debug) | |
}) | |
bundleQue.drain = function() { db.close() } | |
} | |
var listen = function(debug) { | |
components.forEach(function(c) { | |
listenComponent(c, debug) | |
}) | |
} | |
if (require.main === module) { | |
var argv = require('optimist').argv | |
if (argv.listen) { | |
listen(true) | |
} else { | |
bundle(argv.all, !!argv.debug) | |
} | |
} | |
exports.bundle = bundle | |
exports.listen = listen |
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
... | |
exports.bundles = function(req, res, next) { | |
db.rediscli.hgetall('bundles', function(err, hash) { | |
res.locals.bundles = res.locals.bundles || {} | |
var updateLocals = function(name, url) { | |
var v = hash[name] ? '?' + hash[name] : '' | |
res.locals.bundles[name] = '<script src="' + url + v + '"></script>' | |
} | |
updateLocals(assets.libsMeta.name, assets.libsMeta.url) | |
assets.components.forEach(function(c){ | |
updateLocals(c.name, c.url) | |
}) | |
next() | |
}) | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment