Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Created October 25, 2017 22:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save JosePedroDias/e2e96257affa224e6aecda6229966bc4 to your computer and use it in GitHub Desktop.
You want to serve a browser bundle with Reach, superagent and parse jsx scripts with type="text/jsx"? Install and run `npm run bundle`. You can expose additional content in the window too.
window.React = require('react');
window.ReactDOM = require('react-dom');
window.superagent = require('superagent');
(function() {
'use strict';
const jsx = require('jsx-transform');
const jsxScriptEls = Array.prototype.slice.apply( document.body.querySelectorAll('script[type="text/jsx"]') );
jsxScriptEls.forEach(scriptEl => {
window.superagent(scriptEl.src).end((err, res) => {
if (err || !res.ok) { throw err; }
const code = jsx.fromString(res.text, {factory:'React.createElement', passUnknownTagsToFactory:true, arrayChildren:false});
eval(code);
});
});
})();
{
"scripts": {
"bundle": "browserify bootstrap.js > bundle.js"
},
"devDependencies": {
"browserify": "^14.5.0"
},
"dependencies": {
"jsx-transform": "^2.3.0",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"superagent": "^3.7.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment