Skip to content

Commit

Permalink
[Turbopack] allow to disable tree shaking (#71114)
Browse files Browse the repository at this point in the history
### What?

This fixes `experimental.turbo.treeShaking: false` and allows to disable tree shaking
  • Loading branch information
sokra authored Oct 10, 2024
1 parent 39833fd commit 96a7efa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
28 changes: 19 additions & 9 deletions crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,15 +1140,25 @@ impl NextConfig {
}

#[turbo_tasks::function]
pub fn tree_shaking_mode_for_user_code(
self: Vc<Self>,
is_development: bool,
) -> Vc<OptionTreeShaking> {
Vc::cell(Some(if is_development {
TreeShakingMode::ReexportsOnly
} else {
TreeShakingMode::ModuleFragments
}))
pub fn tree_shaking_mode_for_user_code(&self, is_development: bool) -> Vc<OptionTreeShaking> {
let tree_shaking = self
.experimental
.turbo
.as_ref()
.and_then(|v| v.tree_shaking);

OptionTreeShaking(match tree_shaking {
Some(false) => Some(TreeShakingMode::ReexportsOnly),
Some(true) => Some(TreeShakingMode::ModuleFragments),
None => {
if is_development {
Some(TreeShakingMode::ReexportsOnly)
} else {
Some(TreeShakingMode::ModuleFragments)
}
}
})
.cell()
}

#[turbo_tasks::function]
Expand Down
16 changes: 1 addition & 15 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use turbopack_core::{
environment::{Environment, ExecutionEnvironment, NodeJsEnvironment, RuntimeVersions},
free_var_references,
};
use turbopack_ecmascript::{references::esm::UrlRewriteBehavior, TreeShakingMode};
use turbopack_ecmascript::references::esm::UrlRewriteBehavior;
use turbopack_ecmascript_plugins::transform::directives::{
client::ClientDirectiveTransformer, client_disallowed::ClientDisallowedDirectiveTransformer,
};
Expand Down Expand Up @@ -898,20 +898,6 @@ pub async fn get_server_module_options_context(
Ok(module_options_context)
}

#[turbo_tasks::function]
pub fn get_build_module_options_context() -> Vc<ModuleOptionsContext> {
ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext {
enable_typescript_transform: Some(Default::default()),
esm_url_rewrite_behavior: Some(UrlRewriteBehavior::Full),
..Default::default()
},
tree_shaking_mode: Some(TreeShakingMode::ModuleFragments),
..Default::default()
}
.cell()
}

#[turbo_tasks::function]
pub fn get_server_runtime_entries(
_ty: Value<ServerContextType>,
Expand Down
4 changes: 3 additions & 1 deletion turbopack/crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ async fn apply_module_type(
}
}
_ => bail!(
"Invalid module part for reexports only tree shaking mode"
"Invalid module part \"{}\" for reexports only tree shaking \
mode",
part.to_string().await?
),
}
} else if *module.get_exports().needs_facade().await? {
Expand Down

0 comments on commit 96a7efa

Please sign in to comment.