https://kotlinlang.org logo
#javascript
Title
# javascript
x

xxfast

03/22/2023, 10:19 PM
Hi all. Fairly new to kotlin js. So don't mind me if this question doesn't make sense 😅 I want to provide different implmentations for
browser
and
nodejs
. Are we able to have a seperate source set for js browser and js node, so that any consumer of my library be able chose one or the other depending on their setup?
Copy code
js("browser", IR) {
  browser()
}

js("node", IR) {
  nodejs()
}

// browser project
js(IR){
  browser()
}

jsMain by getting {
  dependencies {
    implementation(project(":myLibrary")) // gets sources from :myLibrary:browserMain
  }
}

// node project
js(IR){
  nodejs()
}

jsMain by getting {
  dependencies {
    implementation(project(":myLibrary"))  // gets sources from :myLibrary:nodeMain
  }
}
e

ephemient

03/22/2023, 10:37 PM
a

Adam S

03/22/2023, 10:37 PM
you can add a custom Gradle attribute to distinguish between different targets that have the same platform https://kotlinlang.org/docs/multiplatform-set-up-targets.html#distinguish-several-targets-for-one-platform but tbh I think it will probably be easier to just make separate subprojects! one for browser, another for NodeJS, and another for shared code. Repetitive? Perhaps. Easy to understand and work with? Very much yes.
👀 1
x

xxfast

03/22/2023, 11:25 PM
Can we use these attributes to distinguish the target from jsMain? not sure how we can use the attributes 🤔
e

ephemient

03/22/2023, 11:51 PM
Adam, multiple in one project doesn't work well until https://youtrack.jetbrains.com/issue/KT-28194 is resolved
separate projects is a pain for consumers unless you also ship dependent substitution rules
tl;dr it kinda sucks right now. IMO the least annoying thing to do right now is sniff whether you're in a browser environment or not, at runtime
x

xxfast

03/23/2023, 12:04 AM
how do i do this check at runtime? 😬
x

xxfast

03/23/2023, 1:12 PM
Ended up splitting this up to multiple modules, one for
nodejs
and one for
browser
. Couldn't do a runtime check because okio-nodefilesystem has a module dependency for
js { nodejs() }
@ephemient can you elaborate on what you meant by dependent substitution rules?
5 Views