[Gradle][K/JS] Add a generic target for projects not specific to node but are also not meant for browser environments#5652
Conversation
f32357b to
2663a4c
Compare
|
To be honest, this turned out to be significantly more difficult than I expected. It seems like the gradle plugin isn't really built for allowing different project types to be added easily (NpmProject in particular and the correspondence between subtargets and JsIrBinary being pretty loose) nor does it let you set output directories for each linkSyncTask for the corresponding subtarget that's configured. Although the new subtarget does work as expected, the code now is frankly in a terrible state and not something that should be merged at all, with the configurator for the generic environment using a hack to bypass the required Kotlin generic parameter and with the generic subtarget being completely incompatible with browser and nodejs subtargets. Despite this, I'm opening it for review now, because it's become very clear to me that I'm not going to make any more progress on my own without any outside help. Hopefully I'll be able to clean up the code with the help of reviews. |
|
Paging for reviews, anyone willing to lend a hand? |
|
Hey 👋, thanks for the PR. I'm the maintainer of the KGP JS subsystem. I think the idea of producing environment-agnostic JS code is worth investigating. It's similar to the popular request KT-47038 KJS / MPP: Split JS target into JsBrowser and JsNode. Just thinking out loud: A 'plain' output could support other use-cases, like Web Workers, Node Workers, Deno, or D8. KGP could provide sensible defaults for Browser and NodeJS subtargets, but also support 3rd party subtargets. As you discovered, the codebase is not very flexible. We're working on improving it, but it might take some time. Please could you create an issue https://kotl.in/issue and detail the high-level use case you want to achieve? |
Certainly! I can create an issue detailing the functionality being proposed here. But creating the issue aside, I don't think I can make any more progress on this pull request on my own without help, it's proven to be just a tad too difficult to add this without affecting other things in unwanted ways. |
|
Actually, it appears there already is an existing issue that describes the problem quite well, should we use that instead? |
Kotlin/JS currently only supports 2 execution environments, node and browser. The former is for execution as an application on node, and the latter is for browser scripting on the client. To develop an application in Kotlin that compiles to JS, one needs to use the nodejs option in build.gradle.kts, but this has the caveat that it is very specific to node, building the application as an npm package and pulling in npm + npm dependencies and node itself, among other things. It comes with a lot of extra features and things that may not be desired if the goal is to compile Kotlin to plain JS that can work in a generic environment. To remedy this, I suggest adding a generic target for JS, which will build the project directly into plain, simple executable JS with nothing else included and which is not specific to node or npm. This of course comes with the downside that you can't really use npm with a generic target, but for applications where a minimal build output is desired, it is expected that dependencies would be other libraries written in Kotlin that will be built to yield JS, so this hopefully shouldn't be an issue.