-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(next-core): port remaining next.js custom transforms (#60498)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? --> This PR ports over all of the remaining custom transforms for next-swc into turbopack, and apply it into client / server rules accordingly. Closes PACK-2225
- Loading branch information
Showing
17 changed files
with
400 additions
and
22 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
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(()) | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
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,79 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use next_custom_transforms::transforms::cjs_optimizer::{cjs_optimizer, Config, PackageConfig}; | ||
use rustc_hash::FxHashMap; | ||
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) -> ModuleRule { | ||
// [NOTE]: This isn't user configurable config | ||
// (https://github.com/vercel/next.js/blob/a1d0259ea06592c5ca6df882e9b1d0d0121c5083/packages/next/src/build/swc/options.ts#L395) | ||
// build it internally without accepting customization. | ||
let config = Config { | ||
packages: FxHashMap::from_iter([( | ||
"next/server".to_string(), | ||
PackageConfig { | ||
transforms: FxHashMap::from_iter([ | ||
( | ||
"NextRequest".into(), | ||
"next/dist/server/web/spec-extension/request".into(), | ||
), | ||
( | ||
"NextResponse".into(), | ||
"next/dist/server/web/spec-extension/response".into(), | ||
), | ||
( | ||
"ImageResponse".into(), | ||
"next/dist/server/web/spec-extension/image-response".into(), | ||
), | ||
( | ||
"userAgentFromString".into(), | ||
"next/dist/server/web/spec-extension/user-agent".into(), | ||
), | ||
( | ||
"userAgent".into(), | ||
"next/dist/server/web/spec-extension/user-agent".into(), | ||
), | ||
]), | ||
}, | ||
)]), | ||
}; | ||
|
||
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(()) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
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,50 @@ | ||
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; | ||
|
||
#[allow(dead_code)] | ||
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(()) | ||
} | ||
} |
Oops, something went wrong.