2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 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
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
2023-03-21 18:33:12 -04:00
|
|
|
use deno_core::ModuleCode;
|
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>,
|
2023-04-13 14:03:07 -04:00
|
|
|
emit_options: deno_ast::EmitOptions,
|
|
|
|
// cached hash of the emit options
|
|
|
|
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>,
|
2023-04-13 14:03:07 -04:00
|
|
|
emit_options: deno_ast::EmitOptions,
|
|
|
|
) -> Self {
|
|
|
|
let emit_options_hash = FastInsecureHasher::new()
|
|
|
|
.write_hashable(&emit_options)
|
|
|
|
.finish();
|
|
|
|
Self {
|
|
|
|
emit_cache,
|
|
|
|
parsed_source_cache,
|
|
|
|
emit_options,
|
|
|
|
emit_options_hash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cache_module_emits(
|
|
|
|
&self,
|
|
|
|
graph: &ModuleGraph,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
for module in graph.modules() {
|
|
|
|
if let Module::Esm(module) = module {
|
|
|
|
let is_emittable = matches!(
|
|
|
|
module.media_type,
|
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Jsx
|
|
|
|
| MediaType::Tsx
|
|
|
|
);
|
|
|
|
if is_emittable {
|
|
|
|
self.emit_parsed_source(
|
|
|
|
&module.specifier,
|
|
|
|
module.media_type,
|
|
|
|
&module.source,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:22:33 -04:00
|
|
|
/// Gets a cached emit if the source matches the hash found in the cache.
|
|
|
|
pub fn maybed_cached_emit(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
source: &str,
|
|
|
|
) -> Option<String> {
|
|
|
|
let source_hash = self.get_source_hash(source);
|
|
|
|
self.emit_cache.get_emit_code(specifier, source_hash)
|
|
|
|
}
|
|
|
|
|
2023-04-13 14:03:07 -04:00
|
|
|
pub fn emit_parsed_source(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
media_type: MediaType,
|
|
|
|
source: &Arc<str>,
|
|
|
|
) -> Result<ModuleCode, AnyError> {
|
|
|
|
let source_hash = self.get_source_hash(source);
|
|
|
|
|
|
|
|
if let Some(emit_code) =
|
|
|
|
self.emit_cache.get_emit_code(specifier, source_hash)
|
|
|
|
{
|
|
|
|
Ok(emit_code.into())
|
|
|
|
} else {
|
|
|
|
// this will use a cached version if it exists
|
|
|
|
let parsed_source = self.parsed_source_cache.get_or_parse_module(
|
|
|
|
specifier,
|
|
|
|
source.clone(),
|
|
|
|
media_type,
|
|
|
|
)?;
|
|
|
|
let transpiled_source = parsed_source.transpile(&self.emit_options)?;
|
|
|
|
debug_assert!(transpiled_source.source_map.is_none());
|
|
|
|
self.emit_cache.set_emit_code(
|
|
|
|
specifier,
|
|
|
|
source_hash,
|
|
|
|
&transpiled_source.text,
|
|
|
|
);
|
|
|
|
Ok(transpiled_source.text.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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)
|
|
|
|
.write_u64(self.emit_options_hash)
|
|
|
|
.finish()
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
}
|