Skip to content

Commit

Permalink
Suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bgw committed Nov 15, 2024
1 parent 6230c41 commit 10e487a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct CustomRoutes {
pub struct ModularizeImports(FxIndexMap<String, ModularizeImportPackageConfig>);

#[turbo_tasks::value(transparent)]
pub struct CacheKinds(FxHashSet<String>);
pub struct CacheKinds(FxHashSet<RcStr>);

#[turbo_tasks::value(serialization = "custom", eq = "manual")]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -1168,11 +1168,9 @@ impl NextConfig {
Vc::cell(
self.experimental
.cache_handlers
.clone()
.unwrap_or_default()
.keys()
.cloned()
.collect(),
.as_ref()
.map(|handlers| handlers.keys().map(|kind| RcStr::from(&**kind)).collect())
.unwrap_or_default(),
)
}

Expand Down
1 change: 1 addition & 0 deletions crates/next-custom-transforms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ styled_jsx = { workspace = true }
swc_emotion = { workspace = true }
swc_relay = { workspace = true }
turbopack-ecmascript-plugins = { workspace = true, optional = true }
turbo-tasks = { workspace = true } # only needed for RcStr

react_remove_properties = "0.24.25"
remove_console = "0.25.25"
Expand Down
53 changes: 35 additions & 18 deletions crates/next-custom-transforms/src/transforms/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ use swc_core::{
visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith},
},
};
use turbo_tasks::RcStr;

#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Config {
pub is_react_server_layer: bool,
pub dynamic_io_enabled: bool,
pub hash_salt: String,
pub cache_kinds: FxHashSet<String>,
pub cache_kinds: FxHashSet<RcStr>,
}

enum DirectiveLocation {
Expand Down Expand Up @@ -69,7 +70,7 @@ enum ServerActionsErrorKind {
},
UnknownCacheKind {
span: Span,
cache_kind: String,
cache_kind: RcStr,
},
UseCacheWithoutDynamicIO {
span: Span,
Expand All @@ -85,6 +86,12 @@ enum ServerActionsErrorKind {
// Using BTreeMap to ensure the order of the actions is deterministic.
pub type ActionsMap = BTreeMap<String, String>;

// Directive-level information about a function body
struct BodyInfo {
is_action_fn: bool,
cache_kind: Option<RcStr>,
}

#[tracing::instrument(level = tracing::Level::TRACE, skip_all)]
pub fn server_actions<C: Comments>(file_name: &FileName, config: Config, comments: C) -> impl Pass {
visit_mut_pass(ServerActions {
Expand Down Expand Up @@ -143,7 +150,7 @@ struct ServerActions<C: Comments> {

start_pos: BytePos,
in_action_file: bool,
file_cache_kind: Option<String>,
file_cache_kind: Option<RcStr>,
in_exported_expr: bool,
in_default_export_decl: bool,
in_callee: bool,
Expand Down Expand Up @@ -302,7 +309,7 @@ impl<C: Comments> ServerActions<C> {
}

// Check if the function or arrow function is an action or cache function
fn get_body_info(&mut self, maybe_body: Option<&mut BlockStmt>) -> (bool, Option<String>) {
fn get_body_info(&mut self, maybe_body: Option<&mut BlockStmt>) -> BodyInfo {
let mut is_action_fn = false;
let mut cache_kind = None;

Expand Down Expand Up @@ -346,7 +353,10 @@ impl<C: Comments> ServerActions<C> {
}
}

(is_action_fn, cache_kind)
BodyInfo {
is_action_fn,
cache_kind,
}
}

fn maybe_hoist_and_create_proxy_for_server_action_arrow_expr(
Expand Down Expand Up @@ -887,7 +897,10 @@ impl<C: Comments> VisitMut for ServerActions<C> {
}

fn visit_mut_fn_expr(&mut self, f: &mut FnExpr) {
let (is_action_fn, cache_kind) = self.get_body_info(f.function.body.as_mut());
let BodyInfo {
is_action_fn,
cache_kind,
} = self.get_body_info(f.function.body.as_mut());

let declared_idents_until = self.declared_idents.len();
let current_names = take(&mut self.names);
Expand Down Expand Up @@ -1002,7 +1015,10 @@ impl<C: Comments> VisitMut for ServerActions<C> {
self.in_exported_expr = true
}

let (is_action_fn, cache_kind) = self.get_body_info(f.function.body.as_mut());
let BodyInfo {
is_action_fn,
cache_kind,
} = self.get_body_info(f.function.body.as_mut());

let declared_idents_until = self.declared_idents.len();
let current_names = take(&mut self.names);
Expand Down Expand Up @@ -1142,12 +1158,14 @@ impl<C: Comments> VisitMut for ServerActions<C> {
fn visit_mut_arrow_expr(&mut self, a: &mut ArrowExpr) {
// Arrow expressions need to be visited in prepass to determine if it's
// an action function or not.
let (is_action_fn, cache_kind) =
self.get_body_info(if let BlockStmtOrExpr::BlockStmt(block) = &mut *a.body {
Some(block)
} else {
None
});
let BodyInfo {
is_action_fn,
cache_kind,
} = self.get_body_info(if let BlockStmtOrExpr::BlockStmt(block) = &mut *a.body {
Some(block)
} else {
None
});

let declared_idents_until = self.declared_idents.len();
let current_names = take(&mut self.names);
Expand Down Expand Up @@ -2352,7 +2370,7 @@ fn detect_similar_strings(a: &str, b: &str) -> bool {
fn remove_server_directive_index_in_module(
stmts: &mut Vec<ModuleItem>,
in_action_file: &mut bool,
file_cache_kind: &mut Option<String>,
file_cache_kind: &mut Option<RcStr>,
has_action: &mut bool,
has_cache: &mut bool,
config: &Config,
Expand Down Expand Up @@ -2392,8 +2410,7 @@ fn remove_server_directive_index_in_module(
*file_cache_kind = Some("default".into());
} else {
// Slice the value after "use cache: "
let cache_kind_str: String =
value.split_at("use cache: ".len()).1.into();
let cache_kind_str = RcStr::from(value.split_at("use cache: ".len()).1);

if !config.cache_kinds.contains(&cache_kind_str) {
emit_error(ServerActionsErrorKind::UnknownCacheKind {
Expand Down Expand Up @@ -2503,7 +2520,7 @@ fn has_body_directive(maybe_body: &Option<BlockStmt>) -> (bool, bool) {
fn remove_server_directive_index_in_fn(
stmts: &mut Vec<Stmt>,
is_action_fn: &mut bool,
cache_kind: &mut Option<String>,
cache_kind: &mut Option<RcStr>,
action_span: &mut Option<Span>,
config: &Config,
) {
Expand Down Expand Up @@ -2548,7 +2565,7 @@ fn remove_server_directive_index_in_fn(
*cache_kind = Some("default".into());
} else {
// Slice the value after "use cache: "
let cache_kind_str: String = value.split_at("use cache: ".len()).1.into();
let cache_kind_str = RcStr::from(value.split_at("use cache: ".len()).1);

if !config.cache_kinds.contains(&cache_kind_str) {
emit_error(ServerActionsErrorKind::UnknownCacheKind {
Expand Down

0 comments on commit 10e487a

Please sign in to comment.