Skip to content

Commit c79a59b

Browse files
committed
Improve JSXTransformer
The biggest improvement is that we'll now insert each parsed JSX script back into a `<script>` tag with the body set. This allows the browser to execute these scripts normally. Using `Function(functionBody)` or `eval(functionBody)` both execute in window scope, but `var` assignments don't actually get set on window (unlike everywhere else). I also did some cleanup to make the code a little bit more readable. In my minimal test cases this didn't break anything (scripts loaded in the right order).
1 parent fad7d58 commit c79a59b

File tree

1 file changed

+25
-41
lines changed

1 file changed

+25
-41
lines changed

vendor/browser-transforms.js

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ var visitors = require('./fbtransform/visitors').transformVisitors;
2424
var transform = transform.bind(null, visitors.react);
2525
var docblock = require('./fbtransform/lib/docblock');
2626

27+
var headEl = document.getElementsByTagName('head')[0];
28+
2729
exports.transform = transform;
30+
2831
exports.exec = function(code) {
2932
return eval(transform(code));
3033
};
34+
3135
var run = exports.run = function(code) {
32-
var moduleName =
33-
docblock.parseAsObject(docblock.extract(code)).providesModule;
34-
var jsx =
35-
docblock.parseAsObject(docblock.extract(code)).jsx;
36+
var jsx = docblock.parseAsObject(docblock.extract(code)).jsx;
3637

37-
window.moduleLoads = (window.moduleLoads || []).concat(moduleName);
38-
window.startTime = Date.now();
3938
var functionBody = jsx ? transform(code).code : code;
40-
Function('require', 'module', 'exports', functionBody)(require, module, exports);
41-
window.endTime = Date.now();
42-
require[moduleName] = module.exports;
39+
var scriptEl = document.createElement('script');
40+
41+
scriptEl.innerHTML = functionBody;
42+
headEl.appendChild(scriptEl);
4343
};
4444

4545
if (typeof window === "undefined" || window === null) {
@@ -48,15 +48,17 @@ if (typeof window === "undefined" || window === null) {
4848

4949
var load = exports.load = function(url, callback) {
5050
var xhr;
51-
xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
51+
xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP')
52+
: new XMLHttpRequest();
53+
// Disable async since we need to execute scripts in the order they are in the
54+
// DOM to mirror normal script loading.
5255
xhr.open('GET', url, false);
5356
if ('overrideMimeType' in xhr) {
5457
xhr.overrideMimeType('text/plain');
5558
}
5659
xhr.onreadystatechange = function() {
57-
var _ref;
5860
if (xhr.readyState === 4) {
59-
if ((_ref = xhr.status) === 0 || _ref === 200) {
61+
if (xhr.status === 0 || xhr.status === 200) {
6062
run(xhr.responseText);
6163
} else {
6264
throw new Error("Could not load " + url);
@@ -70,37 +72,19 @@ var load = exports.load = function(url, callback) {
7072
};
7173

7274
runScripts = function() {
73-
var jsxes, execute, index, length, s, scripts;
74-
scripts = document.getElementsByTagName('script');
75-
jsxes = (function() {
76-
var _i, _len, _results;
77-
_results = [];
78-
for (_i = 0, _len = scripts.length; _i < _len; _i++) {
79-
s = scripts[_i];
80-
if (s.type === 'text/jsx') {
81-
_results.push(s);
82-
}
83-
}
84-
return _results;
85-
})();
86-
index = 0;
87-
length = jsxes.length;
88-
(execute = function(j) {
89-
var script;
90-
script = jsxes[j];
91-
if ((script != null ? script.type : void 0) === 'text/jsx') {
92-
if (script.src) {
93-
return load(script.src, execute);
94-
} else {
95-
run(script.innerHTML);
96-
return execute();
97-
}
75+
var scripts = document.getElementsByTagName('script');
76+
scripts = Array.prototype.slice.call(scripts);
77+
var jsxScripts = scripts.filter(function(script) {
78+
return script.type === 'text/jsx';
79+
});
80+
81+
jsxScripts.forEach(function(script) {
82+
if (script.src) {
83+
load(script.src);
84+
} else {
85+
run(script.innerHTML);
9886
}
9987
});
100-
for (var i = 0; i < jsxes.length; i++) {
101-
execute(i);
102-
}
103-
return null;
10488
};
10589

10690
if (window.addEventListener) {

0 commit comments

Comments
 (0)