【问题标题】:How to use "foreign" JavaScript dependencies in Clojurescript?如何在 Clojurescript 中使用“外来”JavaScript 依赖项?
【发布时间】:2016-03-24 23:41:37
【问题描述】:

我正在尝试使用"foreign" JavaScript dependencies in Clojurescript 来解决问题。我已经阅读了谷歌提供的关于这个主题的大部分内容,但我仍然无法理解这个过程。特别是,我对如何依赖 Clojurescript 中的 jsonld.js 库很感兴趣。

有些点我没有得到:

  1. 您是否将 :foreign-libs 放入 deps.cljs 或编译器选项(例如,:compiler 在 project.clj 中的映射)?

  2. :file 中的 :foreign-libs 的值是否被解释为 Java 资源?您将用作外部库的 JavaScript 文件放在哪里?我尝试将它们放入resources 和其他地方,但我所有的尝试都导致java.lang.NullPointerException

重现步骤:

# Create an empty Clojurescript project
lein new figwheel jsonld

# Download jsonld.js library
cd jsonld/resources
curl -O http://cdnjs.cloudflare.com/ajax/libs/jsonld/0.3.15/jsonld.js

# Configure foreign libs in project.clj.
# Add the following into `:compiler` in the dev build:
# :foreign-libs {:file "resources/jsonld.js"
#                :provides ["jsonld"]}

lein figwheel # => java.lang.NullPointerException

或者,如果我向deps.cljs 提供以下内容:

{:foreign-libs {:file "jsonld.js"
                :provides ["jsonld"]}}

然后 Figwheel 启动,但是当我调用 (require '[jsonld]) 时,我得到了这个错误:

WARNING: JavaScript file found on classpath for library `jsonld`, but does not contain a corresponding `goog.provide` declaration
clojure.lang.ExceptionInfo: No such namespace: jsonld, could not locate jsonld.cljs, jsonld.cljc, or Closure namespace "jsonld" {:tag :cljs/analysis-error}

【问题讨论】:

    标签: javascript dependencies clojurescript


    【解决方案1】:

    更新::foreign-libs 选项采用外部库的向量而不是单个映射。

    :foreign-libs 选项可以直接提供给编译器,也可以通过 jar 中的 deps.cljs 文件提供给编译器。 deps.cljs 在您想将 Javascript 库打包到其他人可能使用的 jar 中时最有用 — 以后可能有用但不是您现在需要的。

    您可以找到有关编译器选项in the wiki 的更多信息。 还有一个专门关于using/packaging foreign dependencies in ClojureScript的页面。

    我认为在您的特定示例中,问题在于您提供为:file 的路径。路径是类路径相关的,resources/ 目录的内容被添加到类路径中,这意味着如果你想以 类路径相关的方式指向resources/jsonld.js,它只是jsonld.js

    PS:您也可以提供网址为:file,编译器会为您下载。

    【讨论】:

    • 自从我在指向java.lang.NullPointerException 的堆栈跟踪中看到clojure.java.io/resource,我也尝试将:file 简单地提供为jsonld.js。但是,以这种方式引发了相同的异常。
    • :file 中使用 jsonld.js 库的 URL 会导致相同的异常(堆栈跟踪:gist.github.com/jindrichmynarz/ab0e532e94a6353f9f28)。
    • 呃,这是:foreign-libs [{:file ...}]——注意向量!
    • 你是对的。但是,这只会让我转到另一个堆栈跟踪:gist.github.com/jindrichmynarz/78411164cad89376bfa3。你知道这里发生了什么吗?
    • 这似乎是无花果的问题。我建议在 clojurians.net slack 团队的#figwheel 频道中添加一个新问题或提问。不幸的是,我自己对figwheel了解不多。
    猜你喜欢
    • 2018-08-31
    • 2011-11-22
    • 2022-12-24
    • 1970-01-01
    • 2016-08-14
    • 2012-02-19
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多