aboutsummaryrefslogtreecommitdiffstats
path: root/gradle/compile-js-multiplatform.gradle
blob: d6df7e403a158b3c275d1765c59aa18caceeda2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

apply from: rootProject.file('gradle/node-js.gradle')

kotlin {
    js {
        moduleName = project.name

        // In 1.4.x it has in `both` and `legacy` mode and js() is of type `KotlinJsTarget`
        // `irTarget` is non-null in `both` mode
        // and contains appropriate `irTarget` with type `KotlinJsIrTarget`
        // `irTarget` is null in `legacy` mode
        if (it.irTarget != null) {
            irTarget.nodejs()
            irTarget.compilations['main']?.dependencies {
                api "org.jetbrains.kotlinx:atomicfu-js:$atomicfu_version"
            }
        }
    }

    sourceSets {
        jsTest.dependencies {
            api "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
        }
    }
}

// When source sets are configured
apply from: rootProject.file('gradle/test-mocha-js.gradle')

def compileJsLegacy = tasks.hasProperty("compileKotlinJsLegacy")
        ? compileKotlinJsLegacy
        : compileKotlinJs

def compileTestJsLegacy = tasks.hasProperty("compileTestKotlinJsLegacy")
        ? compileTestKotlinJsLegacy
        : compileTestKotlinJs

compileJsLegacy.configure {
    kotlinOptions.metaInfo = true
    kotlinOptions.sourceMap = true
    kotlinOptions.moduleKind = 'umd'

    kotlinOptions {
        // drop -js suffix from outputFile
        def baseName = project.name - "-js"
        outputFile = new File(outputFileProperty.get().parent, baseName + ".js")
    }
}

compileTestJsLegacy.configure {
    kotlinOptions.metaInfo = true
    kotlinOptions.sourceMap = true
    kotlinOptions.moduleKind = 'umd'
}


task populateNodeModules(type: Copy, dependsOn: compileTestJsLegacy) {
    // we must copy output that is transformed by atomicfu
    from(kotlin.js().compilations.main.output.allOutputs)
    into "$node.nodeModulesDir/node_modules"

    def configuration = configurations.hasProperty("jsLegacyTestRuntimeClasspath")
            ? configurations.jsLegacyTestRuntimeClasspath
            : configurations.jsTestRuntimeClasspath

    from(files {
        configuration.collect { File file ->
            file.name.endsWith(".jar") ?
                    zipTree(file.absolutePath).matching {
                        include '*.js'
                        include '*.js.map'
                    } : files()
        }
    }.builtBy(configuration))
}

npmInstall.dependsOn populateNodeModules