Skip to content

Commit

Permalink
Turbopack + Relay: correctly implement artifact_directory to fix mult…
Browse files Browse the repository at this point in the history
…i-project test (#8655)

Previously, if `artifact_directory` were set, artifacts would be
resolved at a fixed location relative to each file. This correctly
resolves them from the project root.

Test Plan: With vercel/next.js#67403, `TURBOPACK_BUILD=1 TURBOPACK=1
pnpm test-dev
test/integration/relay-graphql-swc-multi-project/test/index.test.js`
  • Loading branch information
wbinnssmith authored Jul 3, 2024
1 parent 2d4b3b8 commit 2503bb6
Showing 1 changed file with 23 additions and 10 deletions.
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

0 comments on commit 2503bb6

Please # to comment.