【问题标题】:Creating a Kotlin React App using gradle使用 gradle 创建 Kotlin React 应用程序
【发布时间】:2018-05-25 02:09:56
【问题描述】:

我一直在期待如何使用 gradle 创建一个 kotlin-react-app(我知道 create-kotlin-react-app CLI 工具,它不使用 radle),但我没有得到任何来源指向我。我偶然发现了 kotlin 前端插件(它可以工作)以及 npm 和 webpack 插件,但我无法将它们配置为运行/创建一个 kotlin-react-project。我不是配置 webpack 的专家,所以对我来说可能更难。

初衷

我打算创建一个多平台项目(是的,在 IntelliJ 中打包的 kotlin 体验)

替代方法

当我失败时,我选择采用这种方法。

  1. 使用 kotlin 多平台插件编写我的代码
  2. 编译成jar
  3. 将其作为库添加到我要创建的 create-react-kotlin-app 中
  4. 运行并等待魔法发生(它ddnt) 事实证明,一些预配置的 webpack 没有编译是因为它在编译时不可用。但 IDE 运行良好,甚至提供了代码补全

谁能给我指个方向?

【问题讨论】:

    标签: gradle webpack kotlin kotlin-frontend


    【解决方案1】:

    使用 kotlin 前端插件时,使用 gradle 构建 React 应用程序很容易。在 IntelliJ 中,按照以下步骤操作

    新模块 > gradle > kotlin (Javascript) > [next,next,next...finish]

    当然,你必须配置 gradle(根据你的喜好)。

    我的配置如下图:-

    buildscript {
        ext.kotlin_version = '1.2.41'
        ext.kotlinx_html_version = "0.6.4"
        ext.kotlin_frontend_version = "0.0.30"
        ext.react_version = "16.4.0-pre.31-kotlin-$kotlin_version"
        ext.react_dom_version = "16.4.0-pre.31-kotlin-$kotlin_version"
        repositories {
            mavenCentral()
            maven {
                url "https://dl.bintray.com/kotlin/kotlin-eap"
            }
        }
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:$kotlin_frontend_version"
        }
    }
    apply plugin: 'org.jetbrains.kotlin.frontend'
    apply plugin: 'kotlin2js'
    sourceCompatibility = 1.8
    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        maven { url "http://dl.bintray.com/kotlin/kotlin-dev" }
        maven { url "http://dl.bintray.com/kotlinx/kotlinx" }
        maven { url "http://dl.bintray.com/kotlin/kotlin-js-wrappers" }
    }
    dependencies {
        compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
        compile "org.jetbrains.kotlinx:kotlinx-html-js:$kotlinx_html_version"
        compile "org.jetbrains:kotlin-react:$react_version"
        compile "org.jetbrains:kotlin-react-dom:$react_dom_version"
    }
    kotlinFrontend {
        npm {
            dependency "style-loader" // production dependency
            dependency "react"
            dependency "react-dom"
            dependency "kotlin"
            dependency "@jetbrains/kotlin-extensions"
            dependency "@jetbrains/kotlin-react"
        }
        webpackBundle {
            bundleName = "main"
            sourceMapEnabled = false   // enable/disable source maps
            contentPath = file("${projectDir}/public") // a file that represents a directory to be served by dev server)
            publicPath = "/"  // web prefix
            host = "localhost" // dev server host
            port = 8088   // dev server port
            stats = "errors-only"  // log level
        }
    }
    task copyDocs(type: Copy) {
        println ":md-react:copyDocs: Copying to public directory"
        from("${projectDir}/build/bundle") {
            include "**/*.js"
            include "*.js"
        }
        into "${projectDir}/public/static"
        println ":md-react:copyDocs: Done copying"
    }
    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.endsWith(".map")) && (path.startsWith("META-INF/resources/") ||
                            !path.startsWith("META-INF/"))
                }
            })
        }
        from compileKotlin2Js.destinationDir
        into "${projectDir}/build/classes/main"
        dependsOn classes
    }
    //run.dependsOn copyDocs
    assemble.dependsOn assembleWeb
    copyDocs.dependsOn bundle
    //assemble.finalizedBy(copyDocs)
    compileKotlin2Js {
        kotlinOptions.outputFile = "${projectDir}/build/classes/main/web.js"
        kotlinOptions.moduleKind = "umd"
        kotlinOptions.sourceMap = true
    }
    

    希望对你有所帮助。

    快乐的黑客

    【讨论】:

    • 谢谢您,您能否举一个新版本的 Kotlin 库的新示例?
    • 请指定库。你问的是 kotlinx 吗?协程?包装纸?哪些库?
    • 所有库。我正在尝试构建 kotlin 全栈应用程序(带有嵌入式 Netty 网络服务器),但大多数示例太旧或无法使用新版本的 Kotlin、Gradle、kotlin-jdk(而不是 kotlin-jre)等。我什至可以不要让这个例子工作。在制作 webapp(Java、Spring、Tomcat、Maven)时从未遇到过此类问题
    • 感谢@andylamax 的回答,我用它创建了一个模板项目gitlab.com/rossdanderson/kotlin-react-bootstrap 具有最新的依赖项。请随时对此进行扩展!
    • @RossAnderson 感谢您的伟大倡议!如果您可以添加许可证,这样每个人都可以扩展它,那就太好了
    【解决方案2】:

    对于任何寻求更新解决方案的人:随着kotlin 1.4-M2 的发布,您可以使用 Intellij Idea 的实验项目向导来创建具有 ktor 后端和 kotlin-react 前端的全栈 Web 应用程序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多