Commit Graph

1409 Commits

Author SHA1 Message Date
Sam Ko a42205401e fix(next/font): update axes error logic (#72442)
## Why?

The current error message when you have have a set `weight: ['400', '700']` and set `axes: ['wdth']` for a variable font is confusing—it should mention to correct your weight property (e.g., `weight: 'variable'` or nothing at all for `weight`).

- Fixes https://github.com/vercel/next.js/issues/72413
2024-11-07 15:35:29 -08:00
Alexander Lyon 322c0ee5ce add nft bundle to SsrChunk (#72305)
A small PR to start inserting nft on the critical path. This is only enabled in production builds and implements the bare minimum to get NFT outputs by inserting it into the server assets.

Followed up by another PR and inserts more hooks / flags elsewhere in the APIs.
2024-11-07 20:15:56 +01:00
Alexander Lyon 3066987f53 Remove the node-file-trace crate (#70998)
### What?

This is a 2nd prefactor for adding node-file-trace into turbopack. When implementing this, we have a few options namely:

- a) expose turbotrace (node-file-trace-rs) as a napi library and try to insert it into the current codebase
- b) expose turbotrace and try to reuse the turbo backend
- c) move turobtrace into the rust process and do it in parallel with everything else

We chose c

We have already ripped out the js code path, this PR nukes the now-orphaned node-file-trace rust binary and napi bindings.

### Why?

The crate is unmaintained and probably will not be used in its current form.
2024-11-07 09:56:00 +01:00
Donny/강동윤 8446b04a3c feat(turbopack): Add an option to disable minification for builds (#72388)
### What?

Add an option to disable SWC minfiier for turbopack builds.

### Why?

Disabling minification was useful for debugging and @sokra suggested adding it as an option under `experimental.turbo`.

### How?

Closes PACK-3371
2024-11-07 16:25:11 +09:00
Donny/강동윤 de17a9b193 feat(turbopack): Implement skipping of side effect free fragments (#71340)
### What?

### Why?

### How?

Closes NEXT-
Fixes #
2024-11-07 16:19:20 +09:00
Benjamin Woodruff 29a627bafc chore(turbopack): Update indexmap dependency from 1.x to 2.x (#72429)
I noticed we were using a pretty old version of indexmap, this updates that.

`indexmap 1.9.3` still exists in the lockfile as there are some other dependencies we have (mostly `wasmer`) that pull it, but as those dependencies are updated, we'll get rid of it.

I specified the lower version range bound as `2.5.0` instead of `2.6.0` because I wanted to avoid introducing [yet another minor version of hashbrown](https://github.com/indexmap-rs/indexmap/blob/master/RELEASES.md), which might *slightly* help with compilation times. This is just a lower bound, so eventually we'll end up on `2.6.0` (or a later version) if dependency asks for it.

Closes PACK-3406
2024-11-06 21:02:46 -08:00
Janka Uryga 7a6b20c1dd fix(font): don't break directives with inserted next/font import (#72350)
the `next_font_loaders` transform replaces imports from `next/font` with
slightly different ones. unfortunately it was inserting these imports
**at the top** of the file, which broke the `server_actions` transform
because it expects "use server"/"use cache" directives to be the first
item in the file.

this PR changes the logic in the font transform to insert the import in
the place where the original `next/font` import occurred instead of at
the top of the file.

(i suppose that we could just reorder the transforms and run
`server_actions` before `next_font_loaders`, but this feels more robust)

Fixes #72179
2024-11-06 21:16:36 +00:00
Benjamin Woodruff 80384ac110 Revert "feat(turbopack): Add an env var to debug-print the fast refresh invalidation reason (#72296)" (#72421)
Not sure why this is happening, but @kdy1 bisected this commit to a null pointer assertion error in napi: https://vercel.slack.com/archives/C03EWR7LGEN/p1730901839682439?thread_ts=1730899709.760659&cid=C03EWR7LGEN

Reverting until I can repro and root cause.

Closes PACK-3402
2024-11-06 11:10:18 -08:00
Niklas Mischkulnig 9e6d378b8e Turbopack: Ignore Unknown module type for NFT (#71713)
Part of NFT

~~Dynamic requests such as `require("./"+ something)` often match too
many files. If we cannot process a matched file with the Turbopack
rules, then it's really an unreachable case anyway. This removes the
error in that case.~~

This doesn't change the behavior of regular bundling, but makes
`primary_modules_raw_iter` not throw the error.

- [x] Cleanup API: add the boolean into `.process()`? Or should this be
passed differently?
- [ ] ~~Should this be a warning instead for user code?~~
2024-11-06 19:42:36 +01:00
Donny/강동윤 4373f96f24 build: Update swc_core to v5.0.1 (#71683)
### What?

ChangeLog:
https://github.com/swc-project/swc/compare/swc_core%40v1.0.6...swc_core%40v5.0.1

### Why?

To keep in sync.

### How?

 - Closes SWC-612

 - Depends on https://github.com/wooorm/mdxjs-rs/pull/55
2024-11-06 22:00:26 +09:00
Hendrik Liebau f3533cb223 Show readable hook names in stack frames (#72340) 2024-11-06 08:54:05 +01:00
Hendrik Liebau 5cc9cbb1aa Handle export default declaration in debug_fn_name transform (#72326)
Follow-up to #68056, adding support for export default declarations with function expressions, e.g.:

```tsx
export default function Page() {
  useEffect(() => {})
}
```

Enabling this feature will be done in a future PR.
2024-11-06 08:07:00 +01:00
Benjamin Woodruff 72f3bab217 refactor(turbopack-core): Migrate Sources type (and related types) to ResolvedVc (#72382)
A hand refactor of some struct fields from `Vc` to `ResolvedVc` to unblock local tasks.

Closes PACK-3392
2024-11-06 15:30:08 +09:00
Donny/강동윤 9fdfe2a886 refactor(turbopack): Use ResolvedVc<T> instead of Vc<T> for turbopack-node (#72320)
### What?

Use `ResolvedVc<T>` instead of `Vc<T>` for **struct fields**.

### Why?

It's required for future works.

### How?



Closes PACK-3381

---------

Co-authored-by: Benjamin Woodruff <benjamin.woodruff@vercel.com>
2024-11-06 11:11:57 +09:00
Benjamin Woodruff f5f1581a8b feat(turbopack): Switch turbo-tasks-memory/print_task_invalidation compile-time feature to an environment variable (#72300)
With this PR, `turbo-tasks-memory` (the new backend doesn't support this yet) can print invalidation information (basically just function names) to stdout when run with the following environment variable:

```
NEXT_TURBOPACK_PRINT_TASK_INVALIDATION=1
```

![Screenshot 2024-11-04 at 5.05.10 PM.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/HAZVitxRNnZz8QMiPn4a/3d334ef6-184c-40df-9769-fd0373de00fb.png)

This was previously behind a compilation feature flag, however:

- We have to generate all the support code regardless of if the flag is set, so it doesn't seem like it would save us anything on binary size.
- The branch should be pretty cheap to check (and probably easy for the CPU to predict), so runtime performance when this flag isn't set should be negligible.
- It's a pretty big lift to ask users to try custom builds.

**Why?** The hope is that this can help debug cases where users get stuck in a fast refresh update loop: https://vercel.slack.com/archives/C03S8ED1DKM/p1730737898652749

This is similar to #72300, except that prints the *reason*, this prints the *tasks* that were actually invalidated. Printing the tasks works even if no reason was supplied, so it can probably be helpful in debugging those cases.

Closes PACK-3375
2024-11-05 15:30:26 -08:00
Will Binns-Smith e129d44c72 Turbopack unhandled errors: ask for a description (#71384)
This instructs users to provide a written description of what they were
doing when filing an issue.
2024-11-05 11:38:51 -08:00
Benjamin Woodruff 8abe1fd55b feat(turbopack): Add an env var to debug-print the fast refresh invalidation reason (#72296)
Setting the environment variable when building (`next dev`)

```
NEXT_TURBOPACK_INCLUDE_UPDATE_REASONS=1
```

causes `[Update Reasons]` messages to be logged to the terminal console (not in the browser) when a file is changed:

![Screenshot 2024-11-04 at 4.31.16 PM.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/HAZVitxRNnZz8QMiPn4a/1af5a4f0-cfba-4ca8-b188-d6be2064b0dc.png)

These messages will not appear if no reason was recorded when the invalidation happened, so this provided on a best-effort basis.

**Why?** The hope is that this can help debug cases where users get stuck in a fast refresh update loop: https://vercel.slack.com/archives/C03S8ED1DKM/p1730737898652749

Closes PACK-3374
2024-11-05 10:49:41 -08:00
Will Binns-Smith 47467160cd Turbopack: use file:// uris for server resources in development (#71984)
Follows up from #71489, extending this to sourcemaps for server bundles.

Test Plan:

- [x] CI
- [x]
https://github.com/vercel/next.js/blob/61dbe6fb00885068e7ae89ed397e2112076a4577/test/development/app-dir/source-mapping/README.md


Closes PACK-3348
2024-11-05 09:31:20 -08:00
Benjamin Woodruff 1d45e0bf47 refactor(turbopack-core): Migrate more resolve options structs to ResolvedVc (#72200)
A hand refactor of all the remaining struct fields from `Vc` to `ResolvedVc`, to unblock local tasks.

This is similar to #71948, as there's a focus on resolve options.

Note: "resolve" in this context means two different things. `turbopack-resolve` is about resolving module locations on disk. `ResolvedVc` is about resolving "cells" representing incremental computations.

Closes PACK-3369
2024-11-04 16:19:08 -08:00
Will Binns-Smith 4be2fb185f Turbopack: font weight sorting feedback (#72109)
This addresses @bgw’s feedback for #72043:

- Defers cloning
https://github.com/vercel/next.js/pull/72043#discussion_r1821815860
- Uses a `BTreeSet` in `FontAxesWeights::Fixed` to ensure it’s always
sorted, rather than sorting at url-generation-time
https://github.com/vercel/next.js/pull/72043#discussion_r1821821359
- `RcStr` `.to_owned()` -> `.clone()`

Test Plan: Unit tests

Co-authored-by: Benjamin Woodruff <benjamin.woodruff@vercel.com>


Closes PACK-3360

Co-authored-by: Benjamin Woodruff <benjamin.woodruff@vercel.com>
2024-11-04 09:40:21 -08:00
Benjamin Woodruff a63e11a00e refactor(turbopack-core): Change OutputAssets type to use ResolvedVc (#72041)
A hand refactor of the `OutputAssets` type in `turbopack-core` to use `ResolvedVc`. This is one of the more common types passed around turbopack, so this touches a lot of code.

Closes PACK-3358
2024-10-31 14:04:18 -07:00
Benjamin Woodruff 9bec7a23fe refactor(turbopack-resolve): Port all struct fields in the turbopack-resolve crate to turbo_tasks::ResolvedVc (#71948)
A hand refactor of all the remaining struct fields in `turbopack-resolve` to `ResolvedVc`.

Note: "resolve" in this context means two different things. `turbopack-resolve` is about resolving module locations on disk. `ResolvedVc` is about resolving "cells" representing incremental computations.

Closes PACK-3344
2024-10-31 13:14:17 -07:00
Benjamin Woodruff 75e2ed1645 refactor(turbopack-core): Change Modules type to use ResolvedVc (#71945)
A hand refactor of the `Modules` (and `OptionModule`) type in `turbopack-core` to use `ResolvedVc`. This is one of the more common types passed around turbopack, so this touches a lot of code.

Closes PACK-3343
2024-10-31 10:04:11 -07:00
Jude Gao 5af6d83a3c fix /app-dir/middleware-matching turbopack test failure (#72098)
Context
- I only fixed webpack codepath for
https://github.com/vercel/next.js/pull/72056
- But we ended up merging it despite the fact the CI would fail due to
Github incident https://www.githubstatus.com/incidents/9yk1fbk0qjjc

Notes
- `middleware.rs` only runs in build time for production turbopack build
- without this fix, the test would fail on 404 for prod mode, but would
land on Chat page for dev mode
2024-10-30 15:33:24 -04:00
Zack Tanner e1e25e16ae use server component transform for segment validation (#72051)
After adding validation that fixing invalid segment configs (when DIO is
enabled), it revealed that errors weren't being displayed in Turbopack,
because `setHMRServerError` wasn't implemented.

This refactors the implementation to use the existing server component
transform when validating if the segments are valid. This has the
benefit of highlighting precisely where the segment is coming, making it
easier to remove. The test that was skipped in Turbopack is also
re-enabled.

<details>
<summary>Screenshot</summary>

![CleanShot 2024-10-29 at 17 03
46@2x](https://github.com/user-attachments/assets/a68c060a-6ae7-4c6c-8243-6c48c2703f66)



</details>
2024-10-30 11:30:37 -07:00
Will Binns-Smith de4f197e2f Turbopack next/font: Order weight variants numerically (#72043)
The Google fonts API requires this. Previously, `get_stylesheet_url`
trusted that the weight axis inputs were sorted correctly, which they
may not be.

This PR creates and uses two new enums:

- `FontAxesWeights`, to capture weights as ints, and moves the
responsibility of sorting weights into `get_stylesheet_url` itself.
- `VariantValue` to wrap each value for variants, as these can be either
a string (an expression representing a variable font weight range) or a
`u16`, representing a fixed font weight. This allows us to defer
stringifying these until format time while still comparing them for
ordering.

Test Plan: Added a unit test for `get_stylesheet_url`.


Closes PACK-3359
2024-10-30 11:19:02 +01:00
Benjamin Woodruff ffc326c6b8 codemod(turbopack): Rewrite Vc fields in structs as ResolvedVc (part 6) (#71941)
Generated using a version of https://github.com/vercel/turbopack-resolved-vc-codemod . This uses Anthropic Claude Sonnet 3.5 for more complicated errors we don't have hardcoded logic for.

- Part 1: #70927
- Part 2: #71172
- Part 3: #71665
- Part 4: #71804
- Part 5: #71861

Closes PACK-3342
2024-10-29 16:01:03 -07:00
Alexander Lyon ee60948e57 remove turbotrace from webpack codepath (#71359)
Closes PACK-3356

### What?

We previously supported a flag `experimental.turbotrace` which would opt in to using turbo-tasks powered machinery for performing the tasks that were previously handled by node-file-trace. There was a bifurcation where both turbopack and webpack would use either node-file-trace or the new turbotrace which lead to untested code paths. This never really gained traction on the webpack side and, with the decision to move turbotrace and nft generation directly into the critical path of a turbo build (as opposed to a post-processing step) is obsolete for turbopack. We rip it out.

### Why?

See above

### How?

Remove the flag, and the code path branches that refer to it. I opted to keep some formatting to preserve the diff but may refactor the now-rustless code later as there is still some control flow that is unnecessary.
2024-10-29 20:33:05 +01:00
Jude Gao a3918569c7 fix global-error styles (#71914) 2024-10-28 14:34:21 +01:00
Shu Ding ba9879bf98 Implement information byte in Server Reference ID and other optimizations (#71463)
With this PR, we're adding one extra leading byte to Server Reference
IDs (both Server Actions and `"use cache"` functions), to include some
static information about the function itself.

The information byte has the following format:

```
0     000000    0
^type ^arg mask ^rest args
```

The type bit represents if the action is a cache function or not. For
cache functions, the type bit is set to `1`. Otherwise, it's `0`.

The arg mask bit is used to determine which arguments are used by the
function itself, up to 6 arguments. The bit is set to `1` if the
argument is used, or being spread or destructured (so it can be
indirectly or partially used). The bit is set to `0` otherwise.

The rest args bit is used to determine if there's a `...` rest argument
in the function signature. If there is, the bit is set to `1`.

For example:

```tsx
async function foo(a, foo, b, bar, ...baz) {
  'use cache';
  return a + b;
}
```

will have it encoded as `[1][101011][1]`. The first bit is set to `1`
because it's a cache function. The second part has `1010` because the
only arguments used are `a` and `b`. The subsequent `11` bits are set to
`1` because there's a `...baz` argument starting from the 5th. The last
bit is set to `1` as well for the same reason.

Note: Currently in this PR we don't track if an argument is actually
referenced in the function body or not. That will be implemented as a
follow-up optimization.

Also, the reference ID is currently hex-encoded so there will be exact 2
characters for easier decoding. This encoding might change though.

With this extra byte, the client can do some further optimizations. More
details can be found in the code comment.
2024-10-28 04:16:20 +01:00
Benjamin Woodruff ea7319c13c codemod(turbopack): Rewrite Vc fields in structs as ResolvedVc (part 4) (#71804)
Generated using a version of https://github.com/vercel/turbopack-resolved-vc-codemod . This uses Anthropic Claude Sonnet 3.5 for more complicated errors we don't have hardcoded logic for.

- Part 1: #70927
- Part 2: #71172
- Part 3: #71665

Closes PACK-3336
2024-10-27 13:15:20 -07:00
Benjamin Woodruff 60b5fa288b codemod(turbopack): Rewrite Vc fields in structs as ResolvedVc (part 3) (#71665)
Generated using a version of https://github.com/vercel/turbopack-resolved-vc-codemod using Anthropic Claude Sonnet 3.5 (`claude-3-5-sonnet-20241022`) for more complicated errors we don't have hardcoded logic for.

- Part 1: #70927
- Part 2: #71172

I kept running the script until I ran out of tokens (the tier 1 rate limit is 1M input tokens). I'll keep improving how I'm calling the LLM (there's still a lot of room for improvement from my side), and try running it again tomorrow to fix more things.

This fixes 13 struct field types. There are still 271 struct fields remaining that need to be codemodded.
2024-10-25 11:09:59 -07:00
Will Binns-Smith 2a10d55417 Replace turbopack://[project]/... sourcemap uris with file://... in development (#71489)
This makes working with devtools more straightforward, reduces our own
overhead when tracing stack frames in the error overlay, etc.

Generated code from Turbopack or Next.js still use
`turbopack://[turbopack]` or `turbopack://[next]` respectively.

Test Plan: 
- [x] Update tests
- [x] CI
- [x]
https://github.com/vercel/next.js/blob/475bdb1f743bf0b3552b40f85b615d87f4ae4904/test/development/app-dir/source-mapping/README.md

---------

Co-authored-by: Benjamin Woodruff <benjamin.woodruff@vercel.com>
Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
2024-10-23 23:11:14 -07:00
Jiachi Liu 064970e083 feat: stitch errors with react owner stack (#70393) 2024-10-23 19:28:03 +02:00
Hendrik Liebau 51d6e76596 Avoid server action function indirection in Turbopack (#71628) 2024-10-23 09:54:51 +02:00
マルコメ 4292752f52 chore: fix wrong path of comments (#71682)
## Description
At #44405, many files moved.
Then sync comment paths.

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2024-10-22 21:25:16 -07:00
Tim Neutkens cfa003c784 Add --turbopack CLI flag (#71657)
Changes `--turbo` -> `--turbopack` to avoid confusion.

- Updated docs
- Updates create-next-app -- For create-next-app it's a rename as
otherwise there is an ordering problem with the prompts
- For the CLI `next dev --turbo` is still supported, will eventually be
a warning in a future version to swap with `--turbopack` but is not a
requirement today, can be handled automatically by the upgrade codemod
- New CLI flag: `next dev --turbopack`

<!-- 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 #

-->

---------

Co-authored-by: Will Binns-Smith <wbinnssmith@gmail.com>
2024-10-22 22:31:05 +00:00
Benjamin Woodruff 9bd38dd10c codemod(turbopack): Rewrite more Vc fields in structs as ResolvedVc (#71172)
**This is a** *(mostly)* **machine-generated PR.**

This uses a much updated version of the ast-grep based codemod script from #70927, which can now match a bunch more patterns.

Codemod scripts: https://github.com/vercel/turbopack-resolved-vc-codemod/tree/90518e64cfaf9328546ff4688692f16f54d4fc3e

I did a small bit of cleanup after running the script with:

```bash
sg -U --pattern '$OBJ.resolve().await?.to_resolved().await?' -r '$OBJ.to_resolved().await?'
```

Notable additions since last time are:
- Runs the compiler/fixer in a loop a few times, as fixing an issue often allows compilation to get further, creating new errors.
- Can apply compiler-suggested edits (usually these are manual derefs needed with `*`)
- Knows how to rewrite `$OBJ.cell()` to `$OBJ.resolved_cell()`
- Knows how to rewrite `Vc::cell($ARG)` to `ResolvedVc::cell($ARG)`
- Knows how to rewrite `$TYPE::cell($ARG)` to `$TYPE::resolved_cell($ARG)`.
- Knows how to add `.to_resolved().await?` to expressions.
2024-10-22 14:51:04 -07:00
Jiwon Choi 4f260d6893 fix: metadata image route Windows path escaping (#71615)
### Why?

When handling the `opengraph-image` size exceeding the limit, we mapped
with string literal between Rust and JavaScript. The Rust string literal
contains backslashes `\` on Windows, incompatible with JavaScript string
syntax.

Fixes #71582

---------

Co-authored-by: Donny/강동윤 <kdy1997.dev@gmail.com>
2024-10-22 21:44:49 +09:00
Niklas Mischkulnig 1970f9fb16 Turbopack: Always use blob: URLs for assets in middleware (#71471)
Closes PACK-3304

This was originally done in https://github.com/vercel/next.js/pull/63209

But I don't think this is correct. 

Without this change, the added test fails with
```
Error: failed to pipe response
    at pipeToNodeResponse (/next.js/packages/next/dist/server/pipe-readable.js:126:15)
    at async handleRequest (/next.js/packages/next/dist/server/lib/router-server.js:275:24)
    at async requestHandlerImpl (/next.js/packages/next/dist/server/lib/router-server.js:384:13)
    at async Server.requestListener (/next.js/packages/next/dist/server/lib/start-server.js:142:13) {
  [cause]: TypeError [ERR_INVALID_URL]: Invalid URL
      at new NodeError (node:internal/errors:405:5)
      at new URL (node:internal/url:676:13)
      at /next.js/test/tmp/next-test-1729241452365-597/.next/server/edge/chunks/ssr/[project]_packages_next_dist_compiled_fd94e8._.js:31684:26 {
    input: '/_next/static/media/noto-sans-v27-latin-regular.7e1666d1.ttf',
    code: 'ERR_INVALID_URL'
  }
}
```
(because the `blob:` prefix is missing for the font static URL)
2024-10-22 09:17:32 +02:00
Hendrik Liebau f2ed75a223 DX improvements for "use cache" functions (#71518)
With this PR, we're assigning names to the transformed `"use cache"`
functions, using the same rules as in #71478. Since we can't use named
function expressions here, we're using `Object.defineProperty` instead,
which we already used in #71478 for server actions with default export
expressions.

In addition, this PR fixes source mapping of `"use cache"` functions,
which was slightly off before, due to the wrong spans being assigned to
the new expressions.

**Before:**

<img width="943" alt="before"
src="https://github.com/user-attachments/assets/f7cf674c-32d4-4f0d-a304-d5de5637a6a1">

**After:**

<img width="943" alt="after"
src="https://github.com/user-attachments/assets/8edda482-a666-4431-bfad-7e21987331eb">
2024-10-21 12:43:40 +00:00
Hendrik Liebau 0a916775d5 Allow "use cache" functions in server action module (client layer) (#71511)
When importing a server action module from a client component, we've
already opted into the server layer, so defining inline `"use cache"`
annotated functions should be allowed.
2024-10-19 17:58:58 +02:00
Hendrik Liebau 4796bffdea Transform "use cache" functions exported via export default expression (#71512)
Follow-up to #71497, adding support for exporting a function from a
`"use cache"` module using a default export expression, e.g.:

```ts
"use cache"

const foo = async () => {}

export default foo
```
2024-10-19 17:57:19 +02:00
Hendrik Liebau daa8e7aa08 Fix handling of local functions in "use cache" modules (#71497) 2024-10-19 06:30:01 +00:00
Hendrik Liebau 8c401f6964 Differentiate error messages in server function transforms (#71491)
Updated error messages to differentiate between the two different directives.

### `"use server"`

- `Server Actions must be async functions.`
- `Only async functions are allowed to be exported in a "use server" file.`

### `"use cache"`

- `"use cache" functions must be async functions.`
- `Only async functions are allowed to be exported in a "use cache" file.`
2024-10-19 01:33:03 +02:00
Hendrik Liebau c54b8a3a36 Assign proper function names to server actions (#71478)
This PR adjusts the server action transforms to assign proper function names for the generated server reference proxies.

- If the original function has a name, this name is used.
- If a function expression is assigned to a variable, the variable's name is used.
- If an anonymous function expression is assigned to a prop, the prop's name is used.

In addition, this PR simplifies the output for exported functions, avoiding unnecessary `registerServerReference` calls.

It also fixes the source mapping of default exported server actions.

**Before:**

<img width="890" alt="before" src="https://github.com/user-attachments/assets/a3d6a5f8-d48e-474d-8f03-b49306fb704f">

**After:**

<img width="890" alt="after" src="https://github.com/user-attachments/assets/8f332690-6c26-46f1-9eef-7680f47ea578">

Next up, we will do the same for `'use cache'` functions.
2024-10-19 00:53:21 +02:00
Niklas Mischkulnig 5c404e9033 Turbopack: Improve externals plugin (#71375)
- Slightly improve externals error message
- Don't try to append `.js` to requests without subpaths
- Simplify request->package name parsing

Before 
![Bildschirmfoto 2024-10-16 um 14 27
05](https://github.com/user-attachments/assets/1308084e-27cb-4d77-a472-b202736acc3b)

After (app code changed to include a subpath for better visualization)
<img width="963" alt="Bildschirmfoto 2024-10-17 um 11 24 39"
src="https://github.com/user-attachments/assets/f2d5caa7-a2f4-498b-88aa-a026aedcabe5">


Without the CSS change, it can look like this if there is no `-` in the
filepath:

<img width="1005" alt="Bildschirmfoto 2024-10-17 um 10 55 13"
src="https://github.com/user-attachments/assets/1a899d46-87cc-4e32-8005-92232fee318f">
2024-10-18 22:53:40 +02:00
Niklas Mischkulnig 3a9a1df08a Turbopack: Make UnableToExternalize a warning (#71477)
- Make UnableToExternalize a warning
- Special case that warning so that it's still printed even inside
node_modules
- Make sure that it's only printed once
2024-10-18 22:39:29 +02:00
Niklas Mischkulnig 21ffe10ebd Turbopack: rscModuleMapping without extra chunks (#71326)
- Also refactors `AppEndpoint` a bit (pulling code out into functions
and hoist common function calls for Node/Edge)
- Use the `server/app/.../page.js` RSC entry chunks in the
`rscModuleMapping` to not have to generate extra chunks.

Alternative implementation for
https://github.com/vercel/next.js/pull/71304
Closes #71304

- [x] ~~It fails with this strange error though:~~
```
Error: Cannot find module '../chunks/ssr/[turbopack]_runtime.js'
Require stack:
next.js/test/e2e/app-dir/use-cache/.next/server/chunks/ssr/[turbopack]_runtime.js
next.js/test/e2e/app-dir/use-cache/.next/server/app/page.js
```

---------

Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de>
2024-10-18 18:02:46 +02:00
Tobias Koppers e8e1eb9718 [Turbopack] refactor to allow different key value stores (#71430)
### What?

refactor BackingStore to allow different KeyValueDatabases, avoid
dynamic calls and have ReadTransactions and ValueBuffer as trait
associated type.
Also adds a default backing storage to be used by next.js
2024-10-18 12:34:40 +02:00