Christian Gaisl
10/17/2021, 1:35 PMjs(IR) {
browser()
binaries.library()
}
which creates a folder inside the “build” folder called “productionLibrary”. This folder I could then import in my Angular App (or any other framework) with npm install absolutePathToProductionLibrary.
I have a few questions about that:
• Is this a/the recommended way to do this?
• I can only find classes annotated with @JsExport in the generated Javascript/Typescript code, do I have to do that for everything?
• I can’t annotate classes in the common/shared module of my Multiplatform library with @JsExport, how do I get those classes to show up in the generated JavaScript code?Svyatoslav Kuzmich [JB]
10/17/2021, 3:43 PMbinaries.executable()
is the one that produces .js + .d.ts
• You have to use @JsExport for everything that you want to see in JS. If you care about bundle size, you might not want to overuse it though.
• @JsExport is available in common code too. If you don't have an access to common/shared sources, you can create wrapper functions and classes and export those.Christian Gaisl
10/17/2021, 6:55 PMexpect annotation class CommonJsExport()
in common module and actual typealias CommonJsExport = JsExport
in Js module
I ran into another problem: When I try to export a class that uses Ktor, my TypeScript file suddenly contains the following:
type Nullable<T> = T | null | undefined
export namespace kotlinx.atomicfu {
function atomic$ref$<T>(initial: T, trace: kotlinx.atomicfu.TraceBase): kotlinx.atomicfu.AtomicRef<T>;
function atomic$boolean$(initial: boolean, trace: kotlinx.atomicfu.TraceBase): kotlinx.atomicfu.AtomicBoolean;
function atomic$int$(initial: number, trace: kotlinx.atomicfu.TraceBase): kotlinx.atomicfu.AtomicInt;
function atomic$long$(initial: kotlin.Long, trace: kotlinx.atomicfu.TraceBase): kotlinx.atomicfu.AtomicLong;
}
export namespace io.ktor.util {
function AttributesJsFn(concurrent: boolean): io.ktor.util.Attributes;
}
This makes my angular app not compile, as it cannot find kotlinx.atomicfu.TraceBase or io.ktor.util.Attributes. If i delete these lines manually, the code works and I can do a network call from my angular app, using ktor from the common code (which is kinda cool 😃)turansky
10/17/2021, 8:41 PMbinaries.executable() also produces ,js + .d.ts files, however they are located inside build/js/node_modules/MyPackageName/kotlinDistibution you can find in
build/distributions
folder of your subprojectandylamax
10/17/2021, 9:01 PMbinaries.library()
is the way to go. We are currently using it with react-native.
• I don't know why @JsExport
is not resolved for you in common code. But it does work like a charm for us.
• Regarding the kotlinx.atomicfu
exports, I am also surprised. We also have them in our exports, but they do not hinder us from using our exported library. We use them with the exports defined in .d.ts
without problemsSvyatoslav Kuzmich [JB]
10/17/2021, 9:31 PM… my App will get unreasonably large for a medium sized App?I would expect code size to be reasonable. What I meant is that currently, if you JsExport declarations that you don’t end up using in JS, your bundle might be bigger than necessary.
However the code does not compile, telling me that @JsExport is an unresolved reference.Could that be because you are not importing it? Could you try adding `
import kotlin.js.JsExport
?
kotlin.js
package is not included by default in common codeChristian Gaisl
10/19/2021, 8:50 PMkotlinx.atomicfu
exports: My angular project does not compile, throwing the same error that my IDE is showing me (red underline in d..ts file): TS2694: Namespace '".../jsLibraryForAngular/build/productionLibrary/jsLibraryForAngular".io.ktor.util' has no exported member 'Attributes'.
However, I am able to successfully compile and run the project by adding "skipLibCheck": true
to my compiler options in my tsconfig.json file. This does seem like a hack though and feels like it’s going to end up causing problems down the road.
The “faulty” lines are annotated even when looking at the .d.ts file from InteliJ, is it the same in your project? (What I am trying to find out is wether the error lies in my angular project configuration or in the d.ts file generation)
@Svyatoslav Kuzmich [JB] manually importing the Annotation with import kotlin.js.JsExport
did the trick, it now works. (The reason why I did not import it earlier is that it was not marked as an unresolved reference and even cmd-clicking it took me to the correct declaration file 🙃)shaktiman_droid
12/21/2021, 10:06 PMkotlinx.atomicfu.TraceBase
error for typscript
? I mean, without using the "skipLibCheck": true
@Svyatoslav Kuzmich [JB] Do you an answer for this? or is there a way to not have atomicfu
in the d.ts
generated file? The TraceBase
reference is not there. Atomicfu
comes from coroutine
library as dependencySvyatoslav Kuzmich [JB]
12/21/2021, 11:48 PMshaktiman_droid
12/22/2021, 12:01 AMcoroutine
or ktor
as dependency and you generate kotlin/js library. Your d.ts
file contains following
export namespace kotlinx.atomicfu {
function atomic$ref$<T>(initial: T, trace: kotlinx.atomicfu.TraceBase): kotlinx.atomicfu.AtomicRef<T>;
function atomic$boolean$(initial: boolean, trace: kotlinx.atomicfu.TraceBase): kotlinx.atomicfu.AtomicBoolean;
function atomic$int$(initial: number, trace: kotlinx.atomicfu.TraceBase): kotlinx.atomicfu.AtomicInt;
function atomic$long$(initial: kotlin.Long, trace: kotlinx.atomicfu.TraceBase): kotlinx.atomicfu.AtomicLong;
}
and it's just that. There is no reference generated for kotlinx.atomicfu.TraceBase
and that causes compilation issue when typescript
app is trying to use the above library.
One needs to add "skipLibCheck": true
so typescript doesn't strictly check for references and ignore that TraceBase
is not found.Svyatoslav Kuzmich [JB]
12/22/2021, 1:01 AMshaktiman_droid
12/22/2021, 1:02 AMshaktiman_droid
12/22/2021, 1:03 AM"skipLibCheck": true
check because it's not ideal. Is there a temporary solution you can think of?Svyatoslav Kuzmich [JB]
12/22/2021, 1:33 AMkotlinx.atomicfu.TraceBase
as a separate d.ts or concatenate it to existing one:
declare namespace kotlinx.atomicfu {
interface TraceBase {}
}
shaktiman_droid
12/22/2021, 1:35 AMpackage kotlinx.atomicfu
@JsExport
external interface TraceBase
shaktiman_droid
12/22/2021, 1:35 AMSvyatoslav Kuzmich [JB]
12/22/2021, 1:52 AMshaktiman_droid
12/22/2021, 1:55 AMshaktiman_droid
12/22/2021, 3:18 PMexport namespace
would be manual effort to add every time for a release. Is there a way to automatically include that in resulting build folder
?Svyatoslav Kuzmich [JB]
12/22/2021, 3:35 PMval copyDtsPatch by task<Copy> {
from("...")
into("$buildDir/...")
}
val yourBuildTask by tasks.named("taskYouUseToBuildDistro") {
dependsOn(copyDtsPatch)
}
But I’m not an expert, could be wrong.shaktiman_droid
12/22/2021, 3:37 PM