Is there a way to know if a compilation or a binar...
# javascript
e
Is there a way to know if a compilation or a binary is targeting
node
or
browser
? Example:
Copy code
compilations.configureEach {
  binaries.forEach {
    // Here I can get jsDevelopmentLibraryDistribution, but I'm missing the Node or Browser disambiguation
    val distributionTaskName = distributionTaskName(it)
j
It's not a disambiguation. It's a test runtime, and it's valid to specify both.
e
I need to attach a custom task to the distribution task created by KGP, which is for the browser or Node. KGP in
KotlinJsIrSubTarget
uses:
Copy code
val distributionTask = registerSubTargetTask<Task>(
    disambiguateCamelCased(
        binary.name,
        DISTRIBUTION_TASK_NAME
    )
)
Where
disambiguateCamelCased
is
Copy code
lowerCamelCaseName(
  target.disambiguationClassifier /* js */,
  disambiguationClassifier /* node | browser */, 
  *names
)
So you end up with two distribution tasks for Node and browser, e.g.:
jsNodeDevelopmentLibraryDistribution
and
jsBrowserDevelopmentLibraryDistribution
Those are the names I need to get
In the end to extract task names I wrote
Copy code
internal fun KotlinJsCompilation.distributionTaskNames(binary: JsBinary): List<String> {
  val tasks = ArrayList<String>(2)

  if (isNodeCompilation()) {
    tasks.add(lowerCamelCaseName(target.disambiguationClassifier, "node", binary.name, "distribution"))
  }

  if (isBrowserCompilation()) {
    tasks.add(lowerCamelCaseName(target.disambiguationClassifier, "browser", binary.name, "distribution"))
  }

  return tasks
}

internal fun KotlinJsCompilation.isNodeCompilation(): Boolean {
  val target = target
  return target is KotlinJsSubTargetContainerDsl && target.isNodejsConfigured
}

internal fun KotlinJsCompilation.isBrowserCompilation(): Boolean {
  val target = target
  return target is KotlinJsSubTargetContainerDsl && target.isBrowserConfigured
}
Not sure if there is a better way.
a
@Ilya Goncharov [JB] ^^
i
Binary and compilation are just entities which are responsible for compile Kotlin code to klib or js file. Neither binary nor compilation doesn’t know about “runtime target”. So yes, it is responsibility of
KotlinJsTarget
indeed, and now all related properties about disambiguation classifier are private
gratitude thank you 2
e
Thank you! The way I've set it up now seems to work decently. I can create my custom tasks and make the KGP distribution tasks depend on them based on
nodejs()
and
browser()
activations. However I'm stuck in how to do the same thing with test compilations, for example, how do I get to build this task name?
Copy code
jsTestTestDevelopmentExecutableCompileSync
For added context:
Copy code
compilations.configureEach {
  val isTestCompilation = compilationName.startsWith("test")
  val distributionTaskNames = mutableListOf<String>()

  binaries.forEach {
    val taskNames = if (isTestCompilation) {
      listOf("jsTestTestDevelopmentExecutableCompileSync")
    } else {
      it.distributionTaskNames()
    }

    distributionTaskNames.addAll(taskNames)

    // Register a task to allow starting a debug session.
    // Obviously we want it only for development purposes.
    if (it.mode == KotlinJsBinaryMode.DEVELOPMENT && target.isNodeConfigured()) {
      VSCodeExecTask.create(compilation = this) {
        dependsOn(taskNames.first())
        debug.set(true)
      }
    }
  }