PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/blanketRequire.js

https://github.com/morkai/blanket
JavaScript | 124 lines | 120 code | 4 blank | 0 comment | 0 complexity | b63405232f4fa0e8e4554af498c78dcf MD5 | raw file
Possible License(s): MIT
  1. var commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
  2. defineRegExp = /(^|[^\.])define\s*\(/,
  3. requireRegExp = /(^|[^\.])require\s*\(\s*['"][^'"]+['"]\s*\)/,
  4. exportsRegExp = /exports\s*=\s*/,
  5. sourceUrlRegExp = /\/\/@\s+sourceURL=/;
  6. var blanketEval = function(data){
  7. return ( window.execScript || function( data ) {
  8. //borrowed from jquery
  9. window[ "eval" ].call( window, data );
  10. } )( data );
  11. };
  12. //if requirejs is already being used, we need to have a plugin tells us whether or not to overload this
  13. var oldloader = requirejs.load;
  14. requirejs.load = function (context, moduleName, url) {
  15. var hasLocation = typeof location !== 'undefined' && location.href,
  16. defaultHostName = hasLocation && location.hostname,
  17. defaultPort = hasLocation && (location.port || undefined),
  18. defaultProtocol = hasLocation && location.protocol && location.protocol.replace(/\:/, '');
  19. requirejs.cget(url, function (content) {
  20. var match = blanket.loadOnly;
  21. if (url.indexOf(match) > -1){
  22. var temp = content.replace(commentRegExp, '');
  23. blanket.instrument({
  24. inputFile: content,
  25. inputFileName: url
  26. },function(instrumented){
  27. //console.log("instrumented:\n"+instrumented);
  28. try{
  29. blanketEval(instrumented);
  30. context.completeLoad(moduleName);
  31. }
  32. catch(err){
  33. console.log("Error parsing instrumented code: "+err);
  34. }
  35. });
  36. }else{
  37. oldloader(context, moduleName, url);
  38. }
  39. }, function (err) {
  40. throw err;
  41. });
  42. };
  43. requirejs.createXhr = function () {
  44. //Would love to dump the ActiveX crap in here. Need IE 6 to die first.
  45. var xhr, i, progId;
  46. if (typeof XMLHttpRequest !== "undefined") {
  47. return new XMLHttpRequest();
  48. } else if (typeof ActiveXObject !== "undefined") {
  49. for (i = 0; i < 3; i += 1) {
  50. progId = progIds[i];
  51. try {
  52. xhr = new ActiveXObject(progId);
  53. } catch (e) {}
  54. if (xhr) {
  55. progIds = [progId]; // so faster next time
  56. break;
  57. }
  58. }
  59. }
  60. return xhr;
  61. };
  62. requirejs.cget = function (url, callback, errback, onXhr) {
  63. var xhr = requirejs.createXhr();
  64. xhr.open('GET', url, true);
  65. //Allow overrides specified in config
  66. if (onXhr) {
  67. onXhr(xhr, url);
  68. }
  69. xhr.onreadystatechange = function (evt) {
  70. var status, err;
  71. //Do not explicitly handle errors, those should be
  72. //visible via console output in the browser.
  73. if (xhr.readyState === 4) {
  74. status = xhr.status;
  75. if (status > 399 && status < 600) {
  76. //An http 4xx or 5xx error. Signal an error.
  77. err = new Error(url + ' HTTP status: ' + status);
  78. err.xhr = xhr;
  79. errback(err);
  80. } else {
  81. callback(xhr.responseText);
  82. }
  83. }
  84. };
  85. xhr.send(null);
  86. };
  87. function collectPageScripts(){
  88. var toArray = Array.prototype.slice;
  89. var scripts = toArray.call(document.scripts);
  90. var scriptNames = scripts.filter(function(elem){
  91. return toArray.call(elem.attributes).some(function(es){
  92. return es.nodeName == "data-cover";
  93. });
  94. }).map(function(s){
  95. return toArray.call(s.attributes).filter(function(sn){
  96. return sn.nodeName == "src";
  97. })[0].nodeValue.replace(".js","");
  98. });
  99. return scriptNames;
  100. }