Permalink
Browse files

CLJS-54: Added example using extern'd functions for use by the GCloju…

…re compiler
  • Loading branch information...
1 parent aae631e commit a788e0cf97902f49d244c1f8572fcc3c9e96a10e @fogus fogus committed Sep 23, 2011
View
@@ -12,22 +12,42 @@ One-time Setup
- Add $CLOJURESCRIPT_HOME/bin to your PATH.
+
+Simple external JavaScript file
+===============================
+
+
+
Run in Development Mode
-=======================
+-----------------------
Development mode allows for each file to be loaded in a separate script tag so
that errors can be easily tracked to the offending file.
- cljsc src > hello-js.js
+ cljsc src > hello-js.js
After running the above command, open hello-js-dev.html. Notice that each required
JavaScript file has been loaded in its most readable form.
Run Optimized JavaScript
-========================
+------------------------
Once an application is ready for production, a single optimized file can be produced.
cljsc src {:optimizations :advanced} > hello-js.js
After running the above command, open hello-js.html to view the result.
+
+
+Using an externed JavaScript file
+=================================
+
+To see how external calls are optimized away, execute the following:
+
+ cljsc src '{:optimizations :advanced :output-to "hello-extern.js"}'
+
+After running the above command, open hello-extern.html to view the result. You should see nothing and possibly a JavaScript error. The solution is to compile the ClojureScript source with a referred externs file as follows:
+
+ cljsc src '{:optimizations :advanced :output-to "hello-extern.js" :externs ["externs.js"]}'
+
+Again after running the above command, open hello-extern.html to view the result. You should see an alert box.
@@ -0,0 +1,7 @@
+external = {};
+
+external.lib = {
+ send_alert : function (msg) {
+ alert("Sending Alert via " + msg + "!");
+ }
+};
@@ -0,0 +1,3 @@
+var external = {};
+external.lib = {};
+external.lib.send_alert = function() {};
@@ -0,0 +1,14 @@
+<html>
+ <head>
+ <title>Hello Externed JavaScript Library</title>
+ </head>
+ <body>
+ <h1>Hello Externed JavaScript Library!</h1>
+ <script type="text/javascript" src="out/goog/base.js"></script>
+ <script type="text/javascript" src="externed-lib.js"></script>
+ <script type="text/javascript" src="hello-extern.js"></script>
+ <script>
+ goog.require('hello.extern_example');
+ </script>
+ </body>
+</html>
@@ -0,0 +1,5 @@
+(ns hello.extern-example)
+
+(defn ^:export foo [] 42)
+
+(external.lib/send_alert "ClojureScript calling a function defined in an externed JavaScript library")

0 comments on commit a788e0c

Please sign in to comment.