简体   繁体   English

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

[英]How to use “foreign” JavaScript dependencies in Clojurescript?

I'm trying to wrap my head around using "foreign" JavaScript dependencies in Clojurescript . 我正试图在Clojurescript中使用“外来”JavaScript依赖项 I've read most everything Google has to offer on this topic but I still fail to understand this process. 我已经阅读了Google在此主题上提供的大部分内容,但我仍然无法理解这一过程。 In particular, I'm interested how to depend on the jsonld.js library from Clojurescript. 特别是,我对如何依赖Clojurescript的jsonld.js库感兴趣。

Some points I don't get: 我得到的一些观点:

  1. Do you put :foreign-libs into deps.cljs or compiler options (eg, :compiler map in project.clj)? 你把:foreign-libs放到deps.cljs或编译器选项中(例如:compiler project.clj中的:compiler映射)?

  2. Is the value of :file in :foreign-libs interpreted as a Java resource? 值为:file :foreign-libs解释为Java资源? Where do you put the JavaScript files you use as foreign libraries? 你把你用作外国图书馆的JavaScript文件放在哪里? I tried putting them into resources and elsewhere, but all my attempts resulted in java.lang.NullPointerException . 我尝试将它们放入resources和其他地方,但我所有的尝试都导致了java.lang.NullPointerException

Steps to reproduce: 重现步骤:

# 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

Alternatively, if I provide deps.cljs with the following content: 或者,如果我向deps.cljs提供以下内容:

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

Then Figwheel starts, but when I call (require '[jsonld]) , I get this error: 然后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}

Update: The :foreign-libs option takes a vector of foreign libs and not a single map. 更新:: :foreign-libs选项采用外部库的向量而不是单个映射。

The :foreign-libs option can either be supplied to the compiler directly or via a deps.cljs file within jars. :foreign-libs选项可以直接提供给编译器,也可以通过jar中的deps.cljs文件提供。 deps.cljs is mostly useful when you want to package a Javascript library within a jar that other people may use — maybe useful later but not what you need right now. 当你想在其他人可能使用的jar中打包Javascript库时, deps.cljs非常有用 - 可能会在以后使用,但现在不需要。

You can find more information about the compiler option in the wiki . 您可以在Wiki中找到有关编译器选项的更多信息。 There also is a page specifically about using/packaging foreign dependencies in ClojureScript . 还有一个专门关于在ClojureScript中使用/打包外部依赖项的页面

I think in your particular example the problem is the path you're supplying as :file . 我认为在您的特定示例中,问题是您提供的路径为:file The path is classpath-relative and the contents of the resources/ directory are added to the classpath, meaning if you want to point to resources/jsonld.js in a classpath-relative way it's just jsonld.js . 路径是类路径相关的, resources/目录的内容被添加到类路径中,这意味着如果你想以类路径相对的方式指向resources/jsonld.js它只是jsonld.js

PS: You can also supply URLs as :file and the compiler will download them for you. PS:您还可以将URL提供为:file ,编译器将为您下载它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM