| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 1 | define([ |
| 2 | 'intern/dojo/request', |
| 3 | 'intern/dojo/node!babel-core', |
| 4 | 'intern/dojo/node!react' |
| 5 | ], function (request, babel, react) { |
| 6 | /** |
| 7 | * React has AMD support so when require is present it will behave as a module |
| 8 | * The react example however expects a global React so we need to put it back |
| 9 | * into global space. |
| 10 | */ |
| 11 | function globalizeReact() { |
| 12 | var global = (function () { |
| 13 | return this; |
| 14 | })(); |
| 15 | global.React = global.React || react; |
| 16 | } |
| 17 | |
| 18 | return { |
| 19 | /** |
| 20 | * A function that is called to load a resource. |
| 21 | * |
| 22 | * @param name The name of the resource to load. |
| 23 | * @param req A local "require" function to use to load other modules. |
| 24 | * @param onload A function to call with the value for name. This tells the loader that the plugin is done |
| 25 | * loading the resource. onload.error() can be called, passing an error object to it, if the plugin |
| 26 | * detects an error condition that means the resource will fail to load correctly. |
| 27 | */ |
| 28 | load: function (name, req, onload) { |
| 29 | globalizeReact(); |
| 30 | |
| 31 | request(req.toUrl(name)).then(function (sourceCode) { |
| 32 | // Compile the JSX source into JavaScript code |
| 33 | var javascriptCode = babel.transform(sourceCode,{presets:['es2015', 'react']}).code; |
| 34 | |
| 35 | // Execute the compiled function. In this case the example code |
| 36 | // puts things into the global space so it needs to be run in a script tag. |
| 37 | var codeNode = document.createTextNode(javascriptCode); |
| 38 | var node = document.createElement('script'); |
| 39 | node.type = 'text/javascript'; |
| 40 | node.appendChild(codeNode); |
| 41 | document.head.appendChild(node); |
| 42 | onload(); |
| 43 | }); |
| 44 | } |
| 45 | }; |
| 46 | }); |