Skip to content

Commit

Permalink
Use bem-xjst in Node.js without bundling via browserify (#407 fixed)
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Jan 30, 2017
1 parent 4ed06f1 commit 2ab837f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 61 deletions.
18 changes: 8 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
var fs = require('fs');
var Compiler = require('./lib/compiler').Compiler;
var Compiler = require('./lib/compiler');
var _cache = {};
var fs = require('fs');

function getEngine(engineName) {
var getEngine = function getEngine(engineName) {
if (_cache[engineName]) return _cache[engineName];

var runtime = require('./lib/' + engineName);
var pathToBundle = require.resolve('./lib/' + engineName + '/bundle');
var sourceBundle = fs.readFileSync(pathToBundle, 'utf8');
var bundleSource = fs.readFileSync(pathToBundle, 'utf8');

runtime.source = sourceBundle;
return _cache[engineName] = new Compiler(engineName, bundleSource);
};

return _cache[engineName] = new Compiler(runtime);
}

module.exports = {
get bemtree() { return getEngine('bemtree'); },
get bemhtml() { return getEngine('bemhtml'); }
get bemhtml() { return getEngine('bemhtml'); },
get bemtree() { return getEngine('bemtree'); }
};
19 changes: 14 additions & 5 deletions lib/bemxjst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function BEMXJST(options) {
// Current tree
this.tree = null;

this.template = {};

// Current match
this.match = null;

Expand Down Expand Up @@ -113,14 +115,20 @@ BEMXJST.prototype.compile = function compile(code) {

this.entities = ent;
this.oninit = out.oninit;

return this.template;
};

BEMXJST.prototype.getTemplate = function(code, options) {
this.compile(code, options);
this.exportApply(this.template);

return this.template;
};

BEMXJST.prototype.recompileInput = function recompileInput(code) {
var args = BEMXJST.prototype.locals;
var out = code.toString();

// Strip the function
out = out.replace(/^function[^{]+{|}$/g, '');
var out = code ? utils.fnToString(code) : '';

// And recompile it with right arguments
out = new Function(args.join(', '), out);
Expand Down Expand Up @@ -519,7 +527,8 @@ BEMXJST.prototype.exportApply = function exportApply(exports) {

// Add templates at run time
exports.compile = function compile(templates) {
return self.compile(templates);
self.compile(templates);
return exports;
};

var sharedContext = {};
Expand Down
75 changes: 29 additions & 46 deletions lib/compiler.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
var vm = require('vm');
var BEMXJSTError = require('./bemxjst/error').BEMXJSTError;
var fnToString = require('./bemxjst/utils').fnToString;
var engines = {
bemhtml: require('./bemhtml'),
bemtree: require('./bemtree')
};

function Compiler(runtime) {
this.runtime = runtime;
function Compiler(engineName, bundleSource) {
this.engineName = engineName;
this.bundleSource = bundleSource;
}

exports.Compiler = Compiler;
Compiler.prototype.compile = function compile(code, options) {
if (!options) options = {};

var api = new engines[this.engineName](options);

if (options.runtimeLint) {
code = fnToString(code);
code = code + ';' + require('fs')
.readFileSync('./runtime-lint/index.js', 'utf8');
}

return api.getTemplate(code, options);
};

Compiler.prototype.generate = function generate(code, options) {
if (!options) options = {};

code = fnToString(code);

var exportName = options.exportName || 'BEMHTML';
var engine = options.engine || 'BEMHTML';
var locals = require('./bemxjst').prototype.locals;

var locals = this.runtime.prototype.locals;

if (options.runtimeLint)
if (options.runtimeLint) {
code = code + ';' + require('fs')
.readFileSync('./runtime-lint/index.js', 'utf8');
.readFileSync('../runtime-lint/index.js', 'utf8');
}

var exportName = this.engineName.toUpperCase();

var source = [
'/// -------------------------------------',
'/// --------- BEM-XJST Runtime Start ----',
'/// -------------------------------------',
'var ' + exportName + ' = function(module, exports) {',
this.runtime.source + ';',
this.bundleSource + ';',
' return module.exports ||',
' exports.' + exportName + ';',
'}({}, {});',
'/// -------------------------------------',
'/// --------- BEM-XJST Runtime End ------',
'/// -------------------------------------',
'',
'var api = new ' + engine + '(' + JSON.stringify(options) + ');',
'var api = new ' + exportName + '(' + JSON.stringify(options) + ');',
'/// -------------------------------------',
'/// ------ BEM-XJST User-code Start -----',
'/// -------------------------------------',
Expand All @@ -51,35 +65,4 @@ Compiler.prototype.generate = function generate(code, options) {
return source;
};

var _compile = function _compile(fn, exports) {
try {
fn(exports, console);
} catch (e) {
if (e instanceof BEMXJSTError)
throw new BEMXJSTError(e.message);
else
throw e;
}

return exports;
};

Compiler.prototype.compile = function compile(code, options) {
if (!options) options = {};

var out = this.generate(code, options);

out = '(function(exports, console) {' + out + '})';
var exports = {};

var fn = options.context === 'this' ?
vm.runInThisContext(out) :
vm.runInNewContext(out, {
console: console,
Error: Error,
BEMXJSTError: BEMXJSTError });

_compile(fn, exports);

return exports;
};
module.exports = Compiler;

0 comments on commit 2ab837f

Please sign in to comment.