configNode.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. exports.config = function(config){
  2. // summary:
  3. // This module provides bootstrap configuration for running dojo in node.js
  4. // any command line arguments with the load flag are pushed into deps
  5. for(var deps = [], args = [], i = 0; i < process.argv.length; i++){
  6. var arg = (process.argv[i] + "").split("=");
  7. if(arg[0] == "load"){
  8. deps.push(arg[1]);
  9. }else{
  10. args.push(arg);
  11. }
  12. }
  13. var fs = require("fs");
  14. // make sure global require exists
  15. //if (typeof global.require=="undefined") {
  16. // global.require= {};
  17. //}
  18. // reset the has cache with node-appropriate values;
  19. var hasCache = {
  20. "host-node":1,
  21. "host-browser":0,
  22. "dom":0,
  23. "dojo-has-api":1,
  24. "dojo-xhr-factory":0,
  25. "dojo-inject-api":1,
  26. "dojo-timeout-api":0,
  27. "dojo-trace-api":1,
  28. "dojo-dom-ready-api":0,
  29. "dojo-publish-privates":1,
  30. "dojo-sniff":0,
  31. "dojo-loader":1,
  32. "dojo-test-xd":0,
  33. "dojo-test-sniff":0
  34. };
  35. for(var p in hasCache){
  36. config.hasCache[p] = hasCache[p];
  37. }
  38. var vm = require('vm');
  39. // reset some configuration switches with node-appropriate values
  40. var nodeConfig = {
  41. baseUrl: __dirname.match(/(.+)[\/\\]_base$/)[1],
  42. commandLineArgs:args,
  43. deps:deps,
  44. timeout:0,
  45. // TODO: really get the locale
  46. locale:"en-us",
  47. loaderPatch: {
  48. log:function(item){
  49. // define debug for console messages during dev instead of console.log
  50. // (node's heavy async makes console.log confusing sometimes)
  51. var util = require("util");
  52. util.debug(util.inspect(item));
  53. },
  54. eval: function(__text, __urlHint){
  55. return vm.runInThisContext(__text, __urlHint);
  56. },
  57. injectUrl: function(url, callback){
  58. try{
  59. vm.runInThisContext(fs.readFileSync(url, "utf8"), url);
  60. callback();
  61. }catch(e){
  62. this.log("failed to load resource (" + url + ")");
  63. this.log(e);
  64. }
  65. },
  66. getText: function(url, sync, onLoad){
  67. // TODO: implement async and http/https handling
  68. onLoad(fs.readFileSync(url, "utf8"));
  69. }
  70. }
  71. };
  72. for(p in nodeConfig){
  73. config[p] = nodeConfig[p];
  74. }
  75. };