https://kotlinlang.org logo
Title
d

deviant

03/04/2018, 11:57 AM
Roman, are there any plans to publish
kotlinx-coroutines-core-js
on npm?
e

elizarov

03/04/2018, 3:14 PM
Will do with the next release
👍 1
d

deviant

03/04/2018, 3:24 PM
i've tried to copy-paste and use it as local npm module. but it requires also
common
module which i don't know how to import into
npm
project. is it possible?
s

spierce7

03/04/2018, 7:36 PM
I got this working. Let me see if I can't explain it to you. Pull the kotlinx.coroutines library locally and put it in the same parent directory your project is in. Then in your project, add the following to settings.gradle:
includeBuild('../kotlinx.coroutines') {
    //noinspection GroovyAssignabilityCheck
    dependencySubstitution {
        //noinspection GroovyAssignabilityCheck
        substitute module('org.jetbrains.kotlinx:kotlinx-coroutines-core-js') with project(':kotlinx-coroutines-core-js')
        //noinspection GroovyAssignabilityCheck
        substitute module('org.jetbrains.kotlinx:kotlinx-coroutines-core-common') with project(':kotlinx-coroutines-core-common')
    }
}
You should now be able to include the js coroutines in your project via the normal
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core-js:<version here>'
means. Now you need to add the kotlin dce plugin to your project:
apply plugin: 'kotlin-dce-js'
Also include the following option to turn off dead code elimination while developing for faster compiles:
runDceKotlinJs.dceOptions.devMode = true
And then all your js libraries will be output to a single location:
./build/kotlin-js-min/main/
Then all you need to do is to add those files to npm. Next to your
node_modules
directory, I created a
node_modules_local/kotlinx-coroutines-core-js
directory, with the following `package.json`:
{
  "main": "<path to your project dir>/build/kotlin-js-min/main/kotlinx-coroutines-core-js.js",
  "name": "kotlinx-coroutines-core-js",
  "version": "1.0.0"
}
And then finally in your main project
package.json
, you can include it to be packaged like this:
"dependencies": {
    ...
    "kotlinx-coroutines-core-js": "file:node_modules_local/kotlinx-coroutines-core-js",
  ...
  },
The advantage to the above approach is that you get dead code elimination with it when you want to release. You just need to change
runDceKotlinJs.dceOptions.devMode
to false, and then you have the unused code stripped out. This is practically essential with the kotlin std-lib imo.
d

deviant

03/04/2018, 7:43 PM
the problem is i don't use gradle. i use
create-react-kotlin-app
which is npm project. i've tried gradle frontend plugin, but experienced various issues with it, so i gave up and switched back to pure npm
s

spierce7

03/04/2018, 8:48 PM
oh, don't do that then lol. You give up all flexibility in your compile system. Still use npm, just grab the compiled js files yourself
d

deviant

03/04/2018, 9:30 PM
do you mean grab somewhere from the gradle's project build folder?
s

spierce7

03/05/2018, 3:31 AM
yes, as I described above.
I'd never give up my gradle compilation step. I'd give up all the flexibility to customize things in the build
there is even a kotlin frontend plugin for integrating with webpack I think. I've not used it, but you get the point
d

deviant

03/05/2018, 8:08 AM
yes. i tried frontend plugin, but got extremely huge compilation time. even in hot reload mode with
-t
param. basically that was the main reason why i'm back to npm+webpack