diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2024-01-24 23:44:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-24 23:44:06 +0100 |
commit | 44f8b05f5bf45453496e80c69dc7850aa98c9af8 (patch) | |
tree | ba31d15374e1b658a444e5727abc7b157a2efc73 /cli/args/mod.rs | |
parent | 801ed74118baabef49842bbfb2164f971d2cdb03 (diff) |
feat: Expand 'imports' section of deno.json (#22087)
This commit adds automatic expansion of "imports" field in "deno.json"
file.
If "npm:" or "jsr:" imports are encountered we automatically try to add
a "directory" remapping.
Previously users had to specify entries for both `foo` and `foo/` to be
able to import like
`import { symbol1 } from "foo";` and `import { symbol2 } from
"foo/some_file.js"`:
```
{
"imports": {
"foo": "npm:@foo/bar",
"foo/": "npm:/@foo/bar/",
}
```
With this change users can only add entry for `foo`:
```
{
"imports": {
"foo": "npm:@foo/bar",
}
```
The entry for `foo/` will be provided automatically.
Similarly if user provides "directory" remapping explicitly, we will not
overwrite it.
Diffstat (limited to 'cli/args/mod.rs')
-rw-r--r-- | cli/args/mod.rs | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 13ee9eeef..1e5d8754d 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -889,7 +889,19 @@ impl CliOptions { .members .iter() .map(|member| { - let import_map_value = member.config_file.to_import_map_value(); + let mut import_map_value = member.config_file.to_import_map_value(); + + let expanded_import_map_value = ::import_map::ext::expand_imports( + ::import_map::ext::ImportMapConfig { + base_url: member.config_file.specifier.clone(), + import_map_value: import_map_value.clone(), + }, + ); + + import_map_value + .as_object_mut() + .unwrap() + .insert("imports".to_string(), expanded_import_map_value); ::import_map::ext::ImportMapConfig { base_url: member.config_file.specifier.clone(), import_map_value, @@ -906,12 +918,24 @@ impl CliOptions { "Workspace config generated this import map {}", serde_json::to_string_pretty(&import_map).unwrap() ); - return import_map::import_map_from_value( + let maybe_import_map_result = import_map::import_map_from_value( // TODO(bartlomieju): maybe should be stored on the workspace config? &self.maybe_config_file.as_ref().unwrap().specifier, import_map, ) .map(Some); + + return match maybe_import_map_result { + Ok(maybe_import_map) => { + if let Some(mut import_map) = maybe_import_map { + import_map.ext_expand_imports(); + Ok(Some(import_map)) + } else { + Ok(None) + } + } + Err(err) => Err(err), + }; } } @@ -919,7 +943,8 @@ impl CliOptions { Some(specifier) => specifier, None => return Ok(None), }; - resolve_import_map_from_specifier( + + let maybe_import_map_result = resolve_import_map_from_specifier( &import_map_specifier, self.maybe_config_file().as_ref(), file_fetcher, @@ -928,7 +953,22 @@ impl CliOptions { .with_context(|| { format!("Unable to load '{import_map_specifier}' import map") }) - .map(Some) + .map(Some); + + match maybe_import_map_result { + Ok(maybe_import_map) => { + if let Some(mut import_map) = maybe_import_map { + let url = import_map.base_url().as_str(); + if url.ends_with("deno.json") || url.ends_with("deno.jsonc") { + import_map.ext_expand_imports(); + } + Ok(Some(import_map)) + } else { + Ok(None) + } + } + Err(err) => Err(err), + } } pub fn node_ipc_fd(&self) -> Option<i64> { |