summaryrefslogtreecommitdiff
path: root/cli/cache/emit.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2022-07-12 18:58:39 -0400
committerGitHub <noreply@github.com>2022-07-12 18:58:39 -0400
commit0c87dd1e9898d7ac93e274d3611ee491a107d47a (patch)
treef626332706ccd12e0719f9b84d6b234d5483659b /cli/cache/emit.rs
parent76107649804e674268becd693b7b2a954eecb3da (diff)
perf: use emit from swc instead of tsc (#15118)
Diffstat (limited to 'cli/cache/emit.rs')
-rw-r--r--cli/cache/emit.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/cli/cache/emit.rs b/cli/cache/emit.rs
new file mode 100644
index 000000000..e1469b862
--- /dev/null
+++ b/cli/cache/emit.rs
@@ -0,0 +1,71 @@
+// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+
+use deno_ast::ModuleSpecifier;
+use deno_core::error::AnyError;
+
+use super::CacheType;
+use super::Cacher;
+
+/// Emit cache for a single file.
+#[derive(Debug, Clone, PartialEq)]
+pub struct SpecifierEmitCacheData {
+ pub source_hash: String,
+ pub text: String,
+ pub map: Option<String>,
+}
+
+pub trait EmitCache {
+ /// Gets the emit data from the cache.
+ fn get_emit_data(
+ &self,
+ specifier: &ModuleSpecifier,
+ ) -> Option<SpecifierEmitCacheData>;
+ /// Sets the emit data in the cache.
+ fn set_emit_data(
+ &self,
+ specifier: ModuleSpecifier,
+ data: SpecifierEmitCacheData,
+ ) -> Result<(), AnyError>;
+ /// Gets the stored hash of the source of the provider specifier
+ /// to tell if the emit is out of sync with the source.
+ /// TODO(13302): this is actually not reliable and should be removed
+ /// once switching to an sqlite db
+ fn get_source_hash(&self, specifier: &ModuleSpecifier) -> Option<String>;
+ /// Gets the emitted JavaScript of the TypeScript source.
+ /// TODO(13302): remove this once switching to an sqlite db
+ fn get_emit_text(&self, specifier: &ModuleSpecifier) -> Option<String>;
+}
+
+impl<T: Cacher> EmitCache for T {
+ fn get_emit_data(
+ &self,
+ specifier: &ModuleSpecifier,
+ ) -> Option<SpecifierEmitCacheData> {
+ Some(SpecifierEmitCacheData {
+ source_hash: self.get_source_hash(specifier)?,
+ text: self.get_emit_text(specifier)?,
+ map: self.get(CacheType::SourceMap, specifier),
+ })
+ }
+
+ fn get_source_hash(&self, specifier: &ModuleSpecifier) -> Option<String> {
+ self.get(CacheType::Version, specifier)
+ }
+
+ fn get_emit_text(&self, specifier: &ModuleSpecifier) -> Option<String> {
+ self.get(CacheType::Emit, specifier)
+ }
+
+ fn set_emit_data(
+ &self,
+ specifier: ModuleSpecifier,
+ data: SpecifierEmitCacheData,
+ ) -> Result<(), AnyError> {
+ self.set(CacheType::Version, &specifier, data.source_hash)?;
+ self.set(CacheType::Emit, &specifier, data.text)?;
+ if let Some(map) = data.map {
+ self.set(CacheType::SourceMap, &specifier, map)?;
+ }
+ Ok(())
+ }
+}