Oliver.O
07/19/2023, 9:38 PMoutputFileName
defaults to the project name. This means copy/pasted index.html
files usually fail. Renaming the project will also make loading the bundle fail. In both cases, one might be unaware of that webpack configuration.
Making webpack default to a fixed name like outputFileName = "app.js"
would eliminate these kinds of problems. What do you think?andylamax
07/19/2023, 9:41 PMOliver.O
07/19/2023, 9:44 PMOliver.O
07/19/2023, 9:45 PMturansky
07/20/2023, 7:11 AMmoduleName
property to fix output file nameturansky
07/20/2023, 7:12 AMkotlin.js {
moduleName = "my-app"
}
Oliver.O
07/20/2023, 7:48 AMturansky
07/20/2023, 8:12 AMAdam S
07/20/2023, 8:45 AMbuild/
to try and figure out what filename to use in my index.html. Since I didnât see my-subproject-name.js
, I assumed that I should use the closest name available, which was incorrect, and led to a confusing error.
I have a few thoughts on trying to make it less confusing:
1. KGP logs the correct filename to use in index.html, e.g. âKotlin/JS compiled module my-subproject-name.jsâ
pro: the name is visible
con: itâs still easy to forget/ignore/confuse, and itâs noisy
2. KGP adds a check - if index.html
doesnât contain my-subproject-name.js
then it logs a warning
pro: catches problems early
con: noisy, could be annoying, additional KGP code to write/maintain,
3. add a find/replace in the Gradle task that copies index.html, and use a token to replace {kotlinJsModule}
with the actual module name
pro: Just Worksâ˘
con: needs additional KGP code, requires some docsOliver.O
07/20/2023, 8:51 AMAdam S
07/20/2023, 9:04 AMkotlin.js().moduleName
to get the default module name - it just returns âundefinedâ
// build.gradle.kts
tasks.jsProcessResources {
val kotlinJsModuleName = provider { kotlin.js().moduleName }
inputs.property("kotlinJsModuleName", kotlinJsModuleName)
expand(
"kotlinJsModuleName" to kotlinJsModuleName
)
}
Adam S
07/20/2023, 9:13 AMkotlin {
js(IR) {
moduleName = project.name
}
}
tasks.jsProcessResources {
val kotlinJsModuleName = provider { kotlin.js().moduleName + ".js" }
inputs.property("kotlinJsModuleName", kotlinJsModuleName)
eachFile {
if (file.name == "index.html") {
// replace `kotlinJsModuleName.get()` with `kotlinJsModuleName` when
// <https://github.com/gradle/gradle/issues/24268> is fixed
expand("kotlinJsModuleName" to kotlinJsModuleName.get())
}
}
}
This correctly replaces
<script src="${kotlinJsModuleName}"></script>
with
<script src="my-subproject.js"></script>
Adam S
07/20/2023, 9:44 AMOliver.O
07/20/2023, 1:09 PMOliver.O
07/20/2023, 1:46 PMapp.js
executables do not occur, as each executable lands in its own project-specific build directory. Clashes occur when bundling multiple apps into another project, of course. With libraries, having many of them consumed by another project is common, so these should always have a unique name.
So I'd support Adam's idea as it solves the current problems and works in all cases.Oliver.O
07/20/2023, 1:47 PMturansky
07/20/2023, 7:10 PMOliver.O
07/21/2023, 8:06 AMturansky
07/21/2023, 11:57 AMturansky
07/21/2023, 12:00 PMindex.html
canât be processed by Kotlin in simple caseOliver.O
07/21/2023, 12:42 PM