Annotation Interface JsModule
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-levelfrontend/directory. - Add-on JARs:
META-INF/frontend/(recommended). The legacy locationMETA-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 ClassesModifier and TypeClassDescriptionstatic @interfaceInternal annotation to enable use of multipleJsModuleannotations. -
Required Element Summary
Required Elements -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionbooleanDefines 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 valueJavaScript module to load before using the annotatedComponentin 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 withJavaScript, 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, includingcontext://andbase://, are passed on unchanged. On any other class, such as aVaadinServiceInitListener, 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@JavaScriptwithJavaScript.Type.MODULEon 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 developmentOnlyDefines 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:
trueto load the module only in development mode,falseto always load it- Since:
- 24.2
- Default:
false
-