Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ rustflags = [
"--cfg",
"tokio_unstable",
]

[patch.crates-io]
import_map = { path = "./import_map/rs-lib" }
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
path = cli/bench/testdata/lsp_benchdata
url = https://github.com/denoland/deno_lsp_benchdata.git
shallow = true
[submodule "import_map"]
path = import_map
url = https://github.com/CertainLach/import_map.git
shallow = true
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ pub struct Flags {
/// Flags that aren't exposed in the CLI, but are used internally.
pub internal: InternalFlags,
pub ignore: Vec<String>,
pub import_map_path: Option<String>,
pub import_map_paths: Vec<String>,
pub env_file: Option<Vec<String>>,
pub inspect_brk: Option<SocketAddr>,
pub inspect_wait: Option<SocketAddr>,
Expand Down Expand Up @@ -4388,6 +4388,7 @@ fn import_map_arg() -> Arg {
<p(245)>Docs: https://docs.deno.com/runtime/manual/basics/import_maps</>",
))
.value_hint(ValueHint::FilePath)
.action(ArgAction::Append)
.help_heading(DEPENDENCY_MANAGEMENT_HEADING)
}

Expand Down Expand Up @@ -6325,7 +6326,10 @@ fn inspect_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
}

fn import_map_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
flags.import_map_path = matches.remove_one::<String>("import-map");
flags.import_map_paths = matches
.remove_many::<String>("import-map")
.unwrap_or_default()
.collect();
}

fn env_file_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
Expand Down Expand Up @@ -8375,7 +8379,7 @@ mod tests {
print: false,
code: "42".to_string(),
}),
import_map_path: Some("import_map.json".to_string()),
import_map_paths: vec!["import_map.json".to_string()],
no_remote: true,
config_flag: ConfigFlag::Path("tsconfig.json".to_owned()),
type_check_mode: TypeCheckMode::None,
Expand Down Expand Up @@ -8470,7 +8474,7 @@ mod tests {
is_default_command: false,
json: false,
}),
import_map_path: Some("import_map.json".to_string()),
import_map_paths: vec!["import_map.json".to_string()],
no_remote: true,
config_flag: ConfigFlag::Path("tsconfig.json".to_owned()),
type_check_mode: TypeCheckMode::None,
Expand Down Expand Up @@ -9058,7 +9062,7 @@ mod tests {
subcommand: DenoSubcommand::Run(RunFlags::new_default(
"script.ts".to_string(),
)),
import_map_path: Some("import_map.json".to_owned()),
import_map_paths: vec!["import_map.json".to_owned()],
code_cache_enabled: true,
..Flags::default()
}
Expand All @@ -9080,7 +9084,7 @@ mod tests {
file: Some("script.ts".to_string()),
json: false,
}),
import_map_path: Some("import_map.json".to_owned()),
import_map_paths: vec!["import_map.json".to_owned()],
..Flags::default()
}
);
Expand All @@ -9100,7 +9104,7 @@ mod tests {
subcommand: DenoSubcommand::Cache(CacheFlags {
files: svec!["script.ts"],
}),
import_map_path: Some("import_map.json".to_owned()),
import_map_paths: vec!["import_map.json".to_owned()],
..Flags::default()
}
);
Expand All @@ -9125,7 +9129,7 @@ mod tests {
lint: false,
filter: None,
}),
import_map_path: Some("import_map.json".to_owned()),
import_map_paths: vec!["import_map.json".to_owned()],
..Flags::default()
}
);
Expand Down Expand Up @@ -9360,7 +9364,7 @@ mod tests {
force: true,
}
),),
import_map_path: Some("import_map.json".to_string()),
import_map_paths: vec!["import_map.json".to_string()],
no_remote: true,
config_flag: ConfigFlag::Path("tsconfig.json".to_owned()),
type_check_mode: TypeCheckMode::None,
Expand Down Expand Up @@ -11081,7 +11085,7 @@ mod tests {
exclude: vec!["exclude.txt".to_string()],
eszip: false
}),
import_map_path: Some("import_map.json".to_string()),
import_map_paths: vec!["import_map.json".to_string()],
no_remote: true,
code_cache_enabled: false,
config_flag: ConfigFlag::Path("tsconfig.json".to_owned()),
Expand Down
77 changes: 29 additions & 48 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,12 +576,11 @@ impl CliOptions {
///
/// This will NOT include the config file if it
/// happens to be an import map.
pub fn resolve_specified_import_map_specifier(
pub fn resolve_specified_import_map_specifiers(
&self,
) -> Result<Option<ModuleSpecifier>, ImportMapSpecifierResolveError> {
resolve_import_map_specifier(
self.flags.import_map_path.as_deref(),
self.workspace().root_deno_json().map(|c| c.as_ref()),
) -> Result<Vec<ModuleSpecifier>, ImportMapSpecifierResolveError> {
resolve_import_map_specifiers(
self.flags.import_map_paths.iter().map(String::as_ref),
&self.initial_cwd,
)
}
Expand Down Expand Up @@ -1322,9 +1321,12 @@ impl CliOptions {
);
}

if let Ok(Some(import_map_path)) = self
.resolve_specified_import_map_specifier()
.map(|ms| ms.and_then(|ref s| s.to_file_path().ok()))
for import_map_path in self
.resolve_specified_import_map_specifiers()
.ok()
.into_iter()
.flatten()
.flat_map(|ms| ms.to_file_path().ok())
{
full_paths.push(import_map_path);
}
Expand Down Expand Up @@ -1417,28 +1419,20 @@ pub struct ImportMapSpecifierResolveError {
source: deno_path_util::ResolveUrlOrPathError,
}

fn resolve_import_map_specifier(
maybe_import_map_path: Option<&str>,
maybe_config_file: Option<&ConfigFile>,
/// Note: It doesn't include deno.json
fn resolve_import_map_specifiers<'i>(
maybe_import_map_paths: impl IntoIterator<Item = &'i str>,
current_dir: &Path,
) -> Result<Option<Url>, ImportMapSpecifierResolveError> {
if let Some(import_map_path) = maybe_import_map_path {
if let Some(config_file) = &maybe_config_file
&& config_file.json.import_map.is_some()
{
log::warn!(
"{} the configuration file \"{}\" contains an entry for \"importMap\" that is being ignored.",
colors::yellow("Warning"),
config_file.specifier,
);
}
let specifier =
deno_path_util::resolve_url_or_path(import_map_path, current_dir)
.map_err(|source| ImportMapSpecifierResolveError { source })?;
Ok(Some(specifier))
} else {
Ok(None)
}
) -> Result<Vec<Url>, ImportMapSpecifierResolveError> {
maybe_import_map_paths
.into_iter()
.map(|import_map_path| {
let specifier =
deno_path_util::resolve_url_or_path(import_map_path, current_dir)
.map_err(|source| ImportMapSpecifierResolveError { source })?;
Ok(specifier)
})
.collect()
}

/// Resolves the no_prompt value based on the cli flags and environment.
Expand Down Expand Up @@ -1717,43 +1711,30 @@ mod test {

#[test]
fn resolve_import_map_flags_take_precedence() {
let config_text = r#"{
"importMap": "import_map.json"
}"#;
let cwd = &std::env::current_dir().unwrap();
let config_specifier = Url::parse("file:///deno/deno.jsonc").unwrap();
let config_file = ConfigFile::new(config_text, config_specifier).unwrap();
let actual = resolve_import_map_specifier(
Some("import-map.json"),
Some(&config_file),
cwd,
);
let actual = resolve_import_map_specifiers(["import-map.json"], cwd);
let import_map_path = cwd.join("import-map.json");
let expected_specifier =
deno_path_util::url_from_file_path(&import_map_path).unwrap();
assert!(actual.is_ok());
let actual = actual.unwrap();
assert_eq!(actual, Some(expected_specifier));
assert_eq!(actual, vec![expected_specifier]);
}

#[test]
fn resolve_import_map_none() {
let config_text = r#"{}"#;
let config_specifier = Url::parse("file:///deno/deno.jsonc").unwrap();
let config_file = ConfigFile::new(config_text, config_specifier).unwrap();
let actual =
resolve_import_map_specifier(None, Some(&config_file), Path::new("/"));
let actual = resolve_import_map_specifiers([], Path::new("/"));
assert!(actual.is_ok());
let actual = actual.unwrap();
assert_eq!(actual, None);
assert_eq!(actual, vec![]);
}

#[test]
fn resolve_import_map_no_config() {
let actual = resolve_import_map_specifier(None, None, Path::new("/"));
let actual = resolve_import_map_specifiers([], Path::new("/"));
assert!(actual.is_ok());
let actual = actual.unwrap();
assert_eq!(actual, None);
assert_eq!(actual, vec![]);
}

#[test]
Expand Down
70 changes: 33 additions & 37 deletions cli/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ struct CliSpecifiedImportMapProvider {
impl SpecifiedImportMapProvider for CliSpecifiedImportMapProvider {
async fn get(
&self,
) -> Result<Option<deno_resolver::workspace::SpecifiedImportMap>, AnyError>
{
) -> Result<Vec<deno_resolver::workspace::SpecifiedImportMap>, AnyError> {
async fn resolve_import_map_value_from_specifier(
specifier: &Url,
file_fetcher: &CliFileFetcher,
Expand All @@ -204,40 +203,37 @@ impl SpecifiedImportMapProvider for CliSpecifiedImportMapProvider {
}
}

let maybe_import_map_specifier =
self.cli_options.resolve_specified_import_map_specifier()?;
match maybe_import_map_specifier {
Some(specifier) => {
let value = match self.eszip_module_loader_provider.get().await? {
Some(eszip) => eszip.load_import_map_value(&specifier)?,
None => resolve_import_map_value_from_specifier(
&specifier,
&self.file_fetcher,
)
.await
.with_context(|| {
format!("Unable to load '{}' import map", specifier)
})?,
};
Ok(Some(deno_resolver::workspace::SpecifiedImportMap {
base_url: specifier,
value,
}))
}
None => {
if let Some(import_map) =
self.workspace_external_import_map_loader.get_or_load()?
{
let path_url = deno_path_util::url_from_file_path(&import_map.path)?;
Ok(Some(deno_resolver::workspace::SpecifiedImportMap {
base_url: path_url,
value: import_map.value.clone(),
}))
} else {
Ok(None)
}
}
let mut out = Vec::new();

let maybe_import_map_specifiers =
self.cli_options.resolve_specified_import_map_specifiers()?;

for specifier in maybe_import_map_specifiers {
let value = match self.eszip_module_loader_provider.get().await? {
Some(eszip) => eszip.load_import_map_value(&specifier)?,
None => resolve_import_map_value_from_specifier(
&specifier,
&self.file_fetcher,
)
.await
.with_context(|| {
format!("Unable to load '{}' import map", specifier)
})?,
};
out.push(deno_resolver::workspace::SpecifiedImportMap {
base_url: specifier,
value,
})
}
for import_map in self.workspace_external_import_map_loader.get_or_load()? {
let path_url = deno_path_util::url_from_file_path(&import_map.path)?;
out.push(deno_resolver::workspace::SpecifiedImportMap {
base_url: path_url,
value: import_map.value.clone(),
})
}

Ok(out)
}
}

Expand Down Expand Up @@ -1192,7 +1188,7 @@ impl CliFactory {
},
node_resolution_cache: Some(Arc::new(NodeResolutionThreadLocalCache)),
npm_system_info: self.flags.subcommand.npm_system_info(),
specified_import_map: Some(Box::new(CliSpecifiedImportMapProvider {
specified_import_map: Box::new(CliSpecifiedImportMapProvider {
cli_options: options.clone(),
eszip_module_loader_provider: self
.eszip_module_loader_provider()?
Expand All @@ -1201,7 +1197,7 @@ impl CliFactory {
workspace_external_import_map_loader: self
.workspace_external_import_map_loader()?
.clone(),
})),
}),
bare_node_builtins: options.unstable_bare_node_builtins(),
unstable_sloppy_imports: options.unstable_sloppy_imports(),
on_mapped_resolution_diagnostic: Some(Arc::new(
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/standalone/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct SerializedResolverWorkspaceJsrPackage {

#[derive(Deserialize, Serialize)]
pub struct SerializedWorkspaceResolver {
pub import_map: Option<SerializedWorkspaceResolverImportMap>,
pub import_maps: Vec<SerializedWorkspaceResolverImportMap>,
pub jsr_pkgs: Vec<SerializedResolverWorkspaceJsrPackage>,
pub package_jsons: BTreeMap<String, serde_json::Value>,
pub pkg_json_resolution: PackageJsonDepResolution,
Expand Down
Loading