diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-06-05 11:04:16 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-05 17:04:16 +0200 |
commit | 7ed90a20d04982ae15a52ae2378cbffd4b6839df (patch) | |
tree | 3297d6f7227fbf1cf80e17a2a376ef4dfa52e6ad /cli/emit.rs | |
parent | 0544d60012006b1c7799d8b6eafacec9567901ad (diff) |
fix: better handling of npm resolution occurring on workers (#24094)
Closes https://github.com/denoland/deno/issues/24063
Diffstat (limited to 'cli/emit.rs')
-rw-r--r-- | cli/emit.rs | 65 |
1 files changed, 46 insertions, 19 deletions
diff --git a/cli/emit.rs b/cli/emit.rs index fc815b6ee..98ee59fab 100644 --- a/cli/emit.rs +++ b/cli/emit.rs @@ -10,7 +10,7 @@ use deno_core::error::AnyError; use deno_core::futures::stream::FuturesUnordered; use deno_core::futures::FutureExt; use deno_core::futures::StreamExt; -use deno_core::ModuleCodeString; +use deno_core::ModuleCodeBytes; use deno_core::ModuleSpecifier; use deno_graph::MediaType; use deno_graph::Module; @@ -90,7 +90,7 @@ impl Emitter { &self, specifier: &ModuleSpecifier, source: &str, - ) -> Option<String> { + ) -> Option<Vec<u8>> { let source_hash = self.get_source_hash(source); self.emit_cache.get_emit_code(specifier, source_hash) } @@ -100,7 +100,7 @@ impl Emitter { specifier: &ModuleSpecifier, media_type: MediaType, source: &Arc<str>, - ) -> Result<ModuleCodeString, AnyError> { + ) -> Result<ModuleCodeBytes, AnyError> { // Note: keep this in sync with the sync version below let helper = EmitParsedSourceHelper(self); match helper.pre_emit_parsed_source(specifier, source) { @@ -139,7 +139,7 @@ impl Emitter { specifier: &ModuleSpecifier, media_type: MediaType, source: &Arc<str>, - ) -> Result<ModuleCodeString, AnyError> { + ) -> Result<ModuleCodeBytes, AnyError> { // Note: keep this in sync with the async version above let helper = EmitParsedSourceHelper(self); match helper.pre_emit_parsed_source(specifier, source) { @@ -172,16 +172,43 @@ impl Emitter { ModuleSpecifier::to_file_path(specifier).unwrap(), ) .await?; - let source_arc: Arc<str> = source_code.into(); - let parsed_source = self - .parsed_source_cache - .remove_or_parse_module(specifier, source_arc, media_type)?; - let mut options = self.transpile_and_emit_options.1.clone(); - options.source_map = SourceMapOption::None; - let transpiled_source = parsed_source - .transpile(&self.transpile_and_emit_options.0, &options)? - .into_source(); - Ok(transpiled_source.text) + match media_type { + MediaType::TypeScript + | MediaType::Mts + | MediaType::Cts + | MediaType::Jsx + | MediaType::Tsx => { + let source_arc: Arc<str> = source_code.into(); + let parsed_source = self + .parsed_source_cache + .remove_or_parse_module(specifier, source_arc, media_type)?; + // HMR doesn't work with embedded source maps for some reason, so set + // the option to not use them (though you should test this out because + // this statement is probably wrong) + let mut options = self.transpile_and_emit_options.1.clone(); + options.source_map = SourceMapOption::None; + let transpiled_source = parsed_source + .transpile(&self.transpile_and_emit_options.0, &options)? + .into_source() + .into_string()?; + Ok(transpiled_source.text) + } + MediaType::JavaScript + | MediaType::Mjs + | MediaType::Cjs + | MediaType::Dts + | MediaType::Dmts + | MediaType::Dcts + | MediaType::Json + | MediaType::Wasm + | MediaType::TsBuildInfo + | MediaType::SourceMap + | MediaType::Unknown => { + // clear this specifier from the parsed source cache as it's now out of date + self.parsed_source_cache.free(specifier); + Ok(source_code) + } + } } /// A hashing function that takes the source code and uses the global emit @@ -196,7 +223,7 @@ impl Emitter { } enum PreEmitResult { - Cached(ModuleCodeString), + Cached(ModuleCodeBytes), NotCached { source_hash: u64 }, } @@ -214,7 +241,7 @@ impl<'a> EmitParsedSourceHelper<'a> { if let Some(emit_code) = self.0.emit_cache.get_emit_code(specifier, source_hash) { - PreEmitResult::Cached(emit_code.into()) + PreEmitResult::Cached(emit_code.into_boxed_slice().into()) } else { PreEmitResult::NotCached { source_hash } } @@ -240,7 +267,7 @@ impl<'a> EmitParsedSourceHelper<'a> { specifier: &ModuleSpecifier, transpile_result: TranspileResult, source_hash: u64, - ) -> ModuleCodeString { + ) -> ModuleCodeBytes { let transpiled_source = match transpile_result { TranspileResult::Owned(source) => source, TranspileResult::Cloned(source) => { @@ -252,8 +279,8 @@ impl<'a> EmitParsedSourceHelper<'a> { self.0.emit_cache.set_emit_code( specifier, source_hash, - &transpiled_source.text, + &transpiled_source.source, ); - transpiled_source.text.into() + transpiled_source.source.into_boxed_slice().into() } } |