Skip to content

Instantly share code, notes, and snippets.

@carolosf
Created November 11, 2018 00:23
Show Gist options
  • Save carolosf/5ed4598ad32282eb99369f2e06e7d991 to your computer and use it in GitHub Desktop.
Save carolosf/5ed4598ad32282eb99369f2e06e7d991 to your computer and use it in GitHub Desktop.
group 'com.example'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.3.0'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin2js'
repositories {
mavenCentral()
maven { url "http://dl.bintray.com/kotlin/kotlinx" }
// maven { url "https://dl.bintray.com/kotlin/kotlin-eap" }
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
compile "io.ktor:ktor-client-js:1.0.0-beta-3"
// compile "io.ktor:ktor-client-core:1.0.0-beta-3"
// compile "io.ktor:ktor-client:1.0.0-beta-3"
// compile "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.0.1"
}
compileKotlin2Js {
kotlinOptions.outputFile = "${projectDir}/web/output.js"
kotlinOptions.moduleKind = "commonjs"
kotlinOptions.sourceMap = true
kotlinOptions.languageVersion = "1.3"
kotlinOptions.sourceMapEmbedSources = "always"
// kotlinOptions.metaInfo = false
}
task assembleWeb(type: Sync) {
configurations.compile.each { File file ->
from(zipTree(file.absolutePath), {
includeEmptyDirs = false
include { fileTreeElement ->
def path = fileTreeElement.path
path.endsWith(".js") && (path.startsWith("META-INF/resources/") ||
!path.startsWith("META-INF/"))
}
})
}
from compileKotlin2Js.destinationDir
into "${projectDir}/web"
dependsOn classes
}
assemble.dependsOn assembleWeb
import io.ktor.client.HttpClient
import io.ktor.client.request.get
external fun require(module:String):dynamic
data class IndexRequest(val title: String, val message: String)
suspend fun main(args: Array<String>) {
println("Hello JavaScript!")
println(args)
val twig = require("twig")
twig.cache(false)
val express = require("express")
val app = express()
// app.set("view engine", "ejs")
app.set("view engine", "twig")
val options: dynamic = object{}
options["allow_async"] = true
options["strict_variables"] = false
app.set("twig options", options)
val client = HttpClient()
val wiki = client.get<String>("https://en.wikipedia.org/wiki/Main_Page")
println(wiki)
// app.get("/") { _, res ->
// res.type("text/plain")
// res.send("i am a beautiful butterfly")
// }
app.get("/") { req, res ->
res.render("index", IndexRequest("hello", "message"))
// res.render("index")
}
app.listen(3000) {
println("Listening on port 3000")
}
}
{
"name": "example",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"express": "^4.16.4",
"kotlin": "^1.3.0",
"kotlinx-coroutines-core": "^1.0.1",
"twig": "^1.12.0"
}
}
@carolosf
Copy link
Author

carolosf commented Nov 11, 2018

/build.gradle
/package.json
/src/main/kotlin/main.kt

yarn install && gradle build && gradle assemble
NODE_PATH=web node web/output.js

@carolosf
Copy link
Author

If you change module kind to umd, leave only the http client call in main() {} and make this /index.html file - it get's further and is stopped by cors - if you load this in a web browser.
`

<script type="text/javascript" src="web/kotlin.js"></script>
<script type="text/javascript" src="web/kotlinx-atomicfu.js"></script>
<script type="text/javascript" src="web/kotlinx-io-js.js"></script>
<script type="text/javascript" src="web/kotlinx-coroutines-core.js"></script>
<script type="text/javascript" src="web/kotlinx-coroutines-io-js.js"></script>

<script type="text/javascript" src="web/ktor-utils-js.js"></script>

<script type="text/javascript" src="web/ktor-http-js.js"></script>

<script type="text/javascript" src="web/ktor-utils-js.js"></script>
<script type="text/javascript" src="web/ktor-client-core-js.js"></script>

<script type="text/javascript" src="web/output.js"></script>
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment