Last active
June 6, 2016 04:22
-
-
Save andrewmcnamara/b0c608867a92b22784ae2ab999925133 to your computer and use it in GitHub Desktop.
Run React/Redux with in browser transform. Based on http://codetheory.in/babel-6-and-above-in-browser/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>React Example</title> | |
| <meta name="description" content="A React example made with HyperDev"> | |
| <link id="favicon" rel="icon" href="https://hyperweb.space/favicon.ico" type="image/x-icon"> | |
| <meta charset="utf-8"> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <link rel="stylesheet" href="/style.css"> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.7.7/babel.min.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.5/redux.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/4.0.6/react-redux.js"></script> | |
| <script type="text/babel" src="/app.jsx"></script> | |
| </head> | |
| <body> | |
| <div id="app"></div> | |
| <script> | |
| // Get all the babel scripts | |
| var babel_scripts = document.querySelectorAll('script[type="text/babel"]'); | |
| // Loop over them | |
| babel_scripts.forEach(function (script, index, arr) { | |
| var request = new XMLHttpRequest(); | |
| request.onload = function () { | |
| transformScript(request.responseText) | |
| }; | |
| request.open('GET', script.src, true); | |
| request.send(null); | |
| }); | |
| function transformScript(text) { | |
| console.log(text); | |
| var output = Babel.transform(text, {presets: ['es2015', 'stage-0', 'react']}).code; | |
| // Create a new script tag of `javascript` type | |
| var new_script = document.createElement('script'); | |
| new_script.setAttribute('type', 'text/javascript'); | |
| // Set the contents of the script to the transpiled code | |
| new_script.textContent = output; | |
| // Remove the babel script | |
| script.remove(); | |
| // Inject the new transpiled JS | |
| document.querySelector('body').appendChild(new_script); | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment