forked from vercel/turborepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(turbopack-ecmascript): accept transform plugin in moduleoptionco…
…ntext (vercel#4721) ### Description - Relates with WEB-955, WEB-848. This PR is a part of implementing WEB-848, supporting relay graphql transform in next.js. Historically we've been expanding `ModuleOptionContext` to have each own config (`enable_mdx`, `enable_emotion`...) but with new CustomTransformPlugin we can pass actual transforms instead, what WEB-955 aims. PR introduces new config option `custom_ecma_transform_plugins` to the ModuleOptionContext to support it. `custom_ecma_transform_plugins` is a struct have two fields, `before` / `after` which allows to specify order of custom transforms if it should be invoked _before_ any core transform kicks in, or otherwise. This effectively deprecates existing options, notably `custom_ecmascript_transforms` and `custom_ecmascript_app_transforms`. It is currently not actively being used (next-swc have 2 places utilizing `custom_ecmascript_transforms`), we can consolidate to the new option instead. One another notable change is customtransformer's transform is now async fn. It is due to `TransformContext` have couple of `Vc` properties, that cannot be accessed in a sync transform fn. Refer counterpart next.js PR (https://github.com/vercel/next.js/pull/48899/files#diff-c0df3b5a390436575f35365774b078cb5303ef395b064b8992dfdd7dba8f3691) as ref.
- Loading branch information
1 parent
486b954
commit 52dc092
Showing
9 changed files
with
144 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod emotion; | ||
pub mod relay; |
57 changes: 57 additions & 0 deletions
57
crates/turbopack-ecmascript-plugins/src/transform/relay.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::path::PathBuf; | ||
|
||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use swc_core::{ | ||
common::{util::take::Take, FileName}, | ||
ecma::{ | ||
ast::{Module, Program}, | ||
visit::FoldWith, | ||
}, | ||
}; | ||
use turbopack_ecmascript::{CustomTransformer, TransformContext}; | ||
|
||
#[derive(Debug)] | ||
pub struct RelayTransformer { | ||
config: swc_relay::Config, | ||
} | ||
|
||
impl RelayTransformer { | ||
pub fn new(config: swc_relay::Config) -> Self { | ||
Self { config } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for RelayTransformer { | ||
async fn transform( | ||
&self, | ||
program: &mut Program, | ||
ctx: &TransformContext<'_>, | ||
) -> Result<Option<Program>> { | ||
// 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) | ||
} else { | ||
let 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_else(|| &self.config), | ||
FileName::Real(PathBuf::from(ctx.file_name_str)), | ||
root, | ||
// [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, | ||
Some(ctx.unresolved_mark), | ||
)); | ||
|
||
Ok(None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters