-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transforms): turbopack ecma plugin for next.js transforms
- Loading branch information
Showing
13 changed files
with
331 additions
and
20 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
packages/next-swc/crates/next-core/src/next_shared/transforms/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
39 changes: 39 additions & 0 deletions
39
packages/next-swc/crates/next-core/src/next_shared/transforms/next_amp_attributes.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 anyhow::Result; | ||
use async_trait::async_trait; | ||
use next_custom_transforms::transforms::amp_attributes::amp_attributes; | ||
use turbo_tasks::Vc; | ||
use turbopack_binding::{ | ||
swc::core::{ | ||
common::util::take::Take, | ||
ecma::{ast::*, visit::FoldWith}, | ||
}, | ||
turbopack::{ | ||
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext}, | ||
turbopack::module_options::{ModuleRule, ModuleRuleEffect}, | ||
}, | ||
}; | ||
|
||
use super::module_rule_match_js_no_url; | ||
|
||
pub fn get_next_amp_attr_rule(enable_mdx_rs: bool) -> ModuleRule { | ||
let transformer = | ||
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextAmpAttributes {}) as _)); | ||
ModuleRule::new( | ||
module_rule_match_js_no_url(enable_mdx_rs), | ||
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![ | ||
transformer, | ||
]))], | ||
) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct NextAmpAttributes {} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for NextAmpAttributes { | ||
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 amp_attributes()); | ||
Ok(()) | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/next-swc/crates/next-core/src/next_shared/transforms/next_cjs_optimizer.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,45 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use next_custom_transforms::transforms::cjs_optimizer::{cjs_optimizer, Config}; | ||
use turbo_tasks::Vc; | ||
use turbopack_binding::{ | ||
swc::core::{ | ||
common::SyntaxContext, | ||
ecma::{ast::*, visit::VisitMutWith}, | ||
}, | ||
turbopack::{ | ||
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext}, | ||
turbopack::module_options::{ModuleRule, ModuleRuleEffect}, | ||
}, | ||
}; | ||
|
||
use super::module_rule_match_js_no_url; | ||
|
||
pub fn get_next_cjs_optimizer_rule(enable_mdx_rs: bool, config: Config) -> ModuleRule { | ||
let transformer = | ||
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextCjsOptimizer { config }) as _)); | ||
ModuleRule::new( | ||
module_rule_match_js_no_url(enable_mdx_rs), | ||
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![ | ||
transformer, | ||
]))], | ||
) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct NextCjsOptimizer { | ||
config: Config, | ||
} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for NextCjsOptimizer { | ||
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> { | ||
let mut visitor = cjs_optimizer( | ||
self.config.clone(), | ||
SyntaxContext::empty().apply_mark(ctx.unresolved_mark), | ||
); | ||
|
||
program.visit_mut_with(&mut visitor); | ||
Ok(()) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...xt-swc/crates/next-core/src/next_shared/transforms/next_disallow_re_export_all_in_page.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,46 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use next_custom_transforms::transforms::disallow_re_export_all_in_page::disallow_re_export_all_in_page; | ||
use turbo_tasks::Vc; | ||
use turbopack_binding::{ | ||
swc::core::{ | ||
common::util::take::Take, | ||
ecma::{ast::*, visit::FoldWith}, | ||
}, | ||
turbopack::{ | ||
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext}, | ||
turbopack::module_options::{ModuleRule, ModuleRuleEffect}, | ||
}, | ||
}; | ||
|
||
use super::module_rule_match_js_no_url; | ||
|
||
pub fn get_next_disallow_export_all_in_page_rule( | ||
enable_mdx_rs: bool, | ||
is_page_file: bool, | ||
) -> ModuleRule { | ||
let transformer = | ||
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextDisallowReExportAllInPage { | ||
is_page_file, | ||
}) as _)); | ||
ModuleRule::new( | ||
module_rule_match_js_no_url(enable_mdx_rs), | ||
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![ | ||
transformer, | ||
]))], | ||
) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct NextDisallowReExportAllInPage { | ||
is_page_file: bool, | ||
} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for NextDisallowReExportAllInPage { | ||
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 disallow_re_export_all_in_page(self.is_page_file)); | ||
Ok(()) | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/next-swc/crates/next-core/src/next_shared/transforms/next_optimize_server_react.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,49 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use next_custom_transforms::transforms::optimize_server_react::{optimize_server_react, Config}; | ||
use turbo_tasks::Vc; | ||
use turbopack_binding::{ | ||
swc::core::{ | ||
common::util::take::Take, | ||
ecma::{ast::*, visit::FoldWith}, | ||
}, | ||
turbopack::{ | ||
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext}, | ||
turbopack::module_options::{ModuleRule, ModuleRuleEffect}, | ||
}, | ||
}; | ||
|
||
use super::module_rule_match_js_no_url; | ||
|
||
pub fn get_next_optimize_server_react_rule( | ||
enable_mdx_rs: bool, | ||
optimize_use_state: bool, | ||
) -> ModuleRule { | ||
let transformer = | ||
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextOptimizeServerReact { | ||
optimize_use_state, | ||
}) as _)); | ||
ModuleRule::new( | ||
module_rule_match_js_no_url(enable_mdx_rs), | ||
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![ | ||
transformer, | ||
]))], | ||
) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct NextOptimizeServerReact { | ||
optimize_use_state: bool, | ||
} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for NextOptimizeServerReact { | ||
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 optimize_server_react(Config { | ||
optimize_use_state: self.optimize_use_state, | ||
})); | ||
Ok(()) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/next-swc/crates/next-core/src/next_shared/transforms/next_page_config.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,46 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use next_custom_transforms::transforms::page_config::page_config; | ||
use turbo_tasks::Vc; | ||
use turbopack_binding::{ | ||
swc::core::{ | ||
common::util::take::Take, | ||
ecma::{ast::*, visit::FoldWith}, | ||
}, | ||
turbopack::{ | ||
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext}, | ||
turbopack::module_options::{ModuleRule, ModuleRuleEffect}, | ||
}, | ||
}; | ||
|
||
use super::module_rule_match_js_no_url; | ||
|
||
pub fn get_next_page_config_rule(enable_mdx_rs: bool, is_page_file: bool) -> ModuleRule { | ||
let transformer = EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextPageConfig { | ||
// [TODO]: update once turbopack build works | ||
is_development: true, | ||
is_page_file, | ||
}) as _)); | ||
ModuleRule::new( | ||
module_rule_match_js_no_url(enable_mdx_rs), | ||
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![ | ||
transformer, | ||
]))], | ||
) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct NextPageConfig { | ||
is_development: bool, | ||
is_page_file: bool, | ||
} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for NextPageConfig { | ||
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 page_config(self.is_development, self.is_page_file)); | ||
Ok(()) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/next-swc/crates/next-core/src/next_shared/transforms/next_pure.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,34 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use next_custom_transforms::transforms::pure::pure_magic; | ||
use turbo_tasks::Vc; | ||
use turbopack_binding::{ | ||
swc::core::ecma::{ast::*, visit::VisitMutWith}, | ||
turbopack::{ | ||
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext}, | ||
turbopack::module_options::{ModuleRule, ModuleRuleEffect}, | ||
}, | ||
}; | ||
|
||
use super::module_rule_match_js_no_url; | ||
|
||
pub fn get_next_pure_rule(enable_mdx_rs: bool) -> ModuleRule { | ||
let transformer = EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextPure {}) as _)); | ||
ModuleRule::new( | ||
module_rule_match_js_no_url(enable_mdx_rs), | ||
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![ | ||
transformer, | ||
]))], | ||
) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct NextPure {} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for NextPure { | ||
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> { | ||
program.visit_mut_with(&mut pure_magic(ctx.comments.clone())); | ||
Ok(()) | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/next-swc/crates/next-core/src/next_shared/transforms/next_shake_exports.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,44 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use next_custom_transforms::transforms::shake_exports::{shake_exports, Config}; | ||
use turbo_tasks::Vc; | ||
use turbopack_binding::{ | ||
swc::core::{ | ||
common::util::take::Take, | ||
ecma::{ast::*, visit::FoldWith}, | ||
}, | ||
turbopack::{ | ||
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext}, | ||
turbopack::module_options::{ModuleRule, ModuleRuleEffect}, | ||
}, | ||
}; | ||
|
||
use super::module_rule_match_js_no_url; | ||
|
||
pub fn get_next_shake_exports_rule(enable_mdx_rs: bool, ignore: Vec<String>) -> ModuleRule { | ||
let transformer = | ||
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextShakeExports { ignore }) as _)); | ||
ModuleRule::new( | ||
module_rule_match_js_no_url(enable_mdx_rs), | ||
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![ | ||
transformer, | ||
]))], | ||
) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct NextShakeExports { | ||
ignore: Vec<String>, | ||
} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for NextShakeExports { | ||
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 shake_exports(Config { | ||
ignore: self.ignore.iter().map(|s| s.clone().into()).collect(), | ||
})); | ||
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
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
Oops, something went wrong.