同一页面上的多个ClojureScript文件 [英] Multiple ClojureScript files on same page

查看:87
本文介绍了同一页面上的多个ClojureScript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Jasmine测试JavaScript的项目。我试图切换到使用ClojureScript作为前端。我的 project.clj 就像

I have a project that is using Jasmine to test the 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}}]})

因此 src / clojurescript 中的所有 .cljs 文件都被编译为 main.js spec / clojurescript 中的所有 .cljs 被编译为 mainSpec.js
加载Jasmine页面时,两个 .js 文件均已加载,但测试未运行。
在控制台中,我收到一个 Error:已经声明了命名空间 goog.debug.Error。
这两个 .js 文件的顶部大约有3万行Google关闭代码,这会导致错误。如果我从 mainSpec.js 删除此代码,它将运行正常。
有什么办法告诉cljsbuild将此代码保留在规范文件之外?

So all the .cljs files in src/clojurescript get compiled to main.js and all the .cljs in spec/clojurescript get compiled to mainSpec.js. When I load the Jasmine page, both the .js files are loaded but the tests aren't run. In the console I get an Error: Namespace "goog.debug.Error" already declared. Both the .js files have the same ~30k lines of google closure code at the top which is causing the error. If I delete this code from mainSpec.js it runs fine. Is there any way to tell cljsbuild to leave this code off the spec file?

推荐答案

正如Jared314和Zubair指出的那样出来,您遇到的问题是由于尝试在同一页面中包含两个clojurescript编译输出而引起的。 Clojurescript / Google Closure希望进行整个世界的编译,也就是说,编译器希望将整个页面的所有代码都传递给编译器,以便它可以对其进行优化,重命名功能并最终吐出一个单个javascript文件。并非旨在产生多个可协同工作的输出文件。

As Jared314 and Zubair pointed out, the problem you're encountering is caused by trying to include two clojurescript compilation outputs in the same page. Clojurescript/Google Closure expect to do a 'whole-world' compile, that is, the compiler expects that all of the code for the entire page is passed to the compiler so that it can optimise it, rename functions, and ultimately spit out a single javascript file. It's not designed to produce multiple output files that work together.

解决问题的正确方法是产生两个单独使用的输出:一个主输出。用于运行您的应用程序的js文件,以及一个spec.js文件,其中包含main中的所有代码以及spec中用于测试的代码。您可以通过设置类似以下内容的项目来做到这一点:

The 'correct' way to solve your problem is to produce two outputs that are used in isolation: a main.js file for running your application, and a spec.js file that include all the code in main plus the code in spec 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"}}]})

您的茉莉花页面应引用spec.js而不是main.js-引用两者都是导致错误的原因。

Your jasmine page should refer to spec.js but not main.js - referring to both is the cause of your error.

这篇关于同一页面上的多个ClojureScript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆