Skip to content
This repository

component-build --use flag to specify plugins #269

Merged
merged 4 commits into from about 1 year ago

3 participants

Nick Jackson TJ Holowaychuk Nami-Doc
Nick Jackson

This allows builder.js plugins/middleware to be specified as part of the component-build cli. For example:

$ component build --use jade-builder

The example jade-builder is a module I just put together from an example in the builder repo.

A relative or absolute path can be specified, or you can specify a module name that would need to be pre-installed in the users project ./node_modules directory. Multiple plugins can be specified by separating the files with a comma.

#268

TJ Holowaychuk
Owner

im definitely not against it, this sort of thing falls apart as soon as options are involved though which does get tricky. At that point it's best to just use the js API, plus if you're building a reasonable size app it's much faster to use middleware and the builder directly than component-build

TJ Holowaychuk visionmedia merged commit 751525d into from February 22, 2013
TJ Holowaychuk visionmedia closed this February 22, 2013
Nami-Doc Nami-Doc commented on the diff February 22, 2013
bin/component-build
@@ -38,6 +40,10 @@ program.on('--help', function(){
38 40
   console.log('    # build standalone as window.$');
39 41
   console.log('    $ component build --standalone $');
40 42
   console.log();
  43
+  console.log('    # build with middleware by requiring a module');
  44
+  console.log('    # separate with commas if necessary');
  45
+  console.log('    $ component build -r jade-builder');
1
Nami-Doc Owner

-r ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Nick Jackson nickjackson deleted the branch February 23, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
18  bin/component-build
@@ -11,6 +11,7 @@ var program = require('commander')
11 11
   , log = component.utils.log
12 12
   , path = require('path')
13 13
   , fs = require('fs')
  14
+  , exists = fs.existsSync || path.existsSync
14 15
   , mkdir = require('mkdirp');
15 16
 
16 17
 // options
@@ -23,6 +24,7 @@ program
23 24
   .option('-v, --verbose', 'output verbose build information')
24 25
   .option('-p, --prefix <str>', 'prefix css asset urls with <str>')
25 26
   .option('-c, --copy', 'copy files instead of linking')
  27
+  .option('-u, --use <name>', 'use the given script(s) or module(s) as builder middleware')
26 28
 
27 29
 // examples
28 30
 
@@ -38,6 +40,10 @@ program.on('--help', function(){
38 40
   console.log('    # build standalone as window.$');
39 41
   console.log('    $ component build --standalone $');
40 42
   console.log();
  43
+  console.log('    # build with middleware by requiring a module');
  44
+  console.log('    # separate with commas if necessary');
  45
+  console.log('    $ component build -r jade-builder');
  46
+  console.log();
41 47
 });
42 48
 
43 49
 // parse argv
@@ -73,6 +79,18 @@ if (program.prefix) builder.prefixUrls(program.prefix);
73 79
 
74 80
 if (conf.paths) builder.addLookup(conf.paths);
75 81
 
  82
+// require middleware plugins
  83
+if (program.use) {
  84
+  var modules = program.use.split(',');
  85
+  modules.forEach(function(mod){
  86
+    var resolved = path.resolve(mod)
  87
+      , real = exists(resolved) || exists(resolved + '.js')
  88
+      , module = path.resolve('node_modules', mod);
  89
+
  90
+    builder.use(real ? require(resolved) : require(module));
  91
+  });
  92
+}
  93
+
76 94
 var start = new Date;
77 95
 
78 96
 // --dev
19  test/build.js
@@ -28,4 +28,23 @@ describe('component build', function(){
28 28
       done();
29 29
     })
30 30
   })
  31
+
  32
+  it('should require middleware with relative path', function(done){
  33
+    exec('cd test/fixtures/path && ' + bin + '-build -v -u ../plugin', function(err, stdout){
  34
+      if (err) return done(err);
  35
+      stdout.should.include('middleware fired!');
  36
+      stdout.should.include('builder config fired!');
  37
+      done();
  38
+    })
  39
+  })
  40
+  
  41
+  it('should require middleware with absolute path', function(done){
  42
+    var plugin = path.join(__dirname, 'fixtures', 'plugin');
  43
+    exec('cd test/fixtures/path && ' + bin + '-build -v -u ' + plugin, function(err, stdout){
  44
+      if (err) return done(err);
  45
+      stdout.should.include('middleware fired!');
  46
+      stdout.should.include('builder config fired!');
  47
+      done();
  48
+    })
  49
+  })
31 50
 })
10  test/fixtures/plugin.js
... ...
@@ -0,0 +1,10 @@
  1
+/*
  2
+  example middleware plugin for builder
  3
+*/
  4
+
  5
+module.exports = function(builder) {
  6
+  console.log('middleware fired!');
  7
+  builder.on('config', function() {
  8
+    console.log('builder config fired!')
  9
+  })
  10
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.