From 9bc221ee9153a40e43bef5a8e380235e67a4ead9 Mon Sep 17 00:00:00 2001 From: Julien Karst Date: Tue, 24 Oct 2023 21:08:02 +0200 Subject: [PATCH] feat: add outputFileExtension to specify which output extension to use --- packages/relay/src/lib.rs | 6 +++- packages/relay/transform/src/lib.rs | 45 ++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/packages/relay/src/lib.rs b/packages/relay/src/lib.rs index c7c296b27..7af81836c 100644 --- a/packages/relay/src/lib.rs +++ b/packages/relay/src/lib.rs @@ -6,7 +6,7 @@ use swc_core::{ ecma::{ast::Program, visit::FoldWith}, plugin::{plugin_transform, proxies::TransformPluginProgramMetadata}, }; -use swc_relay::{relay, Config, RelayLanguageConfig}; +use swc_relay::{relay, Config, RelayLanguageConfig, OutputFileExtension}; #[plugin_transform] fn relay_plugin_transform(program: Program, metadata: TransformPluginProgramMetadata) -> Program { @@ -40,6 +40,9 @@ fn relay_plugin_transform(program: Program, metadata: TransformPluginProgramMeta let language = plugin_config["language"] .as_str() .map_or(RelayLanguageConfig::TypeScript, |v| v.try_into().unwrap()); + let output_file_extension = plugin_config["outputFileExtension"] + .as_str() + .map_or(OutputFileExtension::Undefined, |v| v.try_into().unwrap()); let eager_es_modules = plugin_config["eagerEsModules"] .as_bool() .unwrap_or_default(); @@ -48,6 +51,7 @@ fn relay_plugin_transform(program: Program, metadata: TransformPluginProgramMeta artifact_directory, language, eager_es_modules, + output_file_extension }; let mut relay = relay( diff --git a/packages/relay/transform/src/lib.rs b/packages/relay/transform/src/lib.rs index c697a0cba..200a85e84 100644 --- a/packages/relay/transform/src/lib.rs +++ b/packages/relay/transform/src/lib.rs @@ -21,6 +21,14 @@ pub enum RelayLanguageConfig { Flow, } +#[derive(Copy, Clone, Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum OutputFileExtension { + TypeScript, + JavaScript, + Undefined +} + impl<'a> TryFrom<&'a str> for RelayLanguageConfig { type Error = String; @@ -33,12 +41,31 @@ impl<'a> TryFrom<&'a str> for RelayLanguageConfig { } } + +impl<'a> TryFrom<&'a str> for OutputFileExtension { + type Error = String; + + fn try_from(value: &'a str) -> Result { + match value { + "ts" => Ok(Self::TypeScript), + "js" => Ok(Self::JavaScript), + _ => Err(format!("Unexpected output file extension value '{}'", value)), + } + } +} + impl Default for RelayLanguageConfig { fn default() -> Self { Self::Flow } } +impl Default for OutputFileExtension { + fn default() -> Self { + Self::Undefined + } +} + #[derive(Debug, Clone)] struct RelayImport { path: JsWord, @@ -81,6 +108,8 @@ pub struct Config { pub language: RelayLanguageConfig, #[serde(default)] pub eager_es_modules: bool, + #[serde(default)] + pub output_file_extension: OutputFileExtension, } fn pull_first_operation_name_from_tpl(tpl: &TaggedTpl) -> Option { @@ -159,14 +188,14 @@ impl<'a> Relay<'a> { real_file_name: &Path, definition_name: &str, ) -> Result { - let filename = match &self.config.language { - RelayLanguageConfig::Flow => format!("{}.graphql.js", definition_name), - RelayLanguageConfig::TypeScript => { - format!("{}.graphql", definition_name) - } - RelayLanguageConfig::JavaScript => { - format!("{}.graphql.js", definition_name) - } + let filename = match &self.config.output_file_extension { + OutputFileExtension::JavaScript => format!("{}.graphql.js", definition_name), + OutputFileExtension::TypeScript => format!("{}.graphql.ts", definition_name), + OutputFileExtension::Undefined => match &self.config.language { + RelayLanguageConfig::Flow => format!("{}.graphql.js", definition_name), + RelayLanguageConfig::TypeScript => format!("{}.graphql.ts", definition_name), + RelayLanguageConfig::JavaScript => format!("{}.graphql.js", definition_name), + }, }; if let Some(artifact_directory) = &self.config.artifact_directory {