1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 00:59:24 -05:00
denoland-deno/cli/module_loader.rs

152 lines
4 KiB
Rust
Raw Normal View History

2020-01-02 15:13:47 -05:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
2020-09-05 20:34:02 -04:00
2019-06-09 09:08:20 -04:00
use crate::import_map::ImportMap;
use crate::module_graph::TypeLib;
use crate::permissions::Permissions;
use crate::program_state::ProgramState;
use deno_core::error::AnyError;
use deno_core::futures::future::FutureExt;
use deno_core::futures::Future;
use deno_core::ModuleLoadId;
use deno_core::ModuleLoader;
use deno_core::ModuleSpecifier;
use deno_core::OpState;
use std::cell::RefCell;
2019-11-16 19:17:47 -05:00
use std::pin::Pin;
use std::rc::Rc;
use std::str;
use std::sync::Arc;
pub struct CliModuleLoader {
2019-06-09 09:08:20 -04:00
/// When flags contains a `.import_map_path` option, the content of the
/// import map file will be resolved and set.
pub import_map: Option<ImportMap>,
pub lib: TypeLib,
pub is_main: bool,
}
impl CliModuleLoader {
pub fn new(maybe_import_map: Option<ImportMap>) -> Rc<Self> {
Rc::new(CliModuleLoader {
import_map: maybe_import_map,
lib: TypeLib::DenoWindow,
is_main: true,
})
}
pub fn new_for_worker() -> Rc<Self> {
Rc::new(CliModuleLoader {
import_map: None,
lib: TypeLib::DenoWorker,
is_main: false,
})
}
2020-04-27 19:12:38 -04:00
}
impl ModuleLoader for CliModuleLoader {
2019-06-09 09:08:20 -04:00
fn resolve(
&self,
op_state: Rc<RefCell<OpState>>,
2019-06-09 09:08:20 -04:00
specifier: &str,
referrer: &str,
2019-08-07 12:55:39 -04:00
is_main: bool,
) -> Result<ModuleSpecifier, AnyError> {
let program_state = {
let state = op_state.borrow();
state.borrow::<Arc<ProgramState>>().clone()
};
// FIXME(bartlomieju): hacky way to provide compatibility with repl
let referrer = if referrer.is_empty() && program_state.flags.repl {
"<unknown>"
} else {
referrer
};
2019-08-07 12:55:39 -04:00
if !is_main {
if let Some(import_map) = &self.import_map {
let result = import_map.resolve(specifier, referrer)?;
2019-10-03 09:16:06 -04:00
if let Some(r) = result {
return Ok(r);
2019-06-09 09:08:20 -04:00
}
}
}
let module_specifier =
ModuleSpecifier::resolve_import(specifier, referrer)?;
2019-06-09 09:08:20 -04:00
Ok(module_specifier)
}
2019-06-12 19:55:59 -04:00
fn load(
&self,
op_state: Rc<RefCell<OpState>>,
2019-06-12 19:55:59 -04:00
module_specifier: &ModuleSpecifier,
maybe_referrer: Option<ModuleSpecifier>,
_is_dynamic: bool,
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
let module_specifier = module_specifier.to_owned();
2019-08-07 12:55:39 -04:00
let module_url_specified = module_specifier.to_string();
let program_state = {
let state = op_state.borrow();
state.borrow::<Arc<ProgramState>>().clone()
};
// TODO(@kitsonk) this shouldn't be async
let fut = async move {
let compiled_module = program_state
.fetch_compiled_module(module_specifier, maybe_referrer)?;
Ok(deno_core::ModuleSource {
// Real module name, might be different from initial specifier
// due to redirections.
code: compiled_module.code,
2019-08-07 12:55:39 -04:00
module_url_specified,
module_url_found: compiled_module.name,
})
};
fut.boxed_local()
}
fn prepare_load(
&self,
op_state: Rc<RefCell<OpState>>,
_load_id: ModuleLoadId,
specifier: &ModuleSpecifier,
_maybe_referrer: Option<String>,
is_dynamic: bool,
) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> {
let specifier = specifier.clone();
let maybe_import_map = self.import_map.clone();
let state = op_state.borrow();
// The permissions that should be applied to any dynamically imported module
let dynamic_permissions = state.borrow::<Permissions>().clone();
let program_state = state.borrow::<Arc<ProgramState>>().clone();
let lib = if program_state.flags.unstable {
if self.lib == TypeLib::DenoWindow {
TypeLib::UnstableDenoWindow
} else {
TypeLib::UnstableDenoWorker
}
} else {
self.lib.clone()
};
drop(state);
// TODO(bartlomieju): `prepare_module_load` should take `load_id` param
async move {
program_state
.prepare_module_load(
specifier,
lib,
dynamic_permissions,
is_dynamic,
maybe_import_map,
)
.await
}
.boxed_local()
}
}