Fork me on GitHub
#graalvm
<
2022-09-01
>
hanDerPeder08:09:40

Is it possible to specify additional namespaces that should be included in a native-image? I have namespace that might be dynamically required. Can work around this by adding a require in gen-class ns, but I’m hoping there is a better way.

borkdude09:09:05

@peder.refsnes yes, you should require them at compile time

borkdude09:09:22

dynamic requires also tend to bloat the image. see: https://github.com/borkdude/dynaload

hanDerPeder09:09:41

just want to verify I understand; top-level requires are done at compile-time, while requiring-resolve , etc, are done at run-time, right?

borkdude09:09:09

top level requiring-resolve are also done at compile time :) - non-top level require is done at runtime. so top level is the key thing

hanDerPeder09:09:20

Right, now I have:

(ns foo.bar
  ;; Don't remove, needed for native-image
  (:require [foo.stuff]))
was hoping there was a flag I could pass that would do the same, ie:
native-image -jar ... --include-extra-stuff=foo.stuff

borkdude09:09:48

you can just make another main namespace which requires the stuff and calls your old main namespace. but I highly recommend removing the dynamic requires, see the link I posted earlier

hanDerPeder09:09:13

if I understand the dynaload docs correctly it’s comparable to tree-shaking in js. is that correct?

hanDerPeder09:09:52

okay, not sure that’s applicable in my case. concretely I need to include a custom publisher in mulog, so I don’t controll the code that does the dynamic require. https://github.com/BrunoBonacci/mulog/blob/master/mulog-core/src/com/brunobonacci/mulog/publisher.clj#L213

bruno.bonacci09:09:09

Hi @peder.refsnes, you can pass a publisher also using the :inline type https://cljdoc.org/d/com.brunobonacci/mulog/0.9.0/doc/special-publishers/inline-publishers In that case, you have the burden of the initialisation of your custom publisher. I found this to be an easier approach on GraalVM compiled apps

👍 1
hanDerPeder09:09:09

cool, thanks 🙏