Unverified Commit b24ba17c authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

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
parent 4548fed1
...@@ -44,6 +44,11 @@ pub async fn get_next_client_transforms_rules( ...@@ -44,6 +44,11 @@ pub async fn get_next_client_transforms_rules(
get_next_pages_transforms_rule(pages_dir, ExportFilter::StripDataExports, mdx_rs) get_next_pages_transforms_rule(pages_dir, ExportFilter::StripDataExports, mdx_rs)
.await?, .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) Some(pages_dir)
} }
ClientContextType::App { .. } => { ClientContextType::App { .. } => {
...@@ -58,11 +63,6 @@ pub async fn get_next_client_transforms_rules( ...@@ -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_amp_attr_rule(mdx_rs));
rules.push(get_next_cjs_optimizer_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_pure_rule(mdx_rs));
rules.push(get_next_dynamic_transform_rule(false, false, pages_dir, mode, mdx_rs).await?); rules.push(get_next_dynamic_transform_rule(false, false, pages_dir, mode, mdx_rs).await?);
......
...@@ -41,6 +41,10 @@ pub async fn get_next_server_transforms_rules( ...@@ -41,6 +41,10 @@ pub async fn get_next_server_transforms_rules(
let (is_server_components, pages_dir) = match context_ty { let (is_server_components, pages_dir) = match context_ty {
ServerContextType::Pages { pages_dir } | ServerContextType::PagesApi { pages_dir } => { 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)) (false, Some(pages_dir))
} }
ServerContextType::PagesData { pages_dir } => { ServerContextType::PagesData { pages_dir } => {
...@@ -48,6 +52,10 @@ pub async fn get_next_server_transforms_rules( ...@@ -48,6 +52,10 @@ pub async fn get_next_server_transforms_rules(
get_next_pages_transforms_rule(pages_dir, ExportFilter::StripDefaultExport, mdx_rs) get_next_pages_transforms_rule(pages_dir, ExportFilter::StripDefaultExport, mdx_rs)
.await?, .await?,
); );
rules.push(get_next_disallow_export_all_in_page_rule(
mdx_rs,
pages_dir.await?,
));
(false, Some(pages_dir)) (false, Some(pages_dir))
} }
ServerContextType::AppSSR { .. } => { ServerContextType::AppSSR { .. } => {
...@@ -90,10 +98,6 @@ pub async fn get_next_server_transforms_rules( ...@@ -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_amp_attr_rule(mdx_rs));
rules.push(get_next_cjs_optimizer_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)); rules.push(get_next_pure_rule(mdx_rs));
// [NOTE]: this rule only works in prod config // [NOTE]: this rule only works in prod config
......
...@@ -23,7 +23,8 @@ pub use next_font::get_next_font_transform_rule; ...@@ -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 next_strip_page_exports::get_next_pages_transforms_rule;
pub use relay::get_relay_transform_plugin; pub use relay::get_relay_transform_plugin;
pub use server_actions::get_server_actions_transform_rule; 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::{ use turbopack_binding::turbopack::{
core::reference_type::{ReferenceType, UrlReferenceSubType}, core::reference_type::{ReferenceType, UrlReferenceSubType},
turbopack::module_options::{ModuleRule, ModuleRuleCondition, ModuleRuleEffect, ModuleType}, turbopack::module_options::{ModuleRule, ModuleRuleCondition, ModuleRuleEffect, ModuleType},
...@@ -63,8 +64,7 @@ pub fn get_next_image_rule() -> ModuleRule { ...@@ -63,8 +64,7 @@ pub fn get_next_image_rule() -> ModuleRule {
) )
} }
/// Returns a rule which applies the Next.js dynamic transform. fn match_js_extension(enable_mdx_rs: bool) -> Vec<ModuleRuleCondition> {
pub(crate) fn module_rule_match_js_no_url(enable_mdx_rs: bool) -> ModuleRuleCondition {
let mut conditions = vec![ let mut conditions = vec![
ModuleRuleCondition::ResourcePathEndsWith(".js".to_string()), ModuleRuleCondition::ResourcePathEndsWith(".js".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".jsx".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 ...@@ -81,11 +81,32 @@ pub(crate) fn module_rule_match_js_no_url(enable_mdx_rs: bool) -> ModuleRuleCond
.as_mut(), .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<FileSystemPath>,
) -> ModuleRuleCondition {
let conditions = match_js_extension(enable_mdx_rs);
ModuleRuleCondition::all(vec![ ModuleRuleCondition::all(vec![
ModuleRuleCondition::not(ModuleRuleCondition::ReferenceType(ReferenceType::Url( ModuleRuleCondition::not(ModuleRuleCondition::ReferenceType(ReferenceType::Url(
UrlReferenceSubType::Undefined, UrlReferenceSubType::Undefined,
))), ))),
ModuleRuleCondition::ResourcePathInExactDirectory(pages_directory),
ModuleRuleCondition::any(conditions), ModuleRuleCondition::any(conditions),
]) ])
} }
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use next_custom_transforms::transforms::disallow_re_export_all_in_page::disallow_re_export_all_in_page; 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::{ use turbopack_binding::{
swc::core::{ swc::core::{
common::util::take::Take, common::util::take::Take,
...@@ -13,18 +14,16 @@ use turbopack_binding::{ ...@@ -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( pub fn get_next_disallow_export_all_in_page_rule(
enable_mdx_rs: bool, enable_mdx_rs: bool,
is_page_file: bool, pages_dir: ReadRef<FileSystemPath>,
) -> ModuleRule { ) -> ModuleRule {
let transformer = let transformer =
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextDisallowReExportAllInPage { EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextDisallowReExportAllInPage) as _));
is_page_file,
}) as _));
ModuleRule::new( 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![ vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![
transformer, transformer,
]))], ]))],
...@@ -32,15 +31,13 @@ pub fn get_next_disallow_export_all_in_page_rule( ...@@ -32,15 +31,13 @@ pub fn get_next_disallow_export_all_in_page_rule(
} }
#[derive(Debug)] #[derive(Debug)]
struct NextDisallowReExportAllInPage { struct NextDisallowReExportAllInPage;
is_page_file: bool,
}
#[async_trait] #[async_trait]
impl CustomTransformer for NextDisallowReExportAllInPage { impl CustomTransformer for NextDisallowReExportAllInPage {
async fn transform(&self, program: &mut Program, _ctx: &TransformContext<'_>) -> Result<()> { async fn transform(&self, program: &mut Program, _ctx: &TransformContext<'_>) -> Result<()> {
let p = std::mem::replace(program, Program::Module(Module::dummy())); 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(()) Ok(())
} }
} }
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use next_custom_transforms::transforms::page_config::page_config; 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::{ use turbopack_binding::{
swc::core::{ swc::core::{
common::util::take::Take, common::util::take::Take,
...@@ -13,16 +14,18 @@ use turbopack_binding::{ ...@@ -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<FileSystemPath>,
) -> ModuleRule {
let transformer = EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextPageConfig { let transformer = EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextPageConfig {
// [TODO]: update once turbopack build works // [TODO]: update once turbopack build works
is_development: true, is_development: true,
is_page_file,
}) as _)); }) as _));
ModuleRule::new( 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![ vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![
transformer, transformer,
]))], ]))],
...@@ -32,7 +35,6 @@ pub fn get_next_page_config_rule(enable_mdx_rs: bool, is_page_file: bool) -> Mod ...@@ -32,7 +35,6 @@ pub fn get_next_page_config_rule(enable_mdx_rs: bool, is_page_file: bool) -> Mod
#[derive(Debug)] #[derive(Debug)]
struct NextPageConfig { struct NextPageConfig {
is_development: bool, is_development: bool,
is_page_file: bool,
} }
#[async_trait] #[async_trait]
...@@ -40,7 +42,7 @@ impl CustomTransformer for NextPageConfig { ...@@ -40,7 +42,7 @@ impl CustomTransformer for NextPageConfig {
async fn transform(&self, program: &mut Program, _ctx: &TransformContext<'_>) -> Result<()> { async fn transform(&self, program: &mut Program, _ctx: &TransformContext<'_>) -> Result<()> {
let p = std::mem::replace(program, Program::Module(Module::dummy())); 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(()) Ok(())
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment