vitfo
vitfo

Reputation: 10291

Vaadin @JavaScript for different builds

I use Vaadin @JavaScript annotation to load JavaScript files for my application. It works great but I would need different JavaScript loaded for differents builds. The idea is to have something like these:

@JavaScript("url.from.properties.or.pom")

So for DEV I would get @JavaScript("https://example.com/test/js/embed.js") and for PROD @JavaScript("https://example.com/production/js/embed.js"). The script url value should be taken from application.properties or pom.xml.

I cannot figure out how to do it. I use Vaadin 8 with Maven and Spring Boot. Thank you in advance.

Upvotes: 2

Views: 60

Answers (1)

Leif Åstrand
Leif Åstrand

Reputation: 8001

There's no direct support for what you want to do, but I can come up with three different solutions that you could consider.

  1. Register a DependencyFilter that dynamically rewrites the dependency URL from the annotation depending on the situation.
  2. Create separate Java classes for each case (with all the actual functionality in a shared super class). You can then have either runtime logic or use e.g. different Spring configurations to choose exactly which class to use.
  3. Remove the @JavaScript annotation and instead call JavaScript.eval from onAttach to somehow dynamically inject the script you want.

Upvotes: 3

Related Questions