I can't seem to figure out how to have compiled JS...
# javascript
a
I can't seem to figure out how to have compiled JS code execute on load in the browser. Essentially, what is the equivalent to
Copy code
application {
    mainClass.set("...")
}
for JavaScript?
h
binaries.executable()
and a
main
function.
a
Huh... I have that and it doesn't seem to be executing. Does the main function have to be in a particular file or package?
h
Nope, the location is not relevant.
a
I've created a function called
main
in my JavaScript module, but i can see in the compiled JavaScript that it's in a function called
main
that would presumably have to be called from somewhere. The code won't run on script load.
h
Do you have a sample?
a
adamcooper-sh.js
adamcooper-sh.js.map.cpp
Sure! My main function in Kotlin (just to test) is:
Copy code
fun main() {
    console.log("JavaScript loaded!")
}
And I've attached the compiled JavaScript as snippets
h
Did you add the project js (adamcooper-sh.js) file in your index.html? https://github.com/hfhbd/chartJS-compose/blob/main/src/main/resources/index.html
a
I've been trying to serve the JS from a Ktor server written for JVM, so I don't have an index.html
Copy code
static("scripts") {
                    resource("adamcooper-sh.js")
                    resource("adamcooper-sh.js.map")
                }
h
But even with Ktor and server-side rendering you have to include your js file in HTML.
a
Yes, I was using the Kotlin HTML DSL. I have the script included as:
Copy code
script(type = ScriptType.textJavaScript, src = "/static/scripts/adamcooper-sh.js") {}
in the body
h
Okay, so calling this endpoint results in a "index.html". What is the html content and is the js file accessable? It should be possible to save the generated html response as a hmtl file and open it locally (if the server also runs locally)
a
wordlesolver.html
I do notice that it loads the script from
./wordlesolver_files
instead of
/static
, although it loads the stylesheet from that same path successfully. The logs also show the JS is downloaded:
Copy code
13:10:45.320 [DefaultDispatcher-worker-1] INFO ktor.application - 200 OK: GET - /static/scripts/adamcooper-sh.js
13:10:48.378 [DefaultDispatcher-worker-1] INFO ktor.application - 200 OK: GET - /static/style.css
13:10:48.434 [DefaultDispatcher-worker-11] INFO ktor.application - 200 OK: GET - /static/scripts/adamcooper-sh.js.map