diff --git a/crates/turbopack-ecmascript-plugins/src/transform/relay.rs b/crates/turbopack-ecmascript-plugins/src/transform/relay.rs index d8dae3fb3eff6..83f59a10c5cf0 100644 --- a/crates/turbopack-ecmascript-plugins/src/transform/relay.rs +++ b/crates/turbopack-ecmascript-plugins/src/transform/relay.rs @@ -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::{ @@ -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)] @@ -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( @@ -49,7 +51,11 @@ impl RelayTransformer { ), ..Default::default() }; - Self { config: options } + + Self { + config: options, + project_path: project_path.clone(), + } } } @@ -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,