Kotlin and multiple instances of CssImport

Kotlin still does not support repeatable non-SOURCE annotations. That affects CssImport. And hence, this does not compile:

@Route
@CssImport("./styles/shared-styles.css")
@CssImport(value = "./styles/vaadin-text-field-styles.css", themeFor = "vaadin-text-field") // Compilation ERROR
class MainView(@Autowired service: GreetService) : VerticalLayout() {

Was hoping for a trick such as:

@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS)
annotation class CssImports(vararg val cssImport : CssImport)

@Route
@CssImports(
  CssImport("./styles/shared-styles.css"),
  CssImport(value = "./styles/vaadin-text-field-styles.css", themeFor = "vaadin-text-field")
)
class MainView(@Autowired service: GreetService) : VerticalLayout() {

But no luck. I am guessing Vaadin scanning is not picking it up correctly. Any suggestions?

Would it be possible to use the existing com.vaadin.flow.component.dependency.CssImport.Container annotation instead of defining your own?

Hi, unfortunately that’s a known issue with Kotlin, please see https://youtrack.jetbrains.com/issue/KT-12794 for more details (and please vote too).

The solution is to emulate Java behavior and use @CssImport.Container as Leif pointed out. Please find an example code here: https://github.com/mvysny/karibu10-helloworld-application/blob/master/src/main/kotlin/com/vaadin/flow/demo/helloworld/MainView.kt#L30

Perfect. I missed the existence of @CssImport.Container. Thank you!