This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-04-25
Channels
- # announcements (4)
- # beginners (26)
- # calva (18)
- # cider (24)
- # clojure (35)
- # clojure-brasil (9)
- # clojure-dev (6)
- # clojure-europe (39)
- # clojure-madison (1)
- # clojure-nl (1)
- # clojure-norway (100)
- # clojure-uk (6)
- # clojurescript (17)
- # data-science (15)
- # datalevin (5)
- # emacs (1)
- # events (2)
- # introduce-yourself (2)
- # javascript (1)
- # malli (28)
- # missionary (7)
- # off-topic (59)
- # polylith (20)
- # reitit (2)
- # releases (1)
- # remote-jobs (2)
- # rewrite-clj (5)
- # shadow-cljs (27)
- # sql (5)
- # squint (63)
- # xtdb (8)
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")
yes, you can do almost everything in a build hook but I don't quite have enough information to give an answer
ClassNotFoundException config
is a bit weird, so not sure about that one. would help to know what config
is supposed to be?
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"
the file is like $company-name-space.web.config.cljs
ok there is so many issues with this description. please be mindful of the names and descriptions
issue one: config/GRAPHQL_URL
is not correct. it needs to use the full namespace, so $company-name-space.web.config/GRAPHQL_URL
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?)
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!
You can reference any environment variable via (System/getenv "THAT_VARIABLE")
, if you mean actual system environment variable
hooks run clojure code, so it is not CLJS (which doesn't have access to environment variables directly)
for CLJS you can use https://shadow-cljs.github.io/docs/UsersGuide.html#shadow-env in the build config though
no, the hook is not generating the index.html
, but is creating a JavaScript file that is referenced in the index.html
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!
anything that could just be a function since it didn't need any access to the build state should just be a function 😛
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 😄
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!
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.
Interesting, I wasn't aware of this case, will look into it
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)