Skip to content

Commit

Permalink
import map fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Oct 6, 2023
1 parent c86ea1d commit 2283600
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 113 deletions.
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
18 changes: 8 additions & 10 deletions test/development/basic/next-rs-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ describe('next.rs api', () => {
'/api/edge',
'/api/nodejs',
'/app',
// TODO app edge pages are not supported yet
// '/app-edge',
'/app-edge',
'/app-nodejs',
'/page-edge',
'/page-nodejs',
Expand Down Expand Up @@ -241,14 +240,13 @@ describe('next.rs api', () => {
runtime: 'nodejs',
config: {},
},
// TODO app edge pages are not supported yet
// {
// name: 'app edge page',
// path: '/app-edge',
// type: 'app-page',
// runtime: 'edge',
// config: {},
// },
{
name: 'app edge page',
path: '/app-edge',
type: 'app-page',
runtime: 'edge',
config: {},
},
{
name: 'app Node.js page',
path: '/app-nodejs',
Expand Down

0 comments on commit 2283600

Please sign in to comment.