bem-bl-xjst
Advanced tools
Comparing version 1.3.1 to 2.0.0
var ometajs = require('ometajs'), | ||
esprima = require('esprima'), | ||
escodegen = require('escodegen'), | ||
estraverse = require('estraverse'), | ||
xjst = require('xjst'), | ||
vm = require('vm'), | ||
bemhtml = require('../ometa/bemhtml'), | ||
checks = require('./checks'), | ||
BEMHTMLParser = bemhtml.BEMHTMLParser, | ||
@@ -9,2 +13,14 @@ BEMHTMLToXJST = bemhtml.BEMHTMLToXJST, | ||
var properties = { | ||
_mode: '$$mode', | ||
block: '$$block', | ||
elem: '$$elem', | ||
elemMods: '$$elemMods', | ||
mods: '$$mods' | ||
}; | ||
var propKeys = Object.keys(properties); | ||
var propValues = propKeys.map(function(key) { | ||
return properties[key]; | ||
}); | ||
var api = exports; | ||
@@ -40,6 +56,23 @@ | ||
try { | ||
var xjstJS = options.devMode ? | ||
xjst.compile(xjstTree, '', { 'no-opt': true }) | ||
: | ||
xjst.compile(xjstTree, { engine: 'sort-group' }); | ||
var xjstJS = xjst.compile(xjstTree, '', { | ||
'no-opt': !!options.devMode, | ||
engine: 'sort-group', | ||
afterCompile: function(tree) { | ||
if (options.nochecks) | ||
return; | ||
var errors = []; | ||
errors = errors.concat(checks.treeStructure(tree)); | ||
if (errors.length) { | ||
var red = '\x1b[1;31m'; | ||
var rst = '\x1b[0m'; | ||
console.error(red + '!!! WARNING !!!!'); | ||
for (var i = 0; i < errors.length; i++) | ||
console.error(errors[i]); | ||
console.error('!!! WARNING !!!!' + rst); | ||
} | ||
} | ||
}); | ||
} catch (e) { | ||
@@ -51,3 +84,7 @@ throw new Error("xjst to js compilation failed:\n" + e.stack); | ||
// Replace known context lookups with context vars | ||
xjstJS = replaceContext(xjstJS); | ||
return 'var ' + exportName + ' = function() {\n' + | ||
' var ' + propValues.join(', ') + ';\n' + | ||
' var cache,\n' + | ||
@@ -61,3 +98,17 @@ ' exports = {},\n' + | ||
' return function() {\n' + | ||
' if (context === this) context = undefined;\n' + | ||
' if (context === this) {\n' + | ||
' context = undefined;\n' + | ||
propKeys.map(function(prop) { | ||
return properties[prop] + ' = \'\'\n'; | ||
}).join('') + | ||
' } else {\n' + | ||
propKeys.map(function(prop) { | ||
if (options.preinit) { | ||
return properties[prop] + ' = context.' + prop + | ||
' || \'\';\n'; | ||
} else { | ||
return properties[prop] + ' = \'\';\n'; | ||
} | ||
}).join('') + | ||
' }\n' + | ||
(vars.length > 0 ? ' var ' + vars.join(', ') + ';\n' : '') + | ||
@@ -83,3 +134,3 @@ ' return xjst.' + options.applyFuncName + '.call(\n' + | ||
if (options && options.devMode) context.console = console; | ||
if (options && options.devMode || true) context.console = console; | ||
vm.runInNewContext(body, context); | ||
@@ -89,1 +140,78 @@ | ||
}; | ||
function replaceContext(src) { | ||
function translateProp(prop) { | ||
if (properties.hasOwnProperty(prop)) | ||
return properties[prop]; | ||
else | ||
return false; | ||
}; | ||
function isHash(node) { | ||
var val = node.init; | ||
if (!val) | ||
return false; | ||
if (val.type !== 'ObjectExpression' || val.properties.length !== 3) | ||
return false; | ||
var props = val.properties; | ||
return props.every(function(prop) { | ||
var name = prop.key.name; | ||
var val = prop.value; | ||
if ((name === 'n' || name === 'm') && val.type === 'ObjectExpression') | ||
return true; | ||
if (name === 'd' && val.type === 'FunctionExpression') | ||
return true; | ||
return false; | ||
}); | ||
} | ||
var applyc = null; | ||
var map = null; | ||
var ast = esprima.parse(src); | ||
ast = estraverse.replace(ast, { | ||
enter: function(node) { | ||
var isFunction = node.type === 'FunctionDeclaration' || | ||
node.type === 'FunctionExpression'; | ||
var id = node.id && node.id.name; | ||
if (applyc === null && | ||
isFunction && | ||
(map !== null || /^(applyc|\$\d+)$/.test(id))) { | ||
applyc = node; | ||
} else if (applyc === null && | ||
node.type === 'VariableDeclarator' && | ||
/^__(h|\$m)\d+$/.test(id) && | ||
isHash(node)) { | ||
map = node; | ||
} else if (applyc === null) { | ||
return; | ||
} | ||
if (applyc !== node && isFunction) { | ||
this.skip(); | ||
return; | ||
} | ||
if (node.type === 'MemberExpression' && | ||
node.computed === false && | ||
node.object.type === 'Identifier' && | ||
node.object.name === '__$ctx') { | ||
var prop = translateProp(node.property.name || node.property.value); | ||
if (!prop) | ||
return; | ||
return { type: 'Identifier', name: prop }; | ||
} | ||
}, | ||
leave: function(node) { | ||
if (node === applyc) | ||
applyc = null; | ||
if (node === map) | ||
applyc = null; | ||
} | ||
}); | ||
return escodegen.generate(ast); | ||
} |
{ | ||
"name": "bem-bl-xjst", | ||
"version": "1.3.1", | ||
"version": "2.0.0", | ||
"dependencies": { | ||
"xjst": "~0.5.1", | ||
"ometajs": "~3.3.5" | ||
"xjst": "~0.5.19", | ||
"ometajs": "~3.3.5", | ||
"estraverse": "~1.5.0", | ||
"esprima": "~1.0.4", | ||
"escodegen": "~1.2.0" | ||
}, | ||
@@ -18,1 +21,2 @@ "devDependencies": { | ||
} | ||
@@ -1,2 +0,2 @@ | ||
bem-bl-xjst [![NPM version](https://badge.fury.io/js/bem-bl-xjst.svg)](http://badge.fury.io/js/bem-bl-xjst) [![Build Status](https://travis-ci.org/bem/bem-bl-xjst.svg?branch=support%2F1.x)](https://travis-ci.org/bem/bem-bl-xjst) | ||
bem-bl-xjst [![NPM version](https://badge.fury.io/js/bem-bl-xjst.svg)](http://badge.fury.io/js/bem-bl-xjst) [![Build Status](https://travis-ci.org/bem/bem-bl-xjst.svg?branch=support%2F2.x)](https://travis-ci.org/bem/bem-bl-xjst) | ||
=========== |
Sorry, the diff of this file is not supported yet
38105
9
248
5
+ Addedescodegen@~1.2.0
+ Addedesprima@~1.0.4
+ Addedestraverse@~1.5.0
+ Addedamdefine@1.0.1(transitive)
+ Addedescodegen@1.2.0(transitive)
+ Addedestraverse@1.5.1(transitive)
+ Addedesutils@1.0.0(transitive)
+ Addedsource-map@0.1.43(transitive)
- Removedsource-map@0.7.4(transitive)
Updatedxjst@~0.5.19