Enforce a source file to be compiled first

93 views
Skip to first unread message

Yehonathan Sharvit

unread,
Nov 10, 2014, 9:10:49 AM11/10/14
to clojur...@googlegroups.com
In my project I have a file: init.cljs with initialization code.

For instance:
(enable-console-print!)

(extend-type js/RegExp ;; allow regexp to be called https://gist.github.com/alandipert/2346460
IFn
(-invoke ([this s] (re-matches this s))))

What is the best way to make sure init.cljs is compiled before the rest of the project?

Thomas Heller

unread,
Nov 10, 2014, 9:43:33 AM11/10/14
to clojur...@googlegroups.com
That SHOULD be handled via the dependency graph of your namespaces, that means if one namespace requires code from another to be loaded it should list it in its (:require ...) declaration. This way you ensure that it was loaded in the right order.

init.cljs:

(ns init)

(enable-console-print!)
..

something.cljs:

(ns something
(:require [init]))


As for compilation I cannot speak for other tools (like lein-cljsbuild) but shadow-build [1] compiles in the mentioned order. As long as the (ns ..) declaration is correct I expect other tools to do the right thing too.

HTH,
/thomas

[1] https://github.com/thheller/shadow-build

Yehonathan Sharvit

unread,
Nov 11, 2014, 5:34:10 AM11/11/14
to clojur...@googlegroups.com, clojur...@googlegroups.com
It’s very cumbersome to add  (:require [init]) manually to each file.
Is there another solution?


Sent from Mailbox


--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to a topic in the Google Groups "ClojureScript" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojurescript/mDI0HKfSrg4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.


Thomas Heller

unread,
Nov 11, 2014, 5:57:55 AM11/11/14
to clojur...@googlegroups.com
Yes, but I would recommend using the require. It is much cleaner from the dependency graph perspective and tooling can support you way better. Also it is not that much work, maybe a couple seconds. I also tend to have a "util" namespace with general helper functions which is a good fit for the init stuff. Its better to explicitly depend on something rather than assuming it is loaded, you'll thank me later.

Also you should not have a namespace with such a general name. Suppose another library did the same thing and ship with an init ns, you'd run into all sorts of conflicts. Always use properly scoped namespaces (ns your-app.init) or (ns your-company.init).

I cannot say if its possible with other build tools but in shadow-build you could just add the init ns as a main to make sure it is compiled and loaded before any other if you still want to do it.

But like I said, you really shouldn't.

HTH,
/thomas

Peter Jaros

unread,
Nov 13, 2014, 9:51:19 AM11/13/14
to clojur...@googlegroups.com

It sounds to me like you may be thinking about this backwards. You don’t necessarily want to (enable-console-print!) any time that code is run, just when you’re running your entire app. Rather than require that code from each of your files, I’d suggest putting it at the top of (or requiring it from) the entrypoint into your application, and possibly a dev-specific entrypoint. For instance, in the Chestnut template, there’s a namespace loaded only in dev which kicks off the rest of the application. There’s a similar one from production which doesn’t load development concerns.

Doing it this way keeps your “real” code blissfully unaware of the way it’s being used. (And it’s a lot easier than requiring a utility namespace in every file for the sake of side-effects.)

Peter

You received this message because you are subscribed to the Google Groups "ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojurescrip...@googlegroups.com.

To post to this group, send email to clojur...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.
Reply all
Reply to author
Forward
0 new messages