runtime/cache/
mod.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7use std::path::Path;
8
9pub use cache::*;
10use swc_sourcemap::SourceMap;
11
12mod cache;
13pub mod map;
14
15pub fn locate_in_cache<P: AsRef<Path>>(path: P, script: &str) -> Option<(String, SourceMap)> {
16	let result = Cache::new().map(|cache| {
17		let path = path.as_ref();
18		let folder = cache.find_folder(path)?;
19		match cache.check_cache(path, &folder, script) {
20			Ok(s) => Ok(s),
21			Err(Error::HashedSource(hash)) => cache.save_to_cache(path, &folder, script, Some(&hash)),
22			Err(Error::Other) => cache.save_to_cache(path, &folder, script, None),
23			Err(err) => Err(err),
24		}
25	});
26
27	match result {
28		Some(Ok(s)) => Some(s),
29		Some(Err(Error::HashedSource(_) | Error::Other)) | None => None,
30		Some(Err(err)) => {
31			eprintln!("Error occurred while compiling TypeScript");
32			eprintln!("{err}");
33			None
34		}
35	}
36}