nodejs.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. var Rtl = require("rtl.js");
  3. var Code = require("code.js");
  4. var Context = require("context.js");
  5. var oc = require("oc.js");
  6. var fs = require("fs");
  7. var path = require("path");
  8. var ModuleGenerator = Rtl.Class.extend({
  9. init: function Nodejs$ModuleGenerator(name, imports){
  10. this.__name = name;
  11. this.__imports = imports;
  12. },
  13. prolog: function(){
  14. var result = "";
  15. var modules = this.__imports;
  16. for(var name in modules){
  17. var alias = modules[name];
  18. result += "var " + alias + " = " + (name == "this"
  19. ? "GLOBAL"
  20. : "require(\"" + name + ".js\")") + ";\n";
  21. }
  22. return result;
  23. },
  24. epilog: function(exports){
  25. var result = "";
  26. for(var access in exports){
  27. var e = exports[access];
  28. var code = Code.genExport(e);
  29. if (code){
  30. result += "exports." + e.id() + " = " + code + ";\n";
  31. }
  32. }
  33. return result;
  34. }
  35. });
  36. var RtlCodeUsingWatcher = Rtl.Code.extend({
  37. init: function(){
  38. Rtl.Code.prototype.init.call(this);
  39. this.__used = false;
  40. },
  41. get: function(func){
  42. this.__used = true;
  43. return Rtl.Code.prototype.get.call(this, func);
  44. },
  45. used: function(){return this.__used;},
  46. reset: function(){this.__used = false;}
  47. });
  48. function writeCompiledModule(name, code, outDir){
  49. var filePath = path.join(outDir, name + ".js");
  50. fs.writeFileSync(filePath, code);
  51. }
  52. function compile(sources, handleErrors, outDir){
  53. var rtlCodeWatcher = new RtlCodeUsingWatcher();
  54. var rtl = new Rtl.RTL(rtlCodeWatcher);
  55. var moduleCode = function(name, imports){return new ModuleGenerator(name, imports);};
  56. var compiledFilesStack = [];
  57. oc.compileModules(
  58. sources,
  59. function(name){
  60. var fileName = name;
  61. if (!path.extname(fileName).length)
  62. fileName += ".ob";
  63. compiledFilesStack.push(fileName);
  64. return fs.readFileSync(fileName, "utf8");
  65. },
  66. function(moduleResolver){return new Context.Context(
  67. new Code.Generator(),
  68. moduleCode,
  69. rtl,
  70. moduleResolver);},
  71. function(e){handleErrors("File \"" + compiledFilesStack[compiledFilesStack.length - 1] + "\", " + e);},
  72. function(name, code){
  73. if (rtlCodeWatcher.used()){
  74. code = "var " + rtl.name() + " = require(\"" + rtl.name()
  75. + ".js\")." + rtl.name() + ";\n" + code;
  76. rtlCodeWatcher.reset();
  77. }
  78. writeCompiledModule(name, code, outDir);
  79. compiledFilesStack.pop();
  80. });
  81. var rtlCode = rtl.generate();
  82. if (rtlCode){
  83. rtlCode += "\nexports." + rtl.name() + " = " + rtl.name() + ";";
  84. writeCompiledModule(rtl.name(), rtlCode, outDir);
  85. }
  86. }
  87. exports.compile = compile;