Skip to content

Commit

Permalink
Implement Server Actions
Browse files Browse the repository at this point in the history
Insert react-experimental import mappings

Parse the action map from comment

tmp

tmp

Small cleanup

Traverse from the rsc_entry, not the loader_tree

Use evaluable asset and chunk_item id

add node manifest loader to originModules to avoid memory leak

clear cache for experimental react version too

add use-flight-response as originModule to avoid leaking memory

Merge server actions manifests

Only transform server actions when enabled

Always generate manifest

Cleanup

Fix merge conflicts

Comments, and always generate the manifest

Include next-swc crate in workspace

Use graph traversal to find actions

Reuse primary_referenced_modules

Remove need for swc_file_name

Use new `all_modules_iter` function

Revert "Use new `all_modules_iter` function"

This reverts commit 4239c5f.

Pin to latest turbopack

Use Mapping so that we don't need to hash again

tmp

Fix writing location for manifests

Fix output path

Fix merge conflicts
  • Loading branch information
jridgewell committed Sep 18, 2023
1 parent c6c3891 commit 40344fa
Show file tree
Hide file tree
Showing 24 changed files with 702 additions and 114 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ next-dev = { path = "packages/next-swc/crates/next-dev", default-features = fals
"serializable",
] }
next-dev-tests = { path = "packages/next-swc/crates/next-dev-tests" }
next-swc = { path = "packages/next-swc/crates/core" }
next-transform-font = { path = "packages/next-swc/crates/next-transform-font" }
next-transform-dynamic = { path = "packages/next-swc/crates/next-transform-dynamic" }
next-transform-strip-page-exports = { path = "packages/next-swc/crates/next-transform-strip-page-exports" }
Expand Down
81 changes: 56 additions & 25 deletions packages/next-swc/crates/core/src/server_actions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::convert::{TryFrom, TryInto};
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
};

use hex::encode as hex_encode;
use serde::Deserialize;
Expand All @@ -25,6 +28,9 @@ pub struct Config {
pub enabled: bool,
}

/// A mapping of hashed action id to the action's exported function name.
pub type ActionsMap = HashMap<String, String>;

pub fn server_actions<C: Comments>(
file_name: &FileName,
config: Config,
Expand All @@ -33,7 +39,7 @@ pub fn server_actions<C: Comments>(
as_folder(ServerActions {
config,
comments,
file_name: file_name.clone(),
file_name: file_name.to_string(),
start_pos: BytePos(0),
in_action_file: false,
in_export_decl: false,
Expand All @@ -55,10 +61,40 @@ pub fn server_actions<C: Comments>(
})
}

/// Parses the Server Actions comment for all exported action function names.
///
/// Action names are stored in a leading BlockComment prefixed by
/// `__next_internal_action_entry_do_not_use__`.
pub fn parse_server_actions<C: Comments>(program: &Program, comments: C) -> Option<ActionsMap> {
let byte_pos = match program {
Program::Module(m) => m.span.lo,
Program::Script(s) => s.span.lo,
};
comments.get_leading(byte_pos).and_then(|comments| {
comments.iter().find_map(|c| {
c.text
.split_once("__next_internal_action_entry_do_not_use__")
.and_then(|(_, actions)| match serde_json::from_str(actions) {
Ok(v) => Some(v),
Err(_) => None,
})
})
})
}

/// Serializes the Server Actions into a magic comment prefixed by
/// `__next_internal_action_entry_do_not_use__`.
fn generate_server_actions_comment(actions: ActionsMap) -> String {
format!(
" __next_internal_action_entry_do_not_use__ {} ",
serde_json::to_string(&actions).unwrap()
)
}

struct ServerActions<C: Comments> {
#[allow(unused)]
config: Config,
file_name: FileName,
file_name: String,
comments: C,

start_pos: BytePos,
Expand Down Expand Up @@ -216,7 +252,7 @@ impl<C: Comments> ServerActions<C> {
.cloned()
.map(|id| Some(id.as_arg()))
.collect(),
self.file_name.to_string(),
&self.file_name,
export_name.to_string(),
Some(action_ident.clone()),
);
Expand Down Expand Up @@ -317,7 +353,7 @@ impl<C: Comments> ServerActions<C> {
.cloned()
.map(|id| Some(id.as_arg()))
.collect(),
self.file_name.to_string(),
&self.file_name,
export_name.to_string(),
Some(action_ident.clone()),
);
Expand Down Expand Up @@ -923,8 +959,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let ident = Ident::new(id.0.clone(), DUMMY_SP.with_ctxt(id.1));

if !self.config.is_server {
let action_id =
generate_action_id(self.file_name.to_string(), export_name.to_string());
let action_id = generate_action_id(&self.file_name, export_name);

if export_name == "default" {
let export_expr = ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(
Expand Down Expand Up @@ -973,7 +1008,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
&mut self.annotations,
ident.clone(),
Vec::new(),
self.file_name.to_string(),
&self.file_name,
export_name.to_string(),
None,
);
Expand Down Expand Up @@ -1037,26 +1072,22 @@ impl<C: Comments> VisitMut for ServerActions<C> {
}

if self.has_action {
let actions = if self.in_action_file {
self.exported_idents.iter().map(|e| e.1.clone()).collect()
} else {
self.export_actions.clone()
};
let actions = actions
.into_iter()
.map(|name| (generate_action_id(&self.file_name, &name), name))
.collect::<ActionsMap>();
// Prepend a special comment to the top of the file.
self.comments.add_leading(
self.start_pos,
Comment {
span: DUMMY_SP,
kind: CommentKind::Block,
// Append a list of exported actions.
text: format!(
" __next_internal_action_entry_do_not_use__ {} ",
if self.in_action_file {
self.exported_idents
.iter()
.map(|e| e.1.to_string())
.collect::<Vec<_>>()
.join(",")
} else {
self.export_actions.join(",")
}
)
.into(),
text: generate_server_actions_comment(actions).into(),
},
);

Expand Down Expand Up @@ -1162,7 +1193,7 @@ fn collect_pat_idents(pat: &Pat, closure_idents: &mut Vec<Id>) {
}
}

fn generate_action_id(file_name: String, export_name: String) -> String {
fn generate_action_id(file_name: &str, export_name: &str) -> String {
// Attach a checksum to the action using sha1:
// $$id = sha1('file_name' + ':' + 'export_name');
let mut hasher = Sha1::new();
Expand All @@ -1178,7 +1209,7 @@ fn annotate_ident_as_action(
annotations: &mut Vec<Stmt>,
ident: Ident,
bound: Vec<Option<ExprOrSpread>>,
file_name: String,
file_name: &str,
export_name: String,
maybe_orig_action_ident: Option<Ident>,
) {
Expand All @@ -1188,7 +1219,7 @@ fn annotate_ident_as_action(
// $$id
ExprOrSpread {
spread: None,
expr: Box::new(generate_action_id(file_name, export_name).into()),
expr: Box::new(generate_action_id(file_name, &export_name).into()),
},
// myAction.$$bound = [arg1, arg2, arg3];
// or myAction.$$bound = null; if there are no bound values.
Expand Down
18 changes: 10 additions & 8 deletions packages/next-swc/crates/napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ default = ["rustls-tls"]
# when build (i.e napi --build --features plugin), same for the wasm as well.
# this is due to some of transitive dependencies have features cannot be enabled at the same time
# (i.e wasmer/default vs wasmer/js-default) while cargo merges all the features at once.
plugin = ["turbopack-binding/__swc_core_binding_napi_plugin", "turbopack-binding/__swc_core_binding_napi_plugin_filesystem_cache", "turbopack-binding/__swc_core_binding_napi_plugin_shared_runtime", "next-swc/plugin", "next-core/plugin"]
plugin = [
"turbopack-binding/__swc_core_binding_napi_plugin",
"turbopack-binding/__swc_core_binding_napi_plugin_filesystem_cache",
"turbopack-binding/__swc_core_binding_napi_plugin_shared_runtime",
"next-swc/plugin",
"next-core/plugin",
]
sentry_native_tls = ["sentry", "sentry/native-tls", "native-tls"]
sentry_rustls = ["sentry", "sentry/rustls", "rustls-tls"]

Expand Down Expand Up @@ -41,7 +47,7 @@ napi = { version = "2", default-features = false, features = [
"error_anyhow",
] }
napi-derive = "2"
next-swc = { version = "0.0.0", path = "../core" }
next-swc = { workspace = true }
next-dev = { workspace = true }
next-api = { workspace = true }
next-build = { workspace = true }
Expand All @@ -68,9 +74,7 @@ turbopack-binding = { workspace = true, features = [
] }

[target.'cfg(not(all(target_os = "linux", target_env = "musl", target_arch = "aarch64")))'.dependencies]
turbopack-binding = { workspace = true, features = [
"__turbo_tasks_malloc"
] }
turbopack-binding = { workspace = true, features = ["__turbo_tasks_malloc"] }

# There are few build targets we can't use native-tls which default features rely on,
# allow to specify alternative (rustls) instead via features.
Expand All @@ -89,6 +93,4 @@ serde = "1"
serde_json = "1"
# It is not a mistake this dependency is specified in dep / build-dep both.
shadow-rs = { workspace = true }
turbopack-binding = { workspace = true, features = [
"__turbo_tasks_build"
]}
turbopack-binding = { workspace = true, features = ["__turbo_tasks_build"] }
13 changes: 8 additions & 5 deletions packages/next-swc/crates/next-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ bench = false

[features]
default = ["custom_allocator", "native-tls"]
custom_allocator = ["turbopack-binding/__turbo_tasks_malloc", "turbopack-binding/__turbo_tasks_malloc_custom_allocator"]
custom_allocator = [
"turbopack-binding/__turbo_tasks_malloc",
"turbopack-binding/__turbo_tasks_malloc_custom_allocator",
]
native-tls = ["next-core/native-tls"]
rustls-tls = ["next-core/rustls-tls"]

[dependencies]
anyhow = { workspace = true, features = ["backtrace"] }
futures = { workspace = true }
next-swc = { workspace = true }
indexmap = { workspace = true }
indoc = { workspace = true }
next-core = { workspace = true }
once_cell = { workspace = true }
serde = { workspace = true }
Expand All @@ -37,14 +42,12 @@ turbopack-binding = { workspace = true, features = [
"__turbopack_cli_utils",
"__turbopack_node",
"__turbopack_dev_server",
]}
] }
turbo-tasks = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter", "json"] }

[build-dependencies]
# It is not a mistake this dependency is specified in dep / build-dep both.
shadow-rs = { workspace = true }
turbopack-binding = { workspace = true, features = [
"__turbo_tasks_build"
]}
turbopack-binding = { workspace = true, features = ["__turbo_tasks_build"] }
51 changes: 48 additions & 3 deletions packages/next-swc/crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use turbopack_binding::{
use crate::{
project::Project,
route::{Endpoint, Route, Routes, WrittenEndpoint},
server_actions::create_server_actions_manifest,
};

#[turbo_tasks::value]
Expand Down Expand Up @@ -686,6 +687,26 @@ impl AppEndpoint {
bail!("Entry module must be evaluatable");
};
evaluatable_assets.push(evaluatable);

let (loader, manifest) = create_server_actions_manifest(
app_entry.rsc_entry,
node_root,
&app_entry.pathname,
&app_entry.original_name,
NextRuntime::NodeJs,
Vc::upcast(this.app_project.edge_rsc_module_context()),
Vc::upcast(chunking_context),
this.app_project
.project()
.next_config()
.enable_server_actions(),
)
.await?;
server_assets.push(manifest);
if let Some(loader) = loader {
evaluatable_assets.push(loader);
}

let files = chunking_context.evaluated_chunk_group(
app_entry
.rsc_entry
Expand Down Expand Up @@ -784,6 +805,28 @@ impl AppEndpoint {
}
}
NextRuntime::NodeJs => {
let mut evaluatable_assets =
this.app_project.rsc_runtime_entries().await?.clone_value();

let (loader, manifest) = create_server_actions_manifest(
app_entry.rsc_entry,
node_root,
&app_entry.pathname,
&app_entry.original_name,
NextRuntime::NodeJs,
Vc::upcast(this.app_project.rsc_module_context()),
Vc::upcast(this.app_project.project().rsc_chunking_context()),
this.app_project
.project()
.next_config()
.enable_server_actions(),
)
.await?;
server_assets.push(manifest);
if let Some(loader) = loader {
evaluatable_assets.push(loader);
}

let rsc_chunk = this
.app_project
.project()
Expand All @@ -794,7 +837,7 @@ impl AppEndpoint {
original_name = app_entry.original_name
)),
app_entry.rsc_entry,
this.app_project.rsc_runtime_entries(),
Vc::cell(evaluatable_assets),
);
server_assets.push(rsc_chunk);

Expand All @@ -817,8 +860,10 @@ impl AppEndpoint {
client_assets: Vc::cell(client_assets),
}
}
};
Ok(endpoint_output.cell())
}
.cell();

Ok(endpoint_output)
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod middleware;
mod pages;
pub mod project;
pub mod route;
mod server_actions;
mod versioned_content_map;

// Declare build-time information variables generated in build.rs
Expand Down
Loading

0 comments on commit 40344fa

Please sign in to comment.