Is there a way how I can compile the JavaScript ta...
# multiplatform
s
Is there a way how I can compile the JavaScript target of https://github.com/ashampoo/kim to a JS framework that could be used by other regular JavaScript users? Like someone working in NodeJS? There is a TypeScript project I like to offer my framework and show them how to use it. I need to figure that out first. The artifacts are klib files... I guess that's nothing they could use, right? https://repo1.maven.org/maven2/com/ashampoo/kim-js/0.20/
a
You are going to need add a js target, add
binaries.library()
and then when you build, you should have a javascript bundle ready to be consumed (assuming you have exported your declarations)
🙏 1
s
Ok, I decided to offer a webpack JS first, which I potentially can test in my browser and write a sample for. I assume that others can use such a "normal" JavaScript file, too. I added these lines: https://github.com/Ashampoo/kim/blob/68385d9ca2511f85d3ad8d39689f4dc2c9b2c90f/build.gradle.kts#L172-L182 But the resulting files in
build/kotlin-webpack/js/productionExecutable
are minimal - 500 byte for the JS and 2 KB for the map Can't be right. And why do I get a
map
anyway? I want a JS file that can do everything that the JAR file can do.
Does someone have a working sample project how to create a usable .js file? I guess all I need is a showcase to which I can compare what I do at my lib to figure out what’s wrong.
a
Have you tried this and failed??
Copy code
js {
   browser()
   nodejs()
   binaries.libary()
}
s
Yes, that's not working. I tried this:
Copy code
js(IR) {
    browser {
        binaries.executable()
    }
}
But that's not giving me a usable JS file, too.
Ah, clearing the cache two times changed the error message to this:
Copy code
Could not determine the dependencies of task ':allTests'.
> Could not create task ':jsProductionLibraryCompileSync'.
   > Extension with name 'kotlinNodeJs' does not exist. Currently registered extension names: [ext, versionCatalogs, kotlin, kotlinTestRegistry, base, defaultArtifacts, sourceSets, reporting, javaToolchains, java, kotlinArtifacts, android, androidComponents, buildOutputs, publishing, signing, detekt, sonar, sonarqube, kover, koverMerged, buildTimeTracker, gitVersioning, kotlinScripting]
Copy code
js(IR) {

    useCommonJs()

    binaries.executable()

    browser {}
}
This seems to do something until the unit test fails due to missing ChromeHeadless
Yeah, skipped the jsBrowserTest ... same result... Unusable small kim.js
@andylamax Ok, by skipping the tests I got your code to run, but it generates a really small kim.js Copying this into a browser won't work. I expected a rather big file containing all the functions the JAR has.
I guess I'm missing something. I want an generic JS file that I can give JavaScript devs to they can use my framework. Doesn't seem possible.
a
I see. just so we are clear. You got it to work with
binaries.library()
, not
binaries.executable()
right?
also, are you looking to export just one big js bundle
s
Yes, that all doesn't seem to make a difference
a
Does your yes answer the first question or the second question?
s
Copy code
js(IR) {
    browser()
    nodejs()
    binaries.library()
    binaries.executable()
}
works, if turned off the tests
It just doesn't give me what I expect
a
add this to your
gradle.properties
file
Copy code
kotlin.js.ir.output.granularity=whole-program
s
I guess I'm missing a concept here
a
also, remove the
binaries.executable()
and leave the
binaries.library()
s
So just
Copy code
js(IR) {
    browser()
    nodejs()
}
a
just
Copy code
js(IR) {
    browser()
    nodejs()
    binaries.library()
}
don't forget setting the granularity to whole-program as instructed above
1
s
Isn't "library" for NodeJS and "executable" for Webpack?
I may have read the wrong guide.
After compilation where will I find the correct kim.js file? I have multiple
I can't send someone a file like this I guess. That looks like some wrapper and does not contain the libraries logic.
Ah, the output of
jsBrowserDevelopmentLibraryDistribution
looks way more like what I expect
a
Sorry for not responding earlier. Did it work? And no, the prase "library" is for NodeJS and "executable" for Webpack is wrong
Library is for when you need other developer's to use your JS. Executable is more like for just running the created javascript application (call the main function and all)
s
Ah, I see.
I try to make the generated JS as clear as possible and reduce everything to a single JS file.
Copy code
js(IR) {

    browser {

        // FIXME Not working
        webpackTask {
            sourceMaps = false
        }

        // FIXME Not working
        commonWebpackConfig {
            sourceMaps = false
        }
    }

    // nodejs()

    binaries.library()

    compilations.all {
        compileTaskProvider.configure {
            compilerOptions.freeCompilerArgs.add("-Xir-minimized-member-names=false")
        }
    }
}
jsBrowserDevelopmentLibraryDistribution
looks like it's what I need. But "development" doesn't sound good. But
jsBrowserProductionLibraryDistribution
does produce something that looks like a stub.
Documentation doesn't me help, too. This is really confusing.
I guess something I can tell about in the next Kotlin/JS documentation study 😅
The thing is, there is a project that now uses https://github.com/mattiasw/ExifReader, a native exif javaScript library which is quite limited. My metadata library has far more features and write support, but I guess I need it to export as a JavaScript file.
They also have a source map.. Maybe that's needed
image.png
I guess I give up. What I want to achieve doesn't seem to be possible with Kotlin right now.
a
I have an idea. Why don't we jump into a quick google meet session?
s
Will this be a hack? 😄
a
I don't think so. Its just that I have been exporting javascript libraries from kotlin for a while now. And I can't fully tell whats the problem you are running into
s
Can I come back to you? Need to grab some sleep now.
a
Sure thing, but there is a large chance I won't be able available when you are
s
K. My problem is just that the output of the production distribution does not look something usable.

https://kotlinlang.slack.com/files/U025FAKGGJY/F081WGQFY3F/image.png

I got the feeling by now that Kotlin/JS is not really meant to export JS libs that can be used by others outside of Kotlin
That's what the touchlab article says to it