Skip to content

Commit 7143a96

Browse files
committed
fix(ts-model-api): import of @modelix/ts-model-api was missing
In the JS code generated by the Kotlin compiler the import was missing.
1 parent 0ba9a43 commit 7143a96

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

model-api/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ kotlin {
8383
listOf("sourcesJar", "runKtlintCheckOverJsMainSourceSet", "jsSourcesJar", "jsPackageJson", "compileKotlinJs", "jsProcessResources").forEach {
8484
tasks.named(it) {
8585
dependsOn(":ts-model-api:npm_run_build")
86-
dependsOn(":ts-model-api:npm_run_generateKotlin")
86+
dependsOn(":ts-model-api:patchKotlinExternals")
8787
}
8888
}

model-api/src/jsMain/kotlin/org/modelix/model/api/NodeAdapterCache.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.modelix.model.api
22

3+
import TypedNode
34
import kotlinx.browser.window
45

56
@JsExport
@@ -43,11 +44,9 @@ object NodeAdapterCache {
4344
private fun toINode(node: Any): INode {
4445
if (node is INode) return node
4546
if (node is NodeAdapterJS) return node.node
47+
if (node is TypedNode) return toINode(node.node)
4648

47-
// if (node is TypedNode) return toINode(node.node)
48-
49-
// Workaround, because the line above fails with 'TypedNode is not defined'.
50-
// The import for '@modelix/ts-model-api' seems to be missing in the generated JS.
49+
// Workaround, because ts-model-api is loaded twice by webpack making the instanceof check on TypedNode fail.
5150
val unwrapped = node.asDynamic().node
5251
if (unwrapped != null) return toINode(unwrapped)
5352

ts-model-api/build.gradle.kts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import com.github.gradle.node.npm.task.NpmTask
2-
import com.github.gradle.node.task.NodeTask
32

43
plugins {
54
base
6-
id("com.github.node-gradle.node") version "3.4.0"
5+
id("com.github.node-gradle.node") version "3.5.1"
76
}
87

98
node {
10-
version.set("18.3.0")
11-
npmVersion.set("8.11.0")
9+
version.set("18.12.1")
10+
npmVersion.set("8.19.2")
1211
download.set(true)
1312
}
1413

@@ -20,9 +19,38 @@ tasks.named("npm_run_build") {
2019
outputs.dir("dist")
2120
}
2221

22+
val patchKotlinExternals = tasks.create("patchKotlinExternals") {
23+
dependsOn("npm_run_generateKotlin")
24+
doLast {
25+
val annotationLine = """@file:JsModule("@modelix/ts-model-api") @file:JsNonModule"""
26+
val dukatDir = buildDir.resolve("dukat")
27+
val files = dukatDir.listFiles()?.toList() ?: emptyList()
28+
val matchingFiles = files.filter { it.name.contains("@modelix_ts-model-api") }
29+
if (matchingFiles.isEmpty()) throw RuntimeException("No files found for patching in $dukatDir")
30+
val typealiases = HashSet<String>()
31+
val allImports = HashSet<String>()
32+
for (file in matchingFiles) {
33+
var lines = file.readLines()
34+
if (lines.isEmpty()) continue
35+
if (lines.first() == annotationLine) continue
36+
lines = listOf(annotationLine) + lines
37+
typealiases += lines.filter { it.startsWith("typealias ") }
38+
allImports += lines.filter { it.startsWith("import ") }
39+
lines = lines.filterNot { it.startsWith("typealias ") }
40+
file.writeText(lines.joinToString("\n"))
41+
}
42+
dukatDir.resolve("typealiases.kt").writeText(allImports.joinToString("\n") + "\n\n" + typealiases.joinToString("\n"))
43+
}
44+
}
45+
2346
tasks.named("assemble") {
2447
dependsOn("npm_run_build")
2548
dependsOn("npm_run_generateKotlin")
49+
dependsOn(patchKotlinExternals)
50+
}
51+
52+
tasks.named("npm_run_generateKotlin") {
53+
finalizedBy(patchKotlinExternals)
2654
}
2755

2856
val updateVersion = tasks.register<NpmTask>("updateVersion") {

ts-model-api/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"rootDir": "src",
55
"outDir": "dist",
66
"declarationDir": "dist",
7-
"noUnusedLocals": false
7+
"noUnusedLocals": false,
8+
"module": "UMD",
9+
"resolveJsonModule": false
810
},
911
"include": ["src/**/*.ts"],
1012
"exclude": ["dist"]

0 commit comments

Comments
 (0)