我使用IntelliJ 2022.3.3向导创建了新的Kotlin Full-Stack应用程序. 除了将Gradle设置为版本8.0.2并添加slf4j依赖项外,到目前为止我没有做任何更改,Demo Server和前端如期启动.

然后我想要更改前端端口,以及其他一些东西,但我想保持简单. 所以我添加了一个webpack.config.d文件夹和一个webpack.config.js文件夹. 无论我运行什么Gradle任务,更改都不会反映在webpack.config.evaluated.js中,因此也不会apply.

WebPack.config.js:

module.exports = {
    mode: 'development',
    devServer: {
        port: 8088,
    },
};

Build.gradle.kts:

plugins {
    kotlin("multiplatform") version "1.8.10"

    application
}


group = "de.splitframe"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    maven("https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven")
}

kotlin {
    jvm("backend") {
        jvmToolchain(16)
        withJava()
        testRuns["test"].executionTask.configure {
            useJUnitPlatform()
        }
    }
    js("frontend", IR) {
        binaries.executable()
        browser {
            commonWebpackConfig {
                cssSupport {
                    enabled.set(true)
                }
            }
        }
    }
    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val backendMain by getting {
            dependencies {
                implementation("io.ktor:ktor-server-netty:2.0.2")
                implementation("io.ktor:ktor-server-html-builder-jvm:2.0.2")
                implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.7.2")
                implementation("org.slf4j:slf4j-api:2.0.6")
                implementation("org.slf4j:slf4j-simple:2.0.6")
                implementation("ch.qos.logback:logback-core:1.4.5")
            }
        }
        val backendTest by getting
        val frontendMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlin-wrappers:kotlin-react:18.2.0-pre.346")
                implementation("org.jetbrains.kotlin-wrappers:kotlin-react-dom:18.2.0-pre.346")
                implementation("org.jetbrains.kotlin-wrappers:kotlin-emotion:11.9.3-pre.346")
                implementation("org.jetbrains.kotlin-wrappers:kotlin-react-router-dom:6.3.0-pre.346")
            }
        }
        val frontendTest by getting
    }
}

application {
    applicationDefaultJvmArgs = listOf("-Dio.ktor.development=true")
    mainClass.set("de.splitframe.application.ServerKt")
}

tasks.named<Copy>("backendProcessResources") {
    val frontendBrowserDistribution = tasks.named("frontendBrowserDistribution")
    from(frontendBrowserDistribution)
}

tasks.named<JavaExec>("run") {
    dependsOn(tasks.named<Jar>("backendJar"))
    classpath(tasks.named<Jar>("backendJar"))
}

现在的 struct 是:

enter image description here

Edit: I noticed that my settings are exported to the build though: enter image description here

解决方案:

多亏了@Benjamin-Charais,我找到了问题所在.除了webpack.config.d文件夹的位置必须与src文件夹在同一级别外,我遵循的文档(https://webpack.js.org/guides/development/)也不适合这里.应用我的设置的正确方法是:

;(function(config) {
    config.mode = 'development'
    config.devServer = {
        port: 8088
    }
})(config);

推荐答案

webpack.config.d文件夹需要与src文件夹和build.gradle.kts文件夹处于同一级别

当运行基于webpack的任务时,Kotlin会自动拾取这一点.

另外,只有当JS环境有binaries.executable()个时,才会生成webpack任务.

Expected folder structure for webpack config in Kotlin

当使用IntelliJ创建新的全栈项目时,此 struct 将工作,并在运行jsBrowserWebpack版本时适当地更新webpack配置输出.如截图所示,它使用正确的值,并将它们与webpack配置的其余部分组合在一起.

project with combined webpack

Kotlin相关问答推荐

Kotlin扩展函数未调用Hibernate/JPA中的重写函数

如何在不基于数据 map 的情况下将图例添加到lets plot kotlin

我可以更改方法中泛型类的类型参数边界吗?

Kotlin中是否可以混合使用推断和显式的通用类型参数?

Kotlin SIZE_BYTES

kotlin 父类具有依赖于抽象变量的变量

如何从 var list 或可变列表中获取列表流

gradle 如何 Select 以-jvm结尾的库?

如何为 Kotlin 中的每个循环设置以避免越界异常

有没有办法重用 Job 实例?

什么是 .kotlin_builtins 文件,我可以从我的 uberjars 中省略它们吗?

如何修复 ViewPager2 中的Design assumption violated错误?

Android Kotlin StringRes 数量String

我们如何在Java注释声明中引用Kotlin常量?

Jetpack Compose 折叠工具栏

Kotlin - 如果不为空,则使用修改后的 Obj props 覆盖 Obj props

Recyclerview: listen to padding click events

使用 java lambda 调用 kotlin 函数时,Kotlin 无法访问 kotlin.jvm.functions.Function1

比较Kotlin的NaN

Android studio,构建kotlin时出现奇怪错误:生成错误代码