Skip to content

Commit

Permalink
fix(turbopack): always alias server-only and client-only (#56760)
Browse files Browse the repository at this point in the history
Closes WEB-1773
  • Loading branch information
ForsakenHarmony committed Oct 12, 2023
1 parent 3da643a commit 230099b
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions packages/next-swc/crates/next-core/src/next_import_map.rs
@@ -1,6 +1,7 @@
use std::collections::{BTreeMap, HashMap};

use anyhow::{Context, Result};
use indexmap::{indexmap, IndexMap};
use turbo_tasks::{Value, Vc};
use turbopack_binding::{
turbo::tasks_fs::{glob::Glob, FileSystem, FileSystemPath},
Expand Down Expand Up @@ -94,14 +95,7 @@ pub async fn get_next_client_import_map(
} else {
""
};
import_map.insert_exact_alias(
"server-only",
request_to_import_mapping(app_dir, "next/dist/compiled/server-only"),
);
import_map.insert_exact_alias(
"client-only",
request_to_import_mapping(app_dir, "next/dist/compiled/client-only"),
);

import_map.insert_exact_alias(
"react",
request_to_import_mapping(
Expand Down Expand Up @@ -154,6 +148,18 @@ pub async fn get_next_client_import_map(
ClientContextType::Other => {}
}

// see https://github.com/vercel/next.js/blob/8013ef7372fc545d49dbd060461224ceb563b454/packages/next/src/build/webpack-config.ts#L1449-L1531
insert_exact_alias_map(
&mut import_map,
project_path,
indexmap! {
"server-only" => "next/dist/compiled/server-only/index".to_string(),
"client-only" => "next/dist/compiled/client-only/index".to_string(),
"next/dist/compiled/server-only" => "next/dist/compiled/server-only/index".to_string(),
"next/dist/compiled/client-only" => "next/dist/compiled/client-only/index".to_string(),
},
);

match ty.into_value() {
ClientContextType::Pages { .. }
| ClientContextType::App { .. }
Expand Down Expand Up @@ -829,6 +835,30 @@ async fn insert_next_server_special_aliases(
(_, ServerContextType::Middleware) => {}
}

// see https://github.com/vercel/next.js/blob/8013ef7372fc545d49dbd060461224ceb563b454/packages/next/src/build/webpack-config.ts#L1449-L1531
insert_exact_alias_map(
import_map,
project_path,
indexmap! {
"server-only" => "next/dist/compiled/server-only/empty".to_string(),
"client-only" => "next/dist/compiled/client-only/error".to_string(),
"next/dist/compiled/server-only" => "next/dist/compiled/server-only/empty".to_string(),
"next/dist/compiled/client-only" => "next/dist/compiled/client-only/error".to_string(),
},
);

if ty == ServerContextType::Middleware {
insert_exact_alias_map(
import_map,
project_path,
indexmap! {
"client-only" => "next/dist/compiled/client-only/index".to_string(),
"next/dist/compiled/client-only" => "next/dist/compiled/client-only/index".to_string(),
"next/dist/compiled/client-only/error" => "next/dist/compiled/client-only/index".to_string(),
},
);
}

import_map.insert_exact_alias(
"@vercel/og",
external_if_node(
Expand Down Expand Up @@ -1022,6 +1052,16 @@ fn export_value_to_import_mapping(
}
}

fn insert_exact_alias_map(
import_map: &mut ImportMap,
project_path: Vc<FileSystemPath>,
map: IndexMap<&'static str, String>,
) {
for (pattern, request) in map {
import_map.insert_exact_alias(pattern, request_to_import_mapping(project_path, &request));
}
}

/// Inserts an alias to an alternative of import mappings into an import map.
fn insert_alias_to_alternatives<'a>(
import_map: &mut ImportMap,
Expand Down

0 comments on commit 230099b

Please sign in to comment.