so I can use it directly into a page
# javascript
d
so I can use it directly into a page
k
Why do you want that?
Why can't you just include JS file by
<script src=
?
d
the problem seems to be that the output changes the name of the module to “Kotlin” instead of “kotlin”
k
Not, it's not a problem
The problem you copy-pasted just a portion of generated JS file instead of copying entire file
d
but I don’t need the entire file
and I’m not exactly copy-pasting:
Copy code
fun code(function: () -> Unit): String {

			val functionString = function.toString()
			val functionBody = functionString.substringAfter(‘{’).substringBefore(“return”)

			return functionBody.trim()
		}
k
Why don't you need entire file?
d
What I’m doing is a bunch of classes that represents HTML element, CSS and Javascript. I’m trying to create a website using just kotlin. What I do is I generate an output string that I pass to the Express NodeJS module and that’s how I show a webpage
So, with the scripts I want to directly write JS (using the Kotlin functions implemented in the “org.w3c.dom.*” package) and I want that to be used inside a normal <script> in a HTML document
Copy code
webDocument.elementWith(id = ID.myCanvas.value)?.script = JavaScript.code {

			val mCanvasElement = document.getElementById(“myCanvas”) as HTMLCanvasElement
			val context = mCanvasElement.getContext(“2d”) as CanvasRenderingContext2D
			context.beginPath()
			context.arc(95.0, 50.0, 40.0, 2 * Math.PI, 0.0)
			context.stroke()

			//val algo = 10
			//console.log(algo)
		}
for example, I have that in my code; I need to be able to somehow convert that function that I’m passing to a JS code compilable in the browser directly
b
do you compile kotlin code on the fly?
d
to compile (~ to get the corresponding JS output) that I just use the “toString()” method; to compile my entire project I just use IntelliJIDEA with the appropiate settings and It compiles a “Index.js” and that stuff
b
well, why you can’t pass to express whole generated file?
d
the generated file is a .js; Express would accept just a js file? It need the entire page in a html format
b
sorry, I don’t have any experience with Express
d
no problem
b
why isn’t it static html?
d
because I generate all the html element, css and javascript using just kotlin; I don’t need to generate a file directly (I could, but is not necessary)
k
Are you trying to use same code in server and client?
So, when rendering an HTML page on server, you use Kotlin on node.js. While generating, you take a function and produce a string using
toString
. This won't work
d
not directly, but using this code it does:
Copy code
fun code(function: () -> Unit): String {

			val functionString = function.toString()
			var functionBody = functionString.substringAfter(‘{’).substringBefore(“return”)
			functionBody = functionBody.replace(“Kotlin”, “kotlin”)

			return functionBody.trim()
		}
k
This won't work in general case. You will get more and more functions that borrow declarations from their closure, and you don't provide these declarations
You better separate you logic into common logic, client-side logic and server-side logic. You can serve common logic JS and client-side JS as static files and include them directly using <script> tag
d
you mean compiling a kotlin file, then it generates a javascript file and include it into the <script>, right?
a kotlin file containing only the thing into the <script>
no?
k
Yes
d
Oh right, that’s seems like a logic approach
thank you 🙂