Skip to content

Commit

Permalink
fixing next/dynamic transform for turbopack (#66527)
Browse files Browse the repository at this point in the history
### What?

pass the correct prefer_esm value to the transform
  • Loading branch information
sokra authored Jun 4, 2024
1 parent 3ef03ff commit a8221a0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub async fn get_next_client_transforms_rules(

rules.push(get_next_font_transform_rule(enable_mdx_rs));

let mut is_app_dir = false;

match context_ty {
ClientContextType::Pages { pages_dir } => {
if !foreign_code {
Expand All @@ -59,6 +61,7 @@ pub async fn get_next_client_transforms_rules(
}
}
ClientContextType::App { .. } => {
is_app_dir = true;
rules.push(get_server_actions_transform_rule(
ActionsTransform::Client,
enable_mdx_rs,
Expand All @@ -72,7 +75,9 @@ pub async fn get_next_client_transforms_rules(
rules.push(get_next_cjs_optimizer_rule(enable_mdx_rs));
rules.push(get_next_pure_rule(enable_mdx_rs));

rules.push(get_next_dynamic_transform_rule(false, false, mode, enable_mdx_rs).await?);
rules.push(
get_next_dynamic_transform_rule(false, false, is_app_dir, mode, enable_mdx_rs).await?,
);

rules.push(get_next_image_rule());
rules.push(get_next_page_static_info_assert_rule(
Expand Down
16 changes: 13 additions & 3 deletions packages/next-swc/crates/next-core/src/next_server/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub async fn get_next_server_transforms_rules(
));
}

let mut is_app_dir = false;

let is_server_components = match context_ty {
ServerContextType::Pages { pages_dir } | ServerContextType::PagesApi { pages_dir } => {
if !foreign_code {
Expand Down Expand Up @@ -84,6 +86,7 @@ pub async fn get_next_server_transforms_rules(
ActionsTransform::Client,
mdx_rs,
));
is_app_dir = true;

false
}
Expand All @@ -100,15 +103,22 @@ pub async fn get_next_server_transforms_rules(
client_transition,
));
}
is_app_dir = true;

true
}
ServerContextType::AppRoute { .. } => false,
ServerContextType::AppRoute { .. } => {
is_app_dir = true;
false
}
ServerContextType::Middleware { .. } | ServerContextType::Instrumentation { .. } => false,
};

if !foreign_code {
rules
.push(get_next_dynamic_transform_rule(true, is_server_components, mode, mdx_rs).await?);
rules.push(
get_next_dynamic_transform_rule(true, is_server_components, is_app_dir, mode, mdx_rs)
.await?,
);

rules.push(get_next_amp_attr_rule(mdx_rs));
rules.push(get_next_cjs_optimizer_rule(mdx_rs));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ use crate::mode::NextMode;
pub async fn get_next_dynamic_transform_rule(
is_server_compiler: bool,
is_react_server_layer: bool,
is_app_dir: bool,
mode: Vc<NextMode>,
enable_mdx_rs: bool,
) -> Result<ModuleRule> {
let dynamic_transform = EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextJsDynamic {
is_server_compiler,
is_react_server_layer,
is_app_dir,
mode: *mode.await?,
}) as _));
Ok(ModuleRule::new(
Expand All @@ -44,6 +46,7 @@ pub async fn get_next_dynamic_transform_rule(
struct NextJsDynamic {
is_server_compiler: bool,
is_react_server_layer: bool,
is_app_dir: bool,
mode: NextMode,
}

Expand All @@ -55,7 +58,7 @@ impl CustomTransformer for NextJsDynamic {
self.mode.is_development(),
self.is_server_compiler,
self.is_react_server_layer,
false,
self.is_app_dir,
NextDynamicMode::Webpack,
FileName::Real(ctx.file_path_str.into()),
None,
Expand Down

0 comments on commit a8221a0

Please sign in to comment.