SlideShare une entreprise Scribd logo
1  sur  58
Télécharger pour lire hors ligne
getting started with cljs
         - Siva Jagadeesan




Monday, February 11, 13              1
About me

               Siva Jagadeesan
               Zolodeck :: Clojure + Storm + Datomic + CLJS
               @sivajag
               siva@zololabs.com




Monday, February 11, 13                                       2
What we will cover today

               Compiling ClojureScript
               Javascript Interop
               Setting up development Environment
               Building a simple Web Application using ClojureScript
               and Clojure




Monday, February 11, 13                                                3
Why ClojureScript?




Monday, February 11, 13       4
Why JavaScript?

               Web 2.0 / Web 3.0
               It is lightweight
               It is flexible
               Functions are first class
               It is in every browser




Monday, February 11, 13                   5
Why not Javascript?




Monday, February 11, 13        6
JavaScript as a Compilation Target


               Tools
                     Google Web Toolkit
               Languages
                     CoffeeScript
                     Dart




Monday, February 11, 13                       7
“ClojureScript is a new compiler for
         Clojure that targets JavaScript”

Monday, February 11, 13                         8
Why ClojureScript?




Monday, February 11, 13       9
ClojureScript - Design

                          Clojure   ClojureScript




                           JVM       JavaScript



Monday, February 11, 13                             10
ClojureScript - Compiler
                                 ClojureScript


               No Minification    ClojureScript
               No Optimization     Compiler




                                  JavaScript

Monday, February 11, 13                          11
ClojureScript - Compiler



               Uses Google Closure Compiler for minification and
               optimization




Monday, February 11, 13                                           12
Google Closure Compiler - Optimization




               Whitespace Only
               Simple Optimization
               Advanced Optimization




Monday, February 11, 13                           13
ClojureScript and Google Closure

                          ClojureScript
                                          Optimized
                                          JavaScript
                          ClojureScript
                            Compiler



                                          Google Closure
                           JavaScript       Compiler



Monday, February 11, 13                                    14
Hello World



               lein new cljspress




Monday, February 11, 13             15
Project Structure




Monday, February 11, 13      16
Edit project.clj




Monday, February 11, 13     17
Create src/cljs/cljspress/client.cljs




Monday, February 11, 13                          18
Create resources/public/index.html




Monday, February 11, 13                       19
Compile


               lein repl
               (require 'cljs.closure)
               (cljs.closure/build "src/cljs"
                     {:output-to "resources/public/cljspress.js"
                          :optimizations :advanced})




Monday, February 11, 13                                            20
Compilation Demystified




                          (cljs.closure/build source options-map)




Monday, February 11, 13                                             21
Compilation Demystified - Options


               :optimizations

               :output-to

               :output-dir

               :pretty-print

               :target

               :libs

               :foreign-libs

               :externs



Monday, February 11, 13                     22
Compilation Demystified - Options

         :optimizations



               :none

               :whitespace

               :simple

               :advanced




Monday, February 11, 13                     23
Compilation Demystified - Options

         :output-to & :ouput-dir

                          ClojureScript   :output-to
                                          Optimized
                                          JavaScript
                          ClojureScript
                            Compiler



                                          Google Closure
                           JavaScript       Compiler
                           :output-dir
Monday, February 11, 13                                    24
Loading Optimized Code


    <script src="cljspress.js" type="text/javascript"></script>


    <script>
       cljspress.client.main();
    </script>




Monday, February 11, 13                                           25
Loading Unoptimized Code

   <script src="js/goog/base.js"></script>
   <script src="cljspress.js"></script>
   <script> goog.require('cljspress.client'); </script>
   <script> cljspress.client.main(); </script>

               loads the Google Closure Library from goog/base.js

               loads the dependency information for your application

               load your application

               launches your application




Monday, February 11, 13                                                26
Compilation Demystified - Options

         :pretty-print




               Production - {:optimizations :advanced}

               Development - {:optimizations :whitespace :pretty-print true}




Monday, February 11, 13                                                        27
Hello World ... The Date is ...




Monday, February 11, 13                    28
Edit src/cljs/cljspress/client.cljs




Monday, February 11, 13                        29
Edit src/cljs/cljspress/client.cljs
              n       g
           ro
          W




Monday, February 11, 13                        30
JavaScript Interop

               The js namespace

               Methods and Fields

               Constructor Functions

               Scope of this

               Exceptions




Monday, February 11, 13                31
JavaScript Interop
         The js namespace

               Javascript does not have a concept of namespace

               People use Java Objects as “modules”

               ClojureScript has built-in support for namespaces

               js namespace refers to JavaScript Global namespace




Monday, February 11, 13                                             32
JavaScript Interop
         Methods and Fields

               (def m “Hello World”)

               (def l (.-length m)

               (def r (.join m “-”)




Monday, February 11, 13                33
JavaScript Interop
         Constructor Functions


               (def d (js/Date.))




Monday, February 11, 13             34
JavaScript Interop
         Scope of this

               (def g (Raphael/color “00ff00”))

               (def g (.color js/Raphael “00ff00”))




Monday, February 11, 13                               35
JavaScript Interop
         Exceptions


               (throw (js/Error. “Bad”))




Monday, February 11, 13                    36
Edit src/cljs/cljspress/client.cljs




Monday, February 11, 13                        37
Hello Name ... The Date is ...




Monday, February 11, 13                   38
Edit project.clj




Monday, February 11, 13     39
Create src/clj/cljspress/server.clj




Monday, February 11, 13                        40
Edit src/cljs/cljspress/client.cljs




Monday, February 11, 13                        41
Running


               Start Server

                    lein run

               Compile CLJS

               Goto http://localhost:5000/index




Monday, February 11, 13                           42
Running




Monday, February 11, 13   43
Running




Monday, February 11, 13   44
Consuming Libraries

               Clojurescript Libraries

               Javascript Google Closure Libraries

               JavaScript Plain Old Libraries

                    Compatible with Advanced mode compilation

                    Not Compatible with Advanced mode compilation




Monday, February 11, 13                                             45
Consuming Libraries
         ClojureScript Libraries

               Simple Case

               Include in Classpath

               :require or :use

               Invoke functions like normal ClojureScript Functions




Monday, February 11, 13                                               46
Consuming Libraries
         JavaScript Google Closure Libraries


               Reference the relative path in :libs compiler options

               :require or :use

               Invoke functions using JS interop form




Monday, February 11, 13                                                47
Consuming Libraries
         JavaScript Plain Old Libraries
         with Advanced Mode Compilation


               Reference the relative path and namespace in :foreign-libs compilation option

               :require or :use

               Invoke functions using JS interop form




Monday, February 11, 13                                                                        48
Consuming Libraries
         JavaScript Plain Old Libraries
         without Advanced Mode Compilation


               Create an externs file and add it to :externs compilation option

               Include the library directly as script tag in HTML

               Invoke functions using JS interop form




Monday, February 11, 13                                                          49
lein cljs-build

    “is a Leiningen plugin that makes it quick and easy to
      automatically compile your ClojureScript code into
              Javascript whenever you modify it.”




Monday, February 11, 13                                      50
Edit project.clj to include lein-cljsbuid




Monday, February 11, 13                              51
Basic commands of lein-
         cljsbuild

               lein trampoline cljsbuild once
               lein trampoline cljsbuild auto
               lein trampoline cljsbuild clean




Monday, February 11, 13                          52
Repl please



               lein trampoline cljsbuild rhino-repl




Monday, February 11, 13                               53
Even better Browser Repl

                          ClojureScript   Webpage
                             REPL




                             bREPL         bREPL
                             Server        Client

                             JVM          Browser
Monday, February 11, 13                             54
bRepl Client
         Create src/cljs/cljspress/core.cljs




Monday, February 11, 13                        55
bRepl Server and cljs REPL



               lein trampoline cljsbuild repl-listen
               Refresh the web page
               in Repl type “(js/alert "cool")”
                    You should see a alert box in your web page




Monday, February 11, 13                                           56
Recap

               All Compilation options
               Consuming Different types of libraries
               Using lein cljs-build
               Interactive Development using Browser REPL




Monday, February 11, 13                                     57
Thank you


               @sivajag
               siva@zololabs.com




Monday, February 11, 13            58

Contenu connexe

Tendances

OpenCms Days 2015 Creating Apps for the OpenCms 10 workplace
OpenCms Days 2015  Creating Apps for the OpenCms 10 workplace OpenCms Days 2015  Creating Apps for the OpenCms 10 workplace
OpenCms Days 2015 Creating Apps for the OpenCms 10 workplace Alkacon Software GmbH & Co. KG
 
OpenCms Days 2016: Next generation content repository
OpenCms Days 2016: Next generation content repository OpenCms Days 2016: Next generation content repository
OpenCms Days 2016: Next generation content repository Alkacon Software GmbH & Co. KG
 
OpenCms Days 2015: Keynote - OpenCms 10 X marks the spot
OpenCms Days 2015: Keynote - OpenCms 10 X marks the spotOpenCms Days 2015: Keynote - OpenCms 10 X marks the spot
OpenCms Days 2015: Keynote - OpenCms 10 X marks the spotOpenCms
 
Microservices: Improving the autonomy of our teams with Event-Driven Architec...
Microservices: Improving the autonomy of our teams with Event-Driven Architec...Microservices: Improving the autonomy of our teams with Event-Driven Architec...
Microservices: Improving the autonomy of our teams with Event-Driven Architec...CodelyTV
 
Creating a full stack web app with python, npm, webpack and react
Creating a full stack web app with python, npm, webpack and reactCreating a full stack web app with python, npm, webpack and react
Creating a full stack web app with python, npm, webpack and reactAngela Kristine Juvet Branaes
 
Magento 2 Development
Magento 2 DevelopmentMagento 2 Development
Magento 2 DevelopmentDuke Dao
 
WPE WebKit for Android
WPE WebKit for AndroidWPE WebKit for Android
WPE WebKit for AndroidIgalia
 
OpenCms Days 2014 - Responsive bootstrap templates reloaded
OpenCms Days 2014 - Responsive bootstrap templates reloadedOpenCms Days 2014 - Responsive bootstrap templates reloaded
OpenCms Days 2014 - Responsive bootstrap templates reloadedAlkacon Software GmbH & Co. KG
 
Avoiding the domino effect in our [micro]services (SOLID at macro-design level)
Avoiding the domino effect in our [micro]services (SOLID at macro-design level)Avoiding the domino effect in our [micro]services (SOLID at macro-design level)
Avoiding the domino effect in our [micro]services (SOLID at macro-design level)CodelyTV
 
XML-Free Programming
XML-Free ProgrammingXML-Free Programming
XML-Free ProgrammingStephen Chin
 
What's new in Java EE 7? From HTML5 to JMS 2.0
What's new in Java EE 7? From HTML5 to JMS 2.0What's new in Java EE 7? From HTML5 to JMS 2.0
What's new in Java EE 7? From HTML5 to JMS 2.0Bruno Borges
 
Introduction maven3 and gwt2.5 rc2 - Lesson 01
Introduction maven3 and gwt2.5 rc2 - Lesson 01Introduction maven3 and gwt2.5 rc2 - Lesson 01
Introduction maven3 and gwt2.5 rc2 - Lesson 01rhemsolutions
 
Visage Android Hands-on Lab (OSCON)
Visage Android Hands-on Lab (OSCON)Visage Android Hands-on Lab (OSCON)
Visage Android Hands-on Lab (OSCON)Stephen Chin
 
Java Fx Ajaxworld Rags V1
Java Fx Ajaxworld Rags V1Java Fx Ajaxworld Rags V1
Java Fx Ajaxworld Rags V1rajivmordani
 

Tendances (20)

OpenCms Days 2015 Creating Apps for the OpenCms 10 workplace
OpenCms Days 2015  Creating Apps for the OpenCms 10 workplace OpenCms Days 2015  Creating Apps for the OpenCms 10 workplace
OpenCms Days 2015 Creating Apps for the OpenCms 10 workplace
 
OpenCms Days 2016: Multilingual websites with OpenCms
OpenCms Days 2016:   Multilingual websites with OpenCmsOpenCms Days 2016:   Multilingual websites with OpenCms
OpenCms Days 2016: Multilingual websites with OpenCms
 
OpenCms Days 2013 - Bootstrap your templates
OpenCms Days 2013 - Bootstrap your templatesOpenCms Days 2013 - Bootstrap your templates
OpenCms Days 2013 - Bootstrap your templates
 
OpenCms Days 2016: Next generation content repository
OpenCms Days 2016: Next generation content repository OpenCms Days 2016: Next generation content repository
OpenCms Days 2016: Next generation content repository
 
OpenCms Days 2014 - Updating to OpenCms 9.5
OpenCms Days 2014 - Updating to OpenCms 9.5OpenCms Days 2014 - Updating to OpenCms 9.5
OpenCms Days 2014 - Updating to OpenCms 9.5
 
OpenCms Days 2015: Keynote - OpenCms 10 X marks the spot
OpenCms Days 2015: Keynote - OpenCms 10 X marks the spotOpenCms Days 2015: Keynote - OpenCms 10 X marks the spot
OpenCms Days 2015: Keynote - OpenCms 10 X marks the spot
 
Microservices: Improving the autonomy of our teams with Event-Driven Architec...
Microservices: Improving the autonomy of our teams with Event-Driven Architec...Microservices: Improving the autonomy of our teams with Event-Driven Architec...
Microservices: Improving the autonomy of our teams with Event-Driven Architec...
 
Creating a full stack web app with python, npm, webpack and react
Creating a full stack web app with python, npm, webpack and reactCreating a full stack web app with python, npm, webpack and react
Creating a full stack web app with python, npm, webpack and react
 
Magento 2 Development
Magento 2 DevelopmentMagento 2 Development
Magento 2 Development
 
WPE WebKit for Android
WPE WebKit for AndroidWPE WebKit for Android
WPE WebKit for Android
 
OpenCms Days 2015 Workflow using Docker and Jenkins
OpenCms Days 2015 Workflow using Docker and JenkinsOpenCms Days 2015 Workflow using Docker and Jenkins
OpenCms Days 2015 Workflow using Docker and Jenkins
 
OpenCms Days 2014 - Responsive bootstrap templates reloaded
OpenCms Days 2014 - Responsive bootstrap templates reloadedOpenCms Days 2014 - Responsive bootstrap templates reloaded
OpenCms Days 2014 - Responsive bootstrap templates reloaded
 
Avoiding the domino effect in our [micro]services (SOLID at macro-design level)
Avoiding the domino effect in our [micro]services (SOLID at macro-design level)Avoiding the domino effect in our [micro]services (SOLID at macro-design level)
Avoiding the domino effect in our [micro]services (SOLID at macro-design level)
 
XML-Free Programming
XML-Free ProgrammingXML-Free Programming
XML-Free Programming
 
What's new in Java EE 7? From HTML5 to JMS 2.0
What's new in Java EE 7? From HTML5 to JMS 2.0What's new in Java EE 7? From HTML5 to JMS 2.0
What's new in Java EE 7? From HTML5 to JMS 2.0
 
Introduction maven3 and gwt2.5 rc2 - Lesson 01
Introduction maven3 and gwt2.5 rc2 - Lesson 01Introduction maven3 and gwt2.5 rc2 - Lesson 01
Introduction maven3 and gwt2.5 rc2 - Lesson 01
 
Visage Android Hands-on Lab (OSCON)
Visage Android Hands-on Lab (OSCON)Visage Android Hands-on Lab (OSCON)
Visage Android Hands-on Lab (OSCON)
 
Java Fx Ajaxworld Rags V1
Java Fx Ajaxworld Rags V1Java Fx Ajaxworld Rags V1
Java Fx Ajaxworld Rags V1
 
OpenCms Days 2015 Hidden features of OpenCms
OpenCms Days 2015 Hidden features of OpenCmsOpenCms Days 2015 Hidden features of OpenCms
OpenCms Days 2015 Hidden features of OpenCms
 
drmaatutggf12
drmaatutggf12drmaatutggf12
drmaatutggf12
 

Similaire à Getting started with ClojureScript

XPages Blast - Lotusphere 2013
XPages Blast - Lotusphere 2013XPages Blast - Lotusphere 2013
XPages Blast - Lotusphere 2013Tim Clark
 
Unleashing the Rails Asset Pipeline
Unleashing the Rails Asset PipelineUnleashing the Rails Asset Pipeline
Unleashing the Rails Asset PipelineKenneth Kalmer
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJSJumping Bean
 
Использование Debug утилит в разработке под Android
Использование Debug утилит в разработке под AndroidИспользование Debug утилит в разработке под Android
Использование Debug утилит в разработке под AndroidSoftTechnics
 
Continuous Delivery at Netflix
Continuous Delivery at NetflixContinuous Delivery at Netflix
Continuous Delivery at NetflixRob Spieldenner
 
Devon 2011-f-4-improve your-javascript
Devon 2011-f-4-improve your-javascriptDevon 2011-f-4-improve your-javascript
Devon 2011-f-4-improve your-javascriptDaum DNA
 
Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)
Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)
Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)enpit GmbH & Co. KG
 
Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)
Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)
Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)Andreas Koop
 
Application Architectures in Grails
Application Architectures in GrailsApplication Architectures in Grails
Application Architectures in GrailsPeter Ledbrook
 
Use Ruby to Write (and Test) Your Next Android App
Use Ruby to Write (and Test) Your Next Android AppUse Ruby to Write (and Test) Your Next Android App
Use Ruby to Write (and Test) Your Next Android AppJoel Byler
 
Frontend Engineer Toolbox
Frontend Engineer ToolboxFrontend Engineer Toolbox
Frontend Engineer ToolboxYnon Perek
 
Cloudfoundry Overview
Cloudfoundry OverviewCloudfoundry Overview
Cloudfoundry Overviewrajdeep
 
Improving Your Heroku App Performance with Asset CDN and Unicorn
Improving Your Heroku App Performance with Asset CDN and UnicornImproving Your Heroku App Performance with Asset CDN and Unicorn
Improving Your Heroku App Performance with Asset CDN and UnicornSimon Bagreev
 
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSTek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSPablo Godel
 
Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
Java Tech & Tools | Grails in the Java Enterprise | Peter LedbrookJava Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
Java Tech & Tools | Grails in the Java Enterprise | Peter LedbrookJAX London
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppEdureka!
 
Single Page JavaScript WebApps... A Gradle Story
Single Page JavaScript WebApps... A Gradle StorySingle Page JavaScript WebApps... A Gradle Story
Single Page JavaScript WebApps... A Gradle StoryKon Soulianidis
 
Reverse engineering and instrumentation of android apps
Reverse engineering and instrumentation of android appsReverse engineering and instrumentation of android apps
Reverse engineering and instrumentation of android appsGaurav Lochan
 
An Introduction to AngularJS
An Introduction to AngularJSAn Introduction to AngularJS
An Introduction to AngularJSFalk Hartmann
 

Similaire à Getting started with ClojureScript (20)

XPages Blast - Lotusphere 2013
XPages Blast - Lotusphere 2013XPages Blast - Lotusphere 2013
XPages Blast - Lotusphere 2013
 
Unleashing the Rails Asset Pipeline
Unleashing the Rails Asset PipelineUnleashing the Rails Asset Pipeline
Unleashing the Rails Asset Pipeline
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Использование Debug утилит в разработке под Android
Использование Debug утилит в разработке под AndroidИспользование Debug утилит в разработке под Android
Использование Debug утилит в разработке под Android
 
Continuous Delivery at Netflix
Continuous Delivery at NetflixContinuous Delivery at Netflix
Continuous Delivery at Netflix
 
Devon 2011-f-4-improve your-javascript
Devon 2011-f-4-improve your-javascriptDevon 2011-f-4-improve your-javascript
Devon 2011-f-4-improve your-javascript
 
Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)
Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)
Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)
 
Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)
Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)
Deployment Best Practices on WebLogic Server (DOAG IMC Summit 2013)
 
Application Architectures in Grails
Application Architectures in GrailsApplication Architectures in Grails
Application Architectures in Grails
 
Use Ruby to Write (and Test) Your Next Android App
Use Ruby to Write (and Test) Your Next Android AppUse Ruby to Write (and Test) Your Next Android App
Use Ruby to Write (and Test) Your Next Android App
 
Frontend Engineer Toolbox
Frontend Engineer ToolboxFrontend Engineer Toolbox
Frontend Engineer Toolbox
 
Cloudfoundry Overview
Cloudfoundry OverviewCloudfoundry Overview
Cloudfoundry Overview
 
Improving Your Heroku App Performance with Asset CDN and Unicorn
Improving Your Heroku App Performance with Asset CDN and UnicornImproving Your Heroku App Performance with Asset CDN and Unicorn
Improving Your Heroku App Performance with Asset CDN and Unicorn
 
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSTek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJS
 
Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
Java Tech & Tools | Grails in the Java Enterprise | Peter LedbrookJava Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
 
Single Page JavaScript WebApps... A Gradle Story
Single Page JavaScript WebApps... A Gradle StorySingle Page JavaScript WebApps... A Gradle Story
Single Page JavaScript WebApps... A Gradle Story
 
Reverse engineering and instrumentation of android apps
Reverse engineering and instrumentation of android appsReverse engineering and instrumentation of android apps
Reverse engineering and instrumentation of android apps
 
JRubyConf 2009
JRubyConf 2009JRubyConf 2009
JRubyConf 2009
 
An Introduction to AngularJS
An Introduction to AngularJSAn Introduction to AngularJS
An Introduction to AngularJS
 

Getting started with ClojureScript

  • 1. getting started with cljs - Siva Jagadeesan Monday, February 11, 13 1
  • 2. About me Siva Jagadeesan Zolodeck :: Clojure + Storm + Datomic + CLJS @sivajag siva@zololabs.com Monday, February 11, 13 2
  • 3. What we will cover today Compiling ClojureScript Javascript Interop Setting up development Environment Building a simple Web Application using ClojureScript and Clojure Monday, February 11, 13 3
  • 5. Why JavaScript? Web 2.0 / Web 3.0 It is lightweight It is flexible Functions are first class It is in every browser Monday, February 11, 13 5
  • 6. Why not Javascript? Monday, February 11, 13 6
  • 7. JavaScript as a Compilation Target Tools Google Web Toolkit Languages CoffeeScript Dart Monday, February 11, 13 7
  • 8. “ClojureScript is a new compiler for Clojure that targets JavaScript” Monday, February 11, 13 8
  • 10. ClojureScript - Design Clojure ClojureScript JVM JavaScript Monday, February 11, 13 10
  • 11. ClojureScript - Compiler ClojureScript No Minification ClojureScript No Optimization Compiler JavaScript Monday, February 11, 13 11
  • 12. ClojureScript - Compiler Uses Google Closure Compiler for minification and optimization Monday, February 11, 13 12
  • 13. Google Closure Compiler - Optimization Whitespace Only Simple Optimization Advanced Optimization Monday, February 11, 13 13
  • 14. ClojureScript and Google Closure ClojureScript Optimized JavaScript ClojureScript Compiler Google Closure JavaScript Compiler Monday, February 11, 13 14
  • 15. Hello World lein new cljspress Monday, February 11, 13 15
  • 20. Compile lein repl (require 'cljs.closure) (cljs.closure/build "src/cljs" {:output-to "resources/public/cljspress.js" :optimizations :advanced}) Monday, February 11, 13 20
  • 21. Compilation Demystified (cljs.closure/build source options-map) Monday, February 11, 13 21
  • 22. Compilation Demystified - Options :optimizations :output-to :output-dir :pretty-print :target :libs :foreign-libs :externs Monday, February 11, 13 22
  • 23. Compilation Demystified - Options :optimizations :none :whitespace :simple :advanced Monday, February 11, 13 23
  • 24. Compilation Demystified - Options :output-to & :ouput-dir ClojureScript :output-to Optimized JavaScript ClojureScript Compiler Google Closure JavaScript Compiler :output-dir Monday, February 11, 13 24
  • 25. Loading Optimized Code <script src="cljspress.js" type="text/javascript"></script> <script> cljspress.client.main(); </script> Monday, February 11, 13 25
  • 26. Loading Unoptimized Code <script src="js/goog/base.js"></script> <script src="cljspress.js"></script> <script> goog.require('cljspress.client'); </script> <script> cljspress.client.main(); </script> loads the Google Closure Library from goog/base.js loads the dependency information for your application load your application launches your application Monday, February 11, 13 26
  • 27. Compilation Demystified - Options :pretty-print Production - {:optimizations :advanced} Development - {:optimizations :whitespace :pretty-print true} Monday, February 11, 13 27
  • 28. Hello World ... The Date is ... Monday, February 11, 13 28
  • 30. Edit src/cljs/cljspress/client.cljs n g ro W Monday, February 11, 13 30
  • 31. JavaScript Interop The js namespace Methods and Fields Constructor Functions Scope of this Exceptions Monday, February 11, 13 31
  • 32. JavaScript Interop The js namespace Javascript does not have a concept of namespace People use Java Objects as “modules” ClojureScript has built-in support for namespaces js namespace refers to JavaScript Global namespace Monday, February 11, 13 32
  • 33. JavaScript Interop Methods and Fields (def m “Hello World”) (def l (.-length m) (def r (.join m “-”) Monday, February 11, 13 33
  • 34. JavaScript Interop Constructor Functions (def d (js/Date.)) Monday, February 11, 13 34
  • 35. JavaScript Interop Scope of this (def g (Raphael/color “00ff00”)) (def g (.color js/Raphael “00ff00”)) Monday, February 11, 13 35
  • 36. JavaScript Interop Exceptions (throw (js/Error. “Bad”)) Monday, February 11, 13 36
  • 38. Hello Name ... The Date is ... Monday, February 11, 13 38
  • 42. Running Start Server lein run Compile CLJS Goto http://localhost:5000/index Monday, February 11, 13 42
  • 45. Consuming Libraries Clojurescript Libraries Javascript Google Closure Libraries JavaScript Plain Old Libraries Compatible with Advanced mode compilation Not Compatible with Advanced mode compilation Monday, February 11, 13 45
  • 46. Consuming Libraries ClojureScript Libraries Simple Case Include in Classpath :require or :use Invoke functions like normal ClojureScript Functions Monday, February 11, 13 46
  • 47. Consuming Libraries JavaScript Google Closure Libraries Reference the relative path in :libs compiler options :require or :use Invoke functions using JS interop form Monday, February 11, 13 47
  • 48. Consuming Libraries JavaScript Plain Old Libraries with Advanced Mode Compilation Reference the relative path and namespace in :foreign-libs compilation option :require or :use Invoke functions using JS interop form Monday, February 11, 13 48
  • 49. Consuming Libraries JavaScript Plain Old Libraries without Advanced Mode Compilation Create an externs file and add it to :externs compilation option Include the library directly as script tag in HTML Invoke functions using JS interop form Monday, February 11, 13 49
  • 50. lein cljs-build “is a Leiningen plugin that makes it quick and easy to automatically compile your ClojureScript code into Javascript whenever you modify it.” Monday, February 11, 13 50
  • 51. Edit project.clj to include lein-cljsbuid Monday, February 11, 13 51
  • 52. Basic commands of lein- cljsbuild lein trampoline cljsbuild once lein trampoline cljsbuild auto lein trampoline cljsbuild clean Monday, February 11, 13 52
  • 53. Repl please lein trampoline cljsbuild rhino-repl Monday, February 11, 13 53
  • 54. Even better Browser Repl ClojureScript Webpage REPL bREPL bREPL Server Client JVM Browser Monday, February 11, 13 54
  • 55. bRepl Client Create src/cljs/cljspress/core.cljs Monday, February 11, 13 55
  • 56. bRepl Server and cljs REPL lein trampoline cljsbuild repl-listen Refresh the web page in Repl type “(js/alert "cool")” You should see a alert box in your web page Monday, February 11, 13 56
  • 57. Recap All Compilation options Consuming Different types of libraries Using lein cljs-build Interactive Development using Browser REPL Monday, February 11, 13 57
  • 58. Thank you @sivajag siva@zololabs.com Monday, February 11, 13 58