Annotation Interface JsModule


Defines a JavaScript module dependency on a Component class.

This is a build-time dependency: the referenced file is a bundle source, fed into Vite at build time, and is not served as a static resource at runtime. Use JavaScript for files that should be loaded at runtime (including external URLs and CDN-hosted scripts).

Source locations:

  • Application projects: src/main/frontend/ (recommended), or the legacy top-level frontend/ directory.
  • Add-on JARs: META-INF/frontend/ (recommended). The legacy location META-INF/resources/frontend/ is still supported but deprecated and emits a build-time warning.

For adding multiple JavaScript module files for a single component, use this annotation multiple times.

It is guaranteed that dependencies will be loaded only once. Loading order is guaranteed at the class level (annotations on different classes may appear in different orders relative to each other). @JsModule files load before JavaScript and CssImport.

Runtime URLs (with a scheme such as https://, context:// or base://, protocol-relative URLs (//) and values with a leading /) are deprecated. On a component, an interface or the app shell, they are not bundled but loaded at runtime as <script type="module">. On any other class, they are not loaded at runtime; a value with a leading / is still bundled from the frontend folder, and the frontend build fails if the file is not there. Use JavaScript with JavaScript.Type.MODULE for such files instead. See value().

NOTE: Currently all frontend resources are bundled together into one big bundle. JavaScript files loaded by one class will therefore be present on a view constructed by another class.

Since:
2.0
Author:
Vaadin Ltd
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static @interface 
    Internal annotation to enable use of multiple JsModule annotations.
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    JavaScript module to load before using the annotated Component in the browser.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Defines if the JavaScript should be loaded only when running in development mode (for development tooling etc.) or if it should always be loaded.
  • Element Details

    • value

      String value
      JavaScript module to load before using the annotated Component in the browser.

      The recommended value is a bundler import specifier ? typically a relative path (e.g. "./my-element.js"), an npm package specifier (e.g. "@scope/pkg/foo.js"), or an alias. The bundler resolves these against the configured frontend directory.

      Using a runtime URL (one starting with context://, base://, /, http://, https:// or //, or any other URL containing ://) is deprecated: on a component, an interface (such as a mixin implemented by components) or the app shell, such a value is not bundled but loaded at runtime as a <script type="module">, and a warning is logged during the production build. As with JavaScript, a value with a single leading / that contains a path traversal (..) is rejected with a warning and not loaded at all; protocol-relative // URLs and values with a scheme, including context:// and base://, are passed on unchanged. On any other class, such as a VaadinServiceInitListener, nothing loads the value at runtime: a URL is not loaded at all, and a value with a leading / is still bundled from the frontend folder, which makes the frontend build fail if the file is not there. Use @JavaScript with JavaScript.Type.MODULE on a component or the app shell for runtime modules instead. To bundle a file in the frontend folder, use a relative value such as ./foo.js.

      NOTE: In the case of using JsModule with LitTemplate, the value needs to point to a real file as it will be copied to the templates folder under target folder. An exported alias from the package will not work.

      Returns:
      a JavaScript module identifier
    • developmentOnly

      boolean developmentOnly
      Defines if the JavaScript should be loaded only when running in development mode (for development tooling etc.) or if it should always be loaded.

      By default, scripts are always loaded.

      Returns:
      true to load the module only in development mode, false to always load it
      Since:
      24.2
      Default:
      false