Annotation Interface JsDefinitionImport


@Documented @Target(TYPE) @Retention(RUNTIME) @Repeatable(Container.class) public @interface JsDefinitionImport
Gives the JavaScript of a JsDefinition that extends JsInvokers names from a JavaScript module, so an expression can call what the module exports without the module publishing it on window.
 @JsDefinition
 @JsDefinitionImport(from = "./gridConnector.js", namespace = "connector")
 public interface GridConnectorJs extends JsInvokers {

     @JsExpression("connector.initLazy(this)")
     void initLazy();
 }
 
The attributes name what is imported, like the parts of a JavaScript import statement: @JsDefinitionImport(from = "./my.js", defaultExport = "a", namespace = "b", exports = { "c", "d" }) gives the expressions what import a, { c, d }, * as b from "./my.js" would. Each attribute is optional and they can be combined, but at least one name is needed: a module that is only loaded for its side effects is declared with JsModule instead.

The default export is reached through defaultExport(), or as default of the namespace(). A named export keeps its name and can not be renamed. When two modules export the same name, import one of them as a namespace() instead.

The module is loaded the first time a method of the definition that declares the import is called, not when the application starts, so a module that only some views need does not slow down the others. Later calls use the loaded module. A JsModule on the same interface is still loaded eagerly.

Since the call waits for its modules, even ones that are already loaded, its JavaScript runs after the JavaScript of the calls that follow it in the same round trip, rather than in the order of the calls, and two calls that import different modules may run in either order. When a later call depends on what this one does, make the later call from the result of this one, for example by returning PendingJavaScriptResult and making the next call in its handler.

from() is resolved the way JsModule.value() is: a path starting with ./ names a file in the frontend folder of the project or in META-INF/frontend of a jar, and any other path names an npm package or a file in one.

The imports belong to the interface they are declared on. A method inherited from another interface sees the imports of the interface that declares it, not those of the interface it is called through. Several annotations can import from the same module; they are loaded as one module.

The names are given to the expression as parameters, so each one has to be a JavaScript identifier that is not a reserved word, such as default or this, and not $0, $1, …, which name the arguments of the call, or $executionContext, which Flow reserves. A name can be declared only once by the imports of an interface. The expression runs in a function of its own, so arguments in it holds the imported values rather than the arguments of the call: use $0, $1, … instead.

The annotation applies to an interface whose methods are called through a definition that extends JsInvokers, the definition itself or an interface it extends, and is ignored, with an error in the log of the build, on a class. The functions of a JsSnippets interface are not asynchronous and can not wait for a module to load, and an interface annotated with JsStringFallback stands in for JavaScript that names nothing of a module, so both are refused, as is an annotation that names nothing. The imports are given only to the methods the annotated interface declares itself, so an interface that declares no method of its own, such as a definition that only inherits its methods, is refused too: annotate the interface that declares the methods instead.

See Also:
  • Nested Class Summary

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

    Required Elements
    Modifier and Type
    Required Element
    Description
    The module to import from, resolved the way JsModule.value() is.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    The name the default export of the module is given, like a in import a from ".
    The named exports of the module to import, each under its own name, like c and d in import { c, d } from ".
    The name the whole module is given, like b in import * as b from ".
  • Element Details

    • from

      String from
      The module to import from, resolved the way JsModule.value() is.
      Returns:
      the path of the module
    • defaultExport

      String defaultExport
      The name the default export of the module is given, like a in import a from "./my.js". Empty for none.
      Returns:
      the name of the default export, or an empty string
      Default:
      ""
    • namespace

      String namespace
      The name the whole module is given, like b in import * as b from "./my.js". Empty for none.
      Returns:
      the name of the module, or an empty string
      Default:
      ""
    • exports

      String[] exports
      The named exports of the module to import, each under its own name, like c and d in import { c, d } from "./my.js".
      Returns:
      the names of the exports, empty for none
      Default:
      {}