Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Typescript in Rust Relay compiler #3182

Closed
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use graphql_text_printer::{
};
use graphql_transforms::{RefetchableDerivedFromMetadata, SplitOperationMetaData, MATCH_CONSTANTS};
use interner::StringKey;
use relay_typegen::TypegenLanguage;
use std::path::PathBuf;
use std::sync::Arc;

Expand Down Expand Up @@ -60,7 +61,7 @@ pub fn generate_artifacts(

artifacts.push(Artifact {
source_definition_names: metadata.parent_sources.into_iter().collect(),
path: path_for_js_artifact(
path: path_for_artifact(
project_config,
source_file,
normalization_operation.name.item,
Expand Down Expand Up @@ -175,7 +176,7 @@ fn generate_normalization_artifact<'a>(
.expect("a type fragment should be generated for this operation");
Ok(Artifact {
source_definition_names: vec![source_definition_name],
path: path_for_js_artifact(project_config, source_file, name),
path: path_for_artifact(project_config, source_file, name),
content: ArtifactContent::Operation {
normalization_operation: Arc::clone(normalization_operation),
reader_operation: Arc::clone(reader_operation),
Expand All @@ -201,7 +202,7 @@ fn generate_reader_artifact(
.expect("a type fragment should be generated for this fragment");
Artifact {
source_definition_names: vec![name],
path: path_for_js_artifact(
path: path_for_artifact(
project_config,
reader_fragment.name.location.source_location(),
name,
Expand Down Expand Up @@ -254,15 +255,18 @@ pub fn create_path_for_artifact(
}
}

fn path_for_js_artifact(
fn path_for_artifact(
project_config: &ProjectConfig,
source_file: SourceLocationKey,
definition_name: StringKey,
) -> PathBuf {
create_path_for_artifact(
project_config,
source_file,
format!("{}.graphql.js", definition_name),
match &project_config.typegen_config.language {
TypegenLanguage::Flow => format!("{}.graphql.js", definition_name),
TypegenLanguage::Typescript => format!("{}.graphql.ts", definition_name),
},
false,
)
}
44 changes: 33 additions & 11 deletions compiler/crates/relay-compiler/src/watchman/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,44 @@
* LICENSE file in the root directory of this source tree.
*/

use crate::compiler_state::SourceSet;
use crate::config::{Config, SchemaLocation};
use relay_typegen::TypegenLanguage;
use std::path::PathBuf;
use watchman_client::prelude::*;

pub fn get_watchman_expr(config: &Config) -> Expr {
let mut sources_conditions = vec![
// ending in *.js
Expr::Suffix(vec!["js".into()]),
// in one of the source roots
expr_any(
get_source_roots(&config)
.into_iter()
.map(|path| Expr::DirName(DirNameTerm { path, depth: None }))
.collect(),
),
];
let mut sources_conditions = vec![expr_any(
config
.sources
.iter()
.flat_map(|(path, name)| match name {
SourceSet::SourceSetName(name) => {
std::iter::once((path, config.projects.get(&name))).collect::<Vec<_>>()
}
SourceSet::SourceSetNames(names) => names
.iter()
.map(|name| (path, config.projects.get(name)))
.collect::<Vec<_>>(),
})
.filter_map(|(path, project)| match project {
Some(p) if p.enabled => Some(Expr::All(vec![
// Ending in *.js or *.ts depending on the project lanague.
Expr::Suffix(vec![match &p.typegen_config.language {
TypegenLanguage::Flow => "js",
TypegenLanguage::Typescript => "ts",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with react, this has to be tsx. So could this be an array perhaps?

Copy link
Contributor Author

@MaartenStaa MaartenStaa Sep 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I adjusted it! I set it to look at .jsx as well, just in case people use that.

Edit: I committed this on my phone and see that it doesn't compile. I'll have to take a look at that later. I'm on vacation now so a bit limited.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maraisr Fixed the build now as well.

}
.into()]),
// In the related source root.
Expr::DirName(DirNameTerm {
path: path.clone(),
depth: None,
}),
])),
_ => None,
})
.collect(),
)];
// not excluded by any glob
if !config.excludes.is_empty() {
sources_conditions.push(Expr::Not(Box::new(expr_any(
Expand Down
17 changes: 17 additions & 0 deletions compiler/crates/relay-typegen/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@ use fnv::FnvHashMap;
use interner::StringKey;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub enum TypegenLanguage {
Flow,
Typescript,
}

impl Default for TypegenLanguage {
fn default() -> Self {
Self::Flow
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was even thinking of making Typescript the default, but just realized *.ts files wouldn't work with vanilla JS anymore. The Flow generated *.js files using Flow comment style syntax allows people to just treat them as pure JS files.

Maybe we could even make this optional and skip generating types altogether to make the files a bit smaller.

}
}

#[derive(Debug, Deserialize, Default)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct TypegenConfig {
/// The desired output language, "flow" or "typescript".
#[serde(default)]
pub language: TypegenLanguage,

/// # For Flow type generation
/// When set, enum values are imported from a module with this suffix.
/// For example, an enum Foo and this property set to ".test" would be
Expand Down
Loading