summaryrefslogtreecommitdiff
path: root/cli/args/config_file.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/args/config_file.rs')
-rw-r--r--cli/args/config_file.rs151
1 files changed, 151 insertions, 0 deletions
diff --git a/cli/args/config_file.rs b/cli/args/config_file.rs
index cf88e3999..435e0d715 100644
--- a/cli/args/config_file.rs
+++ b/cli/args/config_file.rs
@@ -762,6 +762,157 @@ impl ConfigFile {
}
}
+/// 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)
+ }
+}
+
+/// An enum that represents the base tsc configuration to return.
+pub enum TsConfigType {
+ /// Return a configuration for bundling, using swc to emit the bundle. This is
+ /// independent of type checking.
+ Bundle,
+ /// Return a configuration to use tsc to type check. This
+ /// is independent of either bundling or emitting via swc.
+ Check { lib: TsTypeLib },
+ /// Return a configuration to use swc to emit single module files.
+ Emit,
+}
+
+pub struct TsConfigForEmit {
+ 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,
+ maybe_config_file: Option<&ConfigFile>,
+) -> Result<TsConfigForEmit, AnyError> {
+ let mut ts_config = match config_type {
+ TsConfigType::Bundle => TsConfig::new(json!({
+ "checkJs": false,
+ "emitDecoratorMetadata": false,
+ "importsNotUsedAsValues": "remove",
+ "inlineSourceMap": false,
+ "inlineSources": false,
+ "sourceMap": false,
+ "jsx": "react",
+ "jsxFactory": "React.createElement",
+ "jsxFragmentFactory": "React.Fragment",
+ })),
+ 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,
+ })),
+ TsConfigType::Emit => TsConfig::new(json!({
+ "checkJs": false,
+ "emitDecoratorMetadata": false,
+ "importsNotUsedAsValues": "remove",
+ "inlineSourceMap": true,
+ "inlineSources": true,
+ "sourceMap": false,
+ "jsx": "react",
+ "jsxFactory": "React.createElement",
+ "jsxFragmentFactory": "React.Fragment",
+ "resolveJsonModule": true,
+ })),
+ };
+ let maybe_ignored_options =
+ ts_config.merge_tsconfig_from_config_file(maybe_config_file)?;
+ Ok(TsConfigForEmit {
+ ts_config,
+ maybe_ignored_options,
+ })
+}
+
+impl From<TsConfig> for deno_ast::EmitOptions {
+ fn from(config: TsConfig) -> Self {
+ let options: EmitConfigOptions = serde_json::from_value(config.0).unwrap();
+ 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,
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;