Skip to content

Instantly share code, notes, and snippets.

@qfox
Created December 23, 2014 11:10
Show Gist options
  • Save qfox/df0380b37566cbaab1de to your computer and use it in GitHub Desktop.
Save qfox/df0380b37566cbaab1de to your computer and use it in GitHub Desktop.
merged-express-bem-engine
var U = require('express-bem/lib/util');
var dropRequireCache = require('enb/lib/fs/drop-require-cache');
module.exports = dynjson;
dynjson.extension = '.dynjson.js';
/**
* dynjson engine for express-bem
* @param {string} name - bundle name
* @param {Object} options - user incoming data
* @param {Function} cb
*/
function dynjson(name, options, cb) {
name = U.fulfillName(name, this.ext);
// pass to render
if (options.forceExec || options.forceLoad) {
dropRequireCache(require, require.resolve(name));
}
var module;
try {
module = require(name);
if (!module || !module.apply) {
cb(new Error('Unknown file format'));
return;
}
// Generates bemjson with dynjson
cb(null, module.apply(options));
} catch (e) {
cb(e);
}
}
module.exports = fullstack;
fullstack.extension = '.bem';
fullstack.targetExtensions = ['.bh.js']; // , '.dynjson.js'];
/**
* fullstackEngine for express-bem
* @param {string} name - bundle name
* @param {Object} options - user data
* - {Object} bemjson: useful for rendering concrete tree
* - {boolean} raw - don't render to html (returns bemjson)
* @param {function(Error, Object)} cb
*/
function fullstack(name, options, cb) {
var _this = this;
// pass options.bemjson directly to bemhtml
if (options.bemjson) {
return _this.thru('bh');
}
// return bemjson if requested
if (options.raw === true) {
return _this.thru('dynjson');
}
// full stack
_this.thru('dynjson', name, options, function (err, bemjson) {
if (err) {
return cb(err);
}
options.bemjson = bemjson;
_this.thru('bh', name, options, function (err, data) {
if (err) {
return cb(err);
}
cb(null, data);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment