- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个库 xx,其命名空间为 xx.core
,我用纯 Clojure 编写它,打算同时针对 Clojure 和 ClojureScript。这样做的实际方法似乎是使用 lein-cljsbuild’s crossovers and conditional comments .到现在为止还挺好。 这个前提现在已经过时了。 lein-cljsbuild 已被弃用,取而代之的是 reader conditionals ,还有许多其他命名空间/宏 ClojureScript 增强功能。见updated answer以下。
假设 xx 有一堆变量,我希望它的用户能够在 Clojure 和 ClojureScript 中使用。这些可以分为三种类型的变量。
.cljs
分开。命名空间在他们自己的特殊
.clj
命名空间,所有宏必须与
xx.core
中的所有其他变量隔离开.
use-macro
或
require-macro
似乎只能访问宏。刚才我对此进行了测试;我尝试将所有内容(宏、type-1 vars 和 type-2 vars)简单地保存在一个单一的
xx/core.clj
文件,并在 ClojureScript 测试文件中使用
(:use-macro xx.core :only […])
引用它。然后编译器为 ClojureScript 文件引用的
WARNING: Use of undeclared Var
中的每个非宏变量发出
xx.core
消息。)
xx.core
,
xx.macro
, 和
xx.util
?…
最佳答案
这个问题的前提在几年前基本上已经过时了。通过这次更新,我正在尽自己的一份力量,不要让过时的信息污染网络。
与 Clojure 不同,ClojureScript 通常在与运行时分开的编译阶段编译宏。还有很多附带的复杂性。但是,由于多项改进,情况已大大改善。
从 version 1.7 in 2015 , Clojure 和 ClojureScript 现在支持 reader conditionals ,它允许在同一个 .cljc
中定义宏和函数Clojure、ClojureScript、Clojure CLR 或所有三个文件:#?(:clj …, :cljs …, :cljr …, :default …)
.仅此一项就可以缓解大部分问题。
此外,ClojureScript 本身现在对 ns
进行了多项增强。这为命名空间的用户消除了许多其他附带的复杂性。它们现在记录在 Differences from Clojure, § Namespaces 中。 .它们包括隐式宏加载、内联宏规范和自动别名 clojure
命名空间:
Implicit macro loading: If a namespace is required or used, and that namespace itself requires or uses macros from its own namespace, then the macros will be implicitly required or used using the same specifications. Furthermore, in this case, macro vars may be included in a :refer or :only spec. This oftentimes leads to simplified library usage, such that the consuming namespace need not be concerned about explicitly distinguishing between whether certain vars are functions or macros. For example:
(ns testme.core (:require [cljs.test :as test :refer [test-var deftest]]))
will result in test/is resolving properly, along with the test-var function and the deftest macro being available unqualified.Inline macro specification: As a convenience, :require can be given either :include-macros true or :refer-macros [syms…]. Both desugar into forms which explicitly load the matching Clojure file containing macros. (This works independently of whether the namespace being required internally requires or uses its own macros.) For example:
(ns testme.core
(:require [foo.core :as foo :refer [foo-fn] :include-macros true]
[woz.core :as woz :refer [woz-fn] :refer-macros [apple jax]]))is sugar for
(ns testme.core
(:require [foo.core :as foo :refer [foo-fn]]
[woz.core :as woz :refer [woz-fn]])
(:require-macros [foo.core :as foo]
[woz.core :as woz :refer [apple jax]]))Auto-aliasing
clojure
namespaces: If a non-existingclojure.*
namespace is required or used and a matching cljs.* namespace exists, thecljs.*
namespace will be loaded and an alias will be automatically established from theclojure.*
namespace to thecljs.*
namespace. For example:(ns testme.core (:require [clojure.test]))
will be automatically converted to
(ns testme.core (:require [cljs.test :as clojure.test]))`
关于macros - 双 Clojure/ClojureScript 库设计和宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13835031/
如何从 clojurescript 程序中获取我所在的 Clojurescript 命名空间?我想这样做确实提供了某些调试信息(它只需要在开发模式下工作) 最佳答案 命名空间在 ClojureScri
我正在考虑使用 ClojureScript 构建一个网站。在客户端和服务器端都编写 ClojureScript 是否有意义,就像 node.js 一样? 最佳答案 这是可能的,但绝对没有理由不在服务器
我正在与Om合作,但我不完全理解以下表达式: (.. e -target -checked) 在这里,“ e”是一个JavaScript事件,“-target -checked”是一种访问属性的方式
我想在 ClojureScript 中前向声明一个函数。种 (defn a [] (b)) ... (defn b [] ...) 我觉得在用法(b)前面放一个(def b nil)是个什么样的作品,
我想在 ClojureScript 中前向声明一个函数。种 (defn a [] (b)) ... (defn b [] ...) 我觉得在用法(b)前面放一个(def b nil)是个什么样的作品,
我不知道像 this 这样的“自托管”clojurescript 实现如何和 this被实现。 但是,鉴于 clojurescript 编译器是用 clojure 编写的并将 clojure 编译为
考虑以下假设的无意义的 ClojureScript 函数: (defn tmp [] (def p 0) (set! p (inc p)) (set! p (inc p)) (set!
我在生产构建部分遇到问题 ClojureScript quickstart .具体来说,当我运行时:java -cp "cljs.jar;src" clojure.main release.clj我得
我正在尝试使用 defmacro在 ClojureScript 中,但出现控制台错误: TypeError: 'undefined' is not an object (evaluating 'crd
我正在寻找一种在 clojureScript 中根据字符串名称调用函数的方法。 就像是: (call "my-fun" args) 欢迎任何帮助 最佳答案 一个应该有效的非常hackish的解决方案:
在 Ubuntu 10.04.1 LTS 上的 FireFox 5.0 中运行的 ClojureScript 程序中,当抛出异常时,我得到一条神秘的线。 '错误:没有为类型对象定义协议(protoco
我正在努力工作 FormidableLabs/radium · GitHub与 reagent-project/reagent · GitHub ,但我已经走到了死胡同。 我能够通过“破解”试剂功能使
是否可以将 Google Closure Templates 与 Clojurescript 一起使用? 我环顾四周,但没有找到任何有关此的信息。 最佳答案 由于 Soy 模板已编译为 JavaScr
我想从 clojurescript 设置 element.style.height 属性,但不幸的是这段代码失败了: (set! (-> (sel1 tr :.ragslide) .-style .-
我正在使用 ClojureScript 编写一些原子(编辑器)包。我遇到了依赖加载问题。 编译后的 ClojureScript 生成如下文件 (main.js): goog.addDependency
我正在为 clojurescript 库编写一些 cljs.test 测试,并且有一些我想加载的 JSON 测试数据。 我不确定我可以使用什么函数来从项目的本地文件加载数据。 我正在使用 lein-d
我有一个从 Leiningen 启动的 ClojureScript 应用程序。我如何在应用程序启动时将环境变量传递给应用程序(可能是它构建时) 最佳答案 有两个构建时间选项: 使用宏 CLJS 宏是
如何在 ClojureScript 中获取时间戳,类似于 Unix 的时间戳,即表示当前时间和日期的单个数字,作为数字。我知道: if (!Date.now) { Date.now = fun
我需要使用 ClojureScript 打开一个新选项卡。 (js/window.open "http://localhost/go/somewhere") 我收到以下错误:未捕获类型错误:windo
我已经阅读了 David Nolen 的基本 Om 教程,但我仍然对应用程序状态和组件本地状态之间的区别感到有些困惑。当引用游标时,它是否也引用了其中之一或两者? 最佳答案 据我了解: 应用程序状态是
我是一名优秀的程序员,十分优秀!