From f1d3a170430501b4fab1a2d2abb5d77528251c77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 5 Oct 2021 01:35:55 +0200 Subject: 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. --- cli/proc_state.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'cli/proc_state.rs') 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 = + let mut maybe_import_map: Option = 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())) -- cgit v1.2.3