2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-10-10 17:26:22 -04:00
|
|
|
|
|
|
|
//! The collection of APIs to be able to take `deno_graph` module graphs and
|
|
|
|
//! populate a cache, emit files, and transform a graph into the structures for
|
|
|
|
//! loading into an isolate.
|
|
|
|
|
2022-06-28 16:45:55 -04:00
|
|
|
use crate::args::config_file::IgnoredCompilerOptions;
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::ConfigFile;
|
|
|
|
use crate::args::EmitConfigOptions;
|
|
|
|
use crate::args::TsConfig;
|
|
|
|
use crate::args::TypeCheckMode;
|
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-07-12 18:58:39 -04:00
|
|
|
use crate::cache::TypeCheckCache;
|
2021-10-10 17:26:22 -04:00
|
|
|
use crate::colors;
|
|
|
|
use crate::diagnostics::Diagnostics;
|
2021-12-16 05:45:41 -05:00
|
|
|
use crate::graph_util::GraphData;
|
|
|
|
use crate::graph_util::ModuleEntry;
|
2021-10-10 17:26:22 -04:00
|
|
|
use crate::tsc;
|
|
|
|
use crate::version;
|
|
|
|
|
2022-01-13 11:58:00 -05:00
|
|
|
use deno_ast::swc::bundler::Hook;
|
|
|
|
use deno_ast::swc::bundler::ModuleRecord;
|
|
|
|
use deno_ast::swc::common::Span;
|
2022-07-19 11:58:18 -04:00
|
|
|
use deno_ast::ParsedSource;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::error::AnyError;
|
2021-12-16 05:45:41 -05:00
|
|
|
use deno_core::parking_lot::RwLock;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Deserializer;
|
|
|
|
use deno_core::serde::Serialize;
|
|
|
|
use deno_core::serde::Serializer;
|
2022-01-13 11:58:00 -05:00
|
|
|
use deno_core::serde_json;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_graph::MediaType;
|
|
|
|
use deno_graph::ModuleGraphError;
|
2022-01-31 17:33:57 -05:00
|
|
|
use deno_graph::ModuleKind;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_graph::ResolutionError;
|
|
|
|
use std::fmt;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
/// A structure representing stats from an emit operation for a graph.
|
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct Stats(pub Vec<(String, u32)>);
|
2021-10-10 17:26:22 -04:00
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for Stats {
|
2022-07-14 17:52:44 -04:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
2021-10-10 17:26:22 -04:00
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let items: Vec<(String, u32)> = Deserialize::deserialize(deserializer)?;
|
|
|
|
Ok(Stats(items))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Stats {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
Serialize::serialize(&self.0, serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Stats {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
writeln!(f, "Compilation statistics:")?;
|
|
|
|
for (key, value) in self.0.clone() {
|
|
|
|
writeln!(f, " {}: {}", key, value)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 16:45:55 -04:00
|
|
|
/// Represents the "default" type library that should be used when type
|
|
|
|
/// checking the code in the module graph. Note that a user provided config
|
|
|
|
/// of `"lib"` would override this value.
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
|
|
|
|
pub enum TsTypeLib {
|
|
|
|
DenoWindow,
|
|
|
|
DenoWorker,
|
|
|
|
UnstableDenoWindow,
|
|
|
|
UnstableDenoWorker,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TsTypeLib {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::DenoWindow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for TsTypeLib {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
let value = match self {
|
|
|
|
Self::DenoWindow => vec!["deno.window".to_string()],
|
|
|
|
Self::DenoWorker => vec!["deno.worker".to_string()],
|
|
|
|
Self::UnstableDenoWindow => {
|
|
|
|
vec!["deno.window".to_string(), "deno.unstable".to_string()]
|
|
|
|
}
|
|
|
|
Self::UnstableDenoWorker => {
|
|
|
|
vec!["deno.worker".to_string(), "deno.unstable".to_string()]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Serialize::serialize(&value, serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
/// An enum that represents the base tsc configuration to return.
|
2022-06-28 16:45:55 -04:00
|
|
|
pub enum TsConfigType {
|
2021-10-10 17:26:22 -04:00
|
|
|
/// Return a configuration for bundling, using swc to emit the bundle. This is
|
|
|
|
/// independent of type checking.
|
|
|
|
Bundle,
|
2022-07-19 11:58:18 -04:00
|
|
|
/// Return a configuration to use tsc to type check. This
|
|
|
|
/// is independent of either bundling or emitting via swc.
|
2022-07-13 15:38:36 -04:00
|
|
|
Check { lib: TsTypeLib },
|
2021-10-10 17:26:22 -04:00
|
|
|
/// Return a configuration to use swc to emit single module files.
|
|
|
|
Emit,
|
|
|
|
}
|
|
|
|
|
2022-06-28 16:45:55 -04:00
|
|
|
pub struct TsConfigWithIgnoredOptions {
|
|
|
|
pub ts_config: TsConfig,
|
|
|
|
pub maybe_ignored_options: Option<IgnoredCompilerOptions>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For a given configuration type and optionally a configuration file,
|
|
|
|
/// return a `TsConfig` struct and optionally any user configuration
|
|
|
|
/// options that were ignored.
|
|
|
|
pub fn get_ts_config_for_emit(
|
|
|
|
config_type: TsConfigType,
|
2021-10-10 17:26:22 -04:00
|
|
|
maybe_config_file: Option<&ConfigFile>,
|
2022-06-28 16:45:55 -04:00
|
|
|
) -> Result<TsConfigWithIgnoredOptions, AnyError> {
|
2021-10-10 17:26:22 -04:00
|
|
|
let mut ts_config = match config_type {
|
2022-06-28 16:45:55 -04:00
|
|
|
TsConfigType::Bundle => TsConfig::new(json!({
|
2021-10-10 17:26:22 -04:00
|
|
|
"checkJs": false,
|
|
|
|
"emitDecoratorMetadata": false,
|
|
|
|
"importsNotUsedAsValues": "remove",
|
|
|
|
"inlineSourceMap": false,
|
|
|
|
"inlineSources": false,
|
|
|
|
"sourceMap": false,
|
|
|
|
"jsx": "react",
|
|
|
|
"jsxFactory": "React.createElement",
|
|
|
|
"jsxFragmentFactory": "React.Fragment",
|
|
|
|
})),
|
2022-07-13 15:38:36 -04:00
|
|
|
TsConfigType::Check { lib } => TsConfig::new(json!({
|
|
|
|
"allowJs": true,
|
|
|
|
"allowSyntheticDefaultImports": true,
|
|
|
|
"checkJs": false,
|
|
|
|
"emitDecoratorMetadata": false,
|
|
|
|
"experimentalDecorators": true,
|
|
|
|
"incremental": true,
|
|
|
|
"jsx": "react",
|
|
|
|
"importsNotUsedAsValues": "remove",
|
|
|
|
"inlineSourceMap": true,
|
|
|
|
"inlineSources": true,
|
|
|
|
"isolatedModules": true,
|
|
|
|
"lib": lib,
|
|
|
|
"module": "esnext",
|
|
|
|
"moduleDetection": "force",
|
|
|
|
"noEmit": true,
|
|
|
|
"resolveJsonModule": true,
|
|
|
|
"sourceMap": false,
|
|
|
|
"strict": true,
|
|
|
|
"target": "esnext",
|
|
|
|
"tsBuildInfoFile": "deno:///.tsbuildinfo",
|
|
|
|
"useDefineForClassFields": true,
|
|
|
|
// TODO(@kitsonk) remove for Deno 2.0
|
|
|
|
"useUnknownInCatchVariables": false,
|
|
|
|
})),
|
2022-06-28 16:45:55 -04:00
|
|
|
TsConfigType::Emit => TsConfig::new(json!({
|
2021-10-10 17:26:22 -04:00
|
|
|
"checkJs": false,
|
|
|
|
"emitDecoratorMetadata": false,
|
|
|
|
"importsNotUsedAsValues": "remove",
|
|
|
|
"inlineSourceMap": true,
|
|
|
|
"inlineSources": true,
|
|
|
|
"sourceMap": false,
|
|
|
|
"jsx": "react",
|
|
|
|
"jsxFactory": "React.createElement",
|
|
|
|
"jsxFragmentFactory": "React.Fragment",
|
2021-12-15 13:22:36 -05:00
|
|
|
"resolveJsonModule": true,
|
2021-10-10 17:26:22 -04:00
|
|
|
})),
|
|
|
|
};
|
2022-06-28 16:45:55 -04:00
|
|
|
let maybe_ignored_options =
|
|
|
|
ts_config.merge_tsconfig_from_config_file(maybe_config_file)?;
|
|
|
|
Ok(TsConfigWithIgnoredOptions {
|
|
|
|
ts_config,
|
|
|
|
maybe_ignored_options,
|
|
|
|
})
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Transform the graph into root specifiers that we can feed `tsc`. We have to
|
|
|
|
/// provide the media type for root modules because `tsc` does not "resolve" the
|
|
|
|
/// media type like other modules, as well as a root specifier needs any
|
2022-07-14 10:40:47 -04:00
|
|
|
/// redirects resolved. We need to include all the emittable files in
|
|
|
|
/// the roots, so they get type checked and optionally emitted,
|
|
|
|
/// otherwise they would be ignored if only imported into JavaScript.
|
2021-12-16 05:45:41 -05:00
|
|
|
fn get_tsc_roots(
|
|
|
|
graph_data: &GraphData,
|
2021-10-10 17:26:22 -04:00
|
|
|
check_js: bool,
|
|
|
|
) -> Vec<(ModuleSpecifier, MediaType)> {
|
2022-07-14 10:40:47 -04:00
|
|
|
graph_data
|
|
|
|
.entries()
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(specifier, module_entry)| match module_entry {
|
|
|
|
ModuleEntry::Module {
|
|
|
|
media_type,
|
|
|
|
ts_check,
|
|
|
|
..
|
|
|
|
} => match &media_type {
|
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Tsx
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Jsx => Some((specifier.clone(), *media_type)),
|
|
|
|
MediaType::JavaScript | MediaType::Mjs | MediaType::Cjs
|
|
|
|
if check_js || *ts_check =>
|
|
|
|
{
|
2021-12-16 05:45:41 -05:00
|
|
|
Some((specifier.clone(), *media_type))
|
|
|
|
}
|
|
|
|
_ => None,
|
2022-07-14 10:40:47 -04:00
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect()
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A hashing function that takes the source code, version and optionally a
|
|
|
|
/// user provided config and generates a string hash which can be stored to
|
|
|
|
/// determine if the cached emit is valid or not.
|
2022-07-19 11:58:18 -04:00
|
|
|
pub fn get_source_hash(source_text: &str, emit_options_hash: u64) -> u64 {
|
|
|
|
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(
|
|
|
|
cache: &EmitCache,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
parsed_source: &ParsedSource,
|
|
|
|
emit_options: &deno_ast::EmitOptions,
|
|
|
|
emit_config_hash: u64,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let source_hash =
|
|
|
|
get_source_hash(parsed_source.text_info().text_str(), emit_config_hash);
|
|
|
|
|
2022-07-20 16:30:57 -04:00
|
|
|
if let Some(emit_code) = cache.get_emit_code(specifier, Some(source_hash)) {
|
2022-07-19 11:58:18 -04:00
|
|
|
Ok(emit_code)
|
|
|
|
} else {
|
|
|
|
let transpiled_source = parsed_source.transpile(emit_options)?;
|
|
|
|
debug_assert!(transpiled_source.source_map.is_none());
|
|
|
|
cache.set_emit_code(specifier, source_hash, &transpiled_source.text);
|
|
|
|
Ok(transpiled_source.text)
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Options for performing a check of a module graph. Note that the decision to
|
|
|
|
/// emit or not is determined by the `ts_config` settings.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct CheckOptions {
|
2021-11-29 17:23:30 -05:00
|
|
|
/// The check flag from the option which can effect the filtering of
|
|
|
|
/// diagnostics in the emit result.
|
2022-06-27 16:54:09 -04:00
|
|
|
pub type_check_mode: TypeCheckMode,
|
2021-10-10 17:26:22 -04:00
|
|
|
/// Set the debug flag on the TypeScript type checker.
|
|
|
|
pub debug: bool,
|
|
|
|
/// The module specifier to the configuration file, passed to tsc so that
|
|
|
|
/// configuration related diagnostics are properly formed.
|
|
|
|
pub maybe_config_specifier: Option<ModuleSpecifier>,
|
|
|
|
/// The derived tsconfig that should be used when checking.
|
|
|
|
pub ts_config: TsConfig,
|
2021-12-16 05:45:41 -05:00
|
|
|
/// If true, `Check <specifier>` will be written to stdout for each root.
|
|
|
|
pub log_checks: bool,
|
2022-07-12 18:58:39 -04:00
|
|
|
/// If true, valid `.tsbuildinfo` files will be ignored and type checking
|
|
|
|
/// will always occur.
|
2021-11-23 16:20:30 -05:00
|
|
|
pub reload: bool,
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
/// The result of a check of a module graph.
|
2021-10-10 17:26:22 -04:00
|
|
|
#[derive(Debug, Default)]
|
2022-07-12 18:58:39 -04:00
|
|
|
pub struct CheckResult {
|
2021-10-10 17:26:22 -04:00
|
|
|
pub diagnostics: Diagnostics,
|
|
|
|
pub stats: Stats,
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
/// Given a set of roots and graph data, type check the module graph.
|
2021-10-10 17:26:22 -04:00
|
|
|
///
|
|
|
|
/// It is expected that it is determined if a check and/or emit is validated
|
|
|
|
/// before the function is called.
|
2022-07-12 18:58:39 -04:00
|
|
|
pub fn check(
|
2022-01-31 17:33:57 -05:00
|
|
|
roots: &[(ModuleSpecifier, ModuleKind)],
|
2021-12-16 05:45:41 -05:00
|
|
|
graph_data: Arc<RwLock<GraphData>>,
|
2022-07-12 18:58:39 -04:00
|
|
|
cache: &TypeCheckCache,
|
2021-10-10 17:26:22 -04:00
|
|
|
options: CheckOptions,
|
2022-07-12 18:58:39 -04:00
|
|
|
) -> Result<CheckResult, AnyError> {
|
2021-10-10 17:26:22 -04:00
|
|
|
let check_js = options.ts_config.get_check_js();
|
2021-12-16 05:45:41 -05:00
|
|
|
let segment_graph_data = {
|
|
|
|
let graph_data = graph_data.read();
|
|
|
|
graph_data.graph_segment(roots).unwrap()
|
|
|
|
};
|
2022-07-12 18:58:39 -04:00
|
|
|
let check_hash = match get_check_hash(&segment_graph_data, &options) {
|
|
|
|
CheckHashResult::NoFiles => return Ok(Default::default()),
|
|
|
|
CheckHashResult::Hash(hash) => hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
// do not type check if we know this is type checked
|
|
|
|
if !options.reload && cache.has_check_hash(check_hash) {
|
2021-12-16 05:45:41 -05:00
|
|
|
return Ok(Default::default());
|
|
|
|
}
|
2022-07-12 18:58:39 -04:00
|
|
|
|
2022-07-14 10:40:47 -04:00
|
|
|
let root_names = get_tsc_roots(&segment_graph_data, check_js);
|
2021-12-16 05:45:41 -05:00
|
|
|
if options.log_checks {
|
2022-01-31 17:33:57 -05:00
|
|
|
for (root, _) in roots {
|
2021-12-16 05:45:41 -05:00
|
|
|
let root_str = root.to_string();
|
|
|
|
// `$deno` specifiers are internal, don't print them.
|
|
|
|
if !root_str.contains("$deno") {
|
|
|
|
log::info!("{} {}", colors::green("Check"), root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 17:26:22 -04:00
|
|
|
// while there might be multiple roots, we can't "merge" the build info, so we
|
|
|
|
// try to retrieve the build info for first root, which is the most common use
|
|
|
|
// case.
|
2021-11-23 16:20:30 -05:00
|
|
|
let maybe_tsbuildinfo = if options.reload {
|
|
|
|
None
|
|
|
|
} else {
|
2022-06-20 17:59:52 -04:00
|
|
|
cache.get_tsbuildinfo(&roots[0].0)
|
2021-11-23 16:20:30 -05:00
|
|
|
};
|
2021-10-10 17:26:22 -04:00
|
|
|
// to make tsc build info work, we need to consistently hash modules, so that
|
|
|
|
// tsc can better determine if an emit is still valid or not, so we provide
|
|
|
|
// that data here.
|
|
|
|
let hash_data = vec![
|
|
|
|
options.ts_config.as_bytes(),
|
|
|
|
version::deno().as_bytes().to_owned(),
|
|
|
|
];
|
|
|
|
|
|
|
|
let response = tsc::exec(tsc::Request {
|
|
|
|
config: options.ts_config,
|
|
|
|
debug: options.debug,
|
2022-07-12 18:58:39 -04:00
|
|
|
graph_data,
|
2021-10-10 17:26:22 -04:00
|
|
|
hash_data,
|
|
|
|
maybe_config_specifier: options.maybe_config_specifier,
|
|
|
|
maybe_tsbuildinfo,
|
|
|
|
root_names,
|
|
|
|
})?;
|
|
|
|
|
2022-06-27 16:54:09 -04:00
|
|
|
let diagnostics = if options.type_check_mode == TypeCheckMode::Local {
|
2021-11-29 17:23:30 -05:00
|
|
|
response.diagnostics.filter(|d| {
|
|
|
|
if let Some(file_name) = &d.file_name {
|
|
|
|
!file_name.starts_with("http")
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
response.diagnostics
|
|
|
|
};
|
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
if let Some(tsbuildinfo) = response.maybe_tsbuildinfo {
|
|
|
|
cache.set_tsbuildinfo(&roots[0].0, &tsbuildinfo);
|
|
|
|
}
|
2022-06-20 17:59:52 -04:00
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
if diagnostics.is_empty() {
|
|
|
|
cache.add_check_hash(check_hash);
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
Ok(CheckResult {
|
2021-11-29 17:23:30 -05:00
|
|
|
diagnostics,
|
2021-10-10 17:26:22 -04:00
|
|
|
stats: response.stats,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
enum CheckHashResult {
|
|
|
|
Hash(u64),
|
|
|
|
NoFiles,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a hash of the inputs for type checking. This can then
|
|
|
|
/// be used to tell
|
|
|
|
fn get_check_hash(
|
2021-12-16 05:45:41 -05:00
|
|
|
graph_data: &GraphData,
|
2022-07-12 18:58:39 -04:00
|
|
|
options: &CheckOptions,
|
|
|
|
) -> CheckHashResult {
|
|
|
|
// twox hash is insecure, but fast so it works for our purposes
|
|
|
|
use std::hash::Hasher;
|
|
|
|
use twox_hash::XxHash64;
|
|
|
|
|
|
|
|
let mut hasher = XxHash64::default();
|
|
|
|
hasher.write_u8(match options.type_check_mode {
|
|
|
|
TypeCheckMode::All => 0,
|
|
|
|
TypeCheckMode::Local => 1,
|
|
|
|
TypeCheckMode::None => 2,
|
|
|
|
});
|
|
|
|
hasher.write(&options.ts_config.as_bytes());
|
|
|
|
|
|
|
|
let check_js = options.ts_config.get_check_js();
|
|
|
|
let mut sorted_entries = graph_data.entries().collect::<Vec<_>>();
|
|
|
|
sorted_entries.sort_by_key(|(s, _)| s.as_str()); // make it deterministic
|
|
|
|
let mut has_file = false;
|
|
|
|
let mut has_file_to_type_check = false;
|
|
|
|
for (specifier, module_entry) in sorted_entries {
|
2021-12-16 05:45:41 -05:00
|
|
|
if let ModuleEntry::Module {
|
2022-04-18 10:52:26 -04:00
|
|
|
code,
|
|
|
|
media_type,
|
|
|
|
ts_check,
|
|
|
|
..
|
2021-12-16 05:45:41 -05:00
|
|
|
} = module_entry
|
|
|
|
{
|
2022-07-12 18:58:39 -04:00
|
|
|
if *ts_check {
|
|
|
|
has_file_to_type_check = true;
|
|
|
|
}
|
|
|
|
|
2021-12-16 05:45:41 -05:00
|
|
|
match media_type {
|
|
|
|
MediaType::TypeScript
|
2022-07-12 18:58:39 -04:00
|
|
|
| MediaType::Dts
|
|
|
|
| MediaType::Dmts
|
|
|
|
| MediaType::Dcts
|
2021-12-16 05:45:41 -05:00
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
2022-07-12 18:58:39 -04:00
|
|
|
| MediaType::Tsx => {
|
|
|
|
has_file = true;
|
|
|
|
has_file_to_type_check = true;
|
|
|
|
}
|
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Mjs
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Jsx => {
|
|
|
|
has_file = true;
|
2022-04-18 10:52:26 -04:00
|
|
|
if !check_js && !ts_check {
|
2021-12-16 05:45:41 -05:00
|
|
|
continue;
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
2022-06-20 17:59:52 -04:00
|
|
|
MediaType::Json
|
|
|
|
| MediaType::TsBuildInfo
|
|
|
|
| MediaType::SourceMap
|
|
|
|
| MediaType::Wasm
|
|
|
|
| MediaType::Unknown => continue,
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
2022-07-12 18:58:39 -04:00
|
|
|
hasher.write(specifier.as_str().as_bytes());
|
|
|
|
hasher.write(code.as_bytes());
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
2022-07-12 18:58:39 -04:00
|
|
|
|
|
|
|
if !has_file || !check_js && !has_file_to_type_check {
|
|
|
|
// no files to type check
|
|
|
|
CheckHashResult::NoFiles
|
|
|
|
} else {
|
|
|
|
CheckHashResult::Hash(hasher.finish())
|
|
|
|
}
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An adapter struct to make a deno_graph::ModuleGraphError display as expected
|
|
|
|
/// in the Deno CLI.
|
|
|
|
#[derive(Debug)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct GraphError(pub ModuleGraphError);
|
2021-10-10 17:26:22 -04:00
|
|
|
|
|
|
|
impl std::error::Error for GraphError {}
|
|
|
|
|
|
|
|
impl From<ModuleGraphError> for GraphError {
|
|
|
|
fn from(err: ModuleGraphError) -> Self {
|
|
|
|
Self(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for GraphError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match &self.0 {
|
|
|
|
ModuleGraphError::ResolutionError(err) => {
|
|
|
|
if matches!(
|
|
|
|
err,
|
2022-01-31 17:33:57 -05:00
|
|
|
ResolutionError::InvalidDowngrade { .. }
|
|
|
|
| ResolutionError::InvalidLocalImport { .. }
|
2021-10-10 17:26:22 -04:00
|
|
|
) {
|
2021-10-21 10:18:18 -04:00
|
|
|
write!(f, "{}", err.to_string_with_range())
|
2021-10-10 17:26:22 -04:00
|
|
|
} else {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => self.0.fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 11:58:00 -05:00
|
|
|
/// This contains the logic for Deno to rewrite the `import.meta` when bundling.
|
|
|
|
pub struct BundleHook;
|
|
|
|
|
|
|
|
impl Hook for BundleHook {
|
|
|
|
fn get_import_meta_props(
|
|
|
|
&self,
|
|
|
|
span: Span,
|
|
|
|
module_record: &ModuleRecord,
|
|
|
|
) -> Result<Vec<deno_ast::swc::ast::KeyValueProp>, AnyError> {
|
|
|
|
use deno_ast::swc::ast;
|
|
|
|
|
|
|
|
Ok(vec![
|
|
|
|
ast::KeyValueProp {
|
|
|
|
key: ast::PropName::Ident(ast::Ident::new("url".into(), span)),
|
|
|
|
value: Box::new(ast::Expr::Lit(ast::Lit::Str(ast::Str {
|
|
|
|
span,
|
|
|
|
value: module_record.file_name.to_string().into(),
|
2022-04-08 12:31:47 -04:00
|
|
|
raw: None,
|
2022-01-13 11:58:00 -05:00
|
|
|
}))),
|
|
|
|
},
|
|
|
|
ast::KeyValueProp {
|
|
|
|
key: ast::PropName::Ident(ast::Ident::new("main".into(), span)),
|
|
|
|
value: Box::new(if module_record.is_entry {
|
|
|
|
ast::Expr::Member(ast::MemberExpr {
|
|
|
|
span,
|
|
|
|
obj: Box::new(ast::Expr::MetaProp(ast::MetaPropExpr {
|
|
|
|
span,
|
|
|
|
kind: ast::MetaPropKind::ImportMeta,
|
|
|
|
})),
|
|
|
|
prop: ast::MemberProp::Ident(ast::Ident::new("main".into(), span)),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
ast::Expr::Lit(ast::Lit::Bool(ast::Bool { span, value: false }))
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 16:54:09 -04:00
|
|
|
impl From<TsConfig> for deno_ast::EmitOptions {
|
|
|
|
fn from(config: TsConfig) -> Self {
|
|
|
|
let options: EmitConfigOptions = serde_json::from_value(config.0).unwrap();
|
2022-01-13 11:58:00 -05:00
|
|
|
let imports_not_used_as_values =
|
|
|
|
match options.imports_not_used_as_values.as_str() {
|
|
|
|
"preserve" => deno_ast::ImportsNotUsedAsValues::Preserve,
|
|
|
|
"error" => deno_ast::ImportsNotUsedAsValues::Error,
|
|
|
|
_ => deno_ast::ImportsNotUsedAsValues::Remove,
|
|
|
|
};
|
|
|
|
let (transform_jsx, jsx_automatic, jsx_development) =
|
|
|
|
match options.jsx.as_str() {
|
|
|
|
"react" => (true, false, false),
|
|
|
|
"react-jsx" => (true, true, false),
|
|
|
|
"react-jsxdev" => (true, true, true),
|
|
|
|
_ => (false, false, false),
|
|
|
|
};
|
|
|
|
deno_ast::EmitOptions {
|
|
|
|
emit_metadata: options.emit_decorator_metadata,
|
|
|
|
imports_not_used_as_values,
|
|
|
|
inline_source_map: options.inline_source_map,
|
|
|
|
inline_sources: options.inline_sources,
|
|
|
|
source_map: options.source_map,
|
|
|
|
jsx_automatic,
|
|
|
|
jsx_development,
|
|
|
|
jsx_factory: options.jsx_factory,
|
|
|
|
jsx_fragment_factory: options.jsx_fragment_factory,
|
|
|
|
jsx_import_source: options.jsx_import_source,
|
|
|
|
transform_jsx,
|
|
|
|
var_decl_imports: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|