forked from cloudflare/kotlin-worker-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.gradle.kts
76 lines (65 loc) · 2.91 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask
import org.jetbrains.kotlin.gradle.targets.js.ir.*
plugins {
kotlin("multiplatform") version "1.9.22"
// kotlin("multiplatform") version "2.0.0-RC1"
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
kotlin {
wasmJs {
nodejs {
}
binaries.executable()
// Comment the next line to turn off optimization by Binaryen
applyBinaryen()
// Workaround for https://youtrack.jetbrains.com/issue/KT-66972
compilations
.configureEach {
binaries.withType<Executable>().configureEach {
linkTask.configure {
val moduleName = linkTask.flatMap {
it.compilerOptions.moduleName
}
val fileName = moduleName.map { module ->
"$module.uninstantiated.mjs"
}
val mjsFile: Provider<RegularFile> = linkTask.flatMap {
it.destinationDirectory.file(fileName.get())
}
doLast {
val module = moduleName.get()
val file = mjsFile.get().asFile
val text = file.readText()
val newText = text
.replace("if \\(!\\w+( && !\\w+)*\\) \\{[^\\}]*\\}".toRegex(), "")
.replace(
"(if \\(isBrowser\\) \\{\\s*wasmInstance[^\\}]*\\})".toRegex(),
"""
$1
const isWorker = navigator.userAgent.includes("Workers")
if (isWorker) {
const { default: wasmModule } = await import('./$module.wasm');
wasmInstance = (await WebAssembly.instantiate(wasmModule, importObject));
}
""".trimIndent()
)
file.writeText(newText)
}
}
}
}
}
}
// To run in Node.js
rootProject.the<NodeJsRootExtension>().apply {
nodeVersion = "22.0.0-nightly2024021536dcd399c0"
nodeDownloadBaseUrl = "https://nodejs.org/download/nightly"
}
tasks.withType<KotlinNpmInstallTask>().configureEach {
args.add("--ignore-engines")
}