Hildebrandt Tobias
08/23/2023, 11:36 AM;(function(config) {
const webpack = require("webpack");
const environmentPlugin = new webpack.DefinePlugin({
'process.env.TEST_VALUE': JSON.stringify(process.env.TESTVALUE),
// TEST_VALUE: process.env.TESTVALUE,
})
console.log("TESTVALUE: " + process.env.TESTVALUE)
console.log(environmentPlugin)
config.plugins.push(environmentPlugin)
})(config);
(I also tried EnvironmentPlugin but get the error TEST_VALUE undefined)
In gradle I added
webpackTask { args.plusAssign(listOf("--env", "TESTVALUE=$testVariable")) }
In my kotlin code I then get process is not defined
and I don't know where to go from here.Hildebrandt Tobias
08/24/2023, 9:32 AMprocess
was a node library.
So I added it via implementation(devNpm("process", "0.11.10"))
And added a wrapper with
@file:JsModule("process")
@file:JsNonModule
external val process: dynamic
I can see the process library in the node_modules in the browser, but
console.log(process)
still gives me process is not defined
turansky
08/24/2023, 9:32 AMturansky
08/24/2023, 9:34 AMexternal val process: dynamic
Hildebrandt Tobias
08/24/2023, 9:35 AMHildebrandt Tobias
08/24/2023, 9:35 AM@file:JsModule("process/browser.js")
turansky
08/24/2023, 9:35 AMexternal object process {
object env {
val TEST_VALUE: String
}
}
Hildebrandt Tobias
08/24/2023, 9:37 AMexternal interface Process {
val env: EnvironmentVariables
}
external object EnvironmentVariables {
val TEST_VALUE: String
}
turansky
08/24/2023, 9:40 AMval a = js("process.env.TEST_VALUE")
to check webpack configurationHildebrandt Tobias
08/24/2023, 9:40 AMturansky
08/24/2023, 9:44 AMturansky
08/24/2023, 9:45 AMturansky
08/24/2023, 9:45 AMDefinePlugin
will be in generated folder webpack.config.d
Hildebrandt Tobias
08/24/2023, 9:46 AMval test = js("process.env.TEST_VALUE")
println(test)
gives `undefined`but there is no crash.turansky
08/24/2023, 9:51 AMprocess.env.TEST_VALUE
call in generated JS. Without intermediate variables
2. Valid DefinePlugin
configuration - you can find it generated sources of compiler pluginHildebrandt Tobias
08/24/2023, 10:01 AMConfiguration with name 'jsMainImplementation' not found.
Hildebrandt Tobias
08/24/2023, 10:02 AMjs("jsFrontend", IR)
and
val jsFrontendMain by getting { ... }
Hildebrandt Tobias
08/24/2023, 10:07 AMconst environmentPlugin = new webpack.DefinePlugin({
'process.env.TEST_VALUE': JSON.stringify("TESTTEST"),
// TEST_VALUE: process.env.TESTVALUE,
})
Works, I now have to get the env into the webpack and I'm fine.
Seskar also looks very good.
But I'll get back to it when I have more breathing room. Thank youHildebrandt Tobias
08/24/2023, 10:10 AMval test = js("process.env.TEST_VALUE")
println(test)
Works, but
@file:JsModule("process")
@file:JsNonModule
package common.environment
external object process {
object env {
val TEST_VALUE: String
}
}
doesn't. But that is okay for now.turansky
08/24/2023, 10:25 AMHildebrandt Tobias
08/24/2023, 10:30 AMHildebrandt Tobias
08/24/2023, 10:49 AMval testVariable = System.getenv("TEST_VARIABLE") ?: "default"
kotlin {
js("jsFrontend", IR) {
binaries.executable()
browser {
commonWebpackConfig(
Action {
// In a split multiplatform project webpack fudges the
// output file name, so we have to set it manually.
outputFileName = "${rootProject.name}-jsFrontend-js-frontend.js"
cssSupport {
enabled.set(true)
}
export = false
}
)
webpackTask { args.plusAssign(listOf("--env", "TESTVALUE=$testVariable")) }
runTask { args.plusAssign(listOf("--env", "TESTVALUE=$testVariable")) }
}
}
sourceSets {
val jsFrontendMain by getting {
dependencies {
implementation(devNpm("process", "0.11.10"))
}
}
}
}
webpack.config.js //this snipped must be the last to be added in the resulting generated config.js
module.exports = (env) => {
const definePlugin = new webpack.DefinePlugin({
'process.env.TEST_VALUE': JSON.stringify(env.TESTVALUE),
})
config.plugins.push(definePlugin)
return config
}
Process.kt
external object process {
object env {
val TEST_VALUE: String
}
}
Client.kt
val testtest = process.env.TEST_VALUE
println(testtest)
Hope it helps.turansky
08/24/2023, 11:20 AMplusAssign
-> +=
?Hildebrandt Tobias
08/24/2023, 11:26 AM