diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-10-05 01:35:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-05 01:35:55 +0200 |
commit | f1d3a170430501b4fab1a2d2abb5d77528251c77 (patch) | |
tree | 4951edeccc1ef25e4dc2703a6a8e2b38a33fbb20 /cli/proc_state.rs | |
parent | 64a7187238c4f291f254bd6eb58138a3a6534898 (diff) |
feat: add --compat flag to provide built-in Node modules (#12293)
This commit adds "--compat" flag. When the flag is passed a set of mappings for
built-in Node modules is injected into the import map. If user doesn't
explicitly provide an import map (using "--import-map" flag) then a map is
created on the fly. If there are already existing mappings in import map that
would clash with built-in Node modules a set of diagnostics is printed to the
terminal with suggestions how to proceed.
Diffstat (limited to 'cli/proc_state.rs')
-rw-r--r-- | cli/proc_state.rs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 2c61ba51f..48dc335f0 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -36,6 +36,7 @@ use deno_tls::rustls_native_certs::load_native_certs; use deno_tls::webpki_roots::TLS_SERVER_ROOTS; use import_map::ImportMap; use log::debug; +use log::info; use log::warn; use std::collections::HashMap; use std::collections::HashSet; @@ -182,7 +183,7 @@ impl ProcState { None }; - let maybe_import_map: Option<ImportMap> = + let mut maybe_import_map: Option<ImportMap> = match flags.import_map_path.as_ref() { None => None, Some(import_map_url) => { @@ -204,6 +205,32 @@ impl ProcState { } }; + if flags.compat { + let mut import_map = match maybe_import_map { + Some(import_map) => import_map, + None => { + // INFO: we're creating an empty import map, with its specifier pointing + // to `CWD/node_import_map.json` to make sure the map still works as expected. + let import_map_specifier = + std::env::current_dir()?.join("node_import_map.json"); + ImportMap::from_json(import_map_specifier.to_str().unwrap(), "{}") + .unwrap() + } + }; + let node_builtins = crate::compat::get_mapped_node_builtins(); + let diagnostics = import_map.update_imports(node_builtins)?; + + if !diagnostics.is_empty() { + info!("Some Node built-ins were not added to the import map:"); + for diagnostic in diagnostics { + info!(" - {}", diagnostic); + } + info!("If you want to use Node built-ins provided by Deno remove listed specifiers from \"imports\" mapping in the import map file."); + } + + maybe_import_map = Some(import_map); + } + let maybe_inspect_host = flags.inspect.or(flags.inspect_brk); let maybe_inspector_server = maybe_inspect_host.map(|host| { Arc::new(InspectorServer::new(host, version::get_user_agent())) |