Build.gradle.kts: how to apply the configuration options compileKotlin2Js

Hello everyone,

I’m new to gradle and trying to use kotlin as language for gradle.

I’m experimenting with kotlin2js.

I want to add some configuration, but they do not work for me:
https://kotlinlang.org/docs/tutorials/javascript/debugging-javascript/debugging-javascript.html

This documentation only tells me how to do it for gradle with groovy
compileKotlin2Js {
kotlinOptions.sourceMap = true
kotlinOptions.sourceMapEmbedSources = “always”

// remaining configuration options

}

I just get an error message if I try to put it into the kotlin file:
Script compilation errors:

Line 18: compileKotlin2Js {
^ Unresolved reference: compileKotlin2Js

Line 19: kotlinOptions.sourceMap = true
^ Unresolved reference: kotlinOptions

Line 20: kotlinOptions.sourceMapEmbedSources = “always”
^ Unresolved reference: kotlinOptions

3 errors

Please can anyone help me here? Thanks

I solved it.

I used this file as an example:

And then I upgraded to gradle 5.0 because of this post:

Upgrading to 5.0 gave me some benefits. E.g. I don’t need to qoute “compile” any more

Here is my gradle file

import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath(“org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}”)
}
}

group = “foo.bla”
version = “1.0-SNAPSHOT”

plugins {
id(“kotlin2js”) version Versions.kotlin apply true
}

dependencies {
compile(kotlin(“stdlib-js”))
}

allprojects {

apply {
    plugin("kotlin-dce-js")
    plugin("kotlin2js")
}

tasks {
    compileKotlin2Js {
        kotlinOptions {
            sourceMap = true
            kotlinOptions.sourceMapEmbedSources = "always"
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile(kotlin("stdlib-js"))
}

}