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;
|
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_graph::MediaType;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-11-25 18:29:48 -05:00
|
|
|
/// A hashing function that takes the source code and emit options
|
|
|
|
/// hash then generates a string hash which can be stored to
|
2021-10-10 17:26:22 -04:00
|
|
|
/// determine if the cached emit is valid or not.
|
2022-11-29 12:43:54 -05:00
|
|
|
pub fn get_source_hash(source_text: &str, emit_options_hash: u64) -> u64 {
|
2022-07-19 11:58:18 -04:00
|
|
|
FastInsecureHasher::new()
|
|
|
|
.write_str(source_text)
|
|
|
|
.write_u64(emit_options_hash)
|
|
|
|
.finish()
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
pub fn emit_parsed_source(
|
2022-08-22 12:14:59 -04:00
|
|
|
emit_cache: &EmitCache,
|
|
|
|
parsed_source_cache: &ParsedSourceCache,
|
2022-07-19 11:58:18 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2022-08-22 12:14:59 -04:00
|
|
|
media_type: MediaType,
|
|
|
|
source: &Arc<str>,
|
2022-07-19 11:58:18 -04:00
|
|
|
emit_options: &deno_ast::EmitOptions,
|
|
|
|
emit_config_hash: u64,
|
|
|
|
) -> Result<String, AnyError> {
|
2022-08-22 12:14:59 -04:00
|
|
|
let source_hash = get_source_hash(source, emit_config_hash);
|
2022-07-19 11:58:18 -04:00
|
|
|
|
2022-11-29 12:43:54 -05:00
|
|
|
if let Some(emit_code) = emit_cache.get_emit_code(specifier, source_hash) {
|
2022-07-19 11:58:18 -04:00
|
|
|
Ok(emit_code)
|
|
|
|
} else {
|
2022-08-22 12:14:59 -04:00
|
|
|
// this will use a cached version if it exists
|
|
|
|
let parsed_source = parsed_source_cache.get_or_parse_module(
|
|
|
|
specifier,
|
|
|
|
source.clone(),
|
|
|
|
media_type,
|
|
|
|
)?;
|
2022-07-19 11:58:18 -04:00
|
|
|
let transpiled_source = parsed_source.transpile(emit_options)?;
|
|
|
|
debug_assert!(transpiled_source.source_map.is_none());
|
2022-08-22 12:14:59 -04:00
|
|
|
emit_cache.set_emit_code(specifier, source_hash, &transpiled_source.text);
|
2022-07-19 11:58:18 -04:00
|
|
|
Ok(transpiled_source.text)
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
}
|