1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

refactor: upgrade to import_map v0.6 (#13368)

This commit is contained in:
David Sherret 2022-01-13 18:17:56 -05:00 committed by GitHub
parent 9975ede773
commit eda6e58520
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 15 deletions

4
Cargo.lock generated
View file

@ -1907,9 +1907,9 @@ checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed"
[[package]]
name = "import_map"
version = "0.4.0"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f91db0bc4eff39727c95caa6e70268df077d7ebde57c1454a9cb8ddeb76b9689"
checksum = "7f64f821df8ee00a0fba2dde6296af519eff7d823542b057c1b8c40ca1d58f4c"
dependencies = [
"indexmap",
"log",

View file

@ -61,7 +61,7 @@ encoding_rs = "=0.8.29"
env_logger = "=0.8.4"
fancy-regex = "=0.7.1"
http = "=0.2.4"
import_map = "=0.4.0"
import_map = "=0.6.0"
jsonc-parser = { version = "=0.17.1", features = ["serde"] }
libc = "=0.2.106"
log = { version = "=0.4.14", features = ["serde"] }

View file

@ -62,6 +62,7 @@ use crate::file_fetcher::get_source_from_data_url;
use crate::fs_util;
use crate::logger;
use crate::lsp::logging::lsp_log;
use crate::proc_state::import_map_from_text;
use crate::tools::fmt::format_file;
use crate::tools::fmt::format_parsed_source;
@ -482,12 +483,9 @@ impl Inner {
)
})?
};
let import_map = Arc::new(ImportMap::from_json(
&import_map_url.to_string(),
&import_map_json,
)?);
let import_map = import_map_from_text(&import_map_url, &import_map_json)?;
self.maybe_import_map_uri = Some(import_map_url);
self.maybe_import_map = Some(import_map);
self.maybe_import_map = Some(Arc::new(import_map));
} else {
self.maybe_import_map = None;
}

View file

@ -7,6 +7,7 @@ use crate::emit;
use crate::errors::get_error_class_name;
use crate::flags;
use crate::graph_util::graph_valid;
use crate::proc_state::import_map_from_text;
use crate::proc_state::ProcState;
use crate::resolver::ImportMapResolver;
use crate::resolver::JsxResolver;
@ -24,7 +25,6 @@ use deno_core::Extension;
use deno_core::ModuleSpecifier;
use deno_core::OpState;
use deno_runtime::permissions::Permissions;
use import_map::ImportMap;
use serde::Deserialize;
use serde::Serialize;
use std::cell::RefCell;
@ -174,8 +174,8 @@ async fn op_emit(
.with_context(|| {
format!("Bad URL (\"{}\") for import map.", import_map_str)
})?;
let import_map = if let Some(value) = args.import_map {
ImportMap::from_json(import_map_specifier.as_str(), &value.to_string())?
let import_map_source = if let Some(value) = args.import_map {
Arc::new(value.to_string())
} else {
let file = ps
.file_fetcher
@ -187,8 +187,10 @@ async fn op_emit(
import_map_specifier, e
))
})?;
ImportMap::from_json(import_map_specifier.as_str(), &file.source)?
file.source
};
let import_map =
import_map_from_text(&import_map_specifier, &import_map_source)?;
Some(ImportMapResolver::new(Arc::new(import_map)))
} else if args.import_map.is_some() {
return Err(generic_error("An importMap was specified, but no importMapPath was provided, which is required."));

View file

@ -51,6 +51,7 @@ use deno_runtime::deno_web::BlobStore;
use deno_runtime::inspector_server::InspectorServer;
use deno_runtime::permissions::Permissions;
use import_map::ImportMap;
use log::warn;
use std::collections::HashSet;
use std::env;
use std::fs::File;
@ -230,7 +231,7 @@ impl ProcState {
import_map_specifier
))?;
let import_map =
ImportMap::from_json(import_map_specifier.as_str(), &file.source)?;
import_map_from_text(&import_map_specifier, &file.source)?;
Some(Arc::new(import_map))
}
};
@ -676,6 +677,25 @@ impl SourceMapGetter for ProcState {
}
}
pub fn import_map_from_text(
specifier: &Url,
json_text: &str,
) -> Result<ImportMap, AnyError> {
let result = ImportMap::from_json_with_diagnostics(specifier, json_text)?;
if !result.diagnostics.is_empty() {
warn!(
"Import map diagnostics:\n{}",
result
.diagnostics
.into_iter()
.map(|d| format!(" - {}", d))
.collect::<Vec<_>>()
.join("\n")
)
}
Ok(result.import_map)
}
fn source_map_from_code(code: String) -> Option<Vec<u8>> {
let lines: Vec<&str> = code.split('\n').collect();
if let Some(last_line) = lines.last() {

View file

@ -31,7 +31,7 @@ impl Resolver for ImportMapResolver {
) -> Result<ModuleSpecifier, AnyError> {
self
.0
.resolve(specifier, referrer.as_str())
.resolve(specifier, referrer)
.map_err(|err| err.into())
}
}

View file

@ -50,7 +50,7 @@ impl Resolver for DocResolver {
) -> Result<ModuleSpecifier, AnyError> {
if let Some(import_map) = &self.import_map {
return import_map
.resolve(specifier, referrer.as_str())
.resolve(specifier, referrer)
.map_err(AnyError::from);
}