From b24ba17cc72809a93be87859754777426d2906bd Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 17 Jan 2024 16:11:09 +0100 Subject: [PATCH] apply page transforms only on pages (#60779) ### What? apply page transforms only on pages as intended ### Why? Before they were applied on all files Closes PACK-2250 --- .../next-core/src/next_client/transforms.rs | 10 +++---- .../next-core/src/next_server/transforms.rs | 12 ++++++--- .../src/next_shared/transforms/mod.rs | 27 ++++++++++++++++--- .../next_disallow_re_export_all_in_page.rs | 19 ++++++------- .../transforms/next_page_config.rs | 16 ++++++----- 5 files changed, 54 insertions(+), 30 deletions(-) diff --git a/packages/next-swc/crates/next-core/src/next_client/transforms.rs b/packages/next-swc/crates/next-core/src/next_client/transforms.rs index a36e8e67d1cc6..e133b90b879b8 100644 --- a/packages/next-swc/crates/next-core/src/next_client/transforms.rs +++ b/packages/next-swc/crates/next-core/src/next_client/transforms.rs @@ -44,6 +44,11 @@ pub async fn get_next_client_transforms_rules( get_next_pages_transforms_rule(pages_dir, ExportFilter::StripDataExports, mdx_rs) .await?, ); + rules.push(get_next_disallow_export_all_in_page_rule( + mdx_rs, + pages_dir.await?, + )); + rules.push(get_next_page_config_rule(mdx_rs, pages_dir.await?)); Some(pages_dir) } ClientContextType::App { .. } => { @@ -58,11 +63,6 @@ pub async fn get_next_client_transforms_rules( rules.push(get_next_amp_attr_rule(mdx_rs)); rules.push(get_next_cjs_optimizer_rule(mdx_rs)); - rules.push(get_next_disallow_export_all_in_page_rule( - mdx_rs, - pages_dir.is_some(), - )); - rules.push(get_next_page_config_rule(mdx_rs, pages_dir.is_some())); rules.push(get_next_pure_rule(mdx_rs)); rules.push(get_next_dynamic_transform_rule(false, false, pages_dir, mode, mdx_rs).await?); diff --git a/packages/next-swc/crates/next-core/src/next_server/transforms.rs b/packages/next-swc/crates/next-core/src/next_server/transforms.rs index ffa57b2c9c734..36c47543b29b1 100644 --- a/packages/next-swc/crates/next-core/src/next_server/transforms.rs +++ b/packages/next-swc/crates/next-core/src/next_server/transforms.rs @@ -41,6 +41,10 @@ pub async fn get_next_server_transforms_rules( let (is_server_components, pages_dir) = match context_ty { ServerContextType::Pages { pages_dir } | ServerContextType::PagesApi { pages_dir } => { + rules.push(get_next_disallow_export_all_in_page_rule( + mdx_rs, + pages_dir.await?, + )); (false, Some(pages_dir)) } ServerContextType::PagesData { pages_dir } => { @@ -48,6 +52,10 @@ pub async fn get_next_server_transforms_rules( get_next_pages_transforms_rule(pages_dir, ExportFilter::StripDefaultExport, mdx_rs) .await?, ); + rules.push(get_next_disallow_export_all_in_page_rule( + mdx_rs, + pages_dir.await?, + )); (false, Some(pages_dir)) } ServerContextType::AppSSR { .. } => { @@ -90,10 +98,6 @@ pub async fn get_next_server_transforms_rules( rules.push(get_next_amp_attr_rule(mdx_rs)); rules.push(get_next_cjs_optimizer_rule(mdx_rs)); - rules.push(get_next_disallow_export_all_in_page_rule( - mdx_rs, - pages_dir.is_some(), - )); rules.push(get_next_pure_rule(mdx_rs)); // [NOTE]: this rule only works in prod config diff --git a/packages/next-swc/crates/next-core/src/next_shared/transforms/mod.rs b/packages/next-swc/crates/next-core/src/next_shared/transforms/mod.rs index 3667d98e76c22..6fec32352a1c3 100644 --- a/packages/next-swc/crates/next-core/src/next_shared/transforms/mod.rs +++ b/packages/next-swc/crates/next-core/src/next_shared/transforms/mod.rs @@ -23,7 +23,8 @@ pub use next_font::get_next_font_transform_rule; pub use next_strip_page_exports::get_next_pages_transforms_rule; pub use relay::get_relay_transform_plugin; pub use server_actions::get_server_actions_transform_rule; -use turbo_tasks::{Value, Vc}; +use turbo_tasks::{ReadRef, Value, Vc}; +use turbo_tasks_fs::FileSystemPath; use turbopack_binding::turbopack::{ core::reference_type::{ReferenceType, UrlReferenceSubType}, turbopack::module_options::{ModuleRule, ModuleRuleCondition, ModuleRuleEffect, ModuleType}, @@ -63,8 +64,7 @@ pub fn get_next_image_rule() -> ModuleRule { ) } -/// Returns a rule which applies the Next.js dynamic transform. -pub(crate) fn module_rule_match_js_no_url(enable_mdx_rs: bool) -> ModuleRuleCondition { +fn match_js_extension(enable_mdx_rs: bool) -> Vec { let mut conditions = vec![ ModuleRuleCondition::ResourcePathEndsWith(".js".to_string()), ModuleRuleCondition::ResourcePathEndsWith(".jsx".to_string()), @@ -81,11 +81,32 @@ pub(crate) fn module_rule_match_js_no_url(enable_mdx_rs: bool) -> ModuleRuleCond .as_mut(), ); } + conditions +} + +/// Returns a rule which applies the Next.js dynamic transform. +pub(crate) fn module_rule_match_js_no_url(enable_mdx_rs: bool) -> ModuleRuleCondition { + let conditions = match_js_extension(enable_mdx_rs); + + ModuleRuleCondition::all(vec![ + ModuleRuleCondition::not(ModuleRuleCondition::ReferenceType(ReferenceType::Url( + UrlReferenceSubType::Undefined, + ))), + ModuleRuleCondition::any(conditions), + ]) +} + +pub(crate) fn module_rule_match_pages_page_file( + enable_mdx_rs: bool, + pages_directory: ReadRef, +) -> ModuleRuleCondition { + let conditions = match_js_extension(enable_mdx_rs); ModuleRuleCondition::all(vec![ ModuleRuleCondition::not(ModuleRuleCondition::ReferenceType(ReferenceType::Url( UrlReferenceSubType::Undefined, ))), + ModuleRuleCondition::ResourcePathInExactDirectory(pages_directory), ModuleRuleCondition::any(conditions), ]) } diff --git a/packages/next-swc/crates/next-core/src/next_shared/transforms/next_disallow_re_export_all_in_page.rs b/packages/next-swc/crates/next-core/src/next_shared/transforms/next_disallow_re_export_all_in_page.rs index 6b22a296761f6..030241a601b40 100644 --- a/packages/next-swc/crates/next-core/src/next_shared/transforms/next_disallow_re_export_all_in_page.rs +++ b/packages/next-swc/crates/next-core/src/next_shared/transforms/next_disallow_re_export_all_in_page.rs @@ -1,7 +1,8 @@ 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 turbo_tasks::{ReadRef, Vc}; +use turbo_tasks_fs::FileSystemPath; use turbopack_binding::{ swc::core::{ common::util::take::Take, @@ -13,18 +14,16 @@ use turbopack_binding::{ }, }; -use super::module_rule_match_js_no_url; +use super::module_rule_match_pages_page_file; pub fn get_next_disallow_export_all_in_page_rule( enable_mdx_rs: bool, - is_page_file: bool, + pages_dir: ReadRef, ) -> ModuleRule { let transformer = - EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextDisallowReExportAllInPage { - is_page_file, - }) as _)); + EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextDisallowReExportAllInPage) as _)); ModuleRule::new( - module_rule_match_js_no_url(enable_mdx_rs), + module_rule_match_pages_page_file(enable_mdx_rs, pages_dir), vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![ transformer, ]))], @@ -32,15 +31,13 @@ pub fn get_next_disallow_export_all_in_page_rule( } #[derive(Debug)] -struct NextDisallowReExportAllInPage { - is_page_file: bool, -} +struct NextDisallowReExportAllInPage; #[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)); + *program = p.fold_with(&mut disallow_re_export_all_in_page(true)); Ok(()) } } diff --git a/packages/next-swc/crates/next-core/src/next_shared/transforms/next_page_config.rs b/packages/next-swc/crates/next-core/src/next_shared/transforms/next_page_config.rs index cc1c78f0d1a43..0d4feb7a3f1bb 100644 --- a/packages/next-swc/crates/next-core/src/next_shared/transforms/next_page_config.rs +++ b/packages/next-swc/crates/next-core/src/next_shared/transforms/next_page_config.rs @@ -1,7 +1,8 @@ use anyhow::Result; use async_trait::async_trait; use next_custom_transforms::transforms::page_config::page_config; -use turbo_tasks::Vc; +use turbo_tasks::{ReadRef, Vc}; +use turbo_tasks_fs::FileSystemPath; use turbopack_binding::{ swc::core::{ common::util::take::Take, @@ -13,16 +14,18 @@ use turbopack_binding::{ }, }; -use super::module_rule_match_js_no_url; +use super::module_rule_match_pages_page_file; -pub fn get_next_page_config_rule(enable_mdx_rs: bool, is_page_file: bool) -> ModuleRule { +pub fn get_next_page_config_rule( + enable_mdx_rs: bool, + pages_dir: ReadRef, +) -> 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), + module_rule_match_pages_page_file(enable_mdx_rs, pages_dir), vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![ transformer, ]))], @@ -32,7 +35,6 @@ pub fn get_next_page_config_rule(enable_mdx_rs: bool, is_page_file: bool) -> Mod #[derive(Debug)] struct NextPageConfig { is_development: bool, - is_page_file: bool, } #[async_trait] @@ -40,7 +42,7 @@ 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)); + *program = p.fold_with(&mut page_config(self.is_development, true)); Ok(()) } }