Last active
March 6, 2021 15:32
-
-
Save kenichi-odo/9fd4c1ee114082302df3f502c3b4bac1 to your computer and use it in GitHub Desktop.
RequireJS + babel-standalone + JSX ref: http://qiita.com/kenichi_odo/items/6767aa077585e395134d
This file contains 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> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js"></script> | |
<script type="text/babel"> | |
// JSXをtext/babel内に書く | |
define('main', ['react', 'react_dom'], function(React, ReactDOM) { | |
var Main = function() {}; | |
Main.prototype.load = function() { | |
ReactDOM.render(<span>Hello World</span>, document.getElementById('container')); | |
}; | |
return Main; | |
}); | |
</script> | |
<script> | |
requirejs.config({ | |
paths: { | |
babel: 'https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.7.7/babel.min', | |
react: 'https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min', | |
react_dom: 'https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min', | |
} | |
}); | |
// まずbabelをロードする | |
require(['babel'], function(Babel) { | |
// text/babel部分を取得(複数あればその分だけ) | |
var babel_scripts = document.querySelectorAll('script[type="text/babel"]'); | |
Array.prototype.forEach.call(babel_scripts, function(script) { | |
// JSXを変換してロードする | |
new Function(Babel.transform(script.textContent, { | |
presets: ['es2015', 'react'], | |
filename: 'embedded', | |
sourceMaps: 'inline' | |
}).code)(); | |
}); | |
// Babelでロードしたdefineの呼び出し | |
require(['main'], function(Main) { | |
// ロードする | |
var main = new Main(); | |
main.load(); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="container"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment