Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(turbopack): add support for edge app pages #56426

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use turbopack_binding::{
core::{
asset::{Asset, AssetContent},
context::AssetContext,
module::Module,
reference_type::ReferenceType,
source::Source,
virtual_source::VirtualSource,
Expand Down Expand Up @@ -101,14 +102,14 @@ pub async fn get_app_page_entry(
let file = File::from(result.build());
let source = VirtualSource::new(source.ident().path(), AssetContent::file(file.into()));

let rsc_entry = context.process(
let mut rsc_entry = context.process(
Vc::upcast(source),
Value::new(ReferenceType::Internal(Vc::cell(inner_assets))),
);

if is_edge {
todo!("edge pages are not supported yet")
}
rsc_entry = wrap_edge_entry(context, project_root, rsc_entry).await?;
};

let Some(rsc_entry) =
Vc::try_resolve_downcast::<Box<dyn EcmascriptChunkPlaceable>>(rsc_entry).await?
Expand All @@ -124,3 +125,30 @@ pub async fn get_app_page_entry(
}
.cell())
}

async fn wrap_edge_entry(
context: Vc<ModuleAssetContext>,
project_root: Vc<FileSystemPath>,
entry: Vc<Box<dyn Module>>,
) -> Result<Vc<Box<dyn Module>>> {
const INNER: &str = "INNER_RSC_ENTRY";

let source = load_next_js_template(
"edge-app-route.js",
project_root,
indexmap! {
"VAR_USERLAND" => INNER.to_string(),
},
indexmap! {},
)
.await?;

let inner_assets = indexmap! {
INNER.to_string() => entry
};

Ok(context.process(
Vc::upcast(source),
Value::new(ReferenceType::Internal(Vc::cell(inner_assets))),
))
}
255 changes: 152 additions & 103 deletions packages/next-swc/crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ pub async fn get_next_client_import_map(
);
import_map.insert_wildcard_alias(
"react-server-dom-webpack/",
request_to_import_mapping(app_dir, "react-server-dom-turbopack/*"),
);
import_map.insert_wildcard_alias(
"react-server-dom-turbopack/",
request_to_import_mapping(
app_dir,
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/*"),
Expand Down Expand Up @@ -247,15 +251,6 @@ pub async fn get_next_server_import_map(

let ty = ty.into_value();

insert_next_server_special_aliases(
&mut import_map,
project_path,
ty,
mode,
NextRuntime::NodeJs,
next_config,
)
.await?;
let external: Vc<ImportMapping> = ImportMapping::External(None).cell();

import_map.insert_exact_alias("next/dist/server/require-hook", external);
Expand All @@ -273,6 +268,12 @@ pub async fn get_next_server_import_map(
ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. }
| ServerContextType::AppRoute { .. } => {
let react_flavor = if *next_config.enable_server_actions().await? {
"-experimental"
} else {
""
};

import_map.insert_exact_alias(
"private-next-rsc-action-proxy",
request_to_import_mapping(
Expand Down Expand Up @@ -302,14 +303,48 @@ pub async fn get_next_server_import_map(
"next/dynamic",
request_to_import_mapping(project_path, "next/dist/shared/lib/app-dynamic"),
);
import_map.insert_exact_alias(
"react-server-dom-webpack/",
ImportMapping::External(Some("react-server-dom-turbopack".into())).cell(),

let mapping = request_to_import_mapping(
project_path,
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/client"),
);
import_map.insert_exact_alias("react-server-dom-webpack/client", mapping);
import_map.insert_exact_alias("react-server-dom-turbopack/client", mapping);

let mapping = request_to_import_mapping(
project_path,
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/client.edge"),
);
import_map.insert_exact_alias("react-server-dom-webpack/client.edge", mapping);
import_map.insert_exact_alias("react-server-dom-turbopack/client.edge", mapping);

let mapping = request_to_import_mapping(
project_path,
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/server.edge"),
);
import_map.insert_exact_alias("react-server-dom-webpack/server.edge", mapping);
import_map.insert_exact_alias("react-server-dom-turbopack/server.edge", mapping);

let mapping = request_to_import_mapping(
project_path,
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/server.node"),
);
import_map.insert_exact_alias("react-server-dom-webpack/server.node", mapping);
import_map.insert_exact_alias("react-server-dom-turbopack/server.node", mapping);
}
ServerContextType::Middleware => {}
}

insert_next_server_special_aliases(
&mut import_map,
project_path,
ty,
mode,
NextRuntime::NodeJs,
next_config,
)
.await?;

Ok(import_map.cell())
}

Expand Down Expand Up @@ -342,22 +377,17 @@ pub async fn get_next_edge_import_map(
.await?;

let ty = ty.into_value();

insert_next_server_special_aliases(
&mut import_map,
project_path,
ty,
mode,
NextRuntime::Edge,
next_config,
)
.await?;

match ty {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => {}
ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. }
| ServerContextType::AppRoute { .. } => {
let react_flavor = if *next_config.enable_server_actions().await? {
"-experimental"
} else {
""
};

import_map.insert_exact_alias(
"next/head",
request_to_import_mapping(project_path, "next/dist/client/components/noop-head"),
Expand All @@ -366,10 +396,48 @@ pub async fn get_next_edge_import_map(
"next/dynamic",
request_to_import_mapping(project_path, "next/dist/shared/lib/app-dynamic"),
);

let mapping = request_to_import_mapping(
project_path,
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/client"),
);
import_map.insert_exact_alias("react-server-dom-webpack/client", mapping);
import_map.insert_exact_alias("react-server-dom-turbopack/client", mapping);

let mapping = request_to_import_mapping(
project_path,
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/client.edge"),
);
import_map.insert_exact_alias("react-server-dom-webpack/client.edge", mapping);
import_map.insert_exact_alias("react-server-dom-turbopack/client.edge", mapping);

let mapping = request_to_import_mapping(
project_path,
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/server.edge"),
);
import_map.insert_exact_alias("react-server-dom-webpack/server.edge", mapping);
import_map.insert_exact_alias("react-server-dom-turbopack/server.edge", mapping);

let mapping = request_to_import_mapping(
project_path,
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/server.node"),
);
import_map.insert_exact_alias("react-server-dom-webpack/server.node", mapping);
import_map.insert_exact_alias("react-server-dom-turbopack/server.node", mapping);
}
ServerContextType::Middleware => {}
}

insert_next_server_special_aliases(
&mut import_map,
project_path,
ty,
mode,
NextRuntime::Edge,
next_config,
)
.await?;

Ok(import_map.cell())
}

Expand Down Expand Up @@ -552,49 +620,28 @@ async fn insert_next_server_special_aliases(
},
),
);
import_map.insert_exact_alias(
"react-server-dom-webpack/client.edge",
request_to_import_mapping(
app_dir,
match (runtime, server_actions) {
(NextRuntime::Edge, true) => {
"next/dist/compiled/react-server-dom-turbopack-experimental/client.edge"
}
(NextRuntime::Edge, false) => {
"next/dist/compiled/react-server-dom-turbopack/client.edge"
}
// When we access the runtime we still use the webpack name. The runtime
// itself will substitute in the turbopack variant
(NextRuntime::NodeJs, _) => {
"next/dist/server/future/route-modules/app-page/vendored/ssr/\
react-server-dom-turbopack-client-edge"
}
},
),
);
// some code also imports react-server-dom-webpack/client on the server
// it should never run so it's fine to just point it to the same place as
// react-server-dom-webpack/client.edge
import_map.insert_exact_alias(
"react-server-dom-webpack/client",
request_to_import_mapping(
app_dir,
match (runtime, server_actions) {
(NextRuntime::Edge, true) => {
"next/dist/compiled/react-server-dom-turbopack-experimental/client.edge"
}
(NextRuntime::Edge, false) => {
"next/dist/compiled/react-server-dom-turbopack/client.edge"
}
// When we access the runtime we still use the webpack name. The runtime
// itself will substitute in the turbopack variant
(NextRuntime::NodeJs, _) => {
"next/dist/server/future/route-modules/app-page/vendored/ssr/\
react-server-dom-turbopack-client-edge"
}
},
),

let mapping = request_to_import_mapping(
app_dir,
match (runtime, server_actions) {
(NextRuntime::Edge, true) => {
"next/dist/compiled/react-server-dom-turbopack-experimental/client.edge"
}
(NextRuntime::Edge, false) => {
"next/dist/compiled/react-server-dom-turbopack/client.edge"
}
// When we access the runtime we still use the webpack name. The runtime
// itself will substitute in the turbopack variant
(NextRuntime::NodeJs, _) => {
"next/dist/server/future/route-modules/app-page/vendored/ssr/\
react-server-dom-turbopack-client-edge"
}
},
);
import_map.insert_exact_alias("react-server-dom-webpack/client.edge", mapping);
import_map.insert_exact_alias("react-server-dom-turbopack/client.edge", mapping);

// import_map.insert_exact_alias("react-server-dom-turbopack/client", mapping);
// not essential but we're providing this alias for people who might use it.
// A note here is that this will point toward the ReactDOMServer on the SSR
// layer TODO: add the rests
Expand All @@ -614,6 +661,7 @@ async fn insert_next_server_special_aliases(
},
),
);

import_map.insert_exact_alias(
"react-dom/server.edge",
request_to_import_mapping(
Expand Down Expand Up @@ -717,46 +765,47 @@ async fn insert_next_server_special_aliases(
},
),
);
import_map.insert_exact_alias(
"react-server-dom-webpack/server.edge",
request_to_import_mapping(
app_dir,
match (runtime, server_actions) {
(NextRuntime::Edge, true) => {
"next/dist/compiled/react-server-dom-turbopack-experimental/server.edge"
}
(NextRuntime::Edge, false) => {
"next/dist/compiled/react-server-dom-turbopack/server.edge"
}
// When we access the runtime we still use the webpack name. The runtime
// itself will substitute in the turbopack variant
(NextRuntime::NodeJs, _) => {
"next/dist/server/future/route-modules/app-page/vendored/rsc/\
react-server-dom-turbopack-server-edge"
}
},
),

let mapping = request_to_import_mapping(
app_dir,
match (runtime, server_actions) {
(NextRuntime::Edge, true) => {
"next/dist/compiled/react-server-dom-turbopack-experimental/server.edge"
}
(NextRuntime::Edge, false) => {
"next/dist/compiled/react-server-dom-turbopack/server.edge"
}
// When we access the runtime we still use the webpack name. The runtime
// itself will substitute in the turbopack variant
(NextRuntime::NodeJs, _) => {
"next/dist/server/future/route-modules/app-page/vendored/rsc/\
react-server-dom-turbopack-server-edge"
}
},
);
import_map.insert_exact_alias(
"react-server-dom-webpack/server.node",
request_to_import_mapping(
app_dir,
match (runtime, server_actions) {
(NextRuntime::Edge, true) => {
"next/dist/compiled/react-server-dom-turbopack-experimental/server.node"
}
(NextRuntime::Edge, false) => {
"next/dist/compiled/react-server-dom-turbopack/server.node"
}
// When we access the runtime we still use the webpack name. The runtime
// itself will substitute in the turbopack variant
(NextRuntime::NodeJs, _) => {
"next/dist/server/future/route-modules/app-page/vendored/rsc/\
react-server-dom-turbopack-server-node"
}
},
),
import_map.insert_exact_alias("react-server-dom-webpack/server.edge", mapping);
import_map.insert_exact_alias("react-server-dom-turbopack/server.edge", mapping);

let mapping = request_to_import_mapping(
app_dir,
match (runtime, server_actions) {
(NextRuntime::Edge, true) => {
"next/dist/compiled/react-server-dom-turbopack-experimental/server.node"
}
(NextRuntime::Edge, false) => {
"next/dist/compiled/react-server-dom-turbopack/server.node"
}
// When we access the runtime we still use the webpack name. The runtime
// itself will substitute in the turbopack variant
(NextRuntime::NodeJs, _) => {
"next/dist/server/future/route-modules/app-page/vendored/rsc/\
react-server-dom-turbopack-server-node"
}
},
);
import_map.insert_exact_alias("react-server-dom-webpack/server.node", mapping);
import_map.insert_exact_alias("react-server-dom-turbopack/server.node", mapping);

// not essential but we're providing this alias for people who might use it.
// A note here is that this will point toward the ReactDOMServer on the SSR
// layer TODO: add the rests
Expand Down