kotlin-frontend-plugin icon indicating copy to clipboard operation
kotlin-frontend-plugin copied to clipboard

Provide gradle kotlin-dsl example

Open fitzoh opened this issue 7 years ago • 10 comments

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

fitzoh avatar Dec 01 '17 15:12 fitzoh

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

fitzoh avatar Dec 01 '17 15:12 fitzoh

Is anyone considering sending a PR?

LouisCAD avatar Jun 25 '18 07:06 LouisCAD

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 avatar Oct 16 '18 12:10 lamba92

@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
}

andylamax avatar Dec 29 '18 20:12 andylamax

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 no WebpackExtension registered... After playing around with the project.extensions collection, I found out that the best way to list all (pseudo-)available extensions was to call project.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 ?

Naliwe avatar Feb 06 '19 18:02 Naliwe

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.

ghost avatar Apr 10 '19 20:04 ghost

Now it is more easy

kotlinFrontend {
    npm {

    }
    bundle("webpack", delegateClosureOf<WebPackExtension> {

    })
}

androidovshchik avatar Oct 19 '19 10:10 androidovshchik

Well now there is the Kotlin/JS plugin!

lamba92 avatar Oct 19 '19 10:10 lamba92

Where is the new plugin's documentation? Thanks.

NorbertSandor avatar Oct 19 '19 13:10 NorbertSandor

There isn't 😂 I managed to make it work trough trial and error! Have a look here:

lamba92 avatar Oct 19 '19 16:10 lamba92