Skip to content

Commit

Permalink
Turbopack dev/build: Ensure @vercel/og is external in Node.js environ…
Browse files Browse the repository at this point in the history
…ment (#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 #

-->
  • Loading branch information
timneutkens committed Sep 5, 2024
1 parent 63e0298 commit 668558a
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,17 +552,26 @@ async fn insert_next_server_special_aliases(
runtime: NextRuntime,
next_config: Vc<NextConfig>,
) -> Result<()> {
let external_if_node = move |context_dir: Vc<FileSystemPath>, request: &str| match runtime {
let external_cjs_if_node = move |context_dir: Vc<FileSystemPath>, request: &str| match runtime {
NextRuntime::Edge => request_to_import_mapping(context_dir, request),
NextRuntime::NodeJs => external_request_to_import_mapping(request),
NextRuntime::NodeJs => external_request_to_cjs_import_mapping(request),
};
let external_esm_if_node = move |context_dir: Vc<FileSystemPath>, request: &str| match runtime {
NextRuntime::Edge => request_to_import_mapping(context_dir, request),
NextRuntime::NodeJs => external_request_to_esm_import_mapping(request),
};

import_map.insert_exact_alias(
"next/dist/compiled/@vercel/og/index.node.js",
external_esm_if_node(project_path, "next/dist/compiled/@vercel/og/index.node.js"),
);

import_map.insert_exact_alias(
"@opentelemetry/api",
// It needs to prefer the local version of @opentelemetry/api
ImportMapping::Alternatives(vec![
external_if_node(project_path, "@opentelemetry/api"),
external_if_node(project_path, "next/dist/compiled/@opentelemetry/api"),
external_cjs_if_node(project_path, "@opentelemetry/api"),
external_cjs_if_node(project_path, "next/dist/compiled/@opentelemetry/api"),
])
.cell(),
);
Expand Down Expand Up @@ -641,7 +650,7 @@ async fn insert_next_server_special_aliases(

import_map.insert_exact_alias(
"@vercel/og",
external_if_node(project_path, "next/dist/server/og/image-response"),
external_cjs_if_node(project_path, "next/dist/server/og/image-response"),
);

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

/// Creates a direct import mapping to the result of resolving an external
/// request.
fn external_request_to_import_mapping(request: &str) -> Vc<ImportMapping> {
fn external_request_to_cjs_import_mapping(request: &str) -> Vc<ImportMapping> {
ImportMapping::External(Some(request.into()), ExternalType::CommonJs).into()
}

/// Creates a direct import mapping to the result of resolving an external
/// request.
fn external_request_to_esm_import_mapping(request: &str) -> Vc<ImportMapping> {
ImportMapping::External(Some(request.into()), ExternalType::EcmaScriptModule).into()
}

0 comments on commit 668558a

Please sign in to comment.