Annotation Interface JsDefinitionImport
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 ClassesModifier and TypeClassDescriptionstatic @interfaceInternal annotation to enable use of multipleJsDefinitionImportannotations. -
Required Element Summary
Required ElementsModifier and TypeRequired ElementDescriptionThe module to import from, resolved the wayJsModule.value()is. -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionThe name the default export of the module is given, likeainimport a from ".String[]The named exports of the module to import, each under its own name, likecanddinimport { c, d } from ".The name the whole module is given, likebinimport * as b from ".
-
Element Details
-
from
String fromThe module to import from, resolved the wayJsModule.value()is.- Returns:
- the path of the module
-
defaultExport
String defaultExportThe name the default export of the module is given, likeainimport a from "./my.js". Empty for none.- Returns:
- the name of the default export, or an empty string
- Default:
""
-
namespace
String namespaceThe name the whole module is given, likebinimport * as b from "./my.js". Empty for none.- Returns:
- the name of the module, or an empty string
- Default:
""
-
exports
String[] exportsThe named exports of the module to import, each under its own name, likecanddinimport { c, d } from "./my.js".- Returns:
- the names of the exports, empty for none
- Default:
{}
-