如何在ClojureScript中编写不可知的JavaScript库? [英] How to author agnostic JavaScript library in ClojureScript?

查看:103
本文介绍了如何在ClojureScript中编写不可知的JavaScript库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含以下内容的cljs文件:

Let's say I have a cljs file containing the following:

(ns foo)
(defn add [x y]
  (+ x y))

,并希望将其作为JavaScript库提供给非ClojureScript开发人员(主要侧重于node.js).我可以这样做:

and wish to make this available as a JavaScript library to non-ClojureScript devs (primarily focused on node.js). I can do this:

clj -m cljs.main -c foo

但是问题在于输出是针对Google Closure的模块系统(例如goog.require)的.我可以使用-t标志(与浏览器或节点相对)将目标设置为none,但是...无法解决此问题.将其设置为node也不能解决问题:没有index.js(在Java中称为main),没有module.exports = blah blah.似乎它面向的是独立的全节点应用程序,而不是库.

But the problem is that the output is geared towards google closure's module system (e.g. goog.require). I can set the target to none with the -t flag (as opposed to browser or node), and that... doesn't fix this. Setting it to node also doesn't fix the issue: no index.js (it's called main like in Java), no module.exports = blah blah. Seems like it's geared towards standalone, full node apps, rather than libraries.

我了解ClojureScript使用google闭包作为其自身的子模块,而且我不是不必要想要摆脱所有这些问题(我不确定您可以).而且我发现es2015本机JavaScript模块由于其静态特性而退出市场.

I understand that ClojureScript uses google closure for it's own sub-modules, and I'm not necessarily looking to get rid of all of that (I'm not sure you could). And I get that es2015 native JavaScript modules are out because of their static nature.

可以手动或通过脚本对输出进行按摩,以使其在npm生态系统中发挥出色的作用,但令我惊讶的是,没有编译器选项可以实际输出npm友好的模块.还是在那里?我只是读错了--help吗?

I could massage the output by hand or by script to play nice with the npm ecosystem, but I'm surprised that there's no compiler option that can actually output a npm-friendly module. Or is there? Am I just reading --help wrong?

推荐答案

阴影-cljs 支持通过:target:npm-module 确实支持您的要求.节点和其他JS工具(例如webpack)可以独立使用单独的命名空间.默认的CLJS工具不支持此模式.

shadow-cljs supports output in a CommonJS format via :target :npm-module which does support exactly what you are asking for. Node and other JS tools (eg. webpack) can consume separate namespaces independently. The default CLJS tools do not support this mode.

但是,ClojureScript是在假设整个程序将由Closure Compiler优化的前提下编写的.这使其不适合编写要包含在其他版本中的库.以这种方式构建的每个库"都将包含其自己的cljs.core版本,因此开始时将非常庞大,并且包括以这种方式构建的2个库是灾难的秘诀,因为它们彼此之间不兼容.

ClojureScript however is very much written with the assumption that your whole program will be optimized by the Closure Compiler. This makes it less than ideal for writing libraries to be included in other builds. Each "library" built this way will contain its own version of cljs.core and therefore will be pretty large to begin and including 2 libraries built this way is a recipe for disaster since they won't be compatible with each other.

这篇关于如何在ClojureScript中编写不可知的JavaScript库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆