Permalink
Browse files

Merge pull request #269 from nickjackson/build-requires

component-build --use flag to specify plugins
  • Loading branch information...
2 parents cf91e19 + d65b060 commit 751525d0f024e987cbf6bee2300dbd148ef0f161 @tj tj committed Feb 22, 2013
Showing with 47 additions and 0 deletions.
  1. +18 −0 bin/component-build
  2. +19 −0 test/build.js
  3. +10 −0 test/fixtures/plugin.js
View
@@ -11,6 +11,7 @@ var program = require('commander')
, log = component.utils.log
, path = require('path')
, fs = require('fs')
+ , exists = fs.existsSync || path.existsSync
, mkdir = require('mkdirp');
// options
@@ -23,6 +24,7 @@ program
.option('-v, --verbose', 'output verbose build information')
.option('-p, --prefix <str>', 'prefix css asset urls with <str>')
.option('-c, --copy', 'copy files instead of linking')
+ .option('-u, --use <name>', 'use the given script(s) or module(s) as builder middleware')
// examples
@@ -38,6 +40,10 @@ program.on('--help', function(){
console.log(' # build standalone as window.$');
console.log(' $ component build --standalone $');
console.log();
+ console.log(' # build with middleware by requiring a module');
+ console.log(' # separate with commas if necessary');
+ console.log(' $ component build -r jade-builder');
+ console.log();
});
// parse argv
@@ -73,6 +79,18 @@ if (program.prefix) builder.prefixUrls(program.prefix);
if (conf.paths) builder.addLookup(conf.paths);
+// require middleware plugins
+if (program.use) {
+ var modules = program.use.split(',');
+ modules.forEach(function(mod){
+ var resolved = path.resolve(mod)
+ , real = exists(resolved) || exists(resolved + '.js')
+ , module = path.resolve('node_modules', mod);
+
+ builder.use(real ? require(resolved) : require(module));
+ });
+}
+
var start = new Date;
// --dev
View
@@ -28,4 +28,23 @@ describe('component build', function(){
done();
})
})
+
+ it('should require middleware with relative path', function(done){
+ exec('cd test/fixtures/path && ' + bin + '-build -v -u ../plugin', function(err, stdout){
+ if (err) return done(err);
+ stdout.should.include('middleware fired!');
+ stdout.should.include('builder config fired!');
+ done();
+ })
+ })
+
+ it('should require middleware with absolute path', function(done){
+ var plugin = path.join(__dirname, 'fixtures', 'plugin');
+ exec('cd test/fixtures/path && ' + bin + '-build -v -u ' + plugin, function(err, stdout){
+ if (err) return done(err);
+ stdout.should.include('middleware fired!');
+ stdout.should.include('builder config fired!');
+ done();
+ })
+ })
})
View
@@ -0,0 +1,10 @@
+/*
+ example middleware plugin for builder
+*/
+
+module.exports = function(builder) {
+ console.log('middleware fired!');
+ builder.on('config', function() {
+ console.log('builder config fired!')
+ })
+}

0 comments on commit 751525d

Please sign in to comment.