Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

compiler: mix code in parts

  • Loading branch information...
commit 9ce722d6514841cccc6bff89f6f5cdecb7b1c8d7 1 parent 9ca57fe
@indutny indutny authored
Showing with 25 additions and 28 deletions.
  1. +5 −6 lib/xjst/compiler.js
  2. +20 −22 lib/xjst/helpers/serializer.js
View
11 lib/xjst/compiler.js
@@ -106,11 +106,7 @@ exports.generate = function generate(ast, options) {
var templates = ast[1],
predicateMap = {},
predicateValues = utils.getPredicateValues(templates),
-
- identifier = new utils.Identifier(),
-
- hashs = {},
- fns = {};
+ identifier = new utils.Identifier();
// Set default options
if (!options) options = {};
@@ -182,10 +178,13 @@ exports.generate = function generate(ast, options) {
// Lift variables from local expressions
tree = xjst.transforms.vars.process(tree);
+ var render = serializer.render(tree);
+
// Finally render tree
body = [
+ render.pre,
XJSTCompiler.match(ast[0], 'other', options.ometa),
- serializer.render(tree)
+ render.post
];
}
View
42 lib/xjst/helpers/serializer.js
@@ -342,26 +342,24 @@ Serializer.prototype.render = function render(ast, type) {
// If type === 'partial' - return body without functions and hashmaps
if (type === 'partial') return res.join('');
- return [
- this.options.merge ?
- 'var _c = exports.config = {};' +
- // Insert .mergeWith template function
- 'exports.mergeWith = ' + utils.mergeWith.toString() + ';\n'
- :
- '',
- 'exports.apply = apply;\n',
- this.options.asyncify ? utils.dispatch.toString() + ';\n' : '',
- 'function ', this.options.asyncify ? 'applySync' : 'apply',
- '(' + (this.options.asyncify ? '__$callback' : '') + ')'
- ].concat(
- '{',
- this.addContext(res.join('')),
- '};\n',
-
- // Insert all switches that was translated to the hashmaps
- this.hashList.render(),
-
- // Insert all switches that was wrapped in the functions
- this.fnList.render()
- ).join('');
+ return {
+ pre: [
+ this.options.merge ?
+ 'var _c = exports.config = {};' +
+ // Insert .mergeWith template function
+ 'exports.mergeWith = ' + utils.mergeWith.toString() + ';\n'
+ :
+ ''
+ ].concat(this.hashList.render(), this.fnList.render()).join(''),
+ post: [
+ 'exports.apply = apply;\n',
+ this.options.asyncify ? utils.dispatch.toString() + ';\n' : '',
+ 'function ', this.options.asyncify ? 'applySync' : 'apply',
+ '(' + (this.options.asyncify ? '__$callback' : '') + ')'
+ ].concat(
+ '{',
+ this.addContext(res.join('')),
+ '};\n'
+ ).join('')
+ };
};
Please sign in to comment.
Something went wrong with that request. Please try again.