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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| compile: function() { var src; var fn; var opts = this.opts; var prepended = ''; var appended = ''; var escapeFn = opts.escapeFunction; var ctor; var sanitizedFilename = opts.filename ? JSON.stringify(opts.filename) : 'undefined';
if (!this.source) { this.generateSource(); prepended += ' var __output = "";\n' + ' function __append(s) { if (s !== undefined && s !== null) __output += s }\n'; if (opts.outputFunctionName) {
if (!_JS_IDENTIFIER.test(opts.outputFunctionName)) { throw new Error('outputFunctionName is not a valid JS identifier.'); } prepended += ' var ' + opts.outputFunctionName + ' = __append;' + '\n'; } if (opts.localsName && !_JS_IDENTIFIER.test(opts.localsName)) { throw new Error('localsName is not a valid JS identifier.'); } if (opts.destructuredLocals && opts.destructuredLocals.length) { var destructuring = ' var __locals = (' + opts.localsName + ' || {}),\n'; for (var i = 0; i < opts.destructuredLocals.length; i++) { var name = opts.destructuredLocals[i]; if (!_JS_IDENTIFIER.test(name)) { throw new Error('destructuredLocals[' + i + '] is not a valid JS identifier.'); } if (i > 0) { destructuring += ',\n '; } destructuring += name + ' = __locals.' + name; } prepended += destructuring + ';\n'; } if (opts._with !== false) { prepended += ' with (' + opts.localsName + ' || {}) {' + '\n'; appended += ' }' + '\n'; } appended += ' return __output;' + '\n'; this.source = prepended + this.source + appended; }
if (opts.compileDebug) { src = 'var __line = 1' + '\n' + ' , __lines = ' + JSON.stringify(this.templateText) + '\n' + ' , __filename = ' + sanitizedFilename + ';' + '\n' + 'try {' + '\n' + this.source + '} catch (e) {' + '\n' + ' rethrow(e, __lines, __filename, __line, escapeFn);' + '\n' + '}' + '\n'; } else { src = this.source; }
if (opts.client) {
src = 'escapeFn = escapeFn || ' + escapeFn.toString() + ';' + '\n' + src; if (opts.compileDebug) { src = 'rethrow = rethrow || ' + rethrow.toString() + ';' + '\n' + src; } }
if (opts.strict) { src = '"use strict";\n' + src; } if (opts.debug) { console.log(src); } if (opts.compileDebug && opts.filename) { src = src + '\n' + '//# sourceURL=' + sanitizedFilename + '\n'; }
try { if (opts.async) { try { ctor = (new Function('return (async function(){}).constructor;'))(); } catch (e) { if (e instanceof SyntaxError) { throw new Error('This environment does not support async/await'); } else { throw e; } } } else { ctor = Function; }
fn = new ctor(opts.localsName + ', escapeFn, include, rethrow', src); } catch (e) { if (e instanceof SyntaxError) { if (opts.filename) { e.message += ' in ' + opts.filename; } e.message += ' while compiling ejs\n\n'; e.message += 'If the above error is not helpful, you may want to try EJS-Lint:\n'; e.message += 'https://github.com/RyanZim/EJS-Lint'; if (!opts.async) { e.message += '\n'; e.message += 'Or, if you meant to create an async function, pass `async: true` as an option.'; } } throw e; }
var returnedFn = opts.client ? fn : function anonymous(data) { var include = function(path, includeData) { var d = utils.shallowCopy(utils.createNullProtoObjWherePossible(), data); if (includeData) { d = utils.shallowCopy(d, includeData); } return includeFile(path, opts)(d); }; return fn.apply(opts.context, [data || utils.createNullProtoObjWherePossible(), escapeFn, include, rethrow]); }; if (opts.filename && typeof Object.defineProperty === 'function') { var filename = opts.filename; var basename = path.basename(filename, path.extname(filename)); try { Object.defineProperty(returnedFn, 'name', { value: basename, writable: false, enumerable: false, configurable: true }); } catch (e) { } } return returnedFn; }
|