Kotlin / kotlin-frontend-plugin

Gradle Kotlin (http://kotlinlang.org) plugin for frontend development
Apache License 2.0
561 stars 69 forks source link

Use gradle kotlin script in examples #111

Open LouisCAD opened 5 years ago

LouisCAD commented 5 years ago

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

AlteaDown commented 5 years ago

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.

NorbertSandor commented 5 years ago

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.)

AlteaDown commented 5 years ago

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.

NorbertSandor commented 5 years ago

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
        ...
    }
}
AlteaDown commented 5 years ago

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

altavir commented 5 years ago
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.

NorbertSandor commented 5 years ago

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

I use simply:

val npm: NpmExtension by project.extensions
npm.apply { ... }
altavir commented 5 years ago

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

gagern commented 5 years ago

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?

LouisCAD commented 5 years ago

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