Kotlin/kotlin-frontend-plugin

Use gradle kotlin script in examples

LouisCAD opened this issue · 10 comments

This would be great for the Definitely Kotlin people, or those who wish to use more static build scripts.

This is the one thing blocking us from using the front end plugin instead of manually running command line command ourselves. Can't seem to get this to work with the Gradle Kotlin DSL.

Can't seem to get this to work with the Gradle Kotlin DSL.

What is your problem exactly? Maybe I can help... (I use it successfully with Kotlin DSL.)

I can't seem to get the syntax right, especially around the dsl within the kotlinFrontend {} lambda. Specifically, I'm trying to make the webpackBundle {}, resolve or work in anyways, but I can't seem to get it to work. So I have tried digging through the source to figure out what I should be putting, but end up not getting very far. An example of a kotlinFrontend {} lambda in Gradle Kotlin DSL would be immensely useful.

An example of a kotlinFrontend {} lambda in Gradle Kotlin DSL would be immensely useful.

An extract of my build.gradle.kts, I hope it helps:

val kotlinFrontend: KotlinFrontendExtension by project
kotlinFrontend.apply {
    downloadNodeJsVersion = "..."

    bundle("webpack") {
        this as WebPackExtension
        bundleName = "main"
        sourceMapEnabled = true
        ...
    }
}

Thank you, that is really helpful, and identifies what I was doing wrong.

extensions.findByType<KotlinFrontendExtension>()?.apply{
...
}

This one is equivalent to groovy closure. One can write an extension to make it shorter like

inline fun <reified T: Any> Project.withExtension(block : T.()->Unit){
  extensions.findByType<T>()?.run(block)
}

I wander, why gradle does not have it.

Still, translating inner parts from Groovy is not always obvious. For example I am currently searching for a way to represent npm block.

I am currently searching for a way to represent npm block.

I use simply:

val npm: NpmExtension by project.extensions
npm.apply { ... }

Thanks. I've been searching in the wrong place, I though that it should be a part of KotlinFrontendExtension.

Reading the above, I managed to simplify my code to the<KotlinFrontendExtension>().apply { … }. It is nice to see this as WebPackExtension confirmed as a pattern. I agree that a complete example might be instructive to have as part of this repository.

Do you folks import all the relevant classes, or is there some magic to avoid that?

Among those already using this gradle plugin with Kotlin DSL successfully, does anyone want to edit the README.md to add such examples?