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

Turbopack + Relay: correctly implement artifact_directory to fix multi-project test #8655

Merged
merged 4 commits into from
Jul 3, 2024
Merged
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
33 changes: 23 additions & 10 deletions crates/turbopack-ecmascript-plugins/src/transform/relay.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

use anyhow::Result;
use anyhow::{Context, Result};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use swc_core::{
Expand All @@ -12,6 +12,7 @@ use swc_core::{
};
use swc_relay::RelayLanguageConfig;
use turbo_tasks::trace::TraceRawVcs;
use turbo_tasks_fs::FileSystemPath;
use turbopack_ecmascript::{CustomTransformer, TransformContext};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
Expand All @@ -33,10 +34,11 @@ pub enum RelayLanguage {
#[derive(Debug)]
pub struct RelayTransformer {
config: swc_relay::Config,
project_path: FileSystemPath,
}

impl RelayTransformer {
pub fn new(config: &RelayConfig) -> Self {
pub fn new(config: &RelayConfig, project_path: &FileSystemPath) -> Self {
let options = swc_relay::Config {
artifact_directory: config.artifact_directory.as_ref().map(PathBuf::from),
language: config.language.as_ref().map_or(
Expand All @@ -49,7 +51,11 @@ impl RelayTransformer {
),
..Default::default()
};
Self { config: options }

Self {
config: options,
project_path: project_path.clone(),
}
}
}

Expand All @@ -59,21 +65,28 @@ impl CustomTransformer for RelayTransformer {
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
// If user supplied artifact_directory, it should be resolvable already.
// Otherwise, supply default relative path (./__generated__)
let (root, config) = if self.config.artifact_directory.is_some() {
(PathBuf::new(), None)
let path_to_proj = PathBuf::from(
ctx.file_path
.parent()
.await?
.get_relative_path_to(&self.project_path)
.context("Expected relative path to relay artifact")?,
);

let config = if self.config.artifact_directory.is_some() {
self.config.clone()
} else {
let config = swc_relay::Config {
swc_relay::Config {
artifact_directory: Some(PathBuf::from("__generated__")),
..self.config
};
(PathBuf::from("."), Some(config))
}
};

let p = std::mem::replace(program, Program::Module(Module::dummy()));
*program = p.fold_with(&mut swc_relay::relay(
config.as_ref().unwrap_or(&self.config),
&config,
FileName::Real(PathBuf::from(ctx.file_name_str)),
root,
path_to_proj,
// [TODO]: pages_dir comes through next-swc-loader
// https://github.com/vercel/next.js/blob/ea472e8058faea8ebdab2ef6d3aab257a1f0d11c/packages/next/src/build/webpack-config.ts#L792
None,
Expand Down
Loading