Hi all, I am trying to create a kotlin multiplatfo...
# javascript
s
Hi all, I am trying to create a kotlin multiplatform library to be used across native platforms and a separate JS web app... I have been able to successfully import the built js module into my web app and that is all fine and working, but I was curious how I could minimize the build size... either I can keep the common library small and import kotlin.js npm package separately, or I can include it in the build... Problem I can't get around is avoiding the entire 1.1mb kotlin.js library... DCE doesn't seem to work for my use case... anyone else run into a similar use case and issue?
b
Is your JS consumer kotlin or plain js based?
s
plain js based
b
That explains why DCE is not helping, as it can only remove dead code on kotlin projects.
Your options are either to wait until kotlin implements es6 support (so that regular tree-shaking with webpack could do its job on kotlin libs) or convert your consumer to Kotlin/JS + JS hybrid and make sure you interact with imported kotlin code only from your kotlin sources
👍 1
How are you publishing your kotlin/JS lib, btw?
s
I publish the /build/js directory to a private git repo and include that with it's release tag in the package.json of my js app
Gotcha, that all makes sense.. thank you for your help
b
With your way of publishing, you might bump into an issue where your package no longer resolves due to kotlin-js-only dependencies that are not on npm (i.e. ktor client). Check out npm-publish plugin when that happens 😉
a
At Quizlet, we have set up some gradle shenanigans to allow us to DCE our own modules, the kotlin stdlib, and any external dependencies (like serialization). Caveats: • we still need to migrate to a more proper way to publish our NPM artifacts (like @Big Chungus’s plugin) instead of hosting raw files on git and pulling them in through npm in our JS project • we have only tested this with the stable JS compiler -- still need to explore migrating to IR The way we've done this is by: • creating a
js-bundle
umbrella module in our project that depends on every other module we want to call from JS • configuring DCE for
js-bundle
to keep any entrypoints we need to access from JS • configuring DCE for
js-bundle
to output to something like
"$buildDir/distributions"
Now, when you run
js-bundle:browserDistribution
, you should have a bunch of minified module-specific files in
js-bundle/build/distributions
-- including your very own stripped-down version of
kotlin.js
At Quizlet, we have some code to stitch together several
package.json
files with properly declared dependencies for every module (everything depends on our custom
kotlin
module,
kotlin
depends on nothing, some modules depend on
serialization
, some modules depend on each other) If you've just got one module you need to call from JS, you might be able to combine everything into one DCEd artifact that includes your minified kotlin stdlib We currently just upload those files into a git repo under separate tags and let yarn deal with things, but we're making progress to publish into an (internal) NPM repository
s
@ankushg I'm pretty new to Kotlin, especially the multiplatform stuff and this is exactly what I want to achieve and I am fine with letting yarn pull a git tagged distribution.. I'm very unclear how to get this distribution DCEd - not sure if you have any example gradle build file and/or directory structure you could share with me