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

fix(turbopack): always alias server-only and client-only #56760

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions packages/next-swc/crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
@@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: since we're just reffing the request string, can we use &'static str instead of String?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was kinda "forward looking" as I'm using format!() in #56675

}
}

/// 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
Loading