Skip to content

Commit

Permalink
feat: add outputFileExtension to specify which output extension to use
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienKode committed Oct 24, 2023
1 parent 12d6b66 commit 9bc221e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
6 changes: 5 additions & 1 deletion packages/relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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(
Expand Down
45 changes: 37 additions & 8 deletions packages/relay/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Self, Self::Error> {
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,
Expand Down Expand Up @@ -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<String> {
Expand Down Expand Up @@ -159,14 +188,14 @@ impl<'a> Relay<'a> {
real_file_name: &Path,
definition_name: &str,
) -> Result<PathBuf, BuildRequirePathError> {
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 {
Expand Down

0 comments on commit 9bc221e

Please sign in to comment.