Skip to content

Commit

Permalink
fix(next-swc/relay): make pages directory optional (#43116)
Browse files Browse the repository at this point in the history
Fixes:
relayjs/relay-examples#241 (comment)

**Context:**
Current relay transformer requires `pages_dir` in its entry point:

https://github.com/vercel/next.js/blob/ecfd2f4cd6a1c3f6bc3cd1cca6b62a3cb705a4e2/packages/next-swc/crates/core/src/relay.rs#L184

But consider the case that `pages_dir` is not provided because the page
system is built entirely in the app directory introduced from Next.js
13.

In this case, transformer causes unconditional panics even though
there's nothing wrong with it.

This PR removes panic in entry point and changes the type of `pages_dir`
into `Option<T>` so that keeps [build stability for existing page
system](https://github.com/vercel/next.js/blob/ecfd2f4cd6a1c3f6bc3cd1cca6b62a3cb705a4e2/packages/next-swc/crates/core/src/relay.rs#L157-L160)
even if `pages_dir` is not provided.
  • Loading branch information
orionmiz committed Nov 20, 2022
1 parent e3387db commit 6aaa9fc
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/next-swc/crates/core/src/relay.rs
Expand Up @@ -27,7 +27,7 @@ impl Default for RelayLanguageConfig {

struct Relay<'a> {
root_dir: PathBuf,
pages_dir: PathBuf,
pages_dir: Option<PathBuf>,
file_name: FileName,
config: &'a Config,
}
Expand Down Expand Up @@ -107,7 +107,11 @@ impl<'a> Relay<'a> {

if let Some(artifact_directory) = &self.config.artifact_directory {
Ok(self.root_dir.join(artifact_directory).join(filename))
} else if real_file_name.starts_with(&self.pages_dir) {
} else if self
.pages_dir
.as_ref()
.map_or(false, |pages_dir| real_file_name.starts_with(pages_dir))
{
Err(BuildRequirePathError::ArtifactDirectoryExpected {
file_name: real_file_name.display().to_string(),
})
Expand Down Expand Up @@ -181,7 +185,7 @@ pub fn relay(config: &Config, file_name: FileName, pages_dir: Option<PathBuf>) -
Relay {
root_dir: std::env::current_dir().unwrap(),
file_name,
pages_dir: pages_dir.unwrap_or_else(|| panic!("pages_dir is expected.")),
pages_dir,
config,
}
}

0 comments on commit 6aaa9fc

Please sign in to comment.