webpack_express_view.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*!
  2. * express
  3. * Copyright(c) 2009-2013 TJ Holowaychuk
  4. * Copyright(c) 2013 Roman Shtylman
  5. * Copyright(c) 2014-2015 Douglas Christopher Wilson
  6. * MIT Licensed
  7. */
  8. /**
  9. * Module dependencies.
  10. * @private
  11. */
  12. var debug = __webpack_require__(/*! debug */ "./node_modules/debug/src/index.js")('express:view');
  13. var path = __webpack_require__(/*! path */ "path");
  14. var fs = __webpack_require__(/*! fs */ "fs");
  15. /**
  16. * Module variables.
  17. * @private
  18. */
  19. var dirname = path.dirname;
  20. var basename = path.basename;
  21. var extname = path.extname;
  22. var join = path.join;
  23. var resolve = path.resolve;
  24. /**
  25. * Module exports.
  26. * @public
  27. */
  28. module.exports = View;
  29. /**
  30. * Initialize a new `View` with the given `name`.
  31. *
  32. * Options:
  33. *
  34. * - `defaultEngine` the default template engine name
  35. * - `engines` template engine require() cache
  36. * - `root` root path for view lookup
  37. *
  38. * @param {string} name
  39. * @param {object} options
  40. * @public
  41. */
  42. function View(name, options) {
  43. var opts = options || {};
  44. this.defaultEngine = opts.defaultEngine;
  45. this.ext = extname(name);
  46. this.name = name;
  47. this.root = opts.root;
  48. if (!this.ext && !this.defaultEngine) {
  49. throw new Error('No default engine was specified and no extension was provided.');
  50. }
  51. var fileName = name;
  52. if (!this.ext) {
  53. // get extension from default engine name
  54. this.ext = this.defaultEngine[0] !== '.'
  55. ? '.' + this.defaultEngine
  56. : this.defaultEngine;
  57. fileName += this.ext;
  58. }
  59. if (!opts.engines[this.ext]) {
  60. // load engine
  61. var mod = this.ext.substr(1)
  62. debug('require "%s"', mod)
  63. // default engine export
  64. var fn = __webpack_require__("./node_modules/express/lib sync recursive")(mod).__express
  65. if (typeof fn !== 'function') {
  66. throw new Error('Module "' + mod + '" does not provide a view engine.')
  67. }
  68. opts.engines[this.ext] = fn
  69. }
  70. // store loaded engine
  71. this.engine = opts.engines[this.ext];
  72. // lookup path
  73. this.path = this.lookup(fileName);
  74. }
  75. /**
  76. * Lookup view by the given `name`
  77. *
  78. * @param {string} name
  79. * @private
  80. */
  81. View.prototype.lookup = function lookup(name) {
  82. var path;
  83. var roots = [].concat(this.root);
  84. debug('lookup "%s"', name);
  85. for (var i = 0; i < roots.length && !path; i++) {
  86. var root = roots[i];
  87. // resolve the path
  88. var loc = resolve(root, name);
  89. var dir = dirname(loc);
  90. var file = basename(loc);
  91. // resolve the file
  92. path = this.resolve(dir, file);
  93. }
  94. return path;
  95. };
  96. /**
  97. * Render with the given options.
  98. *
  99. * @param {object} options
  100. * @param {function} callback
  101. * @private
  102. */
  103. View.prototype.render = function render(options, callback) {
  104. debug('render "%s"', this.path);
  105. this.engine(this.path, options, callback);
  106. };
  107. /**
  108. * Resolve the file within the given directory.
  109. *
  110. * @param {string} dir
  111. * @param {string} file
  112. * @private
  113. */
  114. View.prototype.resolve = function resolve(dir, file) {
  115. var ext = this.ext;
  116. // <path>.<ext>
  117. var path = join(dir, file);
  118. var stat = tryStat(path);
  119. if (stat && stat.isFile()) {
  120. return path;
  121. }
  122. // <path>/index.<ext>
  123. path = join(dir, basename(file, ext), 'index' + ext);
  124. stat = tryStat(path);
  125. if (stat && stat.isFile()) {
  126. return path;
  127. }
  128. };
  129. /**
  130. * Return a stat, maybe.
  131. *
  132. * @param {string} path
  133. * @return {fs.Stats}
  134. * @private
  135. */
  136. function tryStat(path) {
  137. debug('stat "%s"', path);
  138. try {
  139. return fs.statSync(path);
  140. } catch (e) {
  141. return undefined;
  142. }
  143. }
  144. //# sourceURL=webpack:///./node_modules/express/lib/view.js?