Bradleycorn
06/04/2025, 3:23 PM@JsExport
indicate that we cannot:
@ExperimentalJsExport
@Target(allowedTargets = [AnnotationTarget.CLASS, AnnotationTarget.PROPERTY, AnnotationTarget.FUNCTION, AnnotationTarget.FILE])
expect annotation class JsExport(source)
Exports top-level declaration on JS platform.
Compiled module exposes declarations that are marked with this annotation without name mangling.
This annotation can be applied to either files or top-level declarations.
It is currently prohibited to export the following kinds of declarations:
expect declarations
inline functions with reified type parameters
suspend functions
secondary constructors without @JsName
extension properties
enum classes
annotation classes
When I add @JsExport
to a basic enum enum class Num { ONE, TWO, THREE }
... it compiles OK, but when I try to use it in javascript (typesript) code, I get a runtime error Cannot read properties of undefined
when I import and use it, like const one = Num.ONE;
Edoardo Luppi
06/04/2025, 3:25 PM@JsExport
KDoc is outdated, and K/JS docs too (tho, to a lesser extent, and it will be fixed).
https://github.com/JetBrains/kotlin-web-site/pull/4831
I get a runtime errorCould you create a minimal example? It should work.
Bradleycorn
06/04/2025, 3:28 PMEdoardo Luppi
06/04/2025, 3:30 PMBradleycorn
06/04/2025, 3:41 PMjs {
browser {}
binaries.library()
generateTypeScriptDefinitions()
}
But using es2015 modules does not ...
js {
browser {}
// enums stop working with es2015 modules
useEsModules()
binaries.library()
generateTypeScriptDefinitions()
}
Edoardo Luppi
06/04/2025, 3:42 PMBradleycorn
06/04/2025, 3:45 PMimport * as kmp from '@my-org/shared';
// And then
const myClass = new kmp.org.my.shared.MyClass(kmp.org.my.shared.Num.ONE);
with the second configuration it looks like:
import { MyClass, Num } from '@my-org/shared';
// And then:
const myClass = new MyClass(Num.ONE);
Edoardo Luppi
06/04/2025, 3:47 PMbuild/dist/js/productionLibrary
, scroll all the way down, and paste here the export
section?Bradleycorn
06/04/2025, 3:47 PMEdoardo Luppi
06/04/2025, 3:48 PMBradleycorn
06/04/2025, 3:48 PMBradleycorn
06/04/2025, 3:56 PMexport {
MyClass as MyClass,
Num as Num,
};
Edoardo Luppi
06/04/2025, 3:57 PMNum
instance exists and is indeed exported.
Are you sure the error refers to Num.ONE
?Bradleycorn
06/04/2025, 3:57 PMNum.values = values;
Num.valueOf = valueOf;
defineProp(Num, 'ONE', Num_ONE_getInstance);
defineProp(Num, 'TWO', Num_TWO_getInstance);
defineProp(Num, 'THREE', Num_THREE_getInstance);
Bradleycorn
06/04/2025, 3:59 PMconst one = Num.ONE;
And it'll give me
Cannot read properties of undefined (reading 'ONE')on that line. and if I change it to:
const one = Num.valueOf("ONE");
it'll give me:
Cannot read properties of undefined (reading 'valueOf')
Edoardo Luppi
06/04/2025, 4:00 PMEdoardo Luppi
06/04/2025, 4:01 PMBradleycorn
06/04/2025, 4:02 PMBradleycorn
06/04/2025, 4:02 PMBradleycorn
06/04/2025, 4:03 PMEdoardo Luppi
06/04/2025, 4:03 PMBradleycorn
06/04/2025, 4:04 PM$ npm install file:/path/to/my/project/shared/build/dist/js/productionLibrary
Bradleycorn
06/04/2025, 4:04 PMEdoardo Luppi
06/04/2025, 4:06 PMNum
declaration.Bradleycorn
06/04/2025, 4:07 PMEdoardo Luppi
06/04/2025, 4:08 PMBradleycorn
06/04/2025, 4:22 PMBradleycorn
06/04/2025, 4:22 PMEdoardo Luppi
06/04/2025, 4:28 PMfile
protocol. I always avoid it as it causes stale files to be used.Bradleycorn
06/04/2025, 4:33 PMBradleycorn
06/04/2025, 4:34 PMKtList
...
Probably another caching issue, because that was working before.Edoardo Luppi
06/04/2025, 4:36 PMEdoardo Luppi
06/04/2025, 4:37 PMBradleycorn
06/04/2025, 4:39 PMBradleycorn
06/04/2025, 4:43 PMwhole-program
granularity still the workaround? I see on that ticket that you mentioned that per-file
is not yet stabilized (as of 9 months ago).Edoardo Luppi
06/04/2025, 4:44 PMEdoardo Luppi
06/04/2025, 4:44 PMuseCommonJs
, or the default UMD.Bradleycorn
06/04/2025, 4:45 PMBradleycorn
06/04/2025, 4:47 PMwhole-program
does seem to work.
Regarding your comment:
There is no real difference between usingandwhole-program
when consuming it.per-module
The difference might be in incremental compilation (whereIf I understand that correctly, there's no real "cost" to usingmight be faster) and output size (whereper-module
might perform better DCE).whole-program
whole-program
from the client (web app) side. It's really just during development on the kotlin side, it could slow down build times a bit. Is that right?Edoardo Luppi
06/04/2025, 4:48 PMBradleycorn
06/04/2025, 4:49 PMBradleycorn
06/04/2025, 4:51 PMfile:
based npm dependencies ...
The solution for me was do delete the angular cache in my web project, and then re-install the npm package:
$ rm -rf .angular/cache
$ rm -rf nodde_modules/@my-org
$ npm install