如何在Clojurescript中使用“外来”JavaScript依赖项?

时间:2015-12-18 09:47:01

标签: javascript dependencies clojurescript

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

我不明白的一些观点:

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

  2. :file:foreign-libs的值是否被解释为Java资源?你把你用作外国图书馆的JavaScript文件放在哪里?我尝试将它们放入resources和其他地方,但我的所有尝试都产生了java.lang.NullPointerException

  3. 重现的步骤:

    # 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}
    

1 个答案:

答案 0 :(得分:1)

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

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

  

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

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

PS:您还可以提供:file的URL,编译器会为您下载它们。