Import Attributes are an extension to the import syntax that allows specifying additional information to affect how the module is imported. This proposal, other than defining such syntax, also defines the first import attribtue: type
, which is passed to the host to specify the expected type of the loaded module. See the explainer for more information.
Possible future extensions include:
module
attribute, to import an object representing the dependency's defer
attribute, to import a module without evaluating it immediately.Not every import attribute needs to interact with host semantics, for example module
and defer
could be defined completely within ECMA-262.
Many productions operating on grammar are the same whether or not an
The host-defined abstract operation HostLoadImportedModule takes arguments referrer (a specifier (a String)moduleRequest (a
An example of when referrer can be a
<button type="button" onclick="import('./foo.mjs')">Click me</button>
there will be no active script or module at the time the import()
An implementation of HostLoadImportedModule must conform to the following requirements:
If this operation is called multiple times with the same (referrer, specifier) pair(referrer, moduleRequest.[[Specifier]], moduleRequest.[[Type]]) triple and it performs FinishLoadingImportedModule(referrer, specifiermoduleRequest, payload, result) where result is a normal completion, then it must perform FinishLoadingImportedModule(referrer, specifiermoduleRequest, payload, result) with the same result each time.
The abstract operation FinishLoadingImportedModule takes arguments referrer (a specifier (a String)moduleRequest (a
The description of the [[LoadedModules]] field of
A ModuleRequest Record represents the request to import a module with given import attributes. It consists of the following fields:
Field Name | Value Type | Meaning |
---|---|---|
[[Specifier]] | String | The module specifier |
[[Type]] |
a String, or |
The type attribute of this import
|
Field Name | Value Type | Meaning |
---|---|---|
[[Status]] |
|
Initially |
[[EvaluationError]] |
An |
A completion of type |
[[DFSIndex]] |
|
Auxiliary field used during Link and Evaluate only.
If [[Status]] is |
[[DFSAncestorIndex]] |
|
Auxiliary field used during Link and Evaluate only. If [[Status]] is |
[[RequestedModules]] |
|
A type import attribute used by the module represented by this record to request the importation of a module. The |
An ImportEntry Record is a
Field Name | Value Type | Meaning |
---|---|---|
[[ModuleRequest]] |
|
type import attribute of the |
[[ImportName]] | String |
The name under which the desired binding is exported by the module identified by [[ModuleRequest]]. The value |
[[LocalName]] | String | The name that is used to locally access the imported value from within the importing module. |
The import attributes proposal is intended to give key information about how modules are interpreted to hosts. For the Web embedding and environments which aim to be similar to it, the string is interpreted as the "module type". This is not the primary way the module type is determined (which, on the Web, would be the MIME type, and in other environments may be the file extension), but rather a secondary check which is required to pass for the module graph to load.
In the Web embedding, the following changes would be made to the HTML specification for import attributes:
The module map is keyed by the absolute URL and the type. Initially no other import attributes are supported, so they are not present.