Simple image host.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

loader.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var fs = require("fs");
  2. var zlib = require("zlib");
  3. var browserPrefix = require("browser-prefix");
  4. var log = require("mlogger");
  5. var jshint = require("jshint").JSHINT;
  6. var minify = require("./minify.js");
  7. var includeHtml = require("./includeHtml.js");
  8. exports.load = function(endpoints, conf) {
  9. var res = {
  10. endpoints: {},
  11. templates: {},
  12. views: {}
  13. }
  14. //Prepare endpoints
  15. var errs = false;
  16. var eps = Object.keys(endpoints).map(function(i) {
  17. var ep = {
  18. path: endpoints[i],
  19. url: i
  20. }
  21. try {
  22. //The endpoint is a function if the file ends with .node.js
  23. if (/\.node\.js$/.test(ep.path)) {
  24. ep.func = require("../"+conf.dir.web+"/"+ep.path);
  25. return ep;
  26. //If it doesn't end with .node.js, it's a regular text file and will
  27. //just be served as is
  28. } else {
  29. ep.str = fs.readFileSync(conf.dir.web+"/"+ep.path, "utf8");
  30. //Add browser prefixes
  31. if (/\.css$/.test(ep.path)) {
  32. ep.str = browserPrefix(ep.str);
  33. ep.mimeType = "text/css";
  34. if (conf.minify) ep.str = minify.css(ep.str);
  35. } else if (/\.html$/.test(ep.path)) {
  36. ep.mimeType = "text/html";
  37. if (conf.minify) ep.str = minify.html(ep.str);
  38. } else if (/\.js$/.test(ep.path)) {
  39. jshint.errors = [];
  40. jshint(ep.str);
  41. jshint.errors.forEach(function(err) {
  42. if (!err)
  43. return;
  44. log.warn(
  45. conf.dir.web+"/"+ep.path+": "+
  46. "Line "+err.line+": "+
  47. err.reason
  48. );
  49. });
  50. ep.mimeType = "application/javascript";
  51. if (conf.minify && !jshint.errors) ep.str = minify.js(ep.str);
  52. }
  53. return ep;
  54. }
  55. //Errors will usually be because an endpoint doesn't exist
  56. } catch (err) {
  57. if (err.code === "ENOENT" || err.code === "MODULE_NOT_FOUND") {
  58. log.error("File not found: "+conf.dir.web+"/"+ep.path);
  59. errs = true;
  60. } else {
  61. log.warn(conf.dir.web+"/"+ep.path);
  62. throw err;
  63. }
  64. }
  65. });
  66. //No need to proceed if some endpoints don't exist
  67. if (errs) process.exit();
  68. eps.forEach(function(ep) {
  69. res.endpoints[ep.url] = ep;
  70. });
  71. //Prepare all templates
  72. var templates = {};
  73. fs.readdirSync("templates").forEach(function(f) {
  74. var name = f.replace(/\.html$/, "");
  75. res.templates[name] = includeHtml("templates/"+f, conf);
  76. });
  77. //Prepare all views
  78. var views = {};
  79. fs.readdirSync("views").forEach(function(f) {
  80. var name = f.replace(/\.html$/, "");
  81. res.views[name] = includeHtml("views/"+f, conf);
  82. });
  83. return res;
  84. }