Multiple ClojureScript Files on One Page

I have a project that uses Jasmine to test JavaScript. I am trying to switch to using ClojureScript for the front end. My project.clj is like

 (defproject myproject "0.1.0-SNAPSHOT" :dependencies [[org.clojure/clojure "1.5.1"] [org.clojure/clojurescript"0.0-1889"] [org.clojure/google-closure-library-third-party "0.0-2029"] [domina "1.0.0"] [hiccups "0.2.0"]] :plugins [[lein-cljsbuild "0.3.3"]] :cljsbuild { :builds [{ :source-paths ["src/clojurescript"] :compiler { :output-to "public/javascripts/main.js" :optimizations :whitespace :pretty-print true}} { :source-paths ["spec/clojurescript"] :compiler { :output-to "spec/javascripts/mainSpec.js" :optimizations :whitespace :pretty-print true}}]}) 

Thus, all .cljs files in src/clojurescript compiled in main.js , and all .cljs in spec/clojurescript get compiled in mainSpec.js . When I load the Jasmine page, the .js files load, but the tests do not run. In the console, I get Error: Namespace "goog.debug.Error" already declared. Both .js files have the same ~ 30k lines of google close code at the top that cause the error. If I remove this code from mainSpec.js , it will work fine. Is there any way to tell cljsbuild to leave this code outside the spec file?

+4
source share
2 answers

As Jared314 and Zubair pointed out, the problem you are facing is caused by an attempt to include two clojurescript compilation outputs in the same page. Clojurescript / Google Closure is waiting for the compilation of "the whole world", that is, the compiler expects that all the code for the entire page will be passed to the compiler so that it can optimize it, rename functions and ultimately spit out one javascript file. It is not intended to create multiple output files that work together.

The “right” way to solve your problem is to create two outputs that are used in isolation: the main.js file to run your application and the spec.js file, which includes all the code mainly plus the code in for testing. You can do this by setting up your project something like this:

 :cljsbuild { :builds [{ :source-paths ["src/clojurescript"] :compiler {:output-to "public/javascripts/main.js"}} { :source-paths ["src/clojurescript" "spec/clojurescript"] :compiler {:output-to "spec/javascripts/spec.js"}}]}) 

Your jasmine page should link to spec.js, but not main.js - a link to both is the cause of your error.

+1
source

The problem is that the compilations are compiled with the Google Closure library turned on, therefore, an “already declared” error. You can try using :optimizations :advanced in the :compiler options to reduce or eliminate duplicate code.

But if you are still facing the same problem, you may need to compile src and spec to build mainSpec.js .

0
source

All Articles