Kotlin JavaScript Gradle Plugin: Scoped Package Name

How can I set the npm package scope using the kotlin-gradle-plugin?

Looking at PackageJson.kt in kotlin-gradle-pluing I can see that npm scoped package names seem to be supported:

companion object {
    fun scopedName(name: String): ScopedName = if (name.contains("/")) ScopedName(
        scope = name.substringBeforeLast("/").removePrefix("@"),
        name = name.substringAfterLast("/")
    ) else ScopedName(scope = null, name = name)

    operator fun invoke(scope: String, name: String, version: String) =
        PackageJson(ScopedName(scope, name).toString(), version)
}

Then in NpmProjectVisitor.kt I find that the name in package.json is set by using the name of the npm project:

val name = npmProject.name
val packageJson = PackageJson(
        name,
        fixSemver(project.version.toString())
 )

And finally in NpmProject.kt I see how the npm project name is constructed based on the gradle project name:

private fun buildNpmProjectName(): String {
    val project = target.project
    val name = StringBuilder()

    name.append(project.rootProject.name)

    if (project != project.rootProject) {
        name.append("-")
        name.append(project.name)
    }

    if (target.name.isNotEmpty() && target.name.toLowerCase() != "js") {
        name.append("-").append(target.name)
    }

    if (compilation.name != KotlinCompilation.MAIN_COMPILATION_NAME) {
        name.append("-").append(compilation.name)
    }

    return name.toString()
}

Unfortunately it is not allowed to include slashes (‘/’) in gradle project names. So setting @myscope/myproject is not an option.

Looks like this is not implemented yet, see https://youtrack.jetbrains.com/issue/KT-25878#focus=streamItem-27-2999812.0-0

You might want to try this workaround:

js(IR) {
    // ...
    compilations["main"].packageJson {
        customField("name", "@myscope/$name")
    }
}