Skip to content

Commit 2283600

Browse files
import map fixes
1 parent c86ea1d commit 2283600

File tree

2 files changed

+160
-113
lines changed

2 files changed

+160
-113
lines changed

packages/next-swc/crates/next-core/src/next_import_map.rs

Lines changed: 152 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ pub async fn get_next_client_import_map(
132132
);
133133
import_map.insert_wildcard_alias(
134134
"react-server-dom-webpack/",
135+
request_to_import_mapping(app_dir, "react-server-dom-turbopack/*"),
136+
);
137+
import_map.insert_wildcard_alias(
138+
"react-server-dom-turbopack/",
135139
request_to_import_mapping(
136140
app_dir,
137141
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/*"),
@@ -247,15 +251,6 @@ pub async fn get_next_server_import_map(
247251

248252
let ty = ty.into_value();
249253

250-
insert_next_server_special_aliases(
251-
&mut import_map,
252-
project_path,
253-
ty,
254-
mode,
255-
NextRuntime::NodeJs,
256-
next_config,
257-
)
258-
.await?;
259254
let external: Vc<ImportMapping> = ImportMapping::External(None).cell();
260255

261256
import_map.insert_exact_alias("next/dist/server/require-hook", external);
@@ -273,6 +268,12 @@ pub async fn get_next_server_import_map(
273268
ServerContextType::AppSSR { .. }
274269
| ServerContextType::AppRSC { .. }
275270
| ServerContextType::AppRoute { .. } => {
271+
let react_flavor = if *next_config.enable_server_actions().await? {
272+
"-experimental"
273+
} else {
274+
""
275+
};
276+
276277
import_map.insert_exact_alias(
277278
"private-next-rsc-action-proxy",
278279
request_to_import_mapping(
@@ -302,14 +303,48 @@ pub async fn get_next_server_import_map(
302303
"next/dynamic",
303304
request_to_import_mapping(project_path, "next/dist/shared/lib/app-dynamic"),
304305
);
305-
import_map.insert_exact_alias(
306-
"react-server-dom-webpack/",
307-
ImportMapping::External(Some("react-server-dom-turbopack".into())).cell(),
306+
307+
let mapping = request_to_import_mapping(
308+
project_path,
309+
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/client"),
310+
);
311+
import_map.insert_exact_alias("react-server-dom-webpack/client", mapping);
312+
import_map.insert_exact_alias("react-server-dom-turbopack/client", mapping);
313+
314+
let mapping = request_to_import_mapping(
315+
project_path,
316+
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/client.edge"),
317+
);
318+
import_map.insert_exact_alias("react-server-dom-webpack/client.edge", mapping);
319+
import_map.insert_exact_alias("react-server-dom-turbopack/client.edge", mapping);
320+
321+
let mapping = request_to_import_mapping(
322+
project_path,
323+
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/server.edge"),
308324
);
325+
import_map.insert_exact_alias("react-server-dom-webpack/server.edge", mapping);
326+
import_map.insert_exact_alias("react-server-dom-turbopack/server.edge", mapping);
327+
328+
let mapping = request_to_import_mapping(
329+
project_path,
330+
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/server.node"),
331+
);
332+
import_map.insert_exact_alias("react-server-dom-webpack/server.node", mapping);
333+
import_map.insert_exact_alias("react-server-dom-turbopack/server.node", mapping);
309334
}
310335
ServerContextType::Middleware => {}
311336
}
312337

338+
insert_next_server_special_aliases(
339+
&mut import_map,
340+
project_path,
341+
ty,
342+
mode,
343+
NextRuntime::NodeJs,
344+
next_config,
345+
)
346+
.await?;
347+
313348
Ok(import_map.cell())
314349
}
315350

@@ -342,22 +377,17 @@ pub async fn get_next_edge_import_map(
342377
.await?;
343378

344379
let ty = ty.into_value();
345-
346-
insert_next_server_special_aliases(
347-
&mut import_map,
348-
project_path,
349-
ty,
350-
mode,
351-
NextRuntime::Edge,
352-
next_config,
353-
)
354-
.await?;
355-
356380
match ty {
357381
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => {}
358382
ServerContextType::AppSSR { .. }
359383
| ServerContextType::AppRSC { .. }
360384
| ServerContextType::AppRoute { .. } => {
385+
let react_flavor = if *next_config.enable_server_actions().await? {
386+
"-experimental"
387+
} else {
388+
""
389+
};
390+
361391
import_map.insert_exact_alias(
362392
"next/head",
363393
request_to_import_mapping(project_path, "next/dist/client/components/noop-head"),
@@ -366,10 +396,48 @@ pub async fn get_next_edge_import_map(
366396
"next/dynamic",
367397
request_to_import_mapping(project_path, "next/dist/shared/lib/app-dynamic"),
368398
);
399+
400+
let mapping = request_to_import_mapping(
401+
project_path,
402+
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/client"),
403+
);
404+
import_map.insert_exact_alias("react-server-dom-webpack/client", mapping);
405+
import_map.insert_exact_alias("react-server-dom-turbopack/client", mapping);
406+
407+
let mapping = request_to_import_mapping(
408+
project_path,
409+
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/client.edge"),
410+
);
411+
import_map.insert_exact_alias("react-server-dom-webpack/client.edge", mapping);
412+
import_map.insert_exact_alias("react-server-dom-turbopack/client.edge", mapping);
413+
414+
let mapping = request_to_import_mapping(
415+
project_path,
416+
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/server.edge"),
417+
);
418+
import_map.insert_exact_alias("react-server-dom-webpack/server.edge", mapping);
419+
import_map.insert_exact_alias("react-server-dom-turbopack/server.edge", mapping);
420+
421+
let mapping = request_to_import_mapping(
422+
project_path,
423+
&format!("next/dist/compiled/react-server-dom-turbopack{react_flavor}/server.node"),
424+
);
425+
import_map.insert_exact_alias("react-server-dom-webpack/server.node", mapping);
426+
import_map.insert_exact_alias("react-server-dom-turbopack/server.node", mapping);
369427
}
370428
ServerContextType::Middleware => {}
371429
}
372430

431+
insert_next_server_special_aliases(
432+
&mut import_map,
433+
project_path,
434+
ty,
435+
mode,
436+
NextRuntime::Edge,
437+
next_config,
438+
)
439+
.await?;
440+
373441
Ok(import_map.cell())
374442
}
375443

@@ -552,49 +620,28 @@ async fn insert_next_server_special_aliases(
552620
},
553621
),
554622
);
555-
import_map.insert_exact_alias(
556-
"react-server-dom-webpack/client.edge",
557-
request_to_import_mapping(
558-
app_dir,
559-
match (runtime, server_actions) {
560-
(NextRuntime::Edge, true) => {
561-
"next/dist/compiled/react-server-dom-turbopack-experimental/client.edge"
562-
}
563-
(NextRuntime::Edge, false) => {
564-
"next/dist/compiled/react-server-dom-turbopack/client.edge"
565-
}
566-
// When we access the runtime we still use the webpack name. The runtime
567-
// itself will substitute in the turbopack variant
568-
(NextRuntime::NodeJs, _) => {
569-
"next/dist/server/future/route-modules/app-page/vendored/ssr/\
570-
react-server-dom-turbopack-client-edge"
571-
}
572-
},
573-
),
574-
);
575-
// some code also imports react-server-dom-webpack/client on the server
576-
// it should never run so it's fine to just point it to the same place as
577-
// react-server-dom-webpack/client.edge
578-
import_map.insert_exact_alias(
579-
"react-server-dom-webpack/client",
580-
request_to_import_mapping(
581-
app_dir,
582-
match (runtime, server_actions) {
583-
(NextRuntime::Edge, true) => {
584-
"next/dist/compiled/react-server-dom-turbopack-experimental/client.edge"
585-
}
586-
(NextRuntime::Edge, false) => {
587-
"next/dist/compiled/react-server-dom-turbopack/client.edge"
588-
}
589-
// When we access the runtime we still use the webpack name. The runtime
590-
// itself will substitute in the turbopack variant
591-
(NextRuntime::NodeJs, _) => {
592-
"next/dist/server/future/route-modules/app-page/vendored/ssr/\
593-
react-server-dom-turbopack-client-edge"
594-
}
595-
},
596-
),
623+
624+
let mapping = request_to_import_mapping(
625+
app_dir,
626+
match (runtime, server_actions) {
627+
(NextRuntime::Edge, true) => {
628+
"next/dist/compiled/react-server-dom-turbopack-experimental/client.edge"
629+
}
630+
(NextRuntime::Edge, false) => {
631+
"next/dist/compiled/react-server-dom-turbopack/client.edge"
632+
}
633+
// When we access the runtime we still use the webpack name. The runtime
634+
// itself will substitute in the turbopack variant
635+
(NextRuntime::NodeJs, _) => {
636+
"next/dist/server/future/route-modules/app-page/vendored/ssr/\
637+
react-server-dom-turbopack-client-edge"
638+
}
639+
},
597640
);
641+
import_map.insert_exact_alias("react-server-dom-webpack/client.edge", mapping);
642+
import_map.insert_exact_alias("react-server-dom-turbopack/client.edge", mapping);
643+
644+
// import_map.insert_exact_alias("react-server-dom-turbopack/client", mapping);
598645
// not essential but we're providing this alias for people who might use it.
599646
// A note here is that this will point toward the ReactDOMServer on the SSR
600647
// layer TODO: add the rests
@@ -614,6 +661,7 @@ async fn insert_next_server_special_aliases(
614661
},
615662
),
616663
);
664+
617665
import_map.insert_exact_alias(
618666
"react-dom/server.edge",
619667
request_to_import_mapping(
@@ -717,46 +765,47 @@ async fn insert_next_server_special_aliases(
717765
},
718766
),
719767
);
720-
import_map.insert_exact_alias(
721-
"react-server-dom-webpack/server.edge",
722-
request_to_import_mapping(
723-
app_dir,
724-
match (runtime, server_actions) {
725-
(NextRuntime::Edge, true) => {
726-
"next/dist/compiled/react-server-dom-turbopack-experimental/server.edge"
727-
}
728-
(NextRuntime::Edge, false) => {
729-
"next/dist/compiled/react-server-dom-turbopack/server.edge"
730-
}
731-
// When we access the runtime we still use the webpack name. The runtime
732-
// itself will substitute in the turbopack variant
733-
(NextRuntime::NodeJs, _) => {
734-
"next/dist/server/future/route-modules/app-page/vendored/rsc/\
735-
react-server-dom-turbopack-server-edge"
736-
}
737-
},
738-
),
768+
769+
let mapping = request_to_import_mapping(
770+
app_dir,
771+
match (runtime, server_actions) {
772+
(NextRuntime::Edge, true) => {
773+
"next/dist/compiled/react-server-dom-turbopack-experimental/server.edge"
774+
}
775+
(NextRuntime::Edge, false) => {
776+
"next/dist/compiled/react-server-dom-turbopack/server.edge"
777+
}
778+
// When we access the runtime we still use the webpack name. The runtime
779+
// itself will substitute in the turbopack variant
780+
(NextRuntime::NodeJs, _) => {
781+
"next/dist/server/future/route-modules/app-page/vendored/rsc/\
782+
react-server-dom-turbopack-server-edge"
783+
}
784+
},
739785
);
740-
import_map.insert_exact_alias(
741-
"react-server-dom-webpack/server.node",
742-
request_to_import_mapping(
743-
app_dir,
744-
match (runtime, server_actions) {
745-
(NextRuntime::Edge, true) => {
746-
"next/dist/compiled/react-server-dom-turbopack-experimental/server.node"
747-
}
748-
(NextRuntime::Edge, false) => {
749-
"next/dist/compiled/react-server-dom-turbopack/server.node"
750-
}
751-
// When we access the runtime we still use the webpack name. The runtime
752-
// itself will substitute in the turbopack variant
753-
(NextRuntime::NodeJs, _) => {
754-
"next/dist/server/future/route-modules/app-page/vendored/rsc/\
755-
react-server-dom-turbopack-server-node"
756-
}
757-
},
758-
),
786+
import_map.insert_exact_alias("react-server-dom-webpack/server.edge", mapping);
787+
import_map.insert_exact_alias("react-server-dom-turbopack/server.edge", mapping);
788+
789+
let mapping = request_to_import_mapping(
790+
app_dir,
791+
match (runtime, server_actions) {
792+
(NextRuntime::Edge, true) => {
793+
"next/dist/compiled/react-server-dom-turbopack-experimental/server.node"
794+
}
795+
(NextRuntime::Edge, false) => {
796+
"next/dist/compiled/react-server-dom-turbopack/server.node"
797+
}
798+
// When we access the runtime we still use the webpack name. The runtime
799+
// itself will substitute in the turbopack variant
800+
(NextRuntime::NodeJs, _) => {
801+
"next/dist/server/future/route-modules/app-page/vendored/rsc/\
802+
react-server-dom-turbopack-server-node"
803+
}
804+
},
759805
);
806+
import_map.insert_exact_alias("react-server-dom-webpack/server.node", mapping);
807+
import_map.insert_exact_alias("react-server-dom-turbopack/server.node", mapping);
808+
760809
// not essential but we're providing this alias for people who might use it.
761810
// A note here is that this will point toward the ReactDOMServer on the SSR
762811
// layer TODO: add the rests

test/development/basic/next-rs-api.test.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ describe('next.rs api', () => {
204204
'/api/edge',
205205
'/api/nodejs',
206206
'/app',
207-
// TODO app edge pages are not supported yet
208-
// '/app-edge',
207+
'/app-edge',
209208
'/app-nodejs',
210209
'/page-edge',
211210
'/page-nodejs',
@@ -241,14 +240,13 @@ describe('next.rs api', () => {
241240
runtime: 'nodejs',
242241
config: {},
243242
},
244-
// TODO app edge pages are not supported yet
245-
// {
246-
// name: 'app edge page',
247-
// path: '/app-edge',
248-
// type: 'app-page',
249-
// runtime: 'edge',
250-
// config: {},
251-
// },
243+
{
244+
name: 'app edge page',
245+
path: '/app-edge',
246+
type: 'app-page',
247+
runtime: 'edge',
248+
config: {},
249+
},
252250
{
253251
name: 'app Node.js page',
254252
path: '/app-nodejs',

0 commit comments

Comments
 (0)