Felix
06/10/2024, 11:22 AMjvm and js to kotlin 2.0. In this project I've a gradle module producing a ES module (contains useEsModules ). This module consumes a CJS module from NPM (node-jose), however the generated JS code does not seem to be correct.
Looking into build/dist/js/productionLibrary , the generated code contains
import * as Jose from 'node-jose';
and then uses Jose to access the exported symbols, e.g.
Jose.JWK.asKey(...)
Looking into https://nodejs.org/docs/latest-v18.x/api/esm.html#esm_commonjs_namespaces, it seems the correct way of importing a CJS module in an ESM module would be
import {default as Jose} from 'node-jose'
The Jose object is defined in Kotlin as
@JsModule("node-jose")
@JsNonModule
external object Jose {
...
}turansky
06/10/2024, 11:55 AM@file:JsModule("node-jose")
@JsName("default")
external object Jose {
...
}Felix
06/12/2024, 3:07 PM@file:JsNonModule
to be able to use it from UMD.turansky
06/12/2024, 3:33 PMFelix
06/12/2024, 3:35 PMFelix
06/12/2024, 3:37 PM@JsModule depends on the target module? Because I went back to CJS and then I had to revert the change. I.e. use
@JsModule("node-jose")
@JsNonModule
external object Jose {
...
}turansky
06/12/2024, 3:39 PMFelix
06/12/2024, 3:40 PMturansky
06/12/2024, 3:43 PMpackage.json configurationturansky
06/12/2024, 3:44 PMFelix
06/12/2024, 3:45 PMturansky
06/12/2024, 3:47 PMBut I need KMP to produce artefacts for all the formats (CJS, ESM), right?Library?
Felix
06/12/2024, 3:50 PMFelix
06/12/2024, 3:52 PMturansky
06/12/2024, 3:53 PMturansky
06/12/2024, 3:54 PMFelix
06/12/2024, 3:55 PMturansky
06/12/2024, 3:55 PMturansky
06/12/2024, 3:56 PMFelix
06/12/2024, 3:59 PM@JsModule kotlin decoration that we discussed at the beginning of this question does depend on the output format of the module where it is used?
I.e. if the output format is CJS, then I need to use the original annotation format, where @JsModule annotates the class directly, while if the output format is ESM then I need to annotate the file, as you suggested?turansky
06/12/2024, 4:00 PMturansky
06/12/2024, 4:03 PMturansky
06/12/2024, 4:04 PM