component-as-module
Advanced tools
Comparing version 0.2.0 to 0.3.0
0.3.0 / 2013-05-29 | ||
================== | ||
* support for `component-builder` plugins | ||
0.2.0 / 2013-05-13 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -20,2 +20,4 @@ var fs = require('fs') | ||
this._lookup = new Lookup | ||
this._hooks = root && root._hooks || {} | ||
this._files = {} | ||
this.modules = {} | ||
@@ -65,2 +67,46 @@ this.components = {} | ||
/** | ||
* Define hook `name` with callback `fn()`. | ||
* | ||
* @param {String} name | ||
* @param {String} fn | ||
* @api public | ||
*/ | ||
Loader.prototype.hook = function(name, fn) { | ||
this._hooks[name] = this._hooks[name] || [] | ||
this._hooks[name].push(fn) | ||
return this | ||
} | ||
/** | ||
* Add file `type` `filename` contents of `val`. | ||
* | ||
* @param {String} type | ||
* @param {String} filename | ||
* @param {String} val | ||
* @api public | ||
*/ | ||
Loader.prototype.addFile = function(type, filename, val){ | ||
var files = this.config[type] || (this.config[type] = []) | ||
files.push(filename) | ||
this._files[filename] = val | ||
} | ||
/** | ||
* Remove file `type` `filename` | ||
* | ||
* @param {String} type | ||
* @param {String} filename | ||
* @param {String} val | ||
* @api public | ||
*/ | ||
Loader.prototype.removeFile = function(type, filename){ | ||
var files = this.config[type] || (this.config[type] = []) | ||
var i = files.indexOf(filename) | ||
if (~i) files.splice(i, 1) | ||
} | ||
/** | ||
* Register known component `name`. | ||
@@ -114,8 +160,8 @@ * This has higher priority than lookup procedure. | ||
Loader.prototype.config = function() { | ||
if (this._conf) return this._conf | ||
Loader.prototype.json = function() { | ||
if (this.config) return this.config | ||
var path = this.path('component.json') | ||
var json = fs.readFileSync(path, 'utf8') | ||
try { | ||
return this._conf = JSON.parse(json) | ||
return this.config = JSON.parse(json) | ||
} catch(e) { | ||
@@ -127,3 +173,3 @@ throw new Error('Failed to parse ' + path + '\n' + e.message) | ||
Loader.prototype.load = function(file) { | ||
file = file || this.config().main || 'index.js' | ||
file = file || this.json().main || 'index.js' | ||
var script = this.resolve(file) | ||
@@ -138,3 +184,3 @@ if (!script) throw new Error('Failed to require ' + file + ' of '+ this.dir + ' component.') | ||
var path = this.path(script) | ||
var js = fs.readFileSync(path, 'utf8') | ||
var js = this._files[script] || fs.readFileSync(path, 'utf8') | ||
@@ -198,3 +244,5 @@ if (Path.extname(script) == '.json') { | ||
if (this._scripts) return this._scripts | ||
var scripts = this.config().scripts || [] | ||
var config = this.json() | ||
this.performHook('before scripts') | ||
var scripts = config.scripts || [] | ||
return this._scripts = scripts.reduce(function(hash, script) { | ||
@@ -218,3 +266,3 @@ var path = Path.normalize(script) | ||
var config = this.config() | ||
var config = this.json() | ||
@@ -251,2 +299,10 @@ var deps = mix({}, config.dependencies, this.dev && config.development) | ||
Loader.prototype.performHook = function(name) { | ||
var hooks = this._hooks[name] | ||
if (!hooks) return | ||
for (var i = 0; i < hooks.length; i++) { | ||
hooks[i](this) | ||
} | ||
} | ||
function mix(t) { | ||
@@ -253,0 +309,0 @@ var sources = [].slice.call(arguments, 1) |
{ | ||
"name": "component-as-module", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"author": "Eldar Gabdullin <eldargab@gmail.com>", | ||
@@ -15,3 +15,4 @@ "description": "Require components from node programs", | ||
"mocha": "*", | ||
"should": "*" | ||
"should": "*", | ||
"string-to-js": "*" | ||
}, | ||
@@ -18,0 +19,0 @@ "scripts": { |
11543
345
3