Has anyone got experience building a kotlin librar...
# javascript
j
Has anyone got experience building a kotlin library for an isomorphic JS environment (Next.js in our case)? We had a usable version of this previously but it was still a little hacky (we needed to separately install the
kotlin
NPM package; updates were carried out by building and then copying the output of
./build/js/packages/ThisPackage/kotlin/ThisPackage.js
into our Next.js project). However, now we have added a dependency on another internal Kotlin project, I can’t get anything build properly, even in a hacky way.
./build/distributions/ThisPackage.js
contains references to
window
(despite the fact that I have used the node.js build option in
build.gradle.kts
) and the output in
./build/js/package
does not correctly import our other package. Does anyone know how I might do the following: • Get a working amount of code exported using both of our packages (Essential) • Build a single file to copy across (Highly desirable) • Get a node_module on GitHub’s package registry (Nice to have) I’ve tried using the new IR backend which looks like it will get us to where we want to go at some point in the future but, unfortunately, it seems to be either buggy or missing features for the time being. I’ve got the current setup (with mobile config omitted).
Copy code
plugins {
    kotlin("multiplatform") version "1.4.0"
    `maven-publish`
    // ...mobile plugins
    id("org.jlleitschuh.gradle.ktlint") version "9.3.0"
}

repositories {
    mavenCentral()
    maven {
        url = uri("<https://maven.pkg.github.com/our/dependency>") // another internal kotlin project
        credentials(PasswordCredentials::class)
    }
}

kotlin {
    js {
        nodejs {
        }
        useCommonJs()
        binaries.executable()
    }

    // ...mobile config


    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("com.our.dependency:kotlin:0.1") // another internal kotlin project
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }

        // ...mobile config

        val jsMain by getting
        val jsTest by getting {
            dependencies {
                implementation(kotlin("test-js"))
            }
        }
    }
}


publishing {
    repositories {
        maven {
            setUrl("<https://maven.pkg.github.com/our/this-repository>")
            credentials {
                username = System.getenv("GITHUB_USER")
                password = System.getenv("GITHUB_TOKEN")
            }
        }
    }
}
For reference, lack of enum support and this issue below are our current IR blockers https://kotlinlang.slack.com/archives/C0B8L3U69/p1596273872296200
b
You can bundle/publish your Kotlin/JS apps as npm packages with this plugin https://gitlab.com/lt.petuska/npm-publish
It automates everything for kotlin projects in the same way maven-publish does
It also bundles kotlin-only dependencies that are not available on npm, so the resulting package is fully valid npm js package
j
Oh great, this looks like just what I’m after. Will report back with success/failure
b
I can send you examples of couple projects that are already using it if that helps
j
That would be great, thanks!
Also, if you want a single file that you can just drop-in to another js project you can use browserProductionWebpack output
👍 1
it bundles all your code and its dependencies in a single file
j
Do you know why
NpmAccess
is undefined?
Copy code
* What went wrong:
Script compilation error:

  Line 114:   access = NpmAccess.RESTRICTED
                       ^ Unresolved reference: NpmAccess
b
Can you send me your npmPublishing block?
j
Copy code
npmPublishing {
  readme = file("README.MD")
  organization = "orgnamehere" // (Optional) Used as default scope for all publications
  access = NpmAccess.RESTRICTED
  bundleKotlinDependencies = true

  repositories {
    repository("npm-pkg-github") {      
      registry = uri("<https://npm.pkg.github.com>")
      authToken = "sometokenxxxxxx"
    }
  }
}
Incorrect repository value?
b
Ah, you need to import NpmAccess enum
Copy code
import lt.petuska.npm.publish.dsl.NpmAccess
j
Thanks!
b
Will add that to DSL scope with the next release. Thanks for pointing this out!
j
Added a PR to make this more obvious in the README: https://gitlab.com/lt.petuska/npm-publish/-/merge_requests/6
b
Changes target branch to develop and left a fix you need to approve for import package
👍 1
I take it you've managed to get it working for your use-case?
j
Done 👍
b
Looks like you need to rebase your branch on develop
But this will become obsolete next release (planning for next week)
j
Oh, cool. Is that covered by the DSL? What will the change required be?
b
None, it'll be backwards compatible, you'll just be able to remove the import
❤️ 1
j
Fantastic
b
npm-publish@v1.0.1 gradle plugin is out!