You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Exporting enum to a class to be able to map additional properties and functions is increasing the JS bundle size. An alternative could be to export an enum as a string for example:
@KustomExport(exportMode =ExportMode.Enum.asString)
enumclassDirection {
NORTH, SOUTH, WEST, EAST
}
We could have a default mode to auto that could choose the mode asString or asInt if there is only one String/Int parameter, and choose the full wrapper when there is more than 1 constructor param or a method.
importsample._enum.DirectionasCommonDirection// The annotated enum in commonMaintypealiasDirection=Stringpublicfun Direction.importDirection(): CommonDirection=CommonDirection.valueOf(this)
publicfun CommonDirection.exportDirection(): Direction=this.name
@JsExport
publicconstvalDirections_NORTH:Direction="NORTH"
@JsExport
publicconstvalDirections_SOUTH:Direction="SOUTH"
@JsExport
publicconstvalDirections_WEST:Direction="WEST"
@JsExport
publicconstvalDirections_EAST:Direction="EAST"
Bundle size (js) : 428 chars saved (no compressed) when exporting as strings (vs the full fledged export). A big part of the gain come from removing the object Directions and using (const) val instead, if we keep an object Dimensions the gain is only 14 chars. (So it's probably better to remove the object for the full fledged export, also const as no impact on bundle size.)
Limitations:
only available when there is no methods/properties (we may be able to use the 1st param instead of name when only one param is defined)
fun goTo(d: Direction) will be exported as goTo(direction: string); (as expected). It allows passing any string value, but a bad string will generate an IllegalStateException, possibly crashing the app.
Eventually there is an issue today with KSP/KotlinJsIr on multi-modules where external dependencies are not resolvable (WIP). With the current implementation, we expect a class not resolvable to be from another module, and it could be an issue with typealias resolution (not tested yet).
Exporting enum to a class to be able to map additional properties and functions is increasing the JS bundle size. An alternative could be to export an enum as a string for example:
We could have a default mode to auto that could choose the mode asString or asInt if there is only one String/Int parameter, and choose the full wrapper when there is more than 1 constructor param or a method.
Full fledge wrapper
Export as String
Bundle size (js) : 428 chars saved (no compressed) when exporting as strings (vs the full fledged export). A big part of the gain come from removing the object Directions and using (const) val instead, if we keep an object
Dimensions
the gain is only 14 chars. (So it's probably better to remove the object for the full fledged export, alsoconst
as no impact on bundle size.)Limitations:
name
when only one param is defined)fun goTo(d: Direction)
will be exported asgoTo(direction: string);
(as expected). It allows passing any string value, but a bad string will generate an IllegalStateException, possibly crashing the app.Eventually there is an issue today with KSP/KotlinJsIr on multi-modules where external dependencies are not resolvable (WIP). With the current implementation, we expect a class not resolvable to be from another module, and it could be an issue with typealias resolution (not tested yet).
Also a little note about enums, Kotlin 1.6.20 should export enums without an additional layer... https://youtrack.jetbrains.com/issue/KT-37916
The text was updated successfully, but these errors were encountered: