Is it possible to set `-PkobwebEnv` to `PROD` when...
# kobweb
m
Is it possible to set
-PkobwebEnv
to
PROD
when exporting site using
./gradlew kobwebExport -PkobwebReuseServer=false -PkobwebEnv=PROD -PkobwebRunLayout=STATIC -PkobwebBuildTarget=RELEASE -PkobwebExportLayout=STATIC
From docs it seems that it should be set when running exported site using
./gradlew kobwebStart -PkobwebEnv=PROD -PkobwebRunLayout=STATIC
If I set
PROD
when exporting I got following error
Copy code
org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':web'.
        at org.gradle.execution.TaskNameResolver.getExistingTask(TaskNameResolver.java:118)
        at org.gradle.execution.TaskNameResolver.access$000(TaskNameResolver.java:34)
...
...
...
Caused by: org.gradle.api.internal.tasks.DefaultTaskContainer$TaskCreationException: Could not create task ':web:kobwebExport'.
        at org.gradle.api.internal.tasks.DefaultTaskContainer.taskCreationException(DefaultTaskContainer.java:721)
        at org.gradle.api.internal.tasks.DefaultTaskContainer.access$600(DefaultTaskContainer.java:77)
...
...
...
Caused by: java.lang.IllegalStateException: Check failed.
        at com.varabyte.kobweb.gradle.application.KobwebApplicationPlugin$apply$7$10.execute(KobwebApplicationPlugin.kt:326)
        at com.varabyte.kobweb.gradle.application.KobwebApplicationPlugin$apply$7$10.execute(KobwebApplicationPlugin.kt:322)
When I set it to
DEV
it works fine. My usecase is to retrieve
kobwebEnv
value and then use it in the rest of app and because of that I need it in exporting phase (below example in
build.gradle.kts
)
Copy code
val env: String = project.findProperty("kobwebEnv")?.toString() ?: "DEV"
d
You don't want to change that. What are you trying to do?
Like what's the actual behavior you want to exhibit in your final site?
Note that you can do
Copy code
val ctx = rememberPageContext()
if (ctx.isExporting) { ... }
m
My goal is to make 2 (at least) environments (prod and dev for now). I found this flag
-PkobwebEnv
which seems useful to me to do that. If there is any different way of doing this, I'm open for suggestions.
d
You're saying technically what you want to do but not what you actually want to do. What feature are you trying to do that will matter to a user, in other words?
Are you trying to create two copies of your site?
Are you familiar with AppGlobals?
m
Yes, it will matter to a user. Depending on environment, different database will be used. I tried to use AppGlobals but it didn't fit with my usecase since I need to retrieve value in initApp which is called before AppGlobals is initialized, but I found workaround using BuildKonfig.
d
AppGlobals can be controlled by your Gradle script, which should give you full control. In general, you should probably not think about using the ENV or PROD values. That would be like having a different version of your application whether you were using DEBUG or RELEASE mode. It's really meant more for internal behavior / optimizations.
Let me check the code to see if I can initialize AppGlobals earlier
👍 1
m
Thanks for explanation, but do you have any concrete suggestion of doing this with environments for my usecase? I tried with changing build target (debug or release) and with this I can actually resolve my problem. But, I dont like this way because build target should not be used for environmet purposes...
d
I still don't know what you're trying to do for the end user, I am sorry
But give me a second to investigate AppGlobals
I definitely want AppGlobals to be available to init methods
m
1. I want to export app 2. When exporting app I want to set flag (prod, dev, or whatever just to distinguish) 3. I want that value in initApp where I initialize database
d
One sec, I'm fixing code so that you can use AppGlobals in init methods
I just needed to change the order of initialization
I'm pushing out a snapshot right now for 0.19.3. I'll ping here in ~10 minutes when it's available.
👍 1
So try this -- set your Kobweb version to 0.19.3-SNAPSHOT in your
gradle/libs.versions.toml
file
At that point,
AppGlobals
should no longer crash for you in your initApp block
❤️ 2
m
Will try, thanks! Do above steps help you understand problem?
d
Still not sure to be honest. However, you mentioning AppGlobals not working for you in initApp was definitely a bug so worth fixing
And I think now that it is working, you should probably just use that to be honest
If you want to set a flag, that's really the way you're supposed to do it
Just to make sure -- we're talking about the client side and not the server side, right?
m
Yes, client side (frontend app)
👍 1
Just sec, I will attach code snippet so maybe it helps to understand
d
Can you just try setting the flag value and passing it into app globals in your build script first?
If that works for you then I don't need to understand it 🙂
m
Yes, app globals are working now when called from initApp. I see in generated main.kt they are initialized before
🙌 1
image.png
If you can take a quick look, this is how flag is currenlty passed from gradle script to app (build konfig can now be replaced with app globals)
This works, but as I already said, I dont want to use build target for my use case
d
I would probably pass a flag directly into my Gradle command (if you're using the Kobweb CLI, you can probably do something like
kobweb run --gradle -Pkey=value
) and then check for that value in my build script
Thank you for reporting the AppGlobals issue btw.
m
No problem, glad to help Passing flag directly looks promising. One more question:
debug
and
release
build target both obfuscate generated JS file?
d
release obfuscated, debug shouldn't
That's not me doing anything though, that's just delegated to JavaScript build tasks provided by JetBrains
m
Last time I check
web.js
I think it was obfuscated with debug target, but I will check again later. Btw this is a reason why I ask anyway because it also looks strange to me
d
Yeah I'm not sure. Kobweb lists out all the Gradle tasks that are run, so you can try running those tasks directly to see if you can generate the file you're curious about. I admit it's tricky.
👍 1
I do most of my debugging with printlns to be honest.