question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using webjars: refering exported object without import

See original GitHub issue

I killed two days to figure out a feature, but I failed. I use Javalin with Vue (version 2) and would like to use some additional modules. These modules are not available as vanilla CDN/UNPKG package, but they are available as webjars.

I figures out, so long, the following:

I added the webjar support to my Server:

val server = Javalin.create { config ->
            config.enableWebjars()
            ...

I included the webjar in my dependency:

implementation("org.webjars.npm:joeattardi__emoji-button:4.5.0")

I imported the module in my layout.html:

    <script type="module" src="/webjars/joeattardi__emoji-button/4.5.0/dist/index.js"></script>

But I can’t access its exported class: EmojiButton. All the manuals uses ES6 import aliases:

import { EmojiButton } from '@joeattardi/emoji-button';
const picker = new EmojiButton();

But as layout.html is not a module, I can’t use import.

I feel I am almost there, but I simply can’t find the last piece of the puzzle. Is there any example? Also I would highly appreciate any guidance how I could register (use) new Vue components from webjars.

Thanks, Balage

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
balage1551commented, Nov 12, 2021

Finally, after lots of hesitation, I moved on. Javalin is great and I love it, but my application needs some features conflicting with some of the philosophy of Javalin. Having the whole Vue app in Javalin required so much compromise and extra work, that I split the front-end and back-end. The front-end is now handled by Vue CLI (in nodejs) which allows me to use js modules, SPA, router and any of the modules available for Vue while handling dependencies automatically. On the other hand, I still use Javalin for back-end, because it is a flexible, well configurable, light-weight web server.

I’ve even made a utility abstract class, which lets me split the handlers into several classes and which uses Reflections to dynamically scan the classpath for all handlers.


@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class RouteDisabled

abstract class RoutedHandler {
    protected val logger by LogFactory()

    fun addRoutes(server: Javalin) {
        route(server)
    }

    open fun route(server: Javalin) {}

    companion object {
        fun registerAll(server: Javalin) {
            Reflections(RoutedHandler::class.java.packageName)
                .getSubTypesOf(RoutedHandler::class.java)
                .forEach {
                    if (it.isAnnotationPresent(RouteDisabled::class.java)) {
                        logger.warn("DISABLED: ${it.simpleName.substringBeforeLast("Handler")} ")
                    } else {
                        logger.warn("ADDED:    ${it.simpleName.substringBeforeLast("Handler")} ")
                        val i = it.kotlin.objectInstance!!
                        i.addRoutes(server)
                    }
                }
        }
    }
}
0reactions
tipsycommented, Nov 12, 2021

Thanks for the update @balage1551, that sounds like a good solution. JavalinVue was developed with very simple projects in mind, so it makes sense to move to a standard frontend build 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using webjars: refering exported object without import #1431
I use Javalin with Vue (version 2) and would like to use some additional modules. These modules are not available as vanilla CDN/UNPKG...
Read more >
Web Libraries in Jars - WebJars
angular-order-object-by, org.webjars.bower, angular-order-object-by ... imported-template, org.webjars.bowergithub.juicy, imported-template, 3.2.1.
Read more >
import owl.carousel from webpack - jquery - Stack Overflow
Owl.carousel needs jQuery library imported inside it because it uses $ variable, so this is problem and it is why webpack-loader-syntax must ...
Read more >
Maven. Importing | IntelliJ IDEA Documentation - JetBrains
Use this list to specify which JDK to use when the maven project is imported. You can choose one of the following options:...
Read more >
org.webjars.npm:types__use-sync-external-store 0.0.3 ... - Libraries.io
If the module you're referencing is an external module (uses export ), use an import. If the module you're referencing is an ambient...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found