Skip to content

Commit

Permalink
Moved CommonJS requires to top of module
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Jul 4, 2016
1 parent a34b333 commit c0e4de0
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '%';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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() || {};
Expand All @@ -371,7 +372,7 @@ exports.renderFile = function () {
cpOptsInData(data, opts);
}
}
opts.filename = path;
opts.filename = filename;

try {
result = handleCache(opts)(data);
Expand Down

0 comments on commit c0e4de0

Please sign in to comment.