pub trait ModuleLoader {
// Required methods
fn resolve(
&self,
specifier: &str,
referrer: &str,
kind: ResolutionKind,
) -> Result<ModuleSpecifier, ModuleLoaderError>;
fn load(
&self,
module_specifier: &ModuleSpecifier,
maybe_referrer: Option<&ModuleLoadReferrer>,
options: ModuleLoadOptions,
) -> ModuleLoadResponse;
// Provided methods
fn import_meta_resolve(
&self,
specifier: &str,
referrer: &str,
) -> Result<ModuleSpecifier, ModuleLoaderError> { ... }
fn prepare_load(
&self,
_module_specifier: &ModuleSpecifier,
_maybe_referrer: Option<String>,
_options: ModuleLoadOptions,
) -> Pin<Box<dyn Future<Output = Result<(), ModuleLoaderError>>>> { ... }
fn finish_load(&self) { ... }
fn code_cache_ready(
&self,
_module_specifier: ModuleSpecifier,
_hash: u64,
_code_cache: &[u8],
) -> Pin<Box<dyn Future<Output = ()>>> { ... }
fn purge_and_prevent_code_cache(&self, _module_specifier: &str) { ... }
fn get_source_map(&self, _file_name: &str) -> Option<Cow<'_, [u8]>> { ... }
fn load_external_source_map(
&self,
_source_map_url: &str,
) -> Option<Cow<'_, [u8]>> { ... }
fn get_source_mapped_source_line(
&self,
_file_name: &str,
_line_number: usize,
) -> Option<String> { ... }
fn get_host_defined_options<'s, 'i>(
&self,
_scope: &mut PinScope<'s, 'i>,
_name: &str,
) -> Option<Local<'s, Data>> { ... }
}Required Methods§
Sourcefn resolve(
&self,
specifier: &str,
referrer: &str,
kind: ResolutionKind,
) -> Result<ModuleSpecifier, ModuleLoaderError>
fn resolve( &self, specifier: &str, referrer: &str, kind: ResolutionKind, ) -> Result<ModuleSpecifier, ModuleLoaderError>
Returns an absolute URL. When implementing an spec-complaint VM, this should be exactly the algorithm described here: https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier
ResolutionKind::MainModule can be used to resolve from current working directory or
apply import map for child imports.
ResolutionKind::DynamicImport can be used to check permissions or deny
dynamic imports altogether.
Sourcefn load(
&self,
module_specifier: &ModuleSpecifier,
maybe_referrer: Option<&ModuleLoadReferrer>,
options: ModuleLoadOptions,
) -> ModuleLoadResponse
fn load( &self, module_specifier: &ModuleSpecifier, maybe_referrer: Option<&ModuleLoadReferrer>, options: ModuleLoadOptions, ) -> ModuleLoadResponse
Given ModuleSpecifier, load its source code.
is_dyn_import can be used to check permissions or deny
dynamic imports altogether.
Provided Methods§
Sourcefn import_meta_resolve(
&self,
specifier: &str,
referrer: &str,
) -> Result<ModuleSpecifier, ModuleLoaderError>
fn import_meta_resolve( &self, specifier: &str, referrer: &str, ) -> Result<ModuleSpecifier, ModuleLoaderError>
Override to customize the behavior of import.meta.resolve resolution.
Sourcefn prepare_load(
&self,
_module_specifier: &ModuleSpecifier,
_maybe_referrer: Option<String>,
_options: ModuleLoadOptions,
) -> Pin<Box<dyn Future<Output = Result<(), ModuleLoaderError>>>>
fn prepare_load( &self, _module_specifier: &ModuleSpecifier, _maybe_referrer: Option<String>, _options: ModuleLoadOptions, ) -> Pin<Box<dyn Future<Output = Result<(), ModuleLoaderError>>>>
This hook can be used by implementors to do some preparation work before starting loading of modules.
For example implementor might download multiple modules in parallel and transpile them to final JS sources before yielding control back to the runtime.
It’s not required to implement this method.
Sourcefn finish_load(&self)
fn finish_load(&self)
This hook can be used by implementors to do some cleanup work after loading of modules. The hook is called for all loads, whether they succeeded or not.
For example implementor might drop transpilation and static analysis caches before yielding control back to the runtime.
It’s not required to implement this method.
Sourcefn code_cache_ready(
&self,
_module_specifier: ModuleSpecifier,
_hash: u64,
_code_cache: &[u8],
) -> Pin<Box<dyn Future<Output = ()>>>
fn code_cache_ready( &self, _module_specifier: ModuleSpecifier, _hash: u64, _code_cache: &[u8], ) -> Pin<Box<dyn Future<Output = ()>>>
Called when new v8 code cache is available for this module. Implementors can store the provided code cache for future executions of the same module.
It’s not required to implement this method.
Sourcefn purge_and_prevent_code_cache(&self, _module_specifier: &str)
fn purge_and_prevent_code_cache(&self, _module_specifier: &str)
Called when V8 code cache should be ignored for this module. This can happen if eg. module causes a V8 warning, like when using deprecated import assertions. Implementors should make sure that the code cache for this module is purged and not saved anymore.
It’s not required to implement this method.
Sourcefn get_source_map(&self, _file_name: &str) -> Option<Cow<'_, [u8]>>
fn get_source_map(&self, _file_name: &str) -> Option<Cow<'_, [u8]>>
Returns a source map for given file_name.
This function will soon be deprecated or renamed.
Sourcefn load_external_source_map(
&self,
_source_map_url: &str,
) -> Option<Cow<'_, [u8]>>
fn load_external_source_map( &self, _source_map_url: &str, ) -> Option<Cow<'_, [u8]>>
Loads an external source map file referenced by a module.