From 5141d824798070b9df69efb072294894005bca11 Mon Sep 17 00:00:00 2001 From: Hendrik Liebau Date: Mon, 25 Nov 2024 10:20:11 +0100 Subject: [PATCH] Omit unnecessary cache wrapper import When a `"use cache"` module has no exported or annotated cache function, we can omit the import statement for the cache wrapper function. --- .../src/transforms/server_actions.rs | 47 ++++++++++--------- .../server-actions/server-graph/23/output.js | 1 - .../server-actions/server/51/output.js | 1 - .../server-actions/server/56/output.js | 3 -- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/crates/next-custom-transforms/src/transforms/server_actions.rs b/crates/next-custom-transforms/src/transforms/server_actions.rs index a134ac8369f83..1262072b410c9 100644 --- a/crates/next-custom-transforms/src/transforms/server_actions.rs +++ b/crates/next-custom-transforms/src/transforms/server_actions.rs @@ -1316,9 +1316,6 @@ impl VisitMut for ServerActions { let in_cache_file = matches!(self.file_directive, Some(Directive::UseCache { .. })); let in_action_file = matches!(self.file_directive, Some(Directive::UseServer)); - self.has_action = in_action_file; - self.has_cache = in_cache_file; - if in_cache_file { // If we're in a "use cache" file, collect all original IDs from // export specifiers in a pre-pass so that we know which functions @@ -1697,13 +1694,34 @@ impl VisitMut for ServerActions { } } + let mut actions = self.export_actions.take(); + + if in_action_file || in_cache_file && !self.config.is_react_server_layer { + actions.extend( + self.exported_idents + .iter() + .map(|e| (e.1.clone(), e.2.clone())), + ); + + if !actions.is_empty() { + self.has_action |= in_action_file; + self.has_cache |= in_cache_file; + } + }; + + // Make it a hashmap of id -> name. + let actions = actions + .into_iter() + .map(|a| (a.1, a.0)) + .collect::(); + // If it's compiled in the client layer, each export field needs to be // wrapped by a reference creation call. let create_ref_ident = private_ident!("createServerReference"); let call_server_ident = private_ident!("callServer"); let find_source_map_url_ident = private_ident!("findSourceMapURL"); - if should_track_exports && !self.config.is_react_server_layer { + if (self.has_action || self.has_cache) && !self.config.is_react_server_layer { // import { // createServerReference, // callServer, @@ -1828,7 +1846,7 @@ impl VisitMut for ServerActions { // // But it's only needed for the server layer, because on the client // layer they're transformed into references already. - if self.config.is_react_server_layer { + if (self.has_action || self.has_cache) && self.config.is_react_server_layer { new.append(&mut self.extra_items); // For "use cache" files, there's no need to do extra annotations. @@ -1883,23 +1901,6 @@ impl VisitMut for ServerActions { } if self.has_action || self.has_cache { - let mut actions = self.export_actions.clone(); - - // All exported values are considered as actions if the file is an action file. - if in_action_file || in_cache_file && !self.config.is_react_server_layer { - actions.extend( - self.exported_idents - .iter() - .map(|e| (e.1.clone(), e.2.clone())), - ); - }; - - // Make it a hashmap of id -> name. - let actions = actions - .into_iter() - .map(|a| (a.1, a.0)) - .collect::(); - // Prepend a special comment to the top of the file. self.comments.add_leading( self.start_pos, @@ -1911,7 +1912,7 @@ impl VisitMut for ServerActions { ); } - // import { cache as $cache } from "private-next-rsc-cache-wrapper"; + // import { cache as $$cache__ } from "private-next-rsc-cache-wrapper"; if self.has_cache && self.config.is_react_server_layer { new.push(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl { span: DUMMY_SP, diff --git a/crates/next-custom-transforms/tests/errors/server-actions/server-graph/23/output.js b/crates/next-custom-transforms/tests/errors/server-actions/server-graph/23/output.js index d9446d59b1c2f..e6afdb681e4ff 100644 --- a/crates/next-custom-transforms/tests/errors/server-actions/server-graph/23/output.js +++ b/crates/next-custom-transforms/tests/errors/server-actions/server-graph/23/output.js @@ -1,6 +1,5 @@ /* __next_internal_action_entry_do_not_use__ {"006a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0"} */ import { registerServerReference } from "private-next-rsc-server-reference"; import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption"; -import { cache as $$cache__ } from "private-next-rsc-cache-wrapper"; export const /*#__TURBOPACK_DISABLE_EXPORT_MERGING__*/ $$RSC_SERVER_ACTION_0 = async function b() { // this is not allowed here this.foo(); diff --git a/crates/next-custom-transforms/tests/fixture/server-actions/server/51/output.js b/crates/next-custom-transforms/tests/fixture/server-actions/server/51/output.js index 60640e76dbcc7..ffebfc7ac7b7e 100644 --- a/crates/next-custom-transforms/tests/fixture/server-actions/server/51/output.js +++ b/crates/next-custom-transforms/tests/fixture/server-actions/server/51/output.js @@ -1,6 +1,5 @@ /* __next_internal_action_entry_do_not_use__ {"601c36b06e398c97abe5d5d7ae8c672bfddf4e1b91":"$$RSC_SERVER_ACTION_2","609ed0cc47abc4e1c64320cf42b74ae60b58c40f00":"$$RSC_SERVER_ACTION_3","7090b5db271335765a4b0eab01f044b381b5ebd5cd":"$$RSC_SERVER_ACTION_1"} */ import { registerServerReference } from "private-next-rsc-server-reference"; import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption"; -import { cache as $$cache__ } from "private-next-rsc-cache-wrapper"; export const /*#__TURBOPACK_DISABLE_EXPORT_MERGING__*/ $$RSC_SERVER_ACTION_1 = async function $$RSC_SERVER_ACTION_0(a, b, c) { return
{a} diff --git a/crates/next-custom-transforms/tests/fixture/server-actions/server/56/output.js b/crates/next-custom-transforms/tests/fixture/server-actions/server/56/output.js index ca1e3414c1174..568492853d2a5 100644 --- a/crates/next-custom-transforms/tests/fixture/server-actions/server/56/output.js +++ b/crates/next-custom-transforms/tests/fixture/server-actions/server/56/output.js @@ -1,6 +1,3 @@ -/* __next_internal_action_entry_do_not_use__ {} */ import { registerServerReference } from "private-next-rsc-server-reference"; -import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption"; -import { cache as $$cache__ } from "private-next-rsc-cache-wrapper"; // No method nor function property should be considered a cache function. export const obj = { foo () {