PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/loaders/blanket_cs.js

https://github.com/geekdave/blanket
JavaScript | 183 lines | 126 code | 24 blank | 33 comment | 23 complexity | f921cc05b159fbde579985e6afdaf6d9 MD5 | raw file
Possible License(s): MIT
  1. (function(_blanket){
  2. /**
  3. * @license cs 0.4.3 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
  4. * Available via the MIT or new BSD license.
  5. * see: http://github.com/jrburke/require-cs for details
  6. */
  7. /*jslint */
  8. /*global define, window, XMLHttpRequest, importScripts, Packages, java,
  9. ActiveXObject, process, require */
  10. define("cs", ['coffee-script'], function (CoffeeScript) {
  11. 'use strict';
  12. var fs, getXhr,
  13. progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'],
  14. fetchText = function () {
  15. throw new Error('Environment unsupported.');
  16. },
  17. buildMap = {};
  18. if (typeof process !== "undefined" &&
  19. process.versions &&
  20. !!process.versions.node) {
  21. //Using special require.nodeRequire, something added by r.js.
  22. fs = require.nodeRequire('fs');
  23. fetchText = function (path, callback) {
  24. callback(fs.readFileSync(path, 'utf8'));
  25. };
  26. } else if ((typeof window !== "undefined" && window.navigator && window.document) || typeof importScripts !== "undefined") {
  27. // Browser action
  28. getXhr = function () {
  29. //Would love to dump the ActiveX crap in here. Need IE 6 to die first.
  30. var xhr, i, progId;
  31. if (typeof XMLHttpRequest !== "undefined") {
  32. return new XMLHttpRequest();
  33. } else {
  34. for (i = 0; i < 3; i += 1) {
  35. progId = progIds[i];
  36. try {
  37. xhr = new ActiveXObject(progId);
  38. } catch (e) {}
  39. if (xhr) {
  40. progIds = [progId]; // so faster next time
  41. break;
  42. }
  43. }
  44. }
  45. if (!xhr) {
  46. throw new Error("getXhr(): XMLHttpRequest not available");
  47. }
  48. return xhr;
  49. };
  50. fetchText = function (url, callback) {
  51. var xhr = getXhr();
  52. xhr.open('GET', url, true);
  53. xhr.onreadystatechange = function (evt) {
  54. //Do not explicitly handle errors, those should be
  55. //visible via console output in the browser.
  56. if (xhr.readyState === 4) {
  57. callback(xhr.responseText);
  58. }
  59. };
  60. xhr.send(null);
  61. };
  62. // end browser.js adapters
  63. } else if (typeof Packages !== 'undefined') {
  64. //Why Java, why is this so awkward?
  65. fetchText = function (path, callback) {
  66. var stringBuffer, line,
  67. encoding = "utf-8",
  68. file = new java.io.File(path),
  69. lineSeparator = java.lang.System.getProperty("line.separator"),
  70. input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
  71. content = '';
  72. try {
  73. stringBuffer = new java.lang.StringBuffer();
  74. line = input.readLine();
  75. // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
  76. // http://www.unicode.org/faq/utf_bom.html
  77. // Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK:
  78. // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
  79. if (line && line.length() && line.charAt(0) === 0xfeff) {
  80. // Eat the BOM, since we've already found the encoding on this file,
  81. // and we plan to concatenating this buffer with others; the BOM should
  82. // only appear at the top of a file.
  83. line = line.substring(1);
  84. }
  85. stringBuffer.append(line);
  86. while ((line = input.readLine()) !== null) {
  87. stringBuffer.append(lineSeparator);
  88. stringBuffer.append(line);
  89. }
  90. //Make sure we return a JavaScript string and not a Java string.
  91. content = String(stringBuffer.toString()); //String
  92. } finally {
  93. input.close();
  94. }
  95. callback(content);
  96. };
  97. }
  98. return {
  99. fetchText: fetchText,
  100. get: function () {
  101. return CoffeeScript;
  102. },
  103. write: function (pluginName, name, write) {
  104. if (buildMap.hasOwnProperty(name)) {
  105. var text = buildMap[name];
  106. write.asModule(pluginName + "!" + name, text);
  107. }
  108. },
  109. version: '0.4.3',
  110. load: function (name, parentRequire, load, config) {
  111. var path = parentRequire.toUrl(name + '.coffee');
  112. _blanket.requiringFile(path);
  113. var handleText = function(text) {
  114. //Hold on to the transformed text if a build.
  115. if (config.isBuild) {
  116. buildMap[name] = text;
  117. }
  118. //IE with conditional comments on cannot handle the
  119. //sourceURL trick, so skip it if enabled.
  120. /*@if (@_jscript) @else @*/
  121. if (!config.isBuild) {
  122. text += "\r\n//@ sourceURL=" + path;
  123. }
  124. /*@end@*/
  125. load.fromText(name, text);
  126. //Give result to load. Need to wait until the module
  127. //is fully parse, which will happen after this
  128. //execution.
  129. parentRequire([name], function (value) {
  130. load(value);
  131. });
  132. };
  133. fetchText(path, function (text) {
  134. //Do CoffeeScript transform.
  135. try {
  136. text = CoffeeScript.compile(text, config.CoffeeScript);
  137. } catch (err) {
  138. err.message = "In " + path + ", " + err.message;
  139. throw err;
  140. }
  141. _blanket.requiringFile(path,true);
  142. // If this file matches the blanket filter, instrument it.
  143. var match = _blanket.options("filter");
  144. if (_blanket.utils.matchPatternAttribute(path.replace(".coffee",""),match)){
  145. _blanket.instrument({
  146. inputFile: text,
  147. inputFileName: path+" (compiled)"
  148. },function(instrumented){
  149. handleText(instrumented);
  150. });
  151. }
  152. else {
  153. handleText(text);
  154. }
  155. });
  156. }
  157. };
  158. });
  159. })(blanket);