From c0e4de07f35b05c1596373e72db6a170a16f290c Mon Sep 17 00:00:00 2001 From: mde Date: Mon, 4 Jul 2016 14:58:53 -0700 Subject: [PATCH] Moved CommonJS requires to top of module --- lib/ejs.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/ejs.js b/lib/ejs.js index 52c95f32..c2459e32 100644 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -45,7 +45,9 @@ */ var fs = require('fs'); +var path = require('path'); var utils = require('./utils'); + var scopeOptionWarned = false; var _VERSION_STRING = require('../package.json').version; var _DEFAULT_DELIMITER = '%'; @@ -89,7 +91,6 @@ exports.localsName = _DEFAULT_LOCALS_NAME; * @return {String} */ exports.resolveInclude = function(name, filename, isDir) { - var path = require('path'); var dirname = path.dirname; var extname = path.extname; var resolve = path.resolve; @@ -141,35 +142,35 @@ function getIncludePath(path, options){ */ function handleCache(options, template) { - var fn; - var path = options.filename; + var func; + var filename = options.filename; var hasTemplate = arguments.length > 1; if (options.cache) { - if (!path) { + if (!filename) { throw new Error('cache option requires a filename'); } - fn = exports.cache.get(path); - if (fn) { - return fn; + func = exports.cache.get(filename); + if (func) { + return func; } if (!hasTemplate) { - template = fs.readFileSync(path).toString().replace(_BOM, ''); + template = fs.readFileSync(filename).toString().replace(_BOM, ''); } } else if (!hasTemplate) { // istanbul ignore if: should not happen at all - if (!path) { + if (!filename) { throw new Error('Internal EJS error: no file name or template ' + 'provided'); } - template = fs.readFileSync(path).toString().replace(_BOM, ''); + template = fs.readFileSync(filename).toString().replace(_BOM, ''); } - fn = exports.compile(template, options); + func = exports.compile(template, options); if (options.cache) { - exports.cache.set(path, fn); + exports.cache.set(filename, func); } - return fn; + return func; } /** @@ -346,7 +347,7 @@ exports.render = function (template, d, o) { exports.renderFile = function () { var args = Array.prototype.slice.call(arguments); - var path = args.shift(); + var filename = args.shift(); var cb = args.pop(); var data = args.shift() || {}; var opts = args.pop() || {}; @@ -371,7 +372,7 @@ exports.renderFile = function () { cpOptsInData(data, opts); } } - opts.filename = path; + opts.filename = filename; try { result = handleCache(opts)(data);