kotlin-frontend-plugin
kotlin-frontend-plugin copied to clipboard
Provide gradle kotlin-dsl example
It would be nice if there was a reference example for getting a build set up using the gradle kotlin dsl.
Sort of related to gradle/kotlin-dsl#40
For example, I was able to translate this snippet from the docs
kotlinFrontend {
webpackBundle {
bundleName = "main"
}
}
into this
configure<KotlinFrontendExtension>{
bundle("webpack", delegateClosureOf<WebPackExtension>{
bundleName = "main"
})
}
after a non-trivial amount of trial and error/reading source code
Is anyone considering sending a PR?
add those functions at the end of build.gradle.kts
:
fun kotlinFrontend(block: KotlinFrontendExtension.() -> Unit)
= tasks.getByName("kotlinFrontend", block)
fun KotlinFrontendExtension.webpackBundle(block: WebPackExtension.() -> Unit)
= configure(block)
fun KotlinFrontendExtension.npm(block: NpmDependenciesTask.() -> Unit)
= bundle("webpack", delegateClosureOf(block))
now you can do:
kotlinFrontend {
webpackBundle {
bundleName = "ciao mamma"
}
}
Idk why the JetBrains team does not provide those simple extension with the plugin. Maybe because it is written in Groovy or Java? I have not yet figured out how to configure the npm dependencies tho 😢.
@lamba92 to configure the npm do this in your build.gradle.kts
val npm : NpmExtension by project.extensions
npm.apply {
dependency("react","16.x.x") // your depenencies here
devDependency("webpack") // your dev dependencies here
}
alternatively, this should also work
configure<NpmExtension> {
dependency("react","16.x.x") // your depenencies here
devDependency("webpack") // your dev dependencies here
}
Hello,
Any news on how to use extensions in Gradle KTS ? I've been playing around to try and get it working, but it's quite a mess, and still not working ...
For example, when I try to use the webpackBundle
method in Gradle KTS:
- First error : Unresolved references everywhere. I tried the solutions proposed here, which fixed that.
- But then, I saw @lamba92 's solution, which looks better, and kinda maybe shows how to apply that to any extension. Fun part begins here.
- I try that, but as soon as I try to call the
webpackBundle
I just defined, Gradle complains about having noWebpackExtension
registered... After playing around with theproject.extensions
collection, I found out that the best way to list all (pseudo-)available extensions was to callproject.extensions.getByType(Unit::class)
. Obviously, Gradle complains, but it outputs the list of all registered extensions:Extension of type 'Unit' does not exist. Currently registered extension types: [ExtraPropertiesExtension, KotlinSingleJavaTargetExtension, DefaultArtifactPublicationSet, ReportingExtension, SourceSetContainer, JavaPluginExtension, SassExtension, SassTask, KotlinFrontendExtension, NpmExtension, KtorExtension, KarmaExtension, WebPackExtension]
Clearly,WebpackExtension
is registered.
This happens for the KtorExtension
too... It is listed here, but as soon as I actually call a configure block, it disappears.
So, am I doing something terribly wrong or is it plain impossible to use those in Gradle KTS ?
I managed to get an npm task to run using the plugin here: https://github.com/srs/gradle-node-plugin
like this: build.gradle.kts
plugins {
id("com.moowork.node") version "1.3.1"
}
task("build-brunch", com.moowork.gradle.node.npm.NpmTask::class) {
setNpmCommand("run-script", "build")
}
in my case I am using the JS build tool 'Brunch' instead of Webpack.
Now it is more easy
kotlinFrontend {
npm {
}
bundle("webpack", delegateClosureOf<WebPackExtension> {
})
}
Well now there is the Kotlin/JS plugin!
Where is the new plugin's documentation? Thanks.
There isn't 😂 I managed to make it work trough trial and error! Have a look here: