Skip to content

Commit 668558a

Browse files
authored
Turbopack dev/build: Ensure @vercel/og is external in Node.js environment (#69691)
While investigating #65614 I found that in Next.js with webpack we mark this dependency as external but in Turbopack we didn't do that yet, causing it to be bundled and then fail at runtime because the path didn't match exactly. This PR ensures it's marked as external. Didn't add an additional test as we still have some Turbopack build test failures related to opengraph images so potentially this change also fixes those 💯 Fixes PACK-3058 Fixes #65614 <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
1 parent 63e0298 commit 668558a

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

crates/next-core/src/next_import_map.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,17 +552,26 @@ async fn insert_next_server_special_aliases(
552552
runtime: NextRuntime,
553553
next_config: Vc<NextConfig>,
554554
) -> Result<()> {
555-
let external_if_node = move |context_dir: Vc<FileSystemPath>, request: &str| match runtime {
555+
let external_cjs_if_node = move |context_dir: Vc<FileSystemPath>, request: &str| match runtime {
556556
NextRuntime::Edge => request_to_import_mapping(context_dir, request),
557-
NextRuntime::NodeJs => external_request_to_import_mapping(request),
557+
NextRuntime::NodeJs => external_request_to_cjs_import_mapping(request),
558558
};
559+
let external_esm_if_node = move |context_dir: Vc<FileSystemPath>, request: &str| match runtime {
560+
NextRuntime::Edge => request_to_import_mapping(context_dir, request),
561+
NextRuntime::NodeJs => external_request_to_esm_import_mapping(request),
562+
};
563+
564+
import_map.insert_exact_alias(
565+
"next/dist/compiled/@vercel/og/index.node.js",
566+
external_esm_if_node(project_path, "next/dist/compiled/@vercel/og/index.node.js"),
567+
);
559568

560569
import_map.insert_exact_alias(
561570
"@opentelemetry/api",
562571
// It needs to prefer the local version of @opentelemetry/api
563572
ImportMapping::Alternatives(vec![
564-
external_if_node(project_path, "@opentelemetry/api"),
565-
external_if_node(project_path, "next/dist/compiled/@opentelemetry/api"),
573+
external_cjs_if_node(project_path, "@opentelemetry/api"),
574+
external_cjs_if_node(project_path, "next/dist/compiled/@opentelemetry/api"),
566575
])
567576
.cell(),
568577
);
@@ -641,7 +650,7 @@ async fn insert_next_server_special_aliases(
641650

642651
import_map.insert_exact_alias(
643652
"@vercel/og",
644-
external_if_node(project_path, "next/dist/server/og/image-response"),
653+
external_cjs_if_node(project_path, "next/dist/server/og/image-response"),
645654
);
646655

647656
Ok(())
@@ -1061,6 +1070,12 @@ fn request_to_import_mapping(context_path: Vc<FileSystemPath>, request: &str) ->
10611070

10621071
/// Creates a direct import mapping to the result of resolving an external
10631072
/// request.
1064-
fn external_request_to_import_mapping(request: &str) -> Vc<ImportMapping> {
1073+
fn external_request_to_cjs_import_mapping(request: &str) -> Vc<ImportMapping> {
10651074
ImportMapping::External(Some(request.into()), ExternalType::CommonJs).into()
10661075
}
1076+
1077+
/// Creates a direct import mapping to the result of resolving an external
1078+
/// request.
1079+
fn external_request_to_esm_import_mapping(request: &str) -> Vc<ImportMapping> {
1080+
ImportMapping::External(Some(request.into()), ExternalType::EcmaScriptModule).into()
1081+
}

0 commit comments

Comments
 (0)