Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ecmsacript): remove custom transform cloned return (#4881)
### Description Part 1 for WEB-1024. PR refactors custom transformer signature to not allow to return cloned instance. Also moves out few non-core transforms into plugins, so next.js turbopack can use it. Next.js changes: vercel/next.js#49560
- Loading branch information
Showing
15 changed files
with
322 additions
and
27 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
37 changes: 37 additions & 0 deletions
37
crates/turbopack-ecmascript-plugins/src/transform/directives/client.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,37 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use swc_core::ecma::{ast::Program, transforms::base::resolver, visit::VisitMutWith}; | ||
use turbo_tasks::primitives::StringVc; | ||
use turbopack_ecmascript::{CustomTransformer, TransformContext}; | ||
|
||
use super::{is_client_module, server_to_client_proxy::create_proxy_module}; | ||
|
||
#[derive(Debug)] | ||
pub struct ClientDirectiveTransformer { | ||
transition_name: StringVc, | ||
} | ||
|
||
impl ClientDirectiveTransformer { | ||
pub fn new(transition_name: &StringVc) -> Self { | ||
Self { | ||
transition_name: transition_name.clone(), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for ClientDirectiveTransformer { | ||
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> { | ||
if is_client_module(program) { | ||
let transition_name = &*self.transition_name.await?; | ||
*program = create_proxy_module(transition_name, &format!("./{}", ctx.file_name_str)); | ||
program.visit_mut_with(&mut resolver( | ||
ctx.unresolved_mark, | ||
ctx.top_level_mark, | ||
false, | ||
)); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
crates/turbopack-ecmascript-plugins/src/transform/directives/mod.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,39 @@ | ||
use swc_core::ecma::ast::{Lit, Program}; | ||
|
||
pub mod client; | ||
pub mod server; | ||
mod server_to_client_proxy; | ||
|
||
macro_rules! has_directive { | ||
($stmts:expr, $name:literal) => { | ||
$stmts | ||
.map(|item| { | ||
if let Lit::Str(str) = item?.as_expr()?.expr.as_lit()? { | ||
Some(str) | ||
} else { | ||
None | ||
} | ||
}) | ||
.take_while(Option::is_some) | ||
.map(Option::unwrap) | ||
.any(|s| &*s.value == $name) | ||
}; | ||
} | ||
|
||
fn is_client_module(program: &Program) -> bool { | ||
match program { | ||
Program::Module(m) => { | ||
has_directive!(m.body.iter().map(|item| item.as_stmt()), "use client") | ||
} | ||
Program::Script(s) => has_directive!(s.body.iter().map(Some), "use client"), | ||
} | ||
} | ||
|
||
fn is_server_module(program: &Program) -> bool { | ||
match program { | ||
Program::Module(m) => { | ||
has_directive!(m.body.iter().map(|item| item.as_stmt()), "use server") | ||
} | ||
Program::Script(s) => has_directive!(s.body.iter().map(Some), "use server"), | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
crates/turbopack-ecmascript-plugins/src/transform/directives/server.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,42 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use swc_core::{ | ||
ecma::ast::{ModuleItem, Program}, | ||
quote, | ||
}; | ||
use turbopack_ecmascript::{CustomTransformer, TransformContext, UnsupportedServerActionIssue}; | ||
|
||
use super::is_server_module; | ||
|
||
#[derive(Debug)] | ||
pub struct ServerDirectiveTransformer; | ||
|
||
impl ServerDirectiveTransformer { | ||
pub fn new() -> Self { | ||
Self | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for ServerDirectiveTransformer { | ||
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> { | ||
if is_server_module(program) { | ||
let stmt = quote!( | ||
"throw new Error('Server actions (\"use server\") are not yet supported in \ | ||
Turbopack');" as Stmt | ||
); | ||
match program { | ||
Program::Module(m) => m.body = vec![ModuleItem::Stmt(stmt)], | ||
Program::Script(s) => s.body = vec![stmt], | ||
} | ||
UnsupportedServerActionIssue { | ||
context: ctx.file_path, | ||
} | ||
.cell() | ||
.as_issue() | ||
.emit(); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
crates/turbopack-ecmascript-plugins/src/transform/directives/server_to_client_proxy.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,51 @@ | ||
use swc_core::{ | ||
common::DUMMY_SP, | ||
ecma::{ | ||
ast::{ | ||
Expr, ExprStmt, Ident, ImportDecl, ImportDefaultSpecifier, ImportSpecifier, | ||
KeyValueProp, Lit, Module, ModuleDecl, ModuleItem, ObjectLit, Program, Prop, PropName, | ||
PropOrSpread, Stmt, Str, | ||
}, | ||
utils::private_ident, | ||
}, | ||
quote, | ||
}; | ||
use turbopack_ecmascript::TURBOPACK_HELPER; | ||
|
||
pub fn create_proxy_module(transition_name: &str, target_import: &str) -> Program { | ||
let ident = private_ident!("createProxy"); | ||
Program::Module(Module { | ||
body: vec![ | ||
ModuleItem::Stmt(Stmt::Expr(ExprStmt { | ||
expr: Box::new(Expr::Lit(Lit::Str(Str { | ||
value: format!("TURBOPACK {{ transition: {transition_name} }}").into(), | ||
raw: None, | ||
span: DUMMY_SP, | ||
}))), | ||
span: DUMMY_SP, | ||
})), | ||
ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl { | ||
specifiers: vec![ImportSpecifier::Default(ImportDefaultSpecifier { | ||
local: ident.clone(), | ||
span: DUMMY_SP, | ||
})], | ||
src: Box::new(target_import.into()), | ||
type_only: false, | ||
asserts: Some(Box::new(ObjectLit { | ||
span: DUMMY_SP, | ||
props: vec![PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { | ||
key: PropName::Ident(Ident::new(TURBOPACK_HELPER.into(), DUMMY_SP)), | ||
value: Box::new(Expr::Lit(true.into())), | ||
})))], | ||
})), | ||
span: DUMMY_SP, | ||
})), | ||
ModuleItem::Stmt(quote!( | ||
"__turbopack_export_namespace__($proxy);" as Stmt, | ||
proxy = ident, | ||
)), | ||
], | ||
shebang: None, | ||
span: DUMMY_SP, | ||
}) | ||
} |
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,2 +1,6 @@ | ||
pub mod directives; | ||
pub mod emotion; | ||
pub mod modularize_imports; | ||
pub mod relay; | ||
pub mod styled_components; | ||
pub mod styled_jsx; |
63 changes: 63 additions & 0 deletions
63
crates/turbopack-ecmascript-plugins/src/transform/modularize_imports.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,63 @@ | ||
use std::collections::HashMap; | ||
|
||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use indexmap::IndexMap; | ||
use modularize_imports::{modularize_imports, Config, PackageConfig}; | ||
use serde::{Deserialize, Serialize}; | ||
use swc_core::{ | ||
common::util::take::Take, | ||
ecma::{ | ||
ast::{Module, Program}, | ||
visit::FoldWith, | ||
}, | ||
}; | ||
use turbo_tasks::trace::TraceRawVcs; | ||
use turbopack_ecmascript::{CustomTransformer, TransformContext}; | ||
|
||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, TraceRawVcs)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct ModularizeImportPackageConfig { | ||
pub transform: String, | ||
#[serde(default)] | ||
pub prevent_full_import: bool, | ||
#[serde(default)] | ||
pub skip_default_conversion: bool, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct ModularizeImportsTransformer { | ||
packages: HashMap<String, PackageConfig>, | ||
} | ||
|
||
impl ModularizeImportsTransformer { | ||
pub fn new(packages: &IndexMap<String, ModularizeImportPackageConfig>) -> Self { | ||
Self { | ||
packages: packages | ||
.iter() | ||
.map(|(k, v)| { | ||
( | ||
k.clone(), | ||
PackageConfig { | ||
transform: v.transform.clone(), | ||
prevent_full_import: v.prevent_full_import, | ||
skip_default_conversion: v.skip_default_conversion, | ||
}, | ||
) | ||
}) | ||
.collect(), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for ModularizeImportsTransformer { | ||
async fn transform(&self, program: &mut Program, _ctx: &TransformContext<'_>) -> Result<()> { | ||
let p = std::mem::replace(program, Program::Module(Module::dummy())); | ||
*program = p.fold_with(&mut modularize_imports(Config { | ||
packages: self.packages.clone(), | ||
})); | ||
|
||
Ok(()) | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
crates/turbopack-ecmascript-plugins/src/transform/styled_components.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,33 @@ | ||
use std::path::PathBuf; | ||
|
||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use swc_core::{ | ||
common::FileName, | ||
ecma::{ast::Program, visit::VisitMutWith}, | ||
}; | ||
use turbopack_ecmascript::{CustomTransformer, TransformContext}; | ||
|
||
#[derive(Debug)] | ||
pub struct StyledComponentsTransformer { | ||
config: styled_components::Config, | ||
} | ||
|
||
impl StyledComponentsTransformer { | ||
pub fn new(config: styled_components::Config) -> Self { | ||
Self { config } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for StyledComponentsTransformer { | ||
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> { | ||
program.visit_mut_with(&mut styled_components::styled_components( | ||
FileName::Real(PathBuf::from(ctx.file_path_str)), | ||
ctx.file_name_hash, | ||
self.config.clone(), | ||
)); | ||
|
||
Ok(()) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
crates/turbopack-ecmascript-plugins/src/transform/styled_jsx.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,33 @@ | ||
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 StyledJsxTransformer; | ||
|
||
impl StyledJsxTransformer { | ||
pub fn new() -> Self { | ||
Self | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for StyledJsxTransformer { | ||
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> { | ||
let p = std::mem::replace(program, Program::Module(Module::dummy())); | ||
*program = p.fold_with(&mut styled_jsx::visitor::styled_jsx( | ||
ctx.source_map.clone(), | ||
// styled_jsx don't really use that in a relevant way | ||
FileName::Anon, | ||
)); | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.