I made a multiplatform library how can I ship it t...
# javascript
o
I made a multiplatform library how can I ship it to js?
r
Hi. How did you get your MPP library? Is that a library built using MPP gradle plugin?
o
Yes, using mpp gradle plugin
r
How do you want to ship js library? Into npm or just get a .js file?
To make long story short, you should make the following steps 1. Make sure your mpp library is supposed to be built for js 2. Make js target dependent on mpp library
s
How do you publish it? @Roman Artemev [JB]
Would it be possible to replicate the webpack config in this project to create a lib? https://github.com/kalcifer/webpack-library-example
r
@Sean Keane K/JS compiler produces js library from kotlin module. What kind of support do you need?
s
Can you explain how I would publish it and where it is produced? I couldn't find anything in the doc's and had to write a script to make it create a npm module because of usability.
Ok, so I went back to 1.3.70, the project was rebuilt with clear caches. I then get a new
distributions
folder. I ran npm publish with the set
package.json
. But when I run the code I instantly get
Copy code
Uncaught TypeError: m is not a function
    at Object.l (WebSocketSession.kt:24)
e
I think it's only meant to be used as a gradle dependency for the JS target in a MPP project, ie. not as an npm dependency in a non-Kotlin Javascript project, if I understand this comment correctly: https://kotlinlang.slack.com/archives/C0B8L3U69/p1583234228116900?thread_ts=1583228562.114200&cid=C0B8L3U69 Eg. like the kotlin-stdlib-js library: https://bintray.com/bintray/jcenter/org.jetbrains.kotlin%3Akotlin-stdlib-js
s
I was able to bundle the kotlin-stdlib-js, and was hoping maybe webpack could bundle it post DCE and publish it via the webpack output
e
How did you manage that? The webpack output seems to use DCE-ed packages, so I guess that should work?
s
if you check out the
build/distribution
folder in your common code it has everything bundled in one .js file. doesn't seem to have done much DCE'ing though as its 1.5mb. Ill see if I can play with the webpack config
e
Oh, right. I was only looking at
build/js/packages/.../webpack.config.js
.
s
Im going to use webpack to resolve all the dependencies and bundle from the DCE'd code. Ill let you know how it goes
👍 1
o
i just wanted to export the library as a .js file but it depenends on kotlin.js
s
We all have this issue unfortunatly. Im checking to see if I can use webpack to bundle kotlin.js together
👍 1
o
then i found gradle task that will export that kotlin.js file
Copy code
task assembleWeb(type: Sync) {


    configurations.compile.each { File file ->
        from(zipTree(file.absolutePath), {
            includeEmptyDirs = false
            include { fileTreeElement ->
                def path = fileTreeElement.path
                path.endsWith(".js") && (path.startsWith("META-INF/resources/") ||
                        !path.startsWith("META-INF/"))
            }
        })
    }
    from compileKotlin2Js.destinationDir
    into "${projectDir}/web"

    dependsOn classes
}

assemble.dependsOn assembleWeb
s
This is not using the mutiplatform plugin unfortunatly
o
yes you're correct 👍 we need to do it using multiplatform plugin
s
I would like to see what you might come up with with multiplatform :)
o
i ended up getting mylibrary.js from the multiplatform project and the kotlin.js file from other sample project i did using kotlin-js plugin
and i included both files
Copy code
<script src="js/libs/kotlin.js"></script>
<script src="js/libs/mylibrary.js"></script>