diff options
Diffstat (limited to 'cli/module_loader.rs')
-rw-r--r-- | cli/module_loader.rs | 103 |
1 files changed, 67 insertions, 36 deletions
diff --git a/cli/module_loader.rs b/cli/module_loader.rs index 8da205907..235c35fcd 100644 --- a/cli/module_loader.rs +++ b/cli/module_loader.rs @@ -3,6 +3,8 @@ use crate::emit::emit_parsed_source; use crate::emit::TsTypeLib; use crate::graph_util::ModuleEntry; +use crate::node; +use crate::npm::NpmPackageResolver; use crate::proc_state::ProcState; use crate::text_encoding::code_without_source_map; use crate::text_encoding::source_map_from_code; @@ -115,6 +117,57 @@ impl CliModuleLoader { )), } } + + fn load_sync( + &self, + specifier: &ModuleSpecifier, + ) -> Result<ModuleSource, AnyError> { + let code_source = if self.ps.npm_resolver.in_npm_package(specifier) { + let file_path = specifier.to_file_path().unwrap(); + let code = std::fs::read_to_string(file_path)?; + let is_cjs = self.ps.cjs_resolutions.lock().contains(specifier); + + let code = if is_cjs { + // translate cjs to esm if it's cjs and inject node globals + node::translate_cjs_to_esm( + &self.ps.file_fetcher, + specifier, + code, + MediaType::Cjs, + &self.ps.npm_resolver, + )? + } else { + // only inject node globals for esm + node::esm_code_with_node_globals(specifier, code)? + }; + + ModuleCodeSource { + code, + found_url: specifier.clone(), + media_type: MediaType::from(specifier), + } + } else { + self.load_prepared_module(specifier)? + }; + let code = if self.ps.options.is_inspecting() { + // we need the code with the source map in order for + // it to work with --inspect or --inspect-brk + code_source.code + } else { + // reduce memory and throw away the source map + // because we don't need it + code_without_source_map(code_source.code) + }; + Ok(ModuleSource { + code: code.into_bytes().into_boxed_slice(), + module_url_specified: specifier.to_string(), + module_url_found: code_source.found_url.to_string(), + module_type: match code_source.media_type { + MediaType::Json => ModuleType::Json, + _ => ModuleType::JavaScript, + }, + }) + } } impl ModuleLoader for CliModuleLoader { @@ -136,28 +189,7 @@ impl ModuleLoader for CliModuleLoader { // NOTE: this block is async only because of `deno_core` interface // requirements; module was already loaded when constructing module graph // during call to `prepare_load` so we can load it synchronously. - let result = self.load_prepared_module(specifier).map(|code_source| { - let code = if self.ps.options.is_inspecting() { - // we need the code with the source map in order for - // it to work with --inspect or --inspect-brk - code_source.code - } else { - // reduce memory and throw away the source map - // because we don't need it - code_without_source_map(code_source.code) - }; - ModuleSource { - code: code.into_bytes().into_boxed_slice(), - module_url_specified: specifier.to_string(), - module_url_found: code_source.found_url.to_string(), - module_type: match code_source.media_type { - MediaType::Json => ModuleType::Json, - _ => ModuleType::JavaScript, - }, - } - }); - - Box::pin(deno_core::futures::future::ready(result)) + Box::pin(deno_core::futures::future::ready(self.load_sync(specifier))) } fn prepare_load( @@ -167,6 +199,11 @@ impl ModuleLoader for CliModuleLoader { _maybe_referrer: Option<String>, is_dynamic: bool, ) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> { + if self.ps.npm_resolver.in_npm_package(specifier) { + // nothing to prepare + return Box::pin(deno_core::futures::future::ready(Ok(()))); + } + let specifier = specifier.clone(); let ps = self.ps.clone(); let state = op_state.borrow(); @@ -198,21 +235,15 @@ impl ModuleLoader for CliModuleLoader { impl SourceMapGetter for CliModuleLoader { fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>> { - if let Ok(specifier) = resolve_url(file_name) { - match specifier.scheme() { - // we should only be looking for emits for schemes that denote external - // modules, which the disk_cache supports - "wasm" | "file" | "http" | "https" | "data" | "blob" => (), - _ => return None, - } - if let Ok(source) = self.load_prepared_module(&specifier) { - source_map_from_code(&source.code) - } else { - None - } - } else { - None + let specifier = resolve_url(file_name).ok()?; + match specifier.scheme() { + // we should only be looking for emits for schemes that denote external + // modules, which the disk_cache supports + "wasm" | "file" | "http" | "https" | "data" | "blob" => (), + _ => return None, } + let source = self.load_prepared_module(&specifier).ok()?; + source_map_from_code(&source.code) } fn get_source_line( |