Kotlin / kotlin-frontend-plugin

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

Provide gradle kotlin-dsl example #31

Open fitzoh opened 6 years ago

fitzoh commented 6 years ago

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 commented 6 years ago

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

LouisCAD commented 6 years ago

Is anyone considering sending a PR?

lamba92 commented 5 years ago

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

andylamax commented 5 years ago

@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
}
Naliwe commented 5 years ago

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:

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 ?

ghost commented 5 years ago

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.

androidovshchik commented 4 years ago

Now it is more easy

kotlinFrontend {
    npm {

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

    })
}
lamba92 commented 4 years ago

Well now there is the Kotlin/JS plugin!

NorbertSandor commented 4 years ago

Where is the new plugin's documentation? Thanks.

lamba92 commented 4 years ago

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