@@ -365,6 +365,52 @@ pub async fn get_next_edge_import_map(
365
365
) -> Result < Vc < ImportMap > > {
366
366
let mut import_map = ImportMap :: empty ( ) ;
367
367
368
+ // https://github.com/vercel/next.js/blob/786ef25e529e1fb2dda398aebd02ccbc8d0fb673/packages/next/src/build/webpack-config.ts#L815-L861
369
+
370
+ // Alias next/dist imports to next/dist/esm assets
371
+ insert_wildcard_alias_map (
372
+ & mut import_map,
373
+ project_path,
374
+ indexmap ! {
375
+ "next/dist/build/" => "next/dist/esm/build/*" . to_string( ) ,
376
+ "next/dist/client/" => "next/dist/esm/client/*" . to_string( ) ,
377
+ "next/dist/shared/" => "next/dist/esm/shared/*" . to_string( ) ,
378
+ "next/dist/pages/" => "next/dist/esm/pages/*" . to_string( ) ,
379
+ "next/dist/lib/" => "next/dist/esm/lib/*" . to_string( ) ,
380
+ "next/dist/server/" => "next/dist/esm/server/*" . to_string( ) ,
381
+ } ,
382
+ ) ;
383
+
384
+ // Alias the usage of next public APIs
385
+ insert_exact_alias_map (
386
+ & mut import_map,
387
+ project_path,
388
+ indexmap ! {
389
+ "next/app" => "next/dist/esm/pages/_app" . to_string( ) ,
390
+ "next/document" => "next/dist/esm/pages/_document" . to_string( ) ,
391
+ "next/dynamic" => "next/dist/esm/shared/lib/dynamic" . to_string( ) ,
392
+ "next/head" => "next/dist/esm/shared/lib/head" . to_string( ) ,
393
+ "next/headers" => "next/dist/esm/client/components/headers" . to_string( ) ,
394
+ "next/image" => "next/dist/esm/shared/lib/image-external" . to_string( ) ,
395
+ "next/link" => "next/dist/esm/client/link" . to_string( ) ,
396
+ "next/navigation" => "next/dist/esm/client/components/navigation" . to_string( ) ,
397
+ "next/router" => "next/dist/esm/client/router" . to_string( ) ,
398
+ "next/script" => "next/dist/esm/client/script" . to_string( ) ,
399
+ "next/server" => "next/dist/esm/server/web/exports/index" . to_string( ) ,
400
+
401
+ "next/dist/client/components/headers" => "next/dist/esm/client/components/headers" . to_string( ) ,
402
+ "next/dist/client/components/navigation" => "next/dist/esm/client/components/navigation" . to_string( ) ,
403
+ "next/dist/client/link" => "next/dist/esm/client/link" . to_string( ) ,
404
+ "next/dist/client/router" => "next/dist/esm/client/router" . to_string( ) ,
405
+ "next/dist/client/script" => "next/dist/esm/client/script" . to_string( ) ,
406
+ "next/dist/pages/_app" => "next/dist/esm/pages/_app" . to_string( ) ,
407
+ "next/dist/pages/_document" => "next/dist/esm/pages/_document" . to_string( ) ,
408
+ "next/dist/shared/lib/dynamic" => "next/dist/esm/shared/lib/dynamic" . to_string( ) ,
409
+ "next/dist/shared/lib/head" => "next/dist/esm/shared/lib/head" . to_string( ) ,
410
+ "next/dist/shared/lib/image-external" => "next/dist/esm/shared/lib/image-external" . to_string( ) ,
411
+ } ,
412
+ ) ;
413
+
368
414
insert_next_shared_aliases (
369
415
& mut import_map,
370
416
project_path,
@@ -828,17 +874,41 @@ async fn insert_next_server_special_aliases(
828
874
}
829
875
830
876
// see https://github.com/vercel/next.js/blob/8013ef7372fc545d49dbd060461224ceb563b454/packages/next/src/build/webpack-config.ts#L1449-L1531
831
- insert_exact_alias_map (
832
- import_map,
833
- project_path,
834
- indexmap ! {
835
- "server-only" => "next/dist/compiled/server-only/empty" . to_string( ) ,
836
- "client-only" => "next/dist/compiled/client-only/index" . to_string( ) ,
837
- "next/dist/compiled/server-only" => "next/dist/compiled/server-only/empty" . to_string( ) ,
838
- "next/dist/compiled/client-only" => "next/dist/compiled/client-only/index" . to_string( ) ,
839
- } ,
840
- ) ;
877
+ match ty {
878
+ ServerContextType :: Pages { .. }
879
+ | ServerContextType :: PagesData { .. }
880
+ | ServerContextType :: AppSSR { .. } => {
881
+ insert_exact_alias_map (
882
+ import_map,
883
+ project_path,
884
+ indexmap ! {
885
+ "server-only" => "next/dist/compiled/server-only/index" . to_string( ) ,
886
+ "client-only" => "next/dist/compiled/client-only/index" . to_string( ) ,
887
+ "next/dist/compiled/server-only" => "next/dist/compiled/server-only/index" . to_string( ) ,
888
+ "next/dist/compiled/client-only" => "next/dist/compiled/client-only/index" . to_string( ) ,
889
+ } ,
890
+ ) ;
891
+ }
892
+ // TODO: should include `ServerContextType::PagesApi` routes, but that type doesn't exist.
893
+ ServerContextType :: AppRSC { .. }
894
+ | ServerContextType :: AppRoute { .. }
895
+ | ServerContextType :: Middleware => {
896
+ insert_exact_alias_map (
897
+ import_map,
898
+ project_path,
899
+ indexmap ! {
900
+ "server-only" => "next/dist/compiled/server-only/empty" . to_string( ) ,
901
+ "client-only" => "next/dist/compiled/client-only/error" . to_string( ) ,
902
+ "next/dist/compiled/server-only" => "next/dist/compiled/server-only/empty" . to_string( ) ,
903
+ "next/dist/compiled/client-only" => "next/dist/compiled/client-only/error" . to_string( ) ,
904
+ } ,
905
+ ) ;
906
+ }
907
+ }
841
908
909
+ // Potential the bundle introduced into middleware and api can be poisoned by
910
+ // client-only but not being used, so we disabled the `client-only` erroring
911
+ // on these layers. `server-only` is still available.
842
912
if ty == ServerContextType :: Middleware {
843
913
insert_exact_alias_map (
844
914
import_map,
@@ -1054,6 +1124,17 @@ fn insert_exact_alias_map(
1054
1124
}
1055
1125
}
1056
1126
1127
+ fn insert_wildcard_alias_map (
1128
+ import_map : & mut ImportMap ,
1129
+ project_path : Vc < FileSystemPath > ,
1130
+ map : IndexMap < & ' static str , String > ,
1131
+ ) {
1132
+ for ( pattern, request) in map {
1133
+ import_map
1134
+ . insert_wildcard_alias ( pattern, request_to_import_mapping ( project_path, & request) ) ;
1135
+ }
1136
+ }
1137
+
1057
1138
/// Inserts an alias to an alternative of import mappings into an import map.
1058
1139
fn insert_alias_to_alternatives < ' a > (
1059
1140
import_map : & mut ImportMap ,
0 commit comments