【问题标题】:Light Table unable to load 'goog' and other javascript librariesLight Table 无法加载“goog”和其他 javascript 库
【发布时间】:2015-02-17 09:26:52
【问题描述】:

Light Table 0.7.2 使用 clojurescript 运行“实时”浏览器 repl。

我只是在记录一个阻止我使用 Light Table 的问题的解决方案。我找不到任何合适的答案,我花了一段时间才找到解决方法。希望这能帮助遇到类似问题的其他人。

在编辑不是项目核心 cljs 源文件的临时文件时(在 project.clj:cljsbuild:builds:source-paths 中指定),我在定义命名空间时得到以下信息:

(ns foo)
ReferenceError: goog is not defined

在尝试使用 dommy 之类的东西时,我会得到:

(ns foo.bar-2
  (:require
    [dommy.core :as dc]
    [domina.xpath :as dx]))
 Error: goog.require could not find: dommy.core

我在这里发现了类似的问题 [Clojurescript libraries - goog.require could not find

这里是https://groups.google.com/forum/#!searchin/light-table-discussion/goog$20/light-table-discussion/D4xjfDnA2Co/7iaqewIPzGUJ

但是,在第一种情况下,解决方案不起作用。在第二种情况下,我无法理解用户所说的“只是不在 LightTable 中实时评估 clojurescript 命名空间,而是依赖浏览器/DOM 行为来获得反馈”作为一种解决方法。

我不使用项目的主文件的原因是我想在单独的 repl 中做一些快速的一次性。这是我在emacs下能够做到的。通常,在 emacs 下,我将拥有主 cljs 文件,然后是 ncider repl。我会将两者放入同一个命名空间,这样我就可以从任一缓冲区更新共享上下文。

【问题讨论】:

    标签: clojure google-closure-compiler clojurescript lighttable


    【解决方案1】:

    假设: 我假设您已经知道如何启动 Light Table 所需的两个服务器:clojure repl 和浏览器。

    基本上,这个链接: http://grokbase.com/t/gg/clojure/142rsp4rc8/om-trouble-with-goog-reference

    提示我解决方案。

    似乎任何不在项目 src 目录下的文件都完全依赖于浏览器“repl”来查找它需要的任何库。所以解决方案是为你想要包含在 ns 中的任何 js 库添加标签到你的 index.html:

    <html>
        <head>
          <script type='text/javascript' id='lt_ws' src='http://localhost:54792/socket.io/lighttable/ws.js'></script>
          <script src="out/goog/base.js" type="text/javascript"></script> <!-- add this -->
        </head>
        <body>
            hello from mies-test
            <script src="out/mies_test.js" type="text/javascript"></script>
        </body>
    </html>
    

    然后您必须执行以下操作:

    1) 确保您的 project.clj 依赖项下有任何 js 库(“goog”除外):

     :dependencies [[org.clojure/clojure "1.6.0"]
                    [org.clojure/clojurescript "0.0-2755"]
                    [domina "1.0.3"]
                    [prismatic/dommy "1.0.0"]
                    [enfocus "2.1.1"]
    

    2) 运行“lein cljsbuild once”。你需要确保你的输出目录下有 goog/base.js 和 cljs/core.js:

    @HP_LAPTOP_ENVY /c/vtstuff/clojure_stuff/projects/mies2/out
    $ ls *
    cljs_deps.js  mies_test.js
    
    cljs:
    core.cljs  core.cljs.cache.edn  core.js  core.js.map
    
    clojure:
    browser
    
    goog:
    array    base.js  disposable  functions  iter  log        mochikit  promise  structs  uri
    asserts  debug    dom         html       json  math       net       reflect  testing  useragent
    async    deps.js  events      i18n       labs  messaging  object    string   timer
    
    mies2:
    core.cljs  core.cljs.cache.edn  core.js  core.js.map
    

    如果您执行“lein clean”,它将删除“out”目录。然后执行“lein cljsbuild once”将重建它。如果您想要一个干净的开始,请执行此操作。

    3) 刷新您的浏览器网址,例如

    file:///C:/vtstuff/clojure_stuff/projects/mies2/index.html
    

    4) 现在,当我运行 (ns foo) 时,我得到了:

    Error: goog.require could not find: cljs.core
        at Error (<anonymous>)
        at Object.goog.require (file:///C:/vtstuff/clojure_stuff/projects/mies2/out/goog/base.js:470:13)
    

    -> 这似乎是一个不一致的问题。我能够在不将 cljs/core.js 添加到 index.html 的情况下让另一个项目工作。您基本上可以在这里看到任何暂存缓冲区如何完全取决于浏览器 repl 所知道的内容。主 clojure repl 会自动识别任何依赖关系,但在这里我似乎需要将其添加到 index.html:

    <head>
       <script type='text/javascript' id='lt_ws' src='http://localhost:61756/socket.io/lighttable/ws.js'></script>
       <script src="out/goog/base.js" type="text/javascript"></script>
       <script src="out/cljs/core.js" type="text/javascript"></script> <!-- add this -->
    </head>
    

    5) 再次刷新浏览器,现在在 scratch.cljs 中的评估工作:

    (+ 1 1 ) 2
    
    (ns foo) nil
    
    (namespace ::x) "foo"
    

    6) 如果您想将 dommy 添加到您的 ns:

    (ns foo-2
     (:require [dommy.core :as dommy]))
        #<Error: goog.require could not find: dommy.core>
    Error: goog.require could not find: dommy.core
    

    您将在“out”目录下创建 dommy/core.js。您必须从 dommy jar 中提取 .cljs 文件(在您的 maven“.m2”存储库中),然后将它们添加到您的 src 目录中。然后执行“cljsbuild once”以生成 dommy/core.js,然后添加对 index.html 的引用,就像我们为 'goog' 和 'cljs' 所做的那样。我这样做了,它奏效了。但是,我现在意识到最好让您的暂存 repl 拉入“主”cljs,然后从暂存文件中引用工件(或者只是在主 cljs 中执行所有操作,我认为之前的人是谈论当他说“避免'实时评估'”时):

    (ns foo-3
    (:require [mies2.core]))
    
    mies2.core/abc 7
    

    7) 不幸的是,你不能把临时文件和主文件放在同一个 ns 中

    cljs:
    
    (ns  mies2.core)
    
    #<Error: Namespace "mies2.core" already declared.>
    Error: Namespace "mies2.core" already declared.
    

    无论如何,在弄清楚这一切之后,我能够运行一个 three.js 立方体演示。也许一旦我对实时浏览器“repling”有了更多的熟悉,这一切都会变得很明显,但一开始它肯定会给我带来很多困惑。

    愉快的编码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多