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

feat(relay): Support multi-project mode #318

Merged
merged 11 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @swc/plugin-relay

## 2.0.9

### Patch Changes

- be5f997: Support multi-project mode

## 2.0.8

### Patch Changes
Expand Down
22 changes: 22 additions & 0 deletions packages/relay/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ module.exports = {
eagerEsModules: true,
},
],
// Or if you want to use multiple projects
[
"@swc/plugin-relay",
{
projects: [
{
rootDir: path.resolve(__dirname, '../project1'),
},
{
rootDir: path.resolve(__dirname, '../project2'),
}
],
language: "typescript",
eagerEsModules: true,
},
],
],
},
parser: {
Expand Down Expand Up @@ -88,6 +104,12 @@ In this example typescript graphql files will output transpiled import path of `

# @swc/plugin-relay

## 2.0.9

### Patch Changes

- be5f997: Support multi-project mode

## 2.0.8

### Patch Changes
Expand Down
16 changes: 16 additions & 0 deletions packages/relay/README.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ module.exports = {
eagerEsModules: true,
},
],
// Or if you want to use multiple projects
[
"@swc/plugin-relay",
{
projects: [
{
rootDir: path.resolve(__dirname, '../project1'),
},
{
rootDir: path.resolve(__dirname, '../project2'),
}
],
language: "typescript",
eagerEsModules: true,
},
],
],
},
parser: {
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-relay",
"version": "2.0.8",
"version": "2.0.9",
"description": "SWC plugin for relay",
"main": "swc_plugin_relay.wasm",
"types": "./types.d.ts",
Expand Down
8 changes: 6 additions & 2 deletions 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, OutputFileExtension, RelayLanguageConfig};
use swc_relay::{relay, Config, OutputFileExtension, ProjectConfig, RelayLanguageConfig};

#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
Expand All @@ -16,6 +16,9 @@ struct WasmConfig {
#[serde(default)]
artifact_directory: Option<PathBuf>,

#[serde(default)]
projects: Vec<ProjectConfig>,

#[serde(default)]
language: RelayLanguageConfig,

Expand Down Expand Up @@ -50,14 +53,15 @@ fn relay_plugin_transform(program: Program, metadata: TransformPluginProgramMeta
let root_dir = plugin_config.root_dir;

let config = Config {
projects: plugin_config.projects,
artifact_directory: plugin_config.artifact_directory,
language: plugin_config.language,
eager_es_modules: plugin_config.eager_es_modules,
output_file_extension: plugin_config.output_file_extension,
};

let mut relay = relay(
&config,
config,
filename,
root_dir,
None,
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ homepage = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
rust-version = { workspace = true }
version = "0.44.16"
version = "0.44.17"


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
39 changes: 33 additions & 6 deletions packages/relay/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,41 @@ impl RelayImport {
}
}

struct Relay<'a> {
struct Relay {
root_dir: PathBuf,
pages_dir: Option<PathBuf>,
file_name: FileName,
config: &'a Config,
config: Config,
imports: Vec<RelayImport>,
unresolved_mark: Option<Mark>,
}

#[derive(Deserialize, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Config {
#[serde(default)]
pub projects: Vec<ProjectConfig>,

#[serde(default)]
pub artifact_directory: Option<PathBuf>,

#[serde(default)]
pub language: RelayLanguageConfig,

#[serde(default)]
pub eager_es_modules: bool,
#[serde(default)]
pub output_file_extension: OutputFileExtension,
}

#[derive(Deserialize, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ProjectConfig {
pub root_dir: PathBuf,
#[serde(default)]
pub artifact_directory: Option<PathBuf>,
}

fn pull_first_operation_name_from_tpl(tpl: &TaggedTpl) -> Option<String> {
tpl.tpl.quasis.iter().find_map(|quasis| {
static OPERATION_REGEX: Lazy<Regex> =
Expand Down Expand Up @@ -148,7 +162,7 @@ fn build_require_expr_from_path(path: &str, mark: Option<Mark>) -> Expr {
})
}

impl<'a> Fold for Relay<'a> {
impl Fold for Relay {
fn fold_expr(&mut self, expr: Expr) -> Expr {
let expr = expr.fold_children_with(self);

Expand Down Expand Up @@ -190,7 +204,7 @@ enum BuildRequirePathError {
ArtifactDirectoryExpected { file_name: String },
}

impl<'a> Relay<'a> {
impl Relay {
fn path_for_artifact(
&self,
real_file_name: &Path,
Expand All @@ -206,6 +220,19 @@ impl<'a> Relay<'a> {
},
};

if !self.config.projects.is_empty() {
for project in &self.config.projects {
if real_file_name.starts_with(&project.root_dir) {
return Ok(project
.artifact_directory
.as_deref()
.unwrap_or_else(|| &self.root_dir)
.join("__generated__")
.join(filename));
}
}
}

if let Some(artifact_directory) = &self.config.artifact_directory {
Ok(self.root_dir.join(artifact_directory).join(filename))
} else if self
Expand Down Expand Up @@ -303,12 +330,12 @@ impl<'a> Relay<'a> {
}

pub fn relay(
config: &Config,
config: Config,
file_name: FileName,
root_dir: PathBuf,
pages_dir: Option<PathBuf>,
unresolved_mark: Option<Mark>,
) -> impl Fold + '_ {
) -> impl Fold {
Relay {
root_dir,
file_name,
Expand Down
63 changes: 57 additions & 6 deletions packages/relay/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use swc_common::FileName;
use swc_ecma_transforms_testing::test_fixture;
use swc_relay::{relay, Config, OutputFileExtension, RelayLanguageConfig};
use swc_relay::{relay, Config, OutputFileExtension, ProjectConfig, RelayLanguageConfig};

#[testing::fixture("tests/fixture/simple/**/input.js")]
fn fixture(input: PathBuf) {
Expand All @@ -12,7 +12,8 @@ fn fixture(input: PathBuf) {
Default::default(),
&|_| {
relay(
&Config {
Config {
projects: Default::default(),
artifact_directory: None,
language: RelayLanguageConfig::TypeScript,
eager_es_modules: false,
Expand All @@ -38,7 +39,8 @@ fn fixture_es_modules(input: PathBuf) {
Default::default(),
&|_| {
relay(
&Config {
Config {
projects: Default::default(),
artifact_directory: None,
language: RelayLanguageConfig::TypeScript,
eager_es_modules: true,
Expand All @@ -64,7 +66,8 @@ fn fixture_output_file_extension_javascript(input: PathBuf) {
Default::default(),
&|_| {
relay(
&Config {
Config {
projects: Default::default(),
artifact_directory: None,
language: RelayLanguageConfig::TypeScript,
eager_es_modules: true,
Expand All @@ -90,7 +93,8 @@ fn fixture_output_file_extension_typescript(input: PathBuf) {
Default::default(),
&|_| {
relay(
&Config {
Config {
projects: Default::default(),
artifact_directory: None,
language: RelayLanguageConfig::JavaScript,
eager_es_modules: true,
Expand All @@ -107,3 +111,50 @@ fn fixture_output_file_extension_typescript(input: PathBuf) {
Default::default(),
);
}

#[testing::fixture("tests/fixture/multi-projects/**/input.js")]
fn fixture_multi_projects(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");

test_fixture(
Default::default(),
&|_| {
relay(
Config {
projects: vec![
ProjectConfig {
root_dir: Path::new(".")
.canonicalize()
.unwrap()
.join("tests/fixture/multi-projects/project1"),
artifact_directory: Some(
Path::new(".")
.canonicalize()
.unwrap()
.join("tests/projects1"),
),
},
ProjectConfig {
root_dir: Path::new(".")
.canonicalize()
.unwrap()
.join("tests/fixture/multi-projects/project2"),
..Default::default()
},
],
artifact_directory: None,
language: RelayLanguageConfig::JavaScript,
eager_es_modules: true,
output_file_extension: OutputFileExtension::TypeScript,
},
FileName::Real(input.clone()),
Default::default(),
None,
None,
)
},
&input,
&output,
Default::default(),
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
graphql`
fragment Foo on Bar {
id
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import __Foo from "$DIR/tests/projects1/__generated__/Foo.graphql.ts";
__Foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
graphql`
fragment Foo on Bar {
id
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import __Foo from "__generated__/Foo.graphql.ts";
__Foo;
Loading