Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6056 efrain 1
/*! JsRender tmplify submodule v1.0.2: http://jsviews.com/#jsrender */
2
/*! Browserify transform for JsRender templates */
3
/*
4
 * Copyright 2019, Boris Moore
5
 * Released under the MIT License.
6
*/
7
 
8
(function() {
9
"use strict";
10
var jsrender = require('./../jsrender-node.js'),
11
	fs = require('fs'),
12
	path = require('path'),
13
	pathSep = path.sep,
14
	through = require('through2'),
15
	rootDirPath = path.resolve("./"),
16
	rootDirPathLen = rootDirPath.length + 1;
17
 
18
function isTemplate(fileExt, extensions) {
19
	extensions = typeof extensions === "string"
20
		? extensions
21
		: "html jsrender jsr"; // Default extensions
22
	return new RegExp("\\s" + fileExt + "\\s").test(" " + extensions + " ");
23
}
24
 
25
module.exports = function(file, options) {
26
	var nodeFileDirName = path.dirname(file);
27
 
28
	if (!isTemplate(path.extname(file).slice(1), options && (options.extensions || options.e))) {
29
		return through();
30
	}
31
	return through(function(buf, enc, next) {
32
		var createTmplCode, ref, pathFromFileDir,
33
			markup = buf.toString().replace(/^\uFEFF/, ''), // Remove BOM if necessary
34
			tmpl = jsrender.templates(markup),
35
			bundledFile = 'var tmplRefs = [],\n'
36
			+ "  mkup = '" + markup.replace(/['"\\]/g, "\\$&").replace(/[ \t]*(\r\n|\n|\r)/g, '\\n') + "',\n" // Normalize newlines, and escape quotes and \ character
37
			+ '  $ = global.jsrender || global.jQuery;\n\n',
38
			templateName = './' + file.slice(rootDirPathLen).split(pathSep).join('/');
39
 
40
		for (ref in tmpl.refs) {
41
			// Recursively bundle any nested template references, e.g. {{include tmpl="./some/template.html/}}"
42
			fs.stat(ref, function(err, stat) {
43
				// Async check that file exists
44
				if(err && err.code == 'ENOENT') {
45
					throw new Error("Template '" + ref + "' not found at '" + err.path + "'. Use path relative to '" + rootDirPath + "'.");
46
				}
47
			});
48
			pathFromFileDir = './' + path.relative(nodeFileDirName, ref).split(pathSep).join('/');
49
			bundledFile += 'tmplRefs.push(require("' + pathFromFileDir + '"));\n';
50
		}
51
 
52
		createTmplCode = '$.templates("' + templateName + '", mkup)';
53
		bundledFile +=
54
			'module.exports = $ ? ' + createTmplCode
55
			+ ' :\n  function($) {\n'
56
			+ '    if (!$ || !$.views) {throw "Requires jsrender/jQuery";}\n'
57
			+ '    while (tmplRefs.length) {\n      tmplRefs.pop()($); // compile nested template\n    }\n\n'
58
			+ '    return ' + createTmplCode
59
			+ '\n  };';
60
		this.push(bundledFile);
61
		next();
62
	});
63
};
64
}());