view.js
This file
Path
lib/view.js
173 lines · 3.1 KiB · JavaScript
Last changed by
perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
History of this fileTake it
The walk stopped after 200 commits: lines older than fc4eb6d point at it.
docs: add license commentsDouglas Christopher Wilsonbefore adoption95ad276
lines 1–8why, one step back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2013 Roman Shtylman5 * Copyright(c) 2014-2015 Douglas Christopher Wilson6 * MIT Licensed7 */8perf: enable strict modeDouglas Christopher Wilsonbefore adoptione71014f
lines 9–10why, one step back9'use strict';10Deprecate leading colon in name for app.paramDouglas Christopher Wilsonbefore adoptionfc4eb6d
lines 11–12why, one step back11/**12 * Module dependencies.perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
line 13why, one step back13 * @privateDeprecate leading colon in name for app.paramDouglas Christopher Wilsonbefore adoptionfc4eb6d
lines 14–32why, one step back14 */1516var debug = require('debug')('express:view');17var path = require('path');18var fs = require('fs');19var utils = require('./utils');2021/**22 * Module variables.23 * @private24 */2526var dirname = path.dirname;27var basename = path.basename;28var extname = path.extname;29var join = path.join;30var resolve = path.resolve;3132/**perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 33–34why, one step back33 * Module exports.34 * @publicDeprecate leading colon in name for app.paramDouglas Christopher Wilsonbefore adoptionfc4eb6d
lines 35–47why, one step back35 */3637module.exports = View;3839/**40 * Initialize a new `View` with the given `name`.41 *42 * Options:43 *44 * - `defaultEngine` the default template engine name45 * - `engines` template engine require() cache46 * - `root` root path for view lookup47 *perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 48–50why, one step back48 * @param {string} name49 * @param {object} options50 * @publicDeprecate leading colon in name for app.paramDouglas Christopher Wilsonbefore adoptionfc4eb6d
lines 51–53why, one step back51 */5253function View(name, options) {perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 54–57why, one step back54 var opts = options || {};5556 this.defaultEngine = opts.defaultEngine;57 this.ext = extname(name);Deprecate leading colon in name for app.paramDouglas Christopher Wilsonbefore adoptionfc4eb6d
line 58why, one step back58 this.name = name;perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 59–85why, one step back59 this.root = opts.root;6061 if (!this.ext && !this.defaultEngine) {62 throw new Error('No default engine was specified and no extension was provided.');63 }6465 var fileName = name;6667 if (!this.ext) {68 // get extension from default engine name69 this.ext = this.defaultEngine[0] !== '.'70 ? '.' + this.defaultEngine71 : this.defaultEngine;7273 fileName += this.ext;74 }7576 if (!opts.engines[this.ext]) {77 // load engine78 opts.engines[this.ext] = require(this.ext.substr(1)).__express;79 }8081 // store loaded engine82 this.engine = opts.engines[this.ext];8384 // lookup path85 this.path = this.lookup(fileName);Deprecate leading colon in name for app.paramDouglas Christopher Wilsonbefore adoptionfc4eb6d
lines 86–90why, one step back86}8788/**89 * Lookup view by the given `name`90 *perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 91–92why, one step back91 * @param {string} name92 * @privateDeprecate leading colon in name for app.paramDouglas Christopher Wilsonbefore adoptionfc4eb6d
lines 93–116why, one step back93 */9495View.prototype.lookup = function lookup(name) {96 var path;97 var roots = [].concat(this.root);9899 debug('lookup "%s"', name);100101 for (var i = 0; i < roots.length && !path; i++) {102 var root = roots[i];103104 // resolve the path105 var loc = resolve(root, name);106 var dir = dirname(loc);107 var file = basename(loc);108109 // resolve the file110 path = this.resolve(dir, file);111 }112113 return path;114};115116/**perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
line 117why, one step back117 * Render with the given options.Deprecate leading colon in name for app.paramDouglas Christopher Wilsonbefore adoptionfc4eb6d
line 118why, one step back118 *perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 119–121why, one step back119 * @param {object} options120 * @param {function} callback121 * @privateDeprecate leading colon in name for app.paramDouglas Christopher Wilsonbefore adoptionfc4eb6d
lines 122–123why, one step back122 */123perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
line 124why, one step back124View.prototype.render = function render(options, callback) {Deprecate leading colon in name for app.paramDouglas Christopher Wilsonbefore adoptionfc4eb6d
line 125why, one step back125 debug('render "%s"', this.path);perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
line 126why, one step back126 this.engine(this.path, options, callback);Deprecate leading colon in name for app.paramDouglas Christopher Wilsonbefore adoptionfc4eb6d
lines 127–140why, one step back127};128129/**130 * Resolve the file within the given directory.131 *132 * @param {string} dir133 * @param {string} file134 * @private135 */136137View.prototype.resolve = function resolve(dir, file) {138 var ext = this.ext;139140 // <path>.<ext>perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 141–142why, one step back141 var path = join(dir, file);142 var stat = tryStat(path);Deprecate leading colon in name for app.paramDouglas Christopher Wilsonbefore adoptionfc4eb6d
lines 143–173why, one step back143144 if (stat && stat.isFile()) {145 return path;146 }147148 // <path>/index.<ext>149 path = join(dir, basename(file, ext), 'index' + ext);150 stat = tryStat(path);151152 if (stat && stat.isFile()) {153 return path;154 }155};156157/**158 * Return a stat, maybe.159 *160 * @param {string} path161 * @return {fs.Stats}162 * @private163 */164165function tryStat(path) {166 debug('stat "%s"', path);167168 try {169 return fs.statSync(path);170 } catch (e) {171 return undefined;172 }173}