forked from wayfair-archive/tungstenjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (37 loc) · 1.26 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
38
39
40
41
42
/**
* Pre-compiles templates using Ractive and returns a module with all dependencies required
*
* Copyright 2015 Wayfair, LLC
* Available under the Apache Version 2.0 License
*
* https://github.com/wayfair/tungstenjs
*
* @license Apache-2.0
*/
'use strict';
var path = require('path');
var _ = require('underscore');
var utils = require('./shared_utils');
/**
* Compiles given templates
* @param {String} contents Root directory of templates to get stripped off partials
*/
module.exports = function(contents) {
this.cacheable();
var parsedTemplate = utils.compileTemplate(contents, module.src);
var partials = utils.findPartials(parsedTemplate);
var template = JSON.stringify(parsedTemplate);
var templatePath = path.relative(path.dirname(module.dest), __dirname + '/template');
templatePath = templatePath.replace(/\\/g, '/');
var output = 'var Template=require("tungstenjs/src/template/template");';
output += 'var template=new Template(' + template + ');';
output += 'module.exports=template;';
if (partials.length > 0) {
output += 'template.setPartials({';
output += _.map(partials, function(partial) {
return '"' + partial + '":require("./' + partial + '.mustache")';
}).join(',');
output += '});';
}
return output;
};