[英]How to use common dependencies in different clojurescript projects?
[英]How to use “foreign” JavaScript dependencies in Clojurescript?
我正試圖在Clojurescript中使用“外來”JavaScript依賴項 。 我已經閱讀了Google在此主題上提供的大部分內容,但我仍然無法理解這一過程。 特別是,我對如何依賴Clojurescript的jsonld.js庫感興趣。
我得到的一些觀點:
你把:foreign-libs
放到deps.cljs或編譯器選項中(例如:compiler
project.clj中的:compiler
映射)?
值為: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}
更新:: :foreign-libs
選項采用外部庫的向量而不是單個映射。
:foreign-libs
選項可以直接提供給編譯器,也可以通過jar中的deps.cljs
文件提供。 當你想在其他人可能使用的jar中打包Javascript庫時, deps.cljs
非常有用 - 可能會在以后使用,但現在不需要。
您可以在Wiki中找到有關編譯器選項的更多信息。 還有一個專門關於在ClojureScript中使用/打包外部依賴項的頁面 。
我認為在您的特定示例中,問題是您提供的路徑為:file
。 路徑是類路徑相關的, resources/
目錄的內容被添加到類路徑中,這意味着如果你想以類路徑相對的方式指向resources/jsonld.js
它只是jsonld.js
。
PS:您還可以將URL提供為:file
,編譯器將為您下載它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.