Fork me on GitHub
#shadow-cljs
<
2024-04-25
>
Gabriele Lippi09:04:46

Hello, I was wondering if it's possible to use env variables inside build hooks scripts, because I'm trying something like this

(if (= (:shadow.build/mode build-state) :dev)
   config/GRAPHQL_URL
   "another value")               

thheller14:04:41

what is config supposed to be in this context?

thheller14:04:44

yes, you can do almost everything in a build hook but I don't quite have enough information to give an answer

thheller14:04:32

ClassNotFoundException config is a bit weird, so not sure about that one. would help to know what config is supposed to be?

Gabriele Lippi16:04:19

I have a file like config.cljs where I defined some env variables like (goog.define GRAPHQL_URL "") which in turn are overwritten by the compiler with

:release
{:closure-defines
 {config/GRAPHQL_URL         "url"

Gabriele Lippi16:04:11

the file is like $company-name-space.web.config.cljs

thheller16:04:47

ok there is so many issues with this description. please be mindful of the names and descriptions

thheller16:04:07

so I suppose the really actual filename is $company-name-space/web/config.cljs?

thheller16:04:20

and its actual ns is $company-name-space.web.config?

thheller16:04:38

and use are using goog-define and not goog.define?

thheller16:04:20

and then what are you trying to do with that value inside a build hook?

thheller16:04:10

issue one: config/GRAPHQL_URL is not correct. it needs to use the full namespace, so $company-name-space.web.config/GRAPHQL_URL

thheller16:04:59

issue two: what is the purpose of this build hook? you can already define separate development/release values, just via the config? (which appear to already use given the :release key?)

Gabriele Lippi07:04:55

I'm sorry for the issues in the description, I'll be more accurate the next time. The use case I have is that we have to create different snippets of javascript code to inject in the index.html depending on the value of the build: so if it's :release we use a an id for the google analytics snippet, whereas if it's :dev we use another value so that the metrics of the apps are different for production and stage. Right now the problem I'm facing is that I can't reference the value of the google analytics id as env variable and use that env variable inside the hook. Maybe it's possible and I just misunderstood the way hooks and env vars works in shadow. The workaround I found out was to declare the value of those ids in the hooks file and change it depending on the value of the build, but ideally I'd prefer to use the env var if possible. I don't know if with this explanation it makes more sense. Thanks for replying anyway, I really appreciate your support!

thheller07:04:13

so, this build hook is generating the index.html?

thheller07:04:51

You can reference any environment variable via (System/getenv "THAT_VARIABLE"), if you mean actual system environment variable

👍 1
thheller07:04:15

hooks run clojure code, so it is not CLJS (which doesn't have access to environment variables directly)

👍 1
thheller07:04:45

for CLJS you can use https://shadow-cljs.github.io/docs/UsersGuide.html#shadow-env in the build config though

👀 1
Gabriele Lippi07:04:43

no, the hook is not generating the index.html , but is creating a JavaScript file that is referenced in the index.html

Gabriele Lippi07:04:23

and btw, I don't know if this is a proper way to use the hooks in shadow, but I found it pretty cool and useful!

thheller07:04:37

no, it is not recommended use of build hooks

👍 1
thheller07:04:56

anything that could just be a function since it didn't need any access to the build state should just be a function 😛

Gabriele Lippi07:04:02

well in this case I thought I needed the access to the build state to use and id or another depending on :dev or :release but I see your point 😄

Gabriele Lippi07:04:25

anyway, in the company where I work we're almost done migrating our project from an old figwheel config to shadow, it's been very interesting to migrate the project and shadow it's amazing, thank you for your work!

👍 1
thheller07:04:04

FWIW this is what I mean https://shadow-cljs.github.io/docs/UsersGuide.html#clj-run. create a regular function here. call it from a function like this.

Gabriele Lippi07:04:20

Interesting, I wasn't aware of this case, will look into it

Gabriele Lippi09:04:56

but I get this

Note: The following stack trace applies to the reader or compiler, your code was not executed.
CompilerException Syntax error compiling at (web/hooks.clj:22:17). {:clojure.error/phase :compile-syntax-check, :clojure.error/line 22, :clojure.error/column 17, :clojure.error/source "web/hooks.clj"}
	clojure.lang.Compiler.analyze (Compiler.java:6825)
	clojure.lang.Compiler.analyze (Compiler.java:6762)
	clojure.lang.Compiler$IfExpr$Parser.parse (Compiler.java:2841)
	clojure.lang.Compiler.analyzeSeq (Compiler.java:7124)
	clojure.lang.Compiler.analyze (Compiler.java:6806)
	clojure.lang.Compiler.access$300 (Compiler.java:38)
	clojure.lang.Compiler$LetExpr$Parser.parse (Compiler.java:6401)
	clojure.lang.Compiler.analyzeSeq (Compiler.java:7124)
	clojure.lang.Compiler.analyze (Compiler.java:6806)
	clojure.lang.Compiler.analyzeSeq (Compiler.java:7112)
	clojure.lang.Compiler.analyze (Compiler.java:6806)
	clojure.lang.Compiler.analyze (Compiler.java:6762)
Caused by:
ClassNotFoundException config
	java.net.URLClassLoader.findClass (URLClassLoader.java:476)
	clojure.lang.DynamicClassLoader.findClass (DynamicClassLoader.java:69)