diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-02-17 14:32:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-17 14:32:57 +0100 |
commit | f6d6b24506410816833d802e1a8d9cd704f73289 (patch) | |
tree | 6ae2a286708ce0e2777aae0c5de616f7a39d7a6b /cli/program_state.rs | |
parent | 097efa100ad9d2b1f190330f9fb619c95fe86352 (diff) |
feat: support loading import map from URL (#9519)
This commit adds support for loading import maps from URLs,
both remote and local.
This feature is supported in CLI flag as well as in runtime
compiler API.
Diffstat (limited to 'cli/program_state.rs')
-rw-r--r-- | cli/program_state.rs | 38 |
1 files changed, 12 insertions, 26 deletions
diff --git a/cli/program_state.rs b/cli/program_state.rs index 684d33a8e..700981368 100644 --- a/cli/program_state.rs +++ b/cli/program_state.rs @@ -56,7 +56,7 @@ pub struct ProgramState { } impl ProgramState { - pub fn new(flags: flags::Flags) -> Result<Arc<Self>, AnyError> { + pub async fn build(flags: flags::Flags) -> Result<Arc<Self>, AnyError> { let custom_root = env::var("DENO_DIR").map(String::into).ok(); let dir = deno_dir::DenoDir::new(custom_root)?; let deps_cache_location = dir.root.join("deps"); @@ -94,11 +94,20 @@ impl ProgramState { let maybe_import_map: Option<ImportMap> = match flags.import_map_path.as_ref() { None => None, - Some(file_path) => { + Some(import_map_url) => { if !flags.unstable { exit_unstable("--import-map") } - Some(ImportMap::load(file_path)?) + let import_map_specifier = + ModuleSpecifier::resolve_url_or_path(&import_map_url).context( + format!("Bad URL (\"{}\") for import map.", import_map_url), + )?; + let file = file_fetcher + .fetch(&import_map_specifier, &Permissions::allow_all()) + .await?; + let import_map = + ImportMap::from_json(import_map_specifier.as_str(), &file.source)?; + Some(import_map) } }; @@ -276,18 +285,6 @@ impl ProgramState { None } } - - #[cfg(test)] - pub fn mock( - argv: Vec<String>, - maybe_flags: Option<flags::Flags>, - ) -> Arc<ProgramState> { - ProgramState::new(flags::Flags { - argv, - ..maybe_flags.unwrap_or_default() - }) - .unwrap() - } } // TODO(@kitsonk) this is only temporary, but should be refactored to somewhere @@ -346,14 +343,3 @@ fn source_map_from_code(code: String) -> Option<Vec<u8>> { None } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn thread_safe() { - fn f<S: Send + Sync>(_: S) {} - f(ProgramState::mock(vec![], None)); - } -} |