Skip to content

Commit

Permalink
Should be compatible with r.js now.
Browse files Browse the repository at this point in the history
  • Loading branch information
seiffert committed Jun 3, 2013
1 parent b410002 commit ba31256
Showing 1 changed file with 45 additions and 31 deletions.
76 changes: 45 additions & 31 deletions jsx.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(['JSXTransformer', 'react'], function (JSXTransformer, react) {
define(['JSXTransformer'], function (JSXTransformer) {
'use strict';

var fs, getXhr,
Expand All @@ -8,42 +8,52 @@ define(['JSXTransformer', 'react'], function (JSXTransformer, react) {
},
buildMap = {};

getXhr = function () {
var xhr, i, progId;
if (typeof XMLHttpRequest !== "undefined") {
return new XMLHttpRequest();
} else {
for (i = 0; i < 3; i += 1) {
progId = progIds[i];
try {
xhr = new ActiveXObject(progId);
} catch (e) {}
if (typeof process !== "undefined" &&
process.versions &&
!!process.versions.node) {
//Using special require.nodeRequire, something added by r.js.
fs = require.nodeRequire('fs');
fetchText = function (path, callback) {
callback(fs.readFileSync(path, 'utf8'));
};
} else {
getXhr = function () {
var xhr, i, progId;
if (typeof XMLHttpRequest !== "undefined") {
return new XMLHttpRequest();
} else {
for (i = 0; i < 3; i += 1) {
progId = progIds[i];
try {
xhr = new ActiveXObject(progId);
} catch (e) {}

if (xhr) {
progIds = [progId]; // so faster next time
break;
if (xhr) {
progIds = [progId]; // so faster next time
break;
}
}
}
}

if (!xhr) {
throw new Error("getXhr(): XMLHttpRequest not available");
}
if (!xhr) {
throw new Error("getXhr(): XMLHttpRequest not available");
}

return xhr;
};
return xhr;
};

fetchText = function (url, callback) {
var xhr = getXhr();
xhr.open('GET', url, true);
xhr.onreadystatechange = function (evt) {
if (xhr.readyState === 4) {
callback(xhr.responseText);
}
fetchText = function (url, callback) {
var xhr = getXhr();
xhr.open('GET', url, true);
xhr.onreadystatechange = function (evt) {
if (xhr.readyState === 4) {
callback(xhr.responseText);
}
};
xhr.send(null);
};
xhr.send(null);
};

}

return {
write: function (pluginName, name, write) {
if (buildMap.hasOwnProperty(name)) {
Expand All @@ -59,7 +69,11 @@ define(['JSXTransformer', 'react'], function (JSXTransformer, react) {
fetchText(path, function (text) {
try {
if (-1 === text.indexOf('React.DOM')) {
text = '/** @jsx React.DOM */' + text;
text =
'/**' + "\n"
+ ' * @jsx React.DOM' + "\n"
+ ' */' + "\n"
+ text;
}

text = JSXTransformer.transform(text).code;
Expand Down

0 comments on commit ba31256

Please sign in to comment.