0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-08 15:21:26 -05:00

reafactor(cli): use Emit enum and rename ts_build_info (#7952)

This commit is contained in:
Kitson Kelly 2020-10-13 21:54:28 +11:00 committed by GitHub
parent 1956cb8137
commit 000ac5c40b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 117 additions and 173 deletions

View file

@ -13,8 +13,7 @@ use crate::lockfile::Lockfile;
use crate::media_type::MediaType; use crate::media_type::MediaType;
use crate::specifier_handler::CachedModule; use crate::specifier_handler::CachedModule;
use crate::specifier_handler::DependencyMap; use crate::specifier_handler::DependencyMap;
use crate::specifier_handler::EmitMap; use crate::specifier_handler::Emit;
use crate::specifier_handler::EmitType;
use crate::specifier_handler::FetchFuture; use crate::specifier_handler::FetchFuture;
use crate::specifier_handler::SpecifierHandler; use crate::specifier_handler::SpecifierHandler;
use crate::tsc_config::IgnoredCompilerOptions; use crate::tsc_config::IgnoredCompilerOptions;
@ -41,8 +40,6 @@ use std::sync::Mutex;
use std::time::Instant; use std::time::Instant;
use swc_ecmascript::dep_graph::DependencyKind; use swc_ecmascript::dep_graph::DependencyKind;
pub type BuildInfoMap = HashMap<EmitType, String>;
lazy_static! { lazy_static! {
/// Matched the `@deno-types` pragma. /// Matched the `@deno-types` pragma.
static ref DENO_TYPES_RE: Regex = static ref DENO_TYPES_RE: Regex =
@ -163,13 +160,12 @@ fn get_version(source: &str, version: &str, config: &[u8]) -> String {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct Module { struct Module {
dependencies: DependencyMap, dependencies: DependencyMap,
emits: EmitMap,
is_dirty: bool, is_dirty: bool,
is_hydrated: bool, is_hydrated: bool,
is_parsed: bool, is_parsed: bool,
maybe_emit_path: Option<PathBuf>, maybe_emit: Option<Emit>,
maybe_emit_path: Option<(PathBuf, Option<PathBuf>)>,
maybe_import_map: Option<Rc<RefCell<ImportMap>>>, maybe_import_map: Option<Rc<RefCell<ImportMap>>>,
maybe_map_path: Option<PathBuf>,
maybe_parsed_module: Option<ParsedModule>, maybe_parsed_module: Option<ParsedModule>,
maybe_types: Option<(String, ModuleSpecifier)>, maybe_types: Option<(String, ModuleSpecifier)>,
maybe_version: Option<String>, maybe_version: Option<String>,
@ -183,13 +179,12 @@ impl Default for Module {
fn default() -> Self { fn default() -> Self {
Module { Module {
dependencies: HashMap::new(), dependencies: HashMap::new(),
emits: HashMap::new(),
is_dirty: false, is_dirty: false,
is_hydrated: false, is_hydrated: false,
is_parsed: false, is_parsed: false,
maybe_emit: None,
maybe_emit_path: None, maybe_emit_path: None,
maybe_import_map: None, maybe_import_map: None,
maybe_map_path: None,
maybe_parsed_module: None, maybe_parsed_module: None,
maybe_types: None, maybe_types: None,
maybe_version: None, maybe_version: None,
@ -227,8 +222,8 @@ impl Module {
self.media_type = cached_module.media_type; self.media_type = cached_module.media_type;
self.source = cached_module.source; self.source = cached_module.source;
self.source_path = cached_module.source_path; self.source_path = cached_module.source_path;
self.maybe_emit = cached_module.maybe_emit;
self.maybe_emit_path = cached_module.maybe_emit_path; self.maybe_emit_path = cached_module.maybe_emit_path;
self.maybe_map_path = cached_module.maybe_map_path;
if self.maybe_import_map.is_none() { if self.maybe_import_map.is_none() {
if let Some(dependencies) = cached_module.maybe_dependencies { if let Some(dependencies) = cached_module.maybe_dependencies {
self.dependencies = dependencies; self.dependencies = dependencies;
@ -245,9 +240,8 @@ impl Module {
} else { } else {
None None
}; };
self.is_dirty = false;
self.emits = cached_module.emits;
self.maybe_version = cached_module.maybe_version; self.maybe_version = cached_module.maybe_version;
self.is_dirty = false;
self.is_hydrated = true; self.is_hydrated = true;
} }
@ -418,8 +412,8 @@ pub struct TranspileOptions {
/// be able to manipulate and handle the graph. /// be able to manipulate and handle the graph.
#[derive(Debug)] #[derive(Debug)]
pub struct Graph2 { pub struct Graph2 {
build_info: BuildInfoMap,
handler: Rc<RefCell<dyn SpecifierHandler>>, handler: Rc<RefCell<dyn SpecifierHandler>>,
maybe_ts_build_info: Option<String>,
modules: HashMap<ModuleSpecifier, Module>, modules: HashMap<ModuleSpecifier, Module>,
roots: Vec<ModuleSpecifier>, roots: Vec<ModuleSpecifier>,
} }
@ -432,8 +426,8 @@ impl Graph2 {
/// ///
pub fn new(handler: Rc<RefCell<dyn SpecifierHandler>>) -> Self { pub fn new(handler: Rc<RefCell<dyn SpecifierHandler>>) -> Self {
Graph2 { Graph2 {
build_info: HashMap::new(),
handler, handler,
maybe_ts_build_info: None,
modules: HashMap::new(), modules: HashMap::new(),
roots: Vec::new(), roots: Vec::new(),
} }
@ -534,15 +528,21 @@ impl Graph2 {
let files = self.get_info_map(); let files = self.get_info_map();
let total_size = totals.get(&module).unwrap_or(&m.size()).to_owned(); let total_size = totals.get(&module).unwrap_or(&m.size()).to_owned();
let (compiled, map) =
if let Some((emit_path, maybe_map_path)) = &m.maybe_emit_path {
(Some(emit_path.clone()), maybe_map_path.clone())
} else {
(None, None)
};
Ok(ModuleGraphInfo { Ok(ModuleGraphInfo {
compiled: m.maybe_emit_path.clone(), compiled,
dep_count: self.modules.len() - 1, dep_count: self.modules.len() - 1,
file_type: m.media_type, file_type: m.media_type,
files, files,
info, info,
local: m.source_path.clone(), local: m.source_path.clone(),
map: m.maybe_map_path.clone(), map,
module, module,
total_size, total_size,
}) })
@ -550,30 +550,22 @@ impl Graph2 {
/// Update the handler with any modules that are marked as _dirty_ and update /// Update the handler with any modules that are marked as _dirty_ and update
/// any build info if present. /// any build info if present.
fn flush(&mut self, emit_type: &EmitType) -> Result<(), AnyError> { fn flush(&mut self) -> Result<(), AnyError> {
let mut handler = self.handler.borrow_mut(); let mut handler = self.handler.borrow_mut();
for (_, module) in self.modules.iter_mut() { for (_, module) in self.modules.iter_mut() {
if module.is_dirty { if module.is_dirty {
let (code, maybe_map) = module.emits.get(emit_type).unwrap(); if let Some(emit) = &module.maybe_emit {
handler.set_cache( handler.set_cache(&module.specifier, emit)?;
&module.specifier, }
&emit_type,
code.clone(),
maybe_map.clone(),
)?;
module.is_dirty = false;
if let Some(version) = &module.maybe_version { if let Some(version) = &module.maybe_version {
handler.set_version(&module.specifier, version.clone())?; handler.set_version(&module.specifier, version.clone())?;
} }
module.is_dirty = false;
} }
} }
for root_specifier in self.roots.iter() { for root_specifier in self.roots.iter() {
if let Some(build_info) = self.build_info.get(&emit_type) { if let Some(ts_build_info) = &self.maybe_ts_build_info {
handler.set_build_info( handler.set_ts_build_info(root_specifier, ts_build_info.to_owned())?;
root_specifier,
&emit_type,
build_info.to_owned(),
)?;
} }
} }
@ -618,7 +610,6 @@ impl Graph2 {
options: TranspileOptions, options: TranspileOptions,
) -> Result<(Stats, Option<IgnoredCompilerOptions>), AnyError> { ) -> Result<(Stats, Option<IgnoredCompilerOptions>), AnyError> {
let start = Instant::now(); let start = Instant::now();
let emit_type = EmitType::Cli;
let mut ts_config = TsConfig::new(json!({ let mut ts_config = TsConfig::new(json!({
"checkJs": false, "checkJs": false,
@ -662,7 +653,7 @@ impl Graph2 {
} }
let config = ts_config.as_bytes(); let config = ts_config.as_bytes();
// skip modules that already have a valid emit // skip modules that already have a valid emit
if module.emits.contains_key(&emit_type) && module.emit_valid(&config) { if module.maybe_emit.is_some() && module.emit_valid(&config) {
continue; continue;
} }
if module.maybe_parsed_module.is_none() { if module.maybe_parsed_module.is_none() {
@ -671,11 +662,11 @@ impl Graph2 {
let parsed_module = module.maybe_parsed_module.clone().unwrap(); let parsed_module = module.maybe_parsed_module.clone().unwrap();
let emit = parsed_module.transpile(&emit_options)?; let emit = parsed_module.transpile(&emit_options)?;
emit_count += 1; emit_count += 1;
module.emits.insert(emit_type.clone(), emit); module.maybe_emit = Some(Emit::Cli(emit));
module.set_version(&config); module.set_version(&config);
module.is_dirty = true; module.is_dirty = true;
} }
self.flush(&emit_type)?; self.flush()?;
let stats = Stats(vec![ let stats = Stats(vec![
("Files".to_string(), self.modules.len() as u128), ("Files".to_string(), self.modules.len() as u128),
@ -822,9 +813,9 @@ mod tests {
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct MockSpecifierHandler { pub struct MockSpecifierHandler {
pub fixtures: PathBuf, pub fixtures: PathBuf,
pub build_info: HashMap<ModuleSpecifier, String>, pub maybe_ts_build_info: Option<String>,
pub build_info_calls: Vec<(ModuleSpecifier, EmitType, String)>, pub ts_build_info_calls: Vec<(ModuleSpecifier, String)>,
pub cache_calls: Vec<(ModuleSpecifier, EmitType, String, Option<String>)>, pub cache_calls: Vec<(ModuleSpecifier, Emit)>,
pub deps_calls: Vec<(ModuleSpecifier, DependencyMap)>, pub deps_calls: Vec<(ModuleSpecifier, DependencyMap)>,
pub types_calls: Vec<(ModuleSpecifier, String)>, pub types_calls: Vec<(ModuleSpecifier, String)>,
pub version_calls: Vec<(ModuleSpecifier, String)>, pub version_calls: Vec<(ModuleSpecifier, String)>,
@ -871,26 +862,18 @@ mod tests {
fn fetch(&mut self, specifier: ModuleSpecifier) -> FetchFuture { fn fetch(&mut self, specifier: ModuleSpecifier) -> FetchFuture {
Box::pin(future::ready(self.get_cache(specifier))) Box::pin(future::ready(self.get_cache(specifier)))
} }
fn get_build_info( fn get_ts_build_info(
&self, &self,
specifier: &ModuleSpecifier, _specifier: &ModuleSpecifier,
_cache_type: &EmitType,
) -> Result<Option<String>, AnyError> { ) -> Result<Option<String>, AnyError> {
Ok(self.build_info.get(specifier).cloned()) Ok(self.maybe_ts_build_info.clone())
} }
fn set_cache( fn set_cache(
&mut self, &mut self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
cache_type: &EmitType, emit: &Emit,
code: String,
maybe_map: Option<String>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
self.cache_calls.push(( self.cache_calls.push((specifier.clone(), emit.clone()));
specifier.clone(),
cache_type.clone(),
code,
maybe_map,
));
Ok(()) Ok(())
} }
fn set_types( fn set_types(
@ -901,20 +884,15 @@ mod tests {
self.types_calls.push((specifier.clone(), types)); self.types_calls.push((specifier.clone(), types));
Ok(()) Ok(())
} }
fn set_build_info( fn set_ts_build_info(
&mut self, &mut self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
cache_type: &EmitType, ts_build_info: String,
build_info: String,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
self.maybe_ts_build_info = Some(ts_build_info.clone());
self self
.build_info .ts_build_info_calls
.insert(specifier.clone(), build_info.clone()); .push((specifier.clone(), ts_build_info));
self.build_info_calls.push((
specifier.clone(),
cache_type.clone(),
build_info,
));
Ok(()) Ok(())
} }
fn set_deps( fn set_deps(
@ -1068,16 +1046,22 @@ mod tests {
assert_eq!(maybe_ignored_options, None); assert_eq!(maybe_ignored_options, None);
let h = handler.borrow(); let h = handler.borrow();
assert_eq!(h.cache_calls.len(), 2); assert_eq!(h.cache_calls.len(), 2);
assert_eq!(h.cache_calls[0].1, EmitType::Cli); match &h.cache_calls[0].1 {
assert!(h.cache_calls[0] Emit::Cli((code, maybe_map)) => {
.2 assert!(
.contains("# sourceMappingURL=data:application/json;base64,")); code.contains("# sourceMappingURL=data:application/json;base64,")
assert_eq!(h.cache_calls[0].3, None); );
assert_eq!(h.cache_calls[1].1, EmitType::Cli); assert!(maybe_map.is_none());
assert!(h.cache_calls[1] }
.2 };
.contains("# sourceMappingURL=data:application/json;base64,")); match &h.cache_calls[1].1 {
assert_eq!(h.cache_calls[0].3, None); Emit::Cli((code, maybe_map)) => {
assert!(
code.contains("# sourceMappingURL=data:application/json;base64,")
);
assert!(maybe_map.is_none());
}
};
assert_eq!(h.deps_calls.len(), 7); assert_eq!(h.deps_calls.len(), 7);
assert_eq!( assert_eq!(
h.deps_calls[0].0, h.deps_calls[0].0,
@ -1135,10 +1119,14 @@ mod tests {
let h = handler.borrow(); let h = handler.borrow();
assert_eq!(h.cache_calls.len(), 1, "only one file should be emitted"); assert_eq!(h.cache_calls.len(), 1, "only one file should be emitted");
// FIXME(bartlomieju): had to add space in `<div>`, probably a quirk in swc_ecma_codegen // FIXME(bartlomieju): had to add space in `<div>`, probably a quirk in swc_ecma_codegen
assert!( match &h.cache_calls[0].1 {
h.cache_calls[0].2.contains("<div >Hello world!</div>"), Emit::Cli((code, _)) => {
"jsx should have been preserved" assert!(
); code.contains("<div >Hello world!</div>"),
"jsx should have been preserved"
);
}
}
} }
#[tokio::test] #[tokio::test]

View file

@ -16,23 +16,20 @@ use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::error::Error;
use std::fmt; use std::fmt;
use std::path::PathBuf; use std::path::PathBuf;
use std::pin::Pin; use std::pin::Pin;
use std::sync::Arc; use std::sync::Arc;
pub type DependencyMap = HashMap<String, Dependency>; pub type DependencyMap = HashMap<String, Dependency>;
pub type EmitMap = HashMap<EmitType, (String, Option<String>)>;
pub type FetchFuture = pub type FetchFuture =
Pin<Box<(dyn Future<Output = Result<CachedModule, AnyError>> + 'static)>>; Pin<Box<(dyn Future<Output = Result<CachedModule, AnyError>> + 'static)>>;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CachedModule { pub struct CachedModule {
pub emits: EmitMap,
pub maybe_dependencies: Option<DependencyMap>, pub maybe_dependencies: Option<DependencyMap>,
pub maybe_emit_path: Option<PathBuf>, pub maybe_emit: Option<Emit>,
pub maybe_map_path: Option<PathBuf>, pub maybe_emit_path: Option<(PathBuf, Option<PathBuf>)>,
pub maybe_types: Option<String>, pub maybe_types: Option<String>,
pub maybe_version: Option<String>, pub maybe_version: Option<String>,
pub media_type: MediaType, pub media_type: MediaType,
@ -45,10 +42,9 @@ pub struct CachedModule {
impl Default for CachedModule { impl Default for CachedModule {
fn default() -> Self { fn default() -> Self {
CachedModule { CachedModule {
emits: HashMap::new(),
maybe_dependencies: None, maybe_dependencies: None,
maybe_emit: None,
maybe_emit_path: None, maybe_emit_path: None,
maybe_map_path: None,
maybe_types: None, maybe_types: None,
maybe_version: None, maybe_version: None,
media_type: MediaType::Unknown, media_type: MediaType::Unknown,
@ -60,23 +56,21 @@ impl Default for CachedModule {
} }
} }
/// An enum that represents the different types of emitted code that can be /// An enum to own the a specific emit.
/// cached. Different types can utilise different configurations which can ///
/// change the validity of the emitted code. /// Currently there is only one type of emit that is cacheable, but this has
#[allow(unused)] /// been added to future proof the ability for the specifier handler
/// implementations to be able to handle other types of emits, like form a
/// runtime API which might have a different configuration.
#[derive(Debug, Clone, Eq, PartialEq, Hash)] #[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum EmitType { pub enum Emit {
/// Code that was emitted for use by the CLI /// Code that was emitted for use by the CLI
Cli, Cli((String, Option<String>)),
/// Code that was emitted for bundling purposes
Bundle,
/// Code that was emitted based on a request to the runtime APIs
Runtime,
} }
impl Default for EmitType { impl Default for Emit {
fn default() -> Self { fn default() -> Self {
EmitType::Cli Emit::Cli(("".to_string(), None))
} }
} }
@ -100,20 +94,16 @@ pub trait SpecifierHandler {
/// not expected to be cached for each module, but are "lazily" checked when /// not expected to be cached for each module, but are "lazily" checked when
/// a root module is identified. The `emit_type` also indicates what form /// a root module is identified. The `emit_type` also indicates what form
/// of the module the build info is valid for. /// of the module the build info is valid for.
fn get_build_info( fn get_ts_build_info(
&self, &self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
emit_type: &EmitType,
) -> Result<Option<String>, AnyError>; ) -> Result<Option<String>, AnyError>;
/// Set the emitted code (and maybe map) for a given module specifier. The /// Set the emit for the module specifier.
/// cache type indicates what form the emit is related to.
fn set_cache( fn set_cache(
&mut self, &mut self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
emit_type: &EmitType, emit: &Emit,
code: String,
maybe_map: Option<String>,
) -> Result<(), AnyError>; ) -> Result<(), AnyError>;
/// When parsed out of a JavaScript module source, the triple slash reference /// When parsed out of a JavaScript module source, the triple slash reference
@ -125,11 +115,10 @@ pub trait SpecifierHandler {
) -> Result<(), AnyError>; ) -> Result<(), AnyError>;
/// Set the build info for a module specifier, also providing the cache type. /// Set the build info for a module specifier, also providing the cache type.
fn set_build_info( fn set_ts_build_info(
&mut self, &mut self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
emit_type: &EmitType, ts_build_info: String,
build_info: String,
) -> Result<(), AnyError>; ) -> Result<(), AnyError>;
/// Set the graph dependencies for a given module specifier. /// Set the graph dependencies for a given module specifier.
@ -154,29 +143,6 @@ impl fmt::Debug for dyn SpecifierHandler {
} }
} }
/// Errors that could be raised by a `SpecifierHandler` implementation.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum SpecifierHandlerError {
/// An error representing an error the `EmitType` that was supplied to a
/// method of an implementor of the `SpecifierHandler` trait.
UnsupportedEmitType(EmitType),
}
use SpecifierHandlerError::*;
impl fmt::Display for SpecifierHandlerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
UnsupportedEmitType(ref emit_type) => write!(
f,
"The emit type of \"{:?}\" is unsupported for this operation.",
emit_type
),
}
}
}
impl Error for SpecifierHandlerError {}
/// A representation of meta data for a compiled file. /// A representation of meta data for a compiled file.
/// ///
/// *Note* this is currently just a copy of what is located in `tsc.rs` but will /// *Note* this is currently just a copy of what is located in `tsc.rs` but will
@ -249,29 +215,28 @@ impl SpecifierHandler for FetchHandler {
None None
}; };
let mut maybe_map_path: Option<PathBuf> = None; let mut maybe_map_path = None;
let map_path = let map_path =
disk_cache.get_cache_filename_with_extension(&url, "js.map"); disk_cache.get_cache_filename_with_extension(&url, "js.map");
let maybe_map: Option<String> = if let Ok(map) = disk_cache.get(&map_path) let maybe_map = if let Ok(map) = disk_cache.get(&map_path) {
{
maybe_map_path = Some(disk_cache.location.join(map_path)); maybe_map_path = Some(disk_cache.location.join(map_path));
Some(String::from_utf8(map)?) Some(String::from_utf8(map)?)
} else { } else {
None None
}; };
let mut emits = HashMap::new(); let mut maybe_emit = None;
let mut maybe_emit_path: Option<PathBuf> = None; let mut maybe_emit_path = None;
let emit_path = disk_cache.get_cache_filename_with_extension(&url, "js"); let emit_path = disk_cache.get_cache_filename_with_extension(&url, "js");
if let Ok(code) = disk_cache.get(&emit_path) { if let Ok(code) = disk_cache.get(&emit_path) {
maybe_emit_path = Some(disk_cache.location.join(emit_path)); maybe_emit = Some(Emit::Cli((String::from_utf8(code)?, maybe_map)));
emits.insert(EmitType::Cli, (String::from_utf8(code)?, maybe_map)); maybe_emit_path =
Some((disk_cache.location.join(emit_path), maybe_map_path));
}; };
Ok(CachedModule { Ok(CachedModule {
emits,
maybe_dependencies: None, maybe_dependencies: None,
maybe_emit,
maybe_emit_path, maybe_emit_path,
maybe_map_path,
maybe_types: source_file.types_header, maybe_types: source_file.types_header,
maybe_version, maybe_version,
media_type: source_file.media_type, media_type: source_file.media_type,
@ -283,63 +248,54 @@ impl SpecifierHandler for FetchHandler {
.boxed_local() .boxed_local()
} }
fn get_build_info( fn get_ts_build_info(
&self, &self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
emit_type: &EmitType,
) -> Result<Option<String>, AnyError> { ) -> Result<Option<String>, AnyError> {
if emit_type != &EmitType::Cli {
return Err(UnsupportedEmitType(emit_type.clone()).into());
}
let filename = self let filename = self
.disk_cache .disk_cache
.get_cache_filename_with_extension(specifier.as_url(), "buildinfo"); .get_cache_filename_with_extension(specifier.as_url(), "buildinfo");
if let Ok(build_info) = self.disk_cache.get(&filename) { if let Ok(ts_build_info) = self.disk_cache.get(&filename) {
return Ok(Some(String::from_utf8(build_info)?)); return Ok(Some(String::from_utf8(ts_build_info)?));
} }
Ok(None) Ok(None)
} }
fn set_build_info( fn set_ts_build_info(
&mut self, &mut self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
emit_type: &EmitType, ts_build_info: String,
build_info: String,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
if emit_type != &EmitType::Cli {
return Err(UnsupportedEmitType(emit_type.clone()).into());
}
let filename = self let filename = self
.disk_cache .disk_cache
.get_cache_filename_with_extension(specifier.as_url(), "buildinfo"); .get_cache_filename_with_extension(specifier.as_url(), "buildinfo");
self self
.disk_cache .disk_cache
.set(&filename, build_info.as_bytes()) .set(&filename, ts_build_info.as_bytes())
.map_err(|e| e.into()) .map_err(|e| e.into())
} }
fn set_cache( fn set_cache(
&mut self, &mut self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
emit_type: &EmitType, emit: &Emit,
code: String,
maybe_map: Option<String>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
if emit_type != &EmitType::Cli { match emit {
return Err(UnsupportedEmitType(emit_type.clone()).into()); Emit::Cli((code, maybe_map)) => {
} let url = specifier.as_url();
let filename = self let filename =
.disk_cache self.disk_cache.get_cache_filename_with_extension(url, "js");
.get_cache_filename_with_extension(specifier.as_url(), "js"); self.disk_cache.set(&filename, code.as_bytes())?;
self.disk_cache.set(&filename, code.as_bytes())?;
if let Some(map) = maybe_map { if let Some(map) = maybe_map {
let filename = self let filename = self
.disk_cache .disk_cache
.get_cache_filename_with_extension(specifier.as_url(), "js.map"); .get_cache_filename_with_extension(url, "js.map");
self.disk_cache.set(&filename, map.as_bytes())?; self.disk_cache.set(&filename, map.as_bytes())?;
} }
}
};
Ok(()) Ok(())
} }
@ -423,7 +379,7 @@ pub mod tests {
.unwrap(); .unwrap();
let cached_module: CachedModule = let cached_module: CachedModule =
file_fetcher.fetch(specifier.clone()).await.unwrap(); file_fetcher.fetch(specifier.clone()).await.unwrap();
assert_eq!(cached_module.emits.len(), 0); assert!(cached_module.maybe_emit.is_none());
assert!(cached_module.maybe_dependencies.is_none()); assert!(cached_module.maybe_dependencies.is_none());
assert_eq!(cached_module.media_type, MediaType::TypeScript); assert_eq!(cached_module.media_type, MediaType::TypeScript);
assert_eq!( assert_eq!(
@ -443,16 +399,16 @@ pub mod tests {
.unwrap(); .unwrap();
let cached_module: CachedModule = let cached_module: CachedModule =
file_fetcher.fetch(specifier.clone()).await.unwrap(); file_fetcher.fetch(specifier.clone()).await.unwrap();
assert_eq!(cached_module.emits.len(), 0); assert!(cached_module.maybe_emit.is_none());
let code = String::from("some code"); let code = String::from("some code");
file_fetcher file_fetcher
.set_cache(&specifier, &EmitType::Cli, code, None) .set_cache(&specifier, &Emit::Cli((code, None)))
.expect("could not set cache"); .expect("could not set cache");
let cached_module: CachedModule = let cached_module: CachedModule =
file_fetcher.fetch(specifier.clone()).await.unwrap(); file_fetcher.fetch(specifier.clone()).await.unwrap();
assert_eq!(cached_module.emits.len(), 1); assert_eq!(
let actual_emit = cached_module.emits.get(&EmitType::Cli).unwrap(); cached_module.maybe_emit,
assert_eq!(actual_emit.0, "some code"); Some(Emit::Cli(("some code".to_string(), None)))
assert_eq!(actual_emit.1, None); );
} }
} }