2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2022-06-29 11:51:11 -04:00
|
|
|
use crate::args::CliOptions;
|
2022-06-28 16:45:55 -04:00
|
|
|
use crate::args::DenoSubcommand;
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::Flags;
|
|
|
|
use crate::args::TypeCheckMode;
|
2021-10-10 17:26:22 -04:00
|
|
|
use crate::cache;
|
2021-10-06 13:07:04 -04:00
|
|
|
use crate::compat;
|
2021-10-18 13:36:28 -04:00
|
|
|
use crate::compat::NodeEsmResolver;
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::deno_dir;
|
2021-10-10 17:26:22 -04:00
|
|
|
use crate::emit;
|
2022-06-20 17:59:52 -04:00
|
|
|
use crate::emit::EmitCache;
|
2022-06-28 16:45:55 -04:00
|
|
|
use crate::emit::TsConfigType;
|
|
|
|
use crate::emit::TsTypeLib;
|
2020-11-05 19:38:21 -05:00
|
|
|
use crate::file_fetcher::FileFetcher;
|
2021-12-16 05:45:41 -05:00
|
|
|
use crate::graph_util::graph_lock_or_exit;
|
|
|
|
use crate::graph_util::GraphData;
|
|
|
|
use crate::graph_util::ModuleEntry;
|
2020-02-19 08:17:13 -05:00
|
|
|
use crate::http_cache;
|
2021-10-10 17:26:22 -04:00
|
|
|
use crate::lockfile::as_maybe_locker;
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::lockfile::Lockfile;
|
2021-10-10 17:26:22 -04:00
|
|
|
use crate::resolver::ImportMapResolver;
|
2021-11-08 20:26:39 -05:00
|
|
|
use crate::resolver::JsxResolver;
|
2020-10-22 20:50:15 -04:00
|
|
|
|
2022-01-31 17:33:57 -05:00
|
|
|
use deno_ast::MediaType;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
|
|
|
use deno_core::anyhow::Context;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::error::custom_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2021-12-16 05:45:41 -05:00
|
|
|
use deno_core::futures;
|
2021-07-06 23:48:01 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2021-12-16 05:45:41 -05:00
|
|
|
use deno_core::parking_lot::RwLock;
|
2021-02-17 13:47:18 -05:00
|
|
|
use deno_core::resolve_url;
|
2020-10-22 20:50:15 -04:00
|
|
|
use deno_core::url::Url;
|
2021-09-29 04:47:24 -04:00
|
|
|
use deno_core::CompiledWasmModuleStore;
|
2020-12-15 00:52:55 -05:00
|
|
|
use deno_core::ModuleSource;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2021-12-15 13:22:36 -05:00
|
|
|
use deno_core::ModuleType;
|
2021-09-10 21:38:24 -04:00
|
|
|
use deno_core::SharedArrayBufferStore;
|
2022-04-15 10:08:09 -04:00
|
|
|
use deno_core::SourceMapGetter;
|
2021-11-15 18:25:52 -05:00
|
|
|
use deno_graph::create_graph;
|
2021-12-16 05:45:41 -05:00
|
|
|
use deno_graph::source::CacheInfo;
|
|
|
|
use deno_graph::source::LoadFuture;
|
|
|
|
use deno_graph::source::Loader;
|
2022-03-10 20:33:02 -05:00
|
|
|
use deno_graph::source::ResolveResponse;
|
2022-01-31 17:33:57 -05:00
|
|
|
use deno_graph::ModuleKind;
|
|
|
|
use deno_graph::Resolved;
|
2021-09-10 21:38:24 -04:00
|
|
|
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
|
2021-12-01 11:13:11 -05:00
|
|
|
use deno_runtime::deno_tls::rustls::RootCertStore;
|
2021-09-10 21:38:24 -04:00
|
|
|
use deno_runtime::deno_web::BlobStore;
|
|
|
|
use deno_runtime::inspector_server::InspectorServer;
|
|
|
|
use deno_runtime::permissions::Permissions;
|
|
|
|
use import_map::ImportMap;
|
2022-01-13 18:17:56 -05:00
|
|
|
use log::warn;
|
2021-06-19 10:14:43 -04:00
|
|
|
use std::collections::HashSet;
|
2019-11-04 10:38:52 -05:00
|
|
|
use std::env;
|
2021-09-24 11:10:42 -04:00
|
|
|
use std::ops::Deref;
|
2022-06-08 06:07:25 -04:00
|
|
|
use std::path::PathBuf;
|
2019-11-04 10:38:52 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
/// This structure represents state of single "deno" program.
|
|
|
|
///
|
|
|
|
/// It is shared by all created workers (thus V8 isolates).
|
2021-09-24 11:10:42 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ProcState(Arc<Inner>);
|
|
|
|
|
|
|
|
pub struct Inner {
|
2019-11-04 10:38:52 -05:00
|
|
|
pub dir: deno_dir::DenoDir,
|
2020-12-21 08:04:25 -05:00
|
|
|
pub coverage_dir: Option<String>,
|
2020-11-05 19:38:21 -05:00
|
|
|
pub file_fetcher: FileFetcher,
|
2022-06-29 11:51:11 -04:00
|
|
|
pub options: Arc<CliOptions>,
|
2021-12-16 05:45:41 -05:00
|
|
|
graph_data: Arc<RwLock<GraphData>>,
|
2020-10-23 17:01:54 -04:00
|
|
|
pub lockfile: Option<Arc<Mutex<Lockfile>>>,
|
2021-11-08 20:26:39 -05:00
|
|
|
pub maybe_import_map: Option<Arc<ImportMap>>,
|
2020-09-25 04:24:51 -04:00
|
|
|
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
|
2022-06-28 16:45:55 -04:00
|
|
|
pub root_cert_store: RootCertStore,
|
2021-07-05 09:34:37 -04:00
|
|
|
pub blob_store: BlobStore,
|
2021-05-22 12:08:24 -04:00
|
|
|
pub broadcast_channel: InMemoryBroadcastChannel,
|
2021-07-06 13:42:52 -04:00
|
|
|
pub shared_array_buffer_store: SharedArrayBufferStore,
|
2021-09-29 04:47:24 -04:00
|
|
|
pub compiled_wasm_module_store: CompiledWasmModuleStore,
|
2021-11-24 10:55:10 -05:00
|
|
|
maybe_resolver: Option<Arc<dyn deno_graph::source::Resolver + Send + Sync>>,
|
2022-06-08 06:07:25 -04:00
|
|
|
maybe_file_watcher_reporter: Option<FileWatcherReporter>,
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2021-09-24 11:10:42 -04:00
|
|
|
impl Deref for ProcState {
|
|
|
|
type Target = Arc<Inner>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProcState {
|
2022-06-28 16:45:55 -04:00
|
|
|
pub async fn build(flags: Flags) -> Result<Self, AnyError> {
|
2022-06-29 11:51:11 -04:00
|
|
|
Self::from_options(Arc::new(CliOptions::from_flags(flags)?)).await
|
2022-06-28 16:45:55 -04:00
|
|
|
}
|
|
|
|
|
2022-06-29 11:51:11 -04:00
|
|
|
pub async fn from_options(
|
|
|
|
options: Arc<CliOptions>,
|
2022-06-28 16:45:55 -04:00
|
|
|
) -> Result<Self, AnyError> {
|
2022-06-29 11:51:11 -04:00
|
|
|
Self::build_with_sender(options, None).await
|
2022-06-08 06:07:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn build_for_file_watcher(
|
2022-06-28 16:45:55 -04:00
|
|
|
flags: Flags,
|
2022-06-08 06:07:25 -04:00
|
|
|
files_to_watch_sender: tokio::sync::mpsc::UnboundedSender<Vec<PathBuf>>,
|
|
|
|
) -> Result<Self, AnyError> {
|
2022-06-28 16:45:55 -04:00
|
|
|
// resolve the config each time
|
2022-06-29 11:51:11 -04:00
|
|
|
let cli_options = Arc::new(CliOptions::from_flags(flags)?);
|
2022-06-28 16:45:55 -04:00
|
|
|
let ps =
|
2022-06-29 11:51:11 -04:00
|
|
|
Self::build_with_sender(cli_options, Some(files_to_watch_sender.clone()))
|
2022-06-28 16:45:55 -04:00
|
|
|
.await?;
|
2022-06-08 06:07:25 -04:00
|
|
|
|
|
|
|
// Add the extra files listed in the watch flag
|
2022-06-29 11:51:11 -04:00
|
|
|
if let Some(watch_paths) = ps.options.watch_paths() {
|
2022-06-08 06:07:25 -04:00
|
|
|
files_to_watch_sender.send(watch_paths.clone()).unwrap();
|
|
|
|
}
|
|
|
|
|
2022-06-28 16:45:55 -04:00
|
|
|
if let Ok(Some(import_map_path)) = ps
|
2022-06-29 11:51:11 -04:00
|
|
|
.options
|
2022-06-29 20:41:48 -04:00
|
|
|
.resolve_import_map_specifier()
|
2022-06-28 16:45:55 -04:00
|
|
|
.map(|ms| ms.and_then(|ref s| s.to_file_path().ok()))
|
2022-06-08 06:07:25 -04:00
|
|
|
{
|
|
|
|
files_to_watch_sender.send(vec![import_map_path]).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ps)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn build_with_sender(
|
2022-06-29 11:51:11 -04:00
|
|
|
cli_options: Arc<CliOptions>,
|
2022-06-08 06:07:25 -04:00
|
|
|
maybe_sender: Option<tokio::sync::mpsc::UnboundedSender<Vec<PathBuf>>>,
|
|
|
|
) -> Result<Self, AnyError> {
|
2021-07-05 09:34:37 -04:00
|
|
|
let blob_store = BlobStore::default();
|
2021-05-22 12:08:24 -04:00
|
|
|
let broadcast_channel = InMemoryBroadcastChannel::default();
|
2021-07-06 13:42:52 -04:00
|
|
|
let shared_array_buffer_store = SharedArrayBufferStore::default();
|
2021-09-29 04:47:24 -04:00
|
|
|
let compiled_wasm_module_store = CompiledWasmModuleStore::default();
|
2022-06-29 11:51:11 -04:00
|
|
|
let dir = cli_options.resolve_deno_dir()?;
|
2022-06-28 16:45:55 -04:00
|
|
|
let deps_cache_location = dir.root.join("deps");
|
|
|
|
let http_cache = http_cache::HttpCache::new(&deps_cache_location);
|
2022-06-29 11:51:11 -04:00
|
|
|
let root_cert_store = cli_options.resolve_root_cert_store()?;
|
|
|
|
let cache_usage = cli_options.cache_setting();
|
2020-11-05 19:38:21 -05:00
|
|
|
let file_fetcher = FileFetcher::new(
|
2020-02-19 08:17:13 -05:00
|
|
|
http_cache,
|
2020-11-05 19:38:21 -05:00
|
|
|
cache_usage,
|
2022-06-29 11:51:11 -04:00
|
|
|
!cli_options.no_remote(),
|
2021-08-07 08:49:38 -04:00
|
|
|
Some(root_cert_store.clone()),
|
2021-07-05 09:34:37 -04:00
|
|
|
blob_store.clone(),
|
2022-06-29 11:51:11 -04:00
|
|
|
cli_options
|
2022-06-28 16:45:55 -04:00
|
|
|
.unsafely_ignore_certificate_errors()
|
|
|
|
.map(ToOwned::to_owned),
|
2019-11-04 10:38:52 -05:00
|
|
|
)?;
|
|
|
|
|
2022-06-29 11:51:11 -04:00
|
|
|
let lockfile = cli_options
|
2022-06-28 16:45:55 -04:00
|
|
|
.resolve_lock_file()?
|
|
|
|
.map(|f| Arc::new(Mutex::new(f)));
|
2022-06-29 20:41:48 -04:00
|
|
|
let maybe_import_map_specifier =
|
|
|
|
cli_options.resolve_import_map_specifier()?;
|
2022-02-22 18:51:14 -05:00
|
|
|
|
|
|
|
let maybe_import_map =
|
|
|
|
if let Some(import_map_specifier) = maybe_import_map_specifier {
|
|
|
|
let file = file_fetcher
|
|
|
|
.fetch(&import_map_specifier, &mut Permissions::allow_all())
|
|
|
|
.await
|
|
|
|
.context(format!(
|
|
|
|
"Unable to load '{}' import map",
|
|
|
|
import_map_specifier
|
|
|
|
))?;
|
|
|
|
let import_map =
|
|
|
|
import_map_from_text(&import_map_specifier, &file.source)?;
|
|
|
|
Some(Arc::new(import_map))
|
|
|
|
} else {
|
|
|
|
None
|
2020-05-29 10:32:15 -04:00
|
|
|
};
|
|
|
|
|
2022-06-28 16:45:55 -04:00
|
|
|
let maybe_inspector_server =
|
2022-06-29 11:51:11 -04:00
|
|
|
cli_options.resolve_inspector_server().map(Arc::new);
|
2020-09-25 04:24:51 -04:00
|
|
|
|
2022-06-29 11:51:11 -04:00
|
|
|
let coverage_dir = cli_options
|
2022-06-28 16:45:55 -04:00
|
|
|
.coverage_dir()
|
|
|
|
.map(ToOwned::to_owned)
|
2020-12-21 08:04:25 -05:00
|
|
|
.or_else(|| env::var("DENO_UNSTABLE_COVERAGE_DIR").ok());
|
|
|
|
|
2021-11-24 10:55:10 -05:00
|
|
|
// FIXME(bartlomieju): `NodeEsmResolver` is not aware of JSX resolver
|
|
|
|
// created below
|
|
|
|
let node_resolver = NodeEsmResolver::new(
|
|
|
|
maybe_import_map.clone().map(ImportMapResolver::new),
|
|
|
|
);
|
|
|
|
let maybe_import_map_resolver =
|
|
|
|
maybe_import_map.clone().map(ImportMapResolver::new);
|
2022-06-29 11:51:11 -04:00
|
|
|
let maybe_jsx_resolver = cli_options
|
2022-06-28 16:45:55 -04:00
|
|
|
.to_maybe_jsx_import_source_module()
|
|
|
|
.map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone()));
|
2021-11-24 10:55:10 -05:00
|
|
|
let maybe_resolver: Option<
|
|
|
|
Arc<dyn deno_graph::source::Resolver + Send + Sync>,
|
2022-06-29 11:51:11 -04:00
|
|
|
> = if cli_options.compat() {
|
2021-11-24 10:55:10 -05:00
|
|
|
Some(Arc::new(node_resolver))
|
|
|
|
} else if let Some(jsx_resolver) = maybe_jsx_resolver {
|
|
|
|
// the JSX resolver offloads to the import map if present, otherwise uses
|
|
|
|
// the default Deno explicit import resolution.
|
|
|
|
Some(Arc::new(jsx_resolver))
|
|
|
|
} else if let Some(import_map_resolver) = maybe_import_map_resolver {
|
|
|
|
Some(Arc::new(import_map_resolver))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2022-06-08 06:07:25 -04:00
|
|
|
let maybe_file_watcher_reporter =
|
|
|
|
maybe_sender.map(|sender| FileWatcherReporter {
|
|
|
|
sender,
|
|
|
|
file_paths: Arc::new(Mutex::new(vec![])),
|
|
|
|
});
|
|
|
|
|
2021-09-24 11:10:42 -04:00
|
|
|
Ok(ProcState(Arc::new(Inner {
|
2019-11-04 10:38:52 -05:00
|
|
|
dir,
|
2020-12-21 08:04:25 -05:00
|
|
|
coverage_dir,
|
2022-06-29 11:51:11 -04:00
|
|
|
options: cli_options,
|
2019-11-04 10:38:52 -05:00
|
|
|
file_fetcher,
|
2021-10-19 10:01:46 -04:00
|
|
|
graph_data: Default::default(),
|
2019-11-04 10:38:52 -05:00
|
|
|
lockfile,
|
2020-05-29 10:32:15 -04:00
|
|
|
maybe_import_map,
|
2020-09-25 04:24:51 -04:00
|
|
|
maybe_inspector_server,
|
2022-06-28 16:45:55 -04:00
|
|
|
root_cert_store,
|
2021-07-05 09:34:37 -04:00
|
|
|
blob_store,
|
2021-05-22 12:08:24 -04:00
|
|
|
broadcast_channel,
|
2021-07-06 13:42:52 -04:00
|
|
|
shared_array_buffer_store,
|
2021-09-29 04:47:24 -04:00
|
|
|
compiled_wasm_module_store,
|
2021-11-24 10:55:10 -05:00
|
|
|
maybe_resolver,
|
2022-06-08 06:07:25 -04:00
|
|
|
maybe_file_watcher_reporter,
|
2021-09-24 11:10:42 -04:00
|
|
|
})))
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2021-11-15 18:25:52 -05:00
|
|
|
/// This method must be called for a module or a static importer of that
|
|
|
|
/// module before attempting to `load()` it from a `JsRuntime`. It will
|
2021-12-16 05:45:41 -05:00
|
|
|
/// populate `self.graph_data` in memory with the necessary source code, write
|
|
|
|
/// emits where necessary or report any module graph / type checking errors.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub async fn prepare_module_load(
|
2021-09-24 11:10:42 -04:00
|
|
|
&self,
|
2022-03-10 20:33:02 -05:00
|
|
|
roots: Vec<ModuleSpecifier>,
|
2021-10-10 17:26:22 -04:00
|
|
|
is_dynamic: bool,
|
2022-06-28 16:45:55 -04:00
|
|
|
lib: TsTypeLib,
|
2021-05-17 03:44:38 -04:00
|
|
|
root_permissions: Permissions,
|
|
|
|
dynamic_permissions: Permissions,
|
2021-11-15 18:25:52 -05:00
|
|
|
reload_on_watch: bool,
|
2021-04-28 14:17:04 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-03-10 20:33:02 -05:00
|
|
|
let maybe_resolver: Option<&dyn deno_graph::source::Resolver> =
|
|
|
|
if let Some(resolver) = &self.maybe_resolver {
|
|
|
|
Some(resolver.as_ref())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// NOTE(@bartlomieju):
|
|
|
|
// Even though `roots` are fully resolved at this point, we are going
|
|
|
|
// to resolve them through `maybe_resolver` to get module kind for the graph
|
|
|
|
// or default to ESM.
|
|
|
|
//
|
|
|
|
// One might argue that this is a code smell, and I would agree. However
|
|
|
|
// due to flux in "Node compatibility" it's not clear where it should be
|
|
|
|
// decided what `ModuleKind` is decided for root specifier.
|
|
|
|
let roots = roots
|
|
|
|
.into_iter()
|
|
|
|
.map(|r| {
|
|
|
|
if let Some(resolver) = &maybe_resolver {
|
|
|
|
let response =
|
|
|
|
resolver.resolve(r.as_str(), &Url::parse("unused:").unwrap());
|
|
|
|
// TODO(bartlomieju): this should be implemented in `deno_graph`
|
|
|
|
match response {
|
|
|
|
ResolveResponse::CommonJs(_) => (r, ModuleKind::CommonJs),
|
|
|
|
ResolveResponse::Err(_) => unreachable!(),
|
|
|
|
_ => (r, ModuleKind::Esm),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(r, ModuleKind::Esm)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2021-11-15 18:25:52 -05:00
|
|
|
// TODO(bartlomieju): this is very make-shift, is there an existing API
|
|
|
|
// that we could include it like with "maybe_imports"?
|
2022-06-29 11:51:11 -04:00
|
|
|
let roots = if self.options.compat() {
|
2022-01-31 17:33:57 -05:00
|
|
|
let mut r = vec![(compat::GLOBAL_URL.clone(), ModuleKind::Esm)];
|
2021-11-15 18:25:52 -05:00
|
|
|
r.extend(roots);
|
|
|
|
r
|
|
|
|
} else {
|
|
|
|
roots
|
|
|
|
};
|
|
|
|
if !reload_on_watch {
|
2021-12-16 05:45:41 -05:00
|
|
|
let graph_data = self.graph_data.read();
|
2022-06-29 11:51:11 -04:00
|
|
|
if self.options.type_check_mode() == TypeCheckMode::None
|
2021-12-16 05:45:41 -05:00
|
|
|
|| graph_data.is_type_checked(&roots, &lib)
|
2021-11-15 18:25:52 -05:00
|
|
|
{
|
2021-12-22 08:25:06 -05:00
|
|
|
if let Some(result) = graph_data.check(
|
|
|
|
&roots,
|
2022-06-29 11:51:11 -04:00
|
|
|
self.options.type_check_mode() != TypeCheckMode::None,
|
2021-12-22 08:25:06 -05:00
|
|
|
false,
|
|
|
|
) {
|
2021-11-15 18:25:52 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 17:26:22 -04:00
|
|
|
let mut cache = cache::FetchCacher::new(
|
|
|
|
self.dir.gen_cache.clone(),
|
|
|
|
self.file_fetcher.clone(),
|
|
|
|
root_permissions.clone(),
|
|
|
|
dynamic_permissions.clone(),
|
|
|
|
);
|
|
|
|
let maybe_locker = as_maybe_locker(self.lockfile.clone());
|
2022-06-29 11:51:11 -04:00
|
|
|
let maybe_imports = self.options.to_maybe_imports()?;
|
2021-12-16 05:45:41 -05:00
|
|
|
|
|
|
|
struct ProcStateLoader<'a> {
|
|
|
|
inner: &'a mut cache::FetchCacher,
|
|
|
|
graph_data: Arc<RwLock<GraphData>>,
|
|
|
|
reload: bool,
|
|
|
|
}
|
|
|
|
impl Loader for ProcStateLoader<'_> {
|
|
|
|
fn get_cache_info(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<CacheInfo> {
|
|
|
|
self.inner.get_cache_info(specifier)
|
|
|
|
}
|
|
|
|
fn load(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
is_dynamic: bool,
|
|
|
|
) -> LoadFuture {
|
|
|
|
let graph_data = self.graph_data.read();
|
|
|
|
let found_specifier = graph_data.follow_redirect(specifier);
|
|
|
|
match graph_data.get(&found_specifier) {
|
2022-01-13 11:58:00 -05:00
|
|
|
Some(_) if !self.reload => {
|
|
|
|
Box::pin(futures::future::ready(Err(anyhow!(""))))
|
|
|
|
}
|
2021-12-16 05:45:41 -05:00
|
|
|
_ => self.inner.load(specifier, is_dynamic),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut loader = ProcStateLoader {
|
|
|
|
inner: &mut cache,
|
|
|
|
graph_data: self.graph_data.clone(),
|
|
|
|
reload: reload_on_watch,
|
|
|
|
};
|
2022-03-10 20:33:02 -05:00
|
|
|
|
2022-06-08 06:07:25 -04:00
|
|
|
let maybe_file_watcher_reporter: Option<&dyn deno_graph::source::Reporter> =
|
|
|
|
if let Some(reporter) = &self.maybe_file_watcher_reporter {
|
|
|
|
Some(reporter)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2021-11-15 18:25:52 -05:00
|
|
|
let graph = create_graph(
|
|
|
|
roots.clone(),
|
2021-10-10 17:26:22 -04:00
|
|
|
is_dynamic,
|
|
|
|
maybe_imports,
|
2021-12-16 05:45:41 -05:00
|
|
|
&mut loader,
|
2021-10-18 13:36:28 -04:00
|
|
|
maybe_resolver,
|
2021-10-10 17:26:22 -04:00
|
|
|
maybe_locker,
|
|
|
|
None,
|
2022-06-08 06:07:25 -04:00
|
|
|
maybe_file_watcher_reporter,
|
2021-10-10 17:26:22 -04:00
|
|
|
)
|
|
|
|
.await;
|
2022-02-27 08:38:45 -05:00
|
|
|
|
|
|
|
let needs_cjs_esm_translation = graph
|
|
|
|
.modules()
|
|
|
|
.iter()
|
|
|
|
.any(|m| m.kind == ModuleKind::CommonJs);
|
|
|
|
|
|
|
|
if needs_cjs_esm_translation {
|
|
|
|
for module in graph.modules() {
|
|
|
|
// TODO(bartlomieju): this is overly simplistic heuristic, once we are
|
|
|
|
// in compat mode, all files ending with plain `.js` extension are
|
|
|
|
// considered CommonJs modules. Which leads to situation where valid
|
|
|
|
// ESM modules with `.js` extension might undergo translation (it won't
|
|
|
|
// work in this situation).
|
|
|
|
if module.kind == ModuleKind::CommonJs {
|
|
|
|
let translated_source = compat::translate_cjs_to_esm(
|
|
|
|
&self.file_fetcher,
|
|
|
|
&module.specifier,
|
|
|
|
module.maybe_source.as_ref().unwrap().to_string(),
|
|
|
|
module.media_type,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
let mut graph_data = self.graph_data.write();
|
|
|
|
graph_data
|
|
|
|
.add_cjs_esm_translation(&module.specifier, translated_source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
// If there was a locker, validate the integrity of all the modules in the
|
|
|
|
// locker.
|
2021-12-16 05:45:41 -05:00
|
|
|
graph_lock_or_exit(&graph);
|
2021-10-10 17:26:22 -04:00
|
|
|
|
|
|
|
// Determine any modules that have already been emitted this session and
|
|
|
|
// should be skipped.
|
|
|
|
let reload_exclusions: HashSet<ModuleSpecifier> = {
|
2021-12-16 05:45:41 -05:00
|
|
|
let graph_data = self.graph_data.read();
|
|
|
|
graph_data.entries().into_keys().cloned().collect()
|
2021-06-19 10:14:43 -04:00
|
|
|
};
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-12-16 05:45:41 -05:00
|
|
|
{
|
|
|
|
let mut graph_data = self.graph_data.write();
|
|
|
|
graph_data.add_graph(&graph, reload_on_watch);
|
2022-06-29 11:51:11 -04:00
|
|
|
let check_js = self.options.check_js();
|
2021-12-16 05:45:41 -05:00
|
|
|
graph_data
|
2022-03-28 21:48:29 -04:00
|
|
|
.check(
|
|
|
|
&roots,
|
2022-06-29 11:51:11 -04:00
|
|
|
self.options.type_check_mode() != TypeCheckMode::None,
|
2022-03-28 21:48:29 -04:00
|
|
|
check_js,
|
|
|
|
)
|
2021-12-16 05:45:41 -05:00
|
|
|
.unwrap()?;
|
|
|
|
}
|
|
|
|
|
2022-06-29 11:51:11 -04:00
|
|
|
let config_type = if self.options.type_check_mode() == TypeCheckMode::None {
|
2022-06-28 16:45:55 -04:00
|
|
|
TsConfigType::Emit
|
2022-06-27 16:54:09 -04:00
|
|
|
} else {
|
2022-06-28 16:45:55 -04:00
|
|
|
TsConfigType::Check {
|
2022-06-27 16:54:09 -04:00
|
|
|
tsc_emit: true,
|
2022-06-28 16:45:55 -04:00
|
|
|
lib,
|
2022-06-27 16:54:09 -04:00
|
|
|
}
|
|
|
|
};
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2022-06-28 16:45:55 -04:00
|
|
|
let ts_config_result =
|
2022-06-29 11:51:11 -04:00
|
|
|
self.options.resolve_ts_config_for_emit(config_type)?;
|
2021-12-16 05:45:41 -05:00
|
|
|
|
2022-06-28 16:45:55 -04:00
|
|
|
if let Some(ignored_options) = ts_config_result.maybe_ignored_options {
|
2021-12-16 05:45:41 -05:00
|
|
|
log::warn!("{}", ignored_options);
|
|
|
|
}
|
|
|
|
|
2022-06-29 11:51:11 -04:00
|
|
|
if self.options.type_check_mode() == TypeCheckMode::None {
|
2021-12-16 05:45:41 -05:00
|
|
|
let options = emit::EmitOptions {
|
2022-06-28 16:45:55 -04:00
|
|
|
ts_config: ts_config_result.ts_config,
|
2022-06-29 11:51:11 -04:00
|
|
|
reload: self.options.reload_flag(),
|
2021-12-16 05:45:41 -05:00
|
|
|
reload_exclusions,
|
2021-10-10 17:26:22 -04:00
|
|
|
};
|
2022-06-20 17:59:52 -04:00
|
|
|
let emit_result = emit::emit(&graph, &self.dir.gen_cache, options)?;
|
2021-10-10 17:26:22 -04:00
|
|
|
log::debug!("{}", emit_result.stats);
|
2021-12-16 05:45:41 -05:00
|
|
|
} else {
|
2022-06-29 11:51:11 -04:00
|
|
|
let maybe_config_specifier = self.options.maybe_config_file_specifier();
|
2021-12-16 05:45:41 -05:00
|
|
|
let options = emit::CheckOptions {
|
2022-06-29 11:51:11 -04:00
|
|
|
type_check_mode: self.options.type_check_mode(),
|
|
|
|
debug: self.options.log_level() == Some(log::Level::Debug),
|
2021-12-16 05:45:41 -05:00
|
|
|
emit_with_diagnostics: false,
|
|
|
|
maybe_config_specifier,
|
2022-06-28 16:45:55 -04:00
|
|
|
ts_config: ts_config_result.ts_config,
|
2021-12-16 05:45:41 -05:00
|
|
|
log_checks: true,
|
2022-06-29 11:51:11 -04:00
|
|
|
reload: self.options.reload_flag(),
|
2021-12-16 05:45:41 -05:00
|
|
|
reload_exclusions,
|
|
|
|
};
|
|
|
|
let emit_result = emit::check_and_maybe_emit(
|
|
|
|
&roots,
|
|
|
|
self.graph_data.clone(),
|
2022-06-20 17:59:52 -04:00
|
|
|
&self.dir.gen_cache,
|
2021-12-16 05:45:41 -05:00
|
|
|
options,
|
|
|
|
)?;
|
2021-11-15 18:25:52 -05:00
|
|
|
if !emit_result.diagnostics.is_empty() {
|
2021-12-16 05:45:41 -05:00
|
|
|
return Err(anyhow!(emit_result.diagnostics));
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
2021-12-16 05:45:41 -05:00
|
|
|
log::debug!("{}", emit_result.stats);
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
2022-06-29 11:51:11 -04:00
|
|
|
if self.options.type_check_mode() != TypeCheckMode::None {
|
2021-12-16 05:45:41 -05:00
|
|
|
let mut graph_data = self.graph_data.write();
|
2022-06-28 16:45:55 -04:00
|
|
|
graph_data.set_type_checked(&roots, lib);
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// any updates to the lockfile should be updated now
|
2021-04-28 14:17:04 -04:00
|
|
|
if let Some(ref lockfile) = self.lockfile {
|
2021-07-06 23:48:01 -04:00
|
|
|
let g = lockfile.lock();
|
2021-04-28 14:17:04 -04:00
|
|
|
g.write()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn resolve(
|
2021-09-24 11:10:42 -04:00
|
|
|
&self,
|
2021-10-10 17:26:22 -04:00
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2021-11-23 16:30:52 -05:00
|
|
|
if let Ok(referrer) = deno_core::resolve_url_or_path(referrer) {
|
2021-12-16 05:45:41 -05:00
|
|
|
let graph_data = self.graph_data.read();
|
|
|
|
let found_referrer = graph_data.follow_redirect(&referrer);
|
|
|
|
let maybe_resolved = match graph_data.get(&found_referrer) {
|
2022-01-31 17:33:57 -05:00
|
|
|
Some(ModuleEntry::Module { dependencies, .. }) => {
|
|
|
|
dependencies.get(specifier).map(|d| &d.maybe_code)
|
|
|
|
}
|
2021-11-23 16:30:52 -05:00
|
|
|
_ => None,
|
2021-10-19 10:01:46 -04:00
|
|
|
};
|
|
|
|
|
2021-11-23 16:30:52 -05:00
|
|
|
match maybe_resolved {
|
2022-01-31 17:33:57 -05:00
|
|
|
Some(Resolved::Ok { specifier, .. }) => return Ok(specifier.clone()),
|
|
|
|
Some(Resolved::Err(err)) => {
|
2021-11-23 16:30:52 -05:00
|
|
|
return Err(custom_error(
|
|
|
|
"TypeError",
|
|
|
|
format!("{}\n", err.to_string_with_range()),
|
|
|
|
))
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
2022-01-31 17:33:57 -05:00
|
|
|
Some(Resolved::None) | None => {}
|
2020-09-24 18:31:17 -04:00
|
|
|
}
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
2021-10-19 10:01:46 -04:00
|
|
|
|
2021-11-24 10:55:10 -05:00
|
|
|
// FIXME(bartlomieju): this is a hacky way to provide compatibility with REPL
|
|
|
|
// and `Deno.core.evalContext` API. Ideally we should always have a referrer filled
|
|
|
|
// but sadly that's not the case due to missing APIs in V8.
|
2022-06-28 16:45:55 -04:00
|
|
|
let referrer = if referrer.is_empty()
|
2022-06-29 11:51:11 -04:00
|
|
|
&& matches!(self.options.sub_command(), DenoSubcommand::Repl(_))
|
2022-06-28 16:45:55 -04:00
|
|
|
{
|
2021-11-24 10:55:10 -05:00
|
|
|
deno_core::resolve_url_or_path("./$deno$repl.ts").unwrap()
|
2020-09-24 18:31:17 -04:00
|
|
|
} else {
|
2021-11-24 10:55:10 -05:00
|
|
|
deno_core::resolve_url_or_path(referrer).unwrap()
|
2020-10-22 20:50:15 -04:00
|
|
|
};
|
2021-11-24 10:55:10 -05:00
|
|
|
|
|
|
|
let maybe_resolver: Option<&dyn deno_graph::source::Resolver> =
|
|
|
|
if let Some(resolver) = &self.maybe_resolver {
|
|
|
|
Some(resolver.as_ref())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
if let Some(resolver) = &maybe_resolver {
|
2022-01-31 17:33:57 -05:00
|
|
|
resolver.resolve(specifier, &referrer).to_result()
|
2021-10-10 17:26:22 -04:00
|
|
|
} else {
|
2021-11-24 10:55:10 -05:00
|
|
|
deno_core::resolve_import(specifier, referrer.as_str())
|
|
|
|
.map_err(|err| err.into())
|
2020-07-02 11:54:51 -04:00
|
|
|
}
|
2020-05-29 10:32:15 -04:00
|
|
|
}
|
|
|
|
|
2020-12-15 00:52:55 -05:00
|
|
|
pub fn load(
|
2020-05-29 10:32:15 -04:00
|
|
|
&self,
|
2020-12-15 00:52:55 -05:00
|
|
|
specifier: ModuleSpecifier,
|
2020-10-22 20:50:15 -04:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2021-10-10 17:26:22 -04:00
|
|
|
is_dynamic: bool,
|
2020-12-15 00:52:55 -05:00
|
|
|
) -> Result<ModuleSource, AnyError> {
|
2021-10-10 17:26:22 -04:00
|
|
|
log::debug!(
|
|
|
|
"specifier: {} maybe_referrer: {} is_dynamic: {}",
|
|
|
|
specifier,
|
|
|
|
maybe_referrer
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| s.to_string())
|
|
|
|
.unwrap_or_else(|| "<none>".to_string()),
|
|
|
|
is_dynamic
|
|
|
|
);
|
2021-10-19 10:01:46 -04:00
|
|
|
|
2021-12-16 05:45:41 -05:00
|
|
|
let graph_data = self.graph_data.read();
|
2022-06-20 11:20:52 -04:00
|
|
|
let found_url = graph_data.follow_redirect(&specifier);
|
|
|
|
match graph_data.get(&found_url) {
|
2021-12-15 13:22:36 -05:00
|
|
|
Some(ModuleEntry::Module {
|
|
|
|
code, media_type, ..
|
2021-12-16 05:45:41 -05:00
|
|
|
}) => {
|
|
|
|
let code = match media_type {
|
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Unknown
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Mjs
|
2022-02-27 08:38:45 -05:00
|
|
|
| MediaType::Json => {
|
|
|
|
if let Some(source) = graph_data.get_cjs_esm_translation(&specifier)
|
|
|
|
{
|
|
|
|
source.to_owned()
|
|
|
|
} else {
|
2022-05-20 16:40:55 -04:00
|
|
|
code.to_string()
|
2022-02-27 08:38:45 -05:00
|
|
|
}
|
|
|
|
}
|
2022-06-20 11:20:52 -04:00
|
|
|
MediaType::Dts | MediaType::Dcts | MediaType::Dmts => "".to_string(),
|
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Jsx
|
|
|
|
| MediaType::Tsx => {
|
2022-06-20 17:59:52 -04:00
|
|
|
let cached_text = self.dir.gen_cache.get_emit_text(&found_url);
|
|
|
|
match cached_text {
|
|
|
|
Some(text) => text,
|
|
|
|
None => unreachable!("Unexpected missing emit: {}\n\nTry reloading with the --reload CLI flag or deleting your DENO_DIR.", found_url),
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
2022-06-20 11:20:52 -04:00
|
|
|
MediaType::TsBuildInfo | MediaType::Wasm | MediaType::SourceMap => {
|
|
|
|
panic!("Unexpected media type {} for {}", media_type, found_url)
|
|
|
|
}
|
2021-12-16 05:45:41 -05:00
|
|
|
};
|
|
|
|
Ok(ModuleSource {
|
2022-05-05 07:16:25 -04:00
|
|
|
code: code.into_bytes().into_boxed_slice(),
|
2021-12-16 05:45:41 -05:00
|
|
|
module_url_specified: specifier.to_string(),
|
2022-06-20 11:20:52 -04:00
|
|
|
module_url_found: found_url.to_string(),
|
2021-12-16 05:45:41 -05:00
|
|
|
module_type: match media_type {
|
|
|
|
MediaType::Json => ModuleType::Json,
|
|
|
|
_ => ModuleType::JavaScript,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2021-11-15 18:25:52 -05:00
|
|
|
_ => Err(anyhow!(
|
|
|
|
"Loading unprepared module: {}",
|
|
|
|
specifier.to_string()
|
|
|
|
)),
|
2021-10-19 10:01:46 -04:00
|
|
|
}
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
2022-07-01 11:50:16 -04:00
|
|
|
|
|
|
|
pub async fn create_graph(
|
|
|
|
&self,
|
|
|
|
roots: Vec<(ModuleSpecifier, ModuleKind)>,
|
|
|
|
) -> Result<deno_graph::ModuleGraph, AnyError> {
|
|
|
|
let mut cache = cache::FetchCacher::new(
|
|
|
|
self.dir.gen_cache.clone(),
|
|
|
|
self.file_fetcher.clone(),
|
|
|
|
Permissions::allow_all(),
|
|
|
|
Permissions::allow_all(),
|
|
|
|
);
|
|
|
|
let maybe_locker = as_maybe_locker(self.lockfile.clone());
|
|
|
|
let maybe_import_map_resolver =
|
|
|
|
self.maybe_import_map.clone().map(ImportMapResolver::new);
|
|
|
|
let maybe_imports = self.options.to_maybe_imports()?;
|
|
|
|
let maybe_jsx_resolver = self
|
|
|
|
.options
|
|
|
|
.to_maybe_jsx_import_source_module()
|
|
|
|
.map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone()));
|
|
|
|
let maybe_resolver = if maybe_jsx_resolver.is_some() {
|
|
|
|
maybe_jsx_resolver.as_ref().map(|jr| jr.as_resolver())
|
|
|
|
} else {
|
|
|
|
maybe_import_map_resolver
|
|
|
|
.as_ref()
|
|
|
|
.map(|im| im.as_resolver())
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(
|
|
|
|
deno_graph::create_graph(
|
|
|
|
roots,
|
|
|
|
false,
|
|
|
|
maybe_imports,
|
|
|
|
&mut cache,
|
|
|
|
maybe_resolver,
|
|
|
|
maybe_locker,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await,
|
|
|
|
)
|
|
|
|
}
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
// TODO(@kitsonk) this is only temporary, but should be refactored to somewhere
|
|
|
|
// else, like a refactored file_fetcher.
|
2021-09-24 11:10:42 -04:00
|
|
|
impl SourceMapGetter for ProcState {
|
2020-10-22 20:50:15 -04:00
|
|
|
fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>> {
|
2021-02-17 13:47:18 -05:00
|
|
|
if let Ok(specifier) = resolve_url(file_name) {
|
2021-11-15 18:25:52 -05:00
|
|
|
match specifier.scheme() {
|
|
|
|
// we should only be looking for emits for schemes that denote external
|
|
|
|
// modules, which the disk_cache supports
|
|
|
|
"wasm" | "file" | "http" | "https" | "data" | "blob" => (),
|
|
|
|
_ => return None,
|
|
|
|
}
|
2022-06-20 17:59:52 -04:00
|
|
|
if let Some(cache_data) = self.dir.gen_cache.get_emit_data(&specifier) {
|
|
|
|
source_map_from_code(cache_data.text.as_bytes())
|
|
|
|
.or_else(|| cache_data.map.map(|t| t.into_bytes()))
|
2021-10-10 17:26:22 -04:00
|
|
|
} else if let Ok(source) = self.load(specifier, None, false) {
|
2022-06-20 08:42:20 -04:00
|
|
|
source_map_from_code(&source.code)
|
2020-10-22 20:50:15 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2020-06-15 11:53:05 -04:00
|
|
|
} else {
|
2020-10-22 20:50:15 -04:00
|
|
|
None
|
2020-06-10 10:02:41 -04:00
|
|
|
}
|
2020-10-22 20:50:15 -04:00
|
|
|
}
|
2020-06-01 15:01:51 -04:00
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
fn get_source_line(
|
|
|
|
&self,
|
|
|
|
file_name: &str,
|
|
|
|
line_number: usize,
|
|
|
|
) -> Option<String> {
|
2021-02-17 13:47:18 -05:00
|
|
|
if let Ok(specifier) = resolve_url(file_name) {
|
2020-11-15 21:19:31 -05:00
|
|
|
self.file_fetcher.get_source(&specifier).map(|out| {
|
2020-11-05 19:38:21 -05:00
|
|
|
// Do NOT use .lines(): it skips the terminating empty line.
|
2022-02-22 18:51:14 -05:00
|
|
|
// (due to internally using_terminator() instead of .split())
|
2020-11-05 19:38:21 -05:00
|
|
|
let lines: Vec<&str> = out.source.split('\n').collect();
|
2021-10-18 12:05:36 -04:00
|
|
|
if line_number >= lines.len() {
|
|
|
|
format!(
|
|
|
|
"{} Couldn't format source line: Line {} is out of bounds (source may have changed at runtime)",
|
|
|
|
crate::colors::yellow("Warning"), line_number + 1,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
lines[line_number].to_string()
|
|
|
|
}
|
2020-11-05 19:38:21 -05:00
|
|
|
})
|
2020-10-22 20:50:15 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2020-06-01 15:01:51 -04:00
|
|
|
}
|
|
|
|
|
2022-01-13 18:17:56 -05:00
|
|
|
pub fn import_map_from_text(
|
|
|
|
specifier: &Url,
|
|
|
|
json_text: &str,
|
|
|
|
) -> Result<ImportMap, AnyError> {
|
2022-06-14 10:05:37 -04:00
|
|
|
debug_assert!(
|
|
|
|
!specifier.as_str().contains("../"),
|
|
|
|
"Import map specifier incorrectly contained ../: {}",
|
|
|
|
specifier.as_str()
|
|
|
|
);
|
|
|
|
let result = import_map::parse_from_json(specifier, json_text)?;
|
2022-01-13 18:17:56 -05:00
|
|
|
if !result.diagnostics.is_empty() {
|
|
|
|
warn!(
|
|
|
|
"Import map diagnostics:\n{}",
|
|
|
|
result
|
|
|
|
.diagnostics
|
|
|
|
.into_iter()
|
|
|
|
.map(|d| format!(" - {}", d))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n")
|
2022-06-14 10:05:37 -04:00
|
|
|
);
|
2022-01-13 18:17:56 -05:00
|
|
|
}
|
|
|
|
Ok(result.import_map)
|
|
|
|
}
|
|
|
|
|
2022-06-20 08:42:20 -04:00
|
|
|
fn source_map_from_code(code: &[u8]) -> Option<Vec<u8>> {
|
|
|
|
static PREFIX: &[u8] = b"//# sourceMappingURL=data:application/json;base64,";
|
|
|
|
let last_line = code.rsplitn(2, |u| u == &b'\n').next().unwrap();
|
|
|
|
if last_line.starts_with(PREFIX) {
|
|
|
|
let input = last_line.split_at(PREFIX.len()).1;
|
|
|
|
let decoded_map = base64::decode(input)
|
|
|
|
.expect("Unable to decode source map from emitted file.");
|
|
|
|
Some(decoded_map)
|
2021-01-05 18:10:36 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 06:07:25 -04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct FileWatcherReporter {
|
|
|
|
sender: tokio::sync::mpsc::UnboundedSender<Vec<PathBuf>>,
|
|
|
|
file_paths: Arc<Mutex<Vec<PathBuf>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_graph::source::Reporter for FileWatcherReporter {
|
|
|
|
fn on_load(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
modules_done: usize,
|
|
|
|
modules_total: usize,
|
|
|
|
) {
|
|
|
|
let mut file_paths = self.file_paths.lock();
|
|
|
|
if specifier.scheme() == "file" {
|
|
|
|
file_paths.push(specifier.to_file_path().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
if modules_done == modules_total {
|
|
|
|
self.sender.send(file_paths.drain(..).collect()).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|