Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / tests / support / babel.js
diff --git a/skyquake/tests/support/babel.js b/skyquake/tests/support/babel.js
new file mode 100644 (file)
index 0000000..c8f0b73
--- /dev/null
@@ -0,0 +1,46 @@
+define([
+    'intern/dojo/request',
+    'intern/dojo/node!babel-core',
+    'intern/dojo/node!react'
+], function (request, babel, react) {
+    /**
+     * React has AMD support so when require is present it will behave as a module
+     * The react example however expects a global React so we need to put it back
+     * into global space.
+     */
+    function globalizeReact() {
+        var global = (function () {
+            return this;
+        })();
+        global.React = global.React || react;
+    }
+
+    return {
+        /**
+         * A function that is called to load a resource.
+         *
+         * @param name The name of the resource to load.
+         * @param req A local "require" function to use to load other modules.
+         * @param onload A function to call with the value for name. This tells the loader that the plugin is done
+         *        loading the resource. onload.error() can be called, passing an error object to it, if the plugin
+         *        detects an error condition that means the resource will fail to load correctly.
+         */
+        load: function (name, req, onload) {
+            globalizeReact();
+
+            request(req.toUrl(name)).then(function (sourceCode) {
+                // Compile the JSX source into JavaScript code
+                var javascriptCode = babel.transform(sourceCode,{presets:['es2015', 'react']}).code;
+
+                // Execute the compiled function. In this case the example code
+                // puts things into the global space so it needs to be run in a script tag.
+                var codeNode = document.createTextNode(javascriptCode);
+                var node = document.createElement('script');
+                node.type = 'text/javascript';
+                node.appendChild(codeNode);
+                document.head.appendChild(node);
+                onload();
+            });
+        }
+    };
+});