Skip to content

Commit 0f317d2

Browse files
committed
[Wasm, JS]Add documentation of newly added things in KotlinWebpackConfig
1 parent 6928c19 commit 0f317d2

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

libraries/tools/kotlin-gradle-plugin/api/all/kotlin-gradle-plugin.api

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4687,15 +4687,8 @@ public final class org/jetbrains/kotlin/gradle/targets/js/webpack/KotlinWebpackC
46874687
public final class org/jetbrains/kotlin/gradle/targets/js/webpack/KotlinWebpackConfig$DevServer$Static {
46884688
public fun <init> (Ljava/lang/String;Z)V
46894689
public synthetic fun <init> (Ljava/lang/String;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
4690-
public final fun component1 ()Ljava/lang/String;
4691-
public final fun component2 ()Z
4692-
public final fun copy (Ljava/lang/String;Z)Lorg/jetbrains/kotlin/gradle/targets/js/webpack/KotlinWebpackConfig$DevServer$Static;
4693-
public static synthetic fun copy$default (Lorg/jetbrains/kotlin/gradle/targets/js/webpack/KotlinWebpackConfig$DevServer$Static;Ljava/lang/String;ZILjava/lang/Object;)Lorg/jetbrains/kotlin/gradle/targets/js/webpack/KotlinWebpackConfig$DevServer$Static;
4694-
public fun equals (Ljava/lang/Object;)Z
46954690
public final fun getDirectory ()Ljava/lang/String;
46964691
public final fun getWatch ()Z
4697-
public fun hashCode ()I
4698-
public fun toString ()Ljava/lang/String;
46994692
}
47004693

47014694
public final class org/jetbrains/kotlin/gradle/targets/js/webpack/KotlinWebpackConfig$Mode : java/lang/Enum {

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/webpack/KotlinWebpackConfig.kt

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,37 @@ data class KotlinWebpackConfig(
119119

120120
internal val mutableStatics: MutableList<Static> = mutableListOf()
121121

122+
/**
123+
* Adds a static directory to the devServer configuration.
124+
*
125+
* Use this to instruct webpack-dev-server that the given [directory] should be
126+
* included as a static asset source. When [watch] is true, the directory is
127+
* watched for changes and the dev server will reflect updates automatically.
128+
*
129+
* This maps to the `devServer.static` option in the generated webpack config.
130+
*
131+
* @param directory path to a directory to be served as static content.
132+
* @param watch whether the directory should be watched for changes (default: false).
133+
*
134+
* https://webpack.js.org/configuration/dev-server/#devserverstatic
135+
*/
122136
fun static(directory: String, watch: Boolean = false) {
123137
mutableStatics.add(Static(directory, watch))
124138
}
125139

140+
/**
141+
* Snapshot of all static entries configured via [static].
142+
*/
126143
val statics: List<Static>
127144
get() = mutableStatics.toList()
128145

146+
/**
147+
* - actually encoded, combined from user input from [static] and [mutableStatics].
148+
* - 'static' serialized name is to match webpack json config
149+
* - The type is [Any], because it can be either a [String] or a [Static] instance.
150+
*
151+
* https://webpack.js.org/configuration/dev-server/#devserverstatic
152+
*/
129153
@Suppress("DEPRECATION")
130154
@get:SerializedName("static")
131155
internal val actualStatic: List<Any>?
@@ -168,7 +192,10 @@ data class KotlinWebpackConfig(
168192
val changeOrigin: Boolean? = null,
169193
) : Serializable
170194

171-
data class Static(
195+
/**
196+
* https://webpack.js.org/configuration/dev-server/#devserverstatic
197+
*/
198+
class Static(
172199
val directory: String,
173200
val watch: Boolean = false,
174201
) {
@@ -181,7 +208,7 @@ data class KotlinWebpackConfig(
181208
val obj = JsonObject()
182209
obj.addProperty(
183210
"directory",
184-
src.directory.quoteRawJs()
211+
src.directory.quoteRawJsRelativePath()
185212
)
186213
obj.addProperty("watch", src.watch)
187214
return obj
@@ -272,7 +299,7 @@ data class KotlinWebpackConfig(
272299
if (devServer != null) {
273300

274301
appendLine("// dev server")
275-
appendLine("config.devServer = ${json(devServer!!).unquoteRawJs()};")
302+
appendLine("config.devServer = ${json(devServer!!).unquoteRawJsRelativePath()};")
276303
appendLine()
277304
}
278305

@@ -419,11 +446,46 @@ data class KotlinWebpackConfig(
419446
}.toString()
420447
}
421448

422-
internal fun String.quoteRawJs(): String {
449+
/**
450+
* Marks a string value as a raw JavaScript expression to be embedded into the
451+
* generated webpack.config.js.
452+
*
453+
* Gson can only produce JSON (strings, numbers, objects, arrays). However, the
454+
* file we generate is an executable JavaScript file, and in some places we need
455+
* to emit JavaScript expressions rather than quoted JSON strings. This helper
456+
* wraps the receiver string with a special marker understood by [unquoteRawJsRelativePath].
457+
*
458+
* The typical flow is:
459+
* - Kotlin objects are serialized to JSON using Gson.
460+
* - Certain string fields that must become JS expressions are pre-wrapped using
461+
* [quoteRawJsRelativePath].
462+
* - After serialization, the resulting JSON text is post-processed with
463+
* [unquoteRawJsRelativePath] to replace the markers with actual JS code.
464+
*
465+
* Note: The current implementation is used for path-like values that will be
466+
* converted into `require('path').resolve(__dirname, "…")` calls by
467+
* [unquoteRawJsRelativePath]. The string inside the marker should therefore represent a
468+
* path relative to the webpack config directory.
469+
*/
470+
internal fun String.quoteRawJsRelativePath(): String {
423471
return "__RAW_JS__($this)__"
424472
}
425473

426-
private fun String.unquoteRawJs(): String {
474+
/**
475+
* Replaces special markers produced by [quoteRawJsRelativePath] in a JSON string with real
476+
* JavaScript expressions suitable for webpack.config.js.
477+
*
478+
* Specifically, occurrences of a quoted marker
479+
* "__RAW_JS__(<value>)__" in the serialized JSON are replaced with
480+
* `require('path').resolve(__dirname, "<value>")` so that the final output is
481+
* an executable JS expression instead of a plain string.
482+
*
483+
* This is applied to the JSON produced by Gson right before writing the
484+
* webpack configuration file to disk. It allows parts of the configuration to
485+
* contain dynamic, executable JavaScript where needed, while still leveraging
486+
* Gson for the bulk of the serialization.
487+
*/
488+
private fun String.unquoteRawJsRelativePath(): String {
427489
return replace("\"__RAW_JS__\\((.*?)\\)__\"".toRegex()) { match ->
428490
"require('path').resolve(__dirname, \"" + match.groupValues[1] + "\")"
429491
}

0 commit comments

Comments
 (0)