summaryrefslogtreecommitdiff
path: root/cli/proc_state.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2021-10-18 19:36:28 +0200
committerGitHub <noreply@github.com>2021-10-18 19:36:28 +0200
commit617eeabe8369d7bfca7951d1cd55ac58ede1f9fb (patch)
tree61cfd7b8a5a0230528ed9547c12fcd7183125c6a /cli/proc_state.rs
parent5a48d41bddf599b14dd9019ff49821c436ce4542 (diff)
feat(unstable): Node CJS and ESM resolvers for compat mode (#12424)
This commit adds CJS and ESM Node resolvers to the "--compat" mode. The functionality is spread across "cli/compat" module and Node compatibility layer in "deno_std/node"; this stems from the fact that ES module resolution can only be implemented in Rust as it needs to directly integrated with "deno_core"; however "deno_std/node" already provided CJS module resolution. Currently this resolution is only active when running a files using "deno run --compat --unstable <filename>", and is not available in other subcommands, which will be changed in follow up commits.
Diffstat (limited to 'cli/proc_state.rs')
-rw-r--r--cli/proc_state.rs48
1 files changed, 19 insertions, 29 deletions
diff --git a/cli/proc_state.rs b/cli/proc_state.rs
index e45e7c539..461599fc4 100644
--- a/cli/proc_state.rs
+++ b/cli/proc_state.rs
@@ -3,6 +3,7 @@
use crate::cache;
use crate::colors;
use crate::compat;
+use crate::compat::NodeEsmResolver;
use crate::config_file::ConfigFile;
use crate::deno_dir;
use crate::emit;
@@ -195,7 +196,7 @@ impl ProcState {
None
};
- let mut maybe_import_map: Option<ImportMap> =
+ let maybe_import_map: Option<ImportMap> =
match flags.import_map_path.as_ref() {
None => None,
Some(import_map_url) => {
@@ -217,32 +218,6 @@ 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 = compat::get_mapped_node_builtins();
- let diagnostics = import_map.update_imports(node_builtins)?;
-
- if !diagnostics.is_empty() {
- log::info!("Some Node built-ins were not added to the import map:");
- for diagnostic in diagnostics {
- log::info!(" - {}", diagnostic);
- }
- log::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()))
@@ -316,14 +291,29 @@ impl ProcState {
);
let maybe_locker = as_maybe_locker(self.lockfile.clone());
let maybe_imports = self.get_maybe_imports();
- let maybe_resolver =
+ let node_resolver = NodeEsmResolver;
+ let import_map_resolver =
self.maybe_import_map.as_ref().map(ImportMapResolver::new);
+ let maybe_resolver = if self.flags.compat {
+ Some(node_resolver.as_resolver())
+ } else {
+ import_map_resolver.as_ref().map(|im| im.as_resolver())
+ };
+ // TODO(bartlomieju): this is very make-shift, is there an existing API
+ // that we could include it like with "maybe_imports"?
+ let roots = if self.flags.compat {
+ let mut r = vec![compat::GLOBAL_URL.clone()];
+ r.extend(roots);
+ r
+ } else {
+ roots
+ };
let graph = deno_graph::create_graph(
roots,
is_dynamic,
maybe_imports,
&mut cache,
- maybe_resolver.as_ref().map(|im| im.as_resolver()),
+ maybe_resolver,
maybe_locker,
None,
)