forked from wayfair-archive/tungstenjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (33 loc) · 1.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Pre-compiles templates using Ractive and returns a module with all dependencies required
*
* Copyright 2016 Wayfair, LLC
* Available under the Apache Version 2.0 License
*
* https://github.com/wayfair/tungstenjs
*
* @license Apache-2.0
*/
'use strict';
var _ = require('underscore');
var compiler = require('../../src/template/compiler');
/**
* Compiles given templates
* @param {String} contents Root directory of templates to get stripped off partials
*/
module.exports = function(contents) {
this.cacheable();
var templateData = compiler(contents);
var output = 'var Template=require("tungstenjs").Template;';
output += 'var template=new Template(' + JSON.stringify(templateData.templateObj) + ');';
output += 'module.exports=template;';
var partials = templateData.tokens.partials;
if (partials.length > 0) {
output += 'template.setPartials({';
output += _.map(partials, function(partial) {
return '"' + partial + '":require("./' + partial + '.mustache")';
}).join(',');
output += '});';
}
return output;
};