diff options
| author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2022-12-13 13:53:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-13 13:53:32 +0100 |
| commit | 435948e47057a5d8f2ffffebf74b9f84e31770f8 (patch) | |
| tree | 2b31cfb0b63b2436bf2d10e5ce867b7ae61a75d9 /cli/proc_state.rs | |
| parent | 5d9bb8b4b042f4d4fbbe61b31a50a26db7f1ae63 (diff) | |
feat(repl): support npm packages (#16770)
Co-authored-by: David Sherret <dsherret@gmail.com>
Diffstat (limited to 'cli/proc_state.rs')
| -rw-r--r-- | cli/proc_state.rs | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 13c8d2414..cdfc04c08 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -93,7 +93,7 @@ pub struct Inner { pub shared_array_buffer_store: SharedArrayBufferStore, pub compiled_wasm_module_store: CompiledWasmModuleStore, pub parsed_source_cache: ParsedSourceCache, - maybe_resolver: Option<Arc<CliResolver>>, + pub maybe_resolver: Option<Arc<CliResolver>>, maybe_file_watcher_reporter: Option<FileWatcherReporter>, pub node_analysis_cache: NodeAnalysisCache, pub npm_cache: NpmCache, @@ -594,14 +594,36 @@ impl ProcState { // FIXME(bartlomieju): this is a hacky way to provide compatibility with REPL // and `Deno.core.evalContext` API. Ideally we should always have a referrer filled // but sadly that's not the case due to missing APIs in V8. - let referrer = if referrer.is_empty() - && matches!(self.options.sub_command(), DenoSubcommand::Repl(_)) - { + let is_repl = matches!(self.options.sub_command(), DenoSubcommand::Repl(_)); + let referrer = if referrer.is_empty() && is_repl { deno_core::resolve_url_or_path("./$deno$repl.ts").unwrap() } else { deno_core::resolve_url_or_path(referrer).unwrap() }; + // FIXME(bartlomieju): this is another hack way to provide NPM specifier + // support in REPL. This should be fixed. + if is_repl { + let specifier = self + .maybe_resolver + .as_ref() + .and_then(|resolver| { + resolver.resolve(specifier, &referrer).to_result().ok() + }) + .or_else(|| ModuleSpecifier::parse(specifier).ok()); + if let Some(specifier) = specifier { + if let Ok(reference) = NpmPackageReference::from_specifier(&specifier) { + return self + .handle_node_resolve_result(node::node_resolve_npm_reference( + &reference, + deno_runtime::deno_node::NodeResolutionMode::Execution, + &self.npm_resolver, + )) + .with_context(|| format!("Could not resolve '{}'.", reference)); + } + } + } + if let Some(resolver) = &self.maybe_resolver { resolver.resolve(specifier, &referrer).to_result() } else { |
