2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
use crate::cache::EmitCache;
|
2022-07-19 11:58:18 -04:00
|
|
|
use crate::cache::FastInsecureHasher;
|
2022-08-22 12:14:59 -04:00
|
|
|
use crate::cache::ParsedSourceCache;
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2024-04-11 19:00:17 -04:00
|
|
|
use deno_ast::SourceMapOption;
|
2024-04-17 15:15:02 -04:00
|
|
|
use deno_ast::TranspileResult;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::error::AnyError;
|
2024-01-09 23:18:40 -05:00
|
|
|
use deno_core::ModuleCodeString;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_graph::MediaType;
|
2023-04-13 14:03:07 -04:00
|
|
|
use deno_graph::Module;
|
|
|
|
use deno_graph::ModuleGraph;
|
2021-10-10 17:26:22 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-04-13 14:03:07 -04:00
|
|
|
pub struct Emitter {
|
|
|
|
emit_cache: EmitCache,
|
2023-04-14 16:22:33 -04:00
|
|
|
parsed_source_cache: Arc<ParsedSourceCache>,
|
2024-05-18 11:42:03 -04:00
|
|
|
transpile_and_emit_options:
|
|
|
|
Arc<(deno_ast::TranspileOptions, deno_ast::EmitOptions)>,
|
2024-04-11 19:00:17 -04:00
|
|
|
// cached hash of the transpile and emit options
|
|
|
|
transpile_and_emit_options_hash: u64,
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
2023-04-13 14:03:07 -04:00
|
|
|
impl Emitter {
|
|
|
|
pub fn new(
|
|
|
|
emit_cache: EmitCache,
|
2023-04-14 16:22:33 -04:00
|
|
|
parsed_source_cache: Arc<ParsedSourceCache>,
|
2024-04-11 19:00:17 -04:00
|
|
|
transpile_options: deno_ast::TranspileOptions,
|
2023-04-13 14:03:07 -04:00
|
|
|
emit_options: deno_ast::EmitOptions,
|
|
|
|
) -> Self {
|
2024-04-11 19:00:17 -04:00
|
|
|
let transpile_and_emit_options_hash = {
|
|
|
|
let mut hasher = FastInsecureHasher::default();
|
|
|
|
hasher.write_hashable(&transpile_options);
|
2024-04-17 15:15:02 -04:00
|
|
|
hasher.write_hashable(&emit_options);
|
2024-04-11 19:00:17 -04:00
|
|
|
hasher.finish()
|
|
|
|
};
|
2023-04-13 14:03:07 -04:00
|
|
|
Self {
|
|
|
|
emit_cache,
|
|
|
|
parsed_source_cache,
|
2024-05-18 11:42:03 -04:00
|
|
|
transpile_and_emit_options: Arc::new((transpile_options, emit_options)),
|
2024-04-11 19:00:17 -04:00
|
|
|
transpile_and_emit_options_hash,
|
2023-04-13 14:03:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-18 11:42:03 -04:00
|
|
|
pub async fn cache_module_emits(
|
2023-04-13 14:03:07 -04:00
|
|
|
&self,
|
|
|
|
graph: &ModuleGraph,
|
|
|
|
) -> Result<(), AnyError> {
|
2024-05-18 11:42:03 -04:00
|
|
|
// todo(dsherret): we could do this concurrently
|
2023-04-13 14:03:07 -04:00
|
|
|
for module in graph.modules() {
|
2024-01-31 22:15:22 -05:00
|
|
|
if let Module::Js(module) = module {
|
2023-04-13 14:03:07 -04:00
|
|
|
let is_emittable = matches!(
|
|
|
|
module.media_type,
|
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Jsx
|
|
|
|
| MediaType::Tsx
|
|
|
|
);
|
|
|
|
if is_emittable {
|
2024-05-18 11:42:03 -04:00
|
|
|
self
|
|
|
|
.emit_parsed_source(
|
|
|
|
&module.specifier,
|
|
|
|
module.media_type,
|
|
|
|
&module.source,
|
|
|
|
)
|
|
|
|
.await?;
|
2023-04-13 14:03:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:22:33 -04:00
|
|
|
/// Gets a cached emit if the source matches the hash found in the cache.
|
2023-06-26 09:10:27 -04:00
|
|
|
pub fn maybe_cached_emit(
|
2023-04-14 16:22:33 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
source: &str,
|
|
|
|
) -> Option<String> {
|
|
|
|
let source_hash = self.get_source_hash(source);
|
|
|
|
self.emit_cache.get_emit_code(specifier, source_hash)
|
|
|
|
}
|
|
|
|
|
2024-05-18 11:42:03 -04:00
|
|
|
pub async fn emit_parsed_source(
|
2023-04-13 14:03:07 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
media_type: MediaType,
|
|
|
|
source: &Arc<str>,
|
2024-01-09 23:18:40 -05:00
|
|
|
) -> Result<ModuleCodeString, AnyError> {
|
2024-05-18 11:42:03 -04:00
|
|
|
// Note: keep this in sync with the sync version below
|
|
|
|
let helper = EmitParsedSourceHelper(self);
|
|
|
|
match helper.pre_emit_parsed_source(specifier, source) {
|
|
|
|
PreEmitResult::Cached(emitted_text) => Ok(emitted_text),
|
|
|
|
PreEmitResult::NotCached { source_hash } => {
|
|
|
|
let parsed_source_cache = self.parsed_source_cache.clone();
|
|
|
|
let transpile_and_emit_options =
|
|
|
|
self.transpile_and_emit_options.clone();
|
|
|
|
let transpile_result = deno_core::unsync::spawn_blocking({
|
|
|
|
let specifier = specifier.clone();
|
|
|
|
let source = source.clone();
|
|
|
|
move || -> Result<_, AnyError> {
|
|
|
|
EmitParsedSourceHelper::transpile(
|
|
|
|
&parsed_source_cache,
|
|
|
|
&specifier,
|
|
|
|
source.clone(),
|
|
|
|
media_type,
|
|
|
|
&transpile_and_emit_options.0,
|
|
|
|
&transpile_and_emit_options.1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()?;
|
|
|
|
Ok(helper.post_emit_parsed_source(
|
|
|
|
specifier,
|
|
|
|
transpile_result,
|
|
|
|
source_hash,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-13 14:03:07 -04:00
|
|
|
|
2024-05-18 11:42:03 -04:00
|
|
|
pub fn emit_parsed_source_sync(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
media_type: MediaType,
|
|
|
|
source: &Arc<str>,
|
|
|
|
) -> Result<ModuleCodeString, AnyError> {
|
|
|
|
// Note: keep this in sync with the async version above
|
|
|
|
let helper = EmitParsedSourceHelper(self);
|
|
|
|
match helper.pre_emit_parsed_source(specifier, source) {
|
|
|
|
PreEmitResult::Cached(emitted_text) => Ok(emitted_text),
|
|
|
|
PreEmitResult::NotCached { source_hash } => {
|
|
|
|
let transpile_result = EmitParsedSourceHelper::transpile(
|
|
|
|
&self.parsed_source_cache,
|
|
|
|
specifier,
|
|
|
|
source.clone(),
|
|
|
|
media_type,
|
|
|
|
&self.transpile_and_emit_options.0,
|
|
|
|
&self.transpile_and_emit_options.1,
|
|
|
|
)?;
|
|
|
|
Ok(helper.post_emit_parsed_source(
|
|
|
|
specifier,
|
|
|
|
transpile_result,
|
|
|
|
source_hash,
|
|
|
|
))
|
|
|
|
}
|
2023-04-13 14:03:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 20:25:58 -04:00
|
|
|
/// Expects a file URL, panics otherwise.
|
|
|
|
pub async fn load_and_emit_for_hmr(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let media_type = MediaType::from_specifier(specifier);
|
|
|
|
let source_code = tokio::fs::read_to_string(
|
|
|
|
ModuleSpecifier::to_file_path(specifier).unwrap(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
let source_arc: Arc<str> = source_code.into();
|
|
|
|
let parsed_source = self
|
|
|
|
.parsed_source_cache
|
2024-04-14 17:15:17 -04:00
|
|
|
.remove_or_parse_module(specifier, source_arc, media_type)?;
|
2024-05-18 11:42:03 -04:00
|
|
|
let mut options = self.transpile_and_emit_options.1.clone();
|
2024-04-11 19:00:17 -04:00
|
|
|
options.source_map = SourceMapOption::None;
|
2024-04-14 17:15:17 -04:00
|
|
|
let transpiled_source = parsed_source
|
2024-05-18 11:42:03 -04:00
|
|
|
.transpile(&self.transpile_and_emit_options.0, &options)?
|
2024-04-17 15:15:02 -04:00
|
|
|
.into_source();
|
2023-10-30 20:25:58 -04:00
|
|
|
Ok(transpiled_source.text)
|
|
|
|
}
|
|
|
|
|
2023-04-13 14:03:07 -04:00
|
|
|
/// A hashing function that takes the source code and uses the global emit
|
|
|
|
/// options then generates a string hash which can be stored to
|
|
|
|
/// determine if the cached emit is valid or not.
|
2023-04-14 16:22:33 -04:00
|
|
|
fn get_source_hash(&self, source_text: &str) -> u64 {
|
2023-04-13 14:03:07 -04:00
|
|
|
FastInsecureHasher::new()
|
|
|
|
.write_str(source_text)
|
2024-04-11 19:00:17 -04:00
|
|
|
.write_u64(self.transpile_and_emit_options_hash)
|
2023-04-13 14:03:07 -04:00
|
|
|
.finish()
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
}
|
2024-05-18 11:42:03 -04:00
|
|
|
|
|
|
|
enum PreEmitResult {
|
|
|
|
Cached(ModuleCodeString),
|
|
|
|
NotCached { source_hash: u64 },
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper to share code between async and sync emit_parsed_source methods.
|
|
|
|
struct EmitParsedSourceHelper<'a>(&'a Emitter);
|
|
|
|
|
|
|
|
impl<'a> EmitParsedSourceHelper<'a> {
|
|
|
|
pub fn pre_emit_parsed_source(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
source: &Arc<str>,
|
|
|
|
) -> PreEmitResult {
|
|
|
|
let source_hash = self.0.get_source_hash(source);
|
|
|
|
|
|
|
|
if let Some(emit_code) =
|
|
|
|
self.0.emit_cache.get_emit_code(specifier, source_hash)
|
|
|
|
{
|
|
|
|
PreEmitResult::Cached(emit_code.into())
|
|
|
|
} else {
|
|
|
|
PreEmitResult::NotCached { source_hash }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn transpile(
|
|
|
|
parsed_source_cache: &ParsedSourceCache,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
source: Arc<str>,
|
|
|
|
media_type: MediaType,
|
|
|
|
transpile_options: &deno_ast::TranspileOptions,
|
|
|
|
emit_options: &deno_ast::EmitOptions,
|
|
|
|
) -> Result<TranspileResult, AnyError> {
|
|
|
|
// nothing else needs the parsed source at this point, so remove from
|
|
|
|
// the cache in order to not transpile owned
|
|
|
|
let parsed_source = parsed_source_cache
|
|
|
|
.remove_or_parse_module(specifier, source, media_type)?;
|
|
|
|
Ok(parsed_source.transpile(transpile_options, emit_options)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn post_emit_parsed_source(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
transpile_result: TranspileResult,
|
|
|
|
source_hash: u64,
|
|
|
|
) -> ModuleCodeString {
|
|
|
|
let transpiled_source = match transpile_result {
|
|
|
|
TranspileResult::Owned(source) => source,
|
|
|
|
TranspileResult::Cloned(source) => {
|
|
|
|
debug_assert!(false, "Transpile owned failed.");
|
|
|
|
source
|
|
|
|
}
|
|
|
|
};
|
|
|
|
debug_assert!(transpiled_source.source_map.is_none());
|
|
|
|
self.0.emit_cache.set_emit_code(
|
|
|
|
specifier,
|
|
|
|
source_hash,
|
|
|
|
&transpiled_source.text,
|
|
|
|
);
|
|
|
|
transpiled_source.text.into()
|
|
|
|
}
|
|
|
|
}
|