Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Created June 23, 2014 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bodokaiser/ecf3b7a72d6d1929d96f to your computer and use it in GitHub Desktop.
Save bodokaiser/ecf3b7a72d6d1929d96f to your computer and use it in GitHub Desktop.
component build script for component bundles
var fs = require('fs');
var path = require('path');
var util = require('util');
var jade = require('builder-jade');
var bundler = require('component-bundler');
var builder = require('component-builder');
var resolver = require('component-resolver');
var runtime = builder.scripts.require + jade.runtime;
module.exports = function(app) {
var regex = /build\/(.*)\.(css|js)$/;
app.use(function* build(next) {
var options = this.app.builder;
if (regex && regex.test(this.path)) {
var tree = yield* resolve(options.path);
for (var name in tree) {
var bundle = tree[name];
var style = yield buildStyles(bundle);
var script = yield buildScripts(bundle);
if (!Object.keys(tree).indexOf(name)) {
script = runtime + script;
}
yield writeFile(path.join(options.scripts, name + '.js'), script);
yield writeFile(path.join(options.styles, name + '.css'), style);
}
// WTF??? Not touching regex in my setup. WEIRD BUG :(
if (app.env === 'production') regex = null;
}
yield next;
});
};
function* resolve(path) {
var tree = yield resolver(path, {
install: true
});
var meta = {
locals: Object.keys(tree.locals)
};
return bundler.pages(meta)(tree);
}
function buildScripts(nodes) {
return new builder.scripts(nodes)
.use('scripts', builder.plugins.js())
.use('templates', jade({ runtime: true }))
.use('templates', builder.plugins.string())
.end();
}
function buildStyles(nodes) {
return new builder.styles(nodes)
.use('styles', builder.plugins.css())
.end();
}
function writeFile(path, chunk) {
return function(callback) {
fs.writeFile(path, chunk, callback);
}
}
@timaschew
Copy link

you can write
tree.hasOwnProperty(name)
instead of
!Object.keys(tree).indexOf(name)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment