Including a cljs namespace automatically apon use of clj macro

242 views
Skip to first unread message

cri...@procrustes.net

unread,
Jul 17, 2017, 10:54:38 AM7/17/17
to ClojureScript
Hi there,

I have a macro for use in cljs that injects code to make a function call to a function in a cljs namespace.  Now when I use that macro I have to :require the corresponding cljs namespace or during compilation I am flooded with superfluous: "WARNING: Use of undeclared Var mycljsnamespace/func at line ...." messages.

Is there a way to make the use of the macro in a cljs namespace add the cljs require statement to ensure that these warnings are suppressed?


and when using it, the compilation looks like:

WARNING: Use of undeclared Var infinitelives.pixi.canvas/get-default-canvas at line 15 src/cljs/...
WARNING: Use of undeclared Var infinitelives.pixi.canvas/get-default-canvas at line 15 src/cljs/...
WARNING: Use of undeclared Var infinitelives.pixi.canvas/get-default-canvas at line 11 src/cljs/...
...

at this stage in my present project its at least 50 lines of these.

if I require that name space in the client code that warning disappears but that requires the user of the macro to know that in advance, and be disciplined enough to do it.

How do I *auto require* the namespace, when the macro is used?


In it, Jozef Wagner states: "AFAIK there is no way to do this automatically"

Is this still the case?

I see CLJS has supported since 1.9.293 : CLJS-1346: Support require outside of ns

Can I make the macro inject the require into its output code so the "WARNING: Use of undeclared Var" is not emmitted?

I have experimented with this but have been unable to make it work.

Is this possible to do? If so, how can this be done?

Regards

Crispin

Thomas Heller

unread,
Jul 18, 2017, 3:19:38 AM7/18/17
to ClojureScript
You can easily achieve this by creating a CLJS namespace that self-requires the macros. Having dedicated .macros namespaces is generally not recommended.

pixi/something.cljs
(ns pixi.something
  (:require-macros [pixi.something]))

pixi/something.clj
(ns pixi.something)

(defmacro foo [..] ..)


user/code.cljs
(ns user.code
  (:require [pixi.something :refer (foo)))

(foo)

The user code does not need to worry about whether foo is a macro or not, it is handled transparently by the compiler. You never need to :refer-macros or :require-macros in the user code this way. And you can manage dependent CLJS namespaces that only the macro uses by requiring them in the .cljs companion file.

HTH,
/thomas

cri...@procrustes.net

unread,
Jul 18, 2017, 11:23:23 AM7/18/17
to ClojureScript
Ah this is just what I was looking for. I will need to rearrange some of the namespaces but this looks like a perfect way of implementing it. Thanks Thomas!

Crispin
Reply all
Reply to author
Forward
0 new messages