Commit Graph

1409 Commits

Author SHA1 Message Date
Benjamin Woodruff 7cb934edad chore(turbo-tasks): Remove the ability to opt-out of NonLocalValue for VcValueType and VcValueTrait (#75053)
These opt-outs are dangerous to leave around because they can break local tasks. 100% of the codebase has been migrated over to use `NonLocalValue` for `VcValueType`, so these opt-outs aren't needed.
2025-01-22 07:31:57 -08:00
Benjamin Woodruff f71af2972b fix(turbopack/napi): Flush optional task cache hit statistics upon completion of build (#75122)
```
cd ~/front/apps/next-site
rm -rf .next && NEXT_TURBOPACK_TASK_STATISTICS=~/build-stats.json TURBOPACK=1 TURBOPACK_BUILD=1 TURBOPACK_PERSISTENT_CACHE=0 /bin/time -v pnpm next build --experimental-build-mode=compile
jq 'to_entries | sort_by(.value.cache_hit, -.value.cache_miss) | from_entries' <build-stats.json >build-stats-sorted.json
```

Gives the following `build-stats-sorted.json` file: https://gist.github.com/bgw/cd3bafbbe08a428334ed9bec10885769
2025-01-21 16:03:58 -08:00
Tobias Koppers d4d78b5768 [Turbopack] pass sourceMap flag to webpack loaders and postcss (#75144)
### What?

* pass sourceMap flag to webpack loaders
* run postcss transform without SourceMaps when specified
2025-01-21 23:52:49 +01:00
Tobias Koppers 59c5e2ff7f [Turbopack] allow to disable source maps (#75136)
### What?

add `experimental.turbo.sourceMaps: boolean` which defaults to `true`

When set to `false` it disables:
* emitting source maps for .js and .css
* extracting source map from input source files
* source maps handling in minify

The PR also disables source maps for externals tracing in general since we don't need that
2025-01-21 23:00:51 +01:00
Benjamin Woodruff 029a8199ce refactor(turbopack): Only perform strongly consistent reads/resolves on VcOperation (#75016)
This PR only allows strongly consistent reads to happen on `VcOperation`:

- Moves `Vc::strongly_consistent` to `OperationVc::read_strongly_consistent`.
- Moves `Vc::resolve_strongly_consistent` to `OperationVc::resolve_strongly_consistent` (and makes it return a `ResolvedVc` instead of a `Vc`).

Both these use `OperationVc::connect()` to connect before reading/resolving.

## Why do this?

- For resolved Vcs, the task id associated with the Vc may be unintuitive, as it could be the task that constructs the cell, not the function you most recently called.

- For local Vcs, the task id is our own task. We can't strongly resolve ourselves.

So, the solution is to force strong resolution to occur on `OperationVc`s, where the task pointed to is as obvious as possible.

## Unit Tests

There are still some unit tests depending on `Vc::strongly_consistent`/`Vc::resolve_strongly_consistent`. I'll fix those and completely remove these methods in a subsequent PR.
2025-01-21 08:28:21 -08:00
Tobias Koppers 6ac9b21edd [Turbopack] Low hanging fixes and improvement on module graph (#75106)
### What?

Performance and memory fixes during creation of the module graph

-30% memory on module graph, -20% runtime on module graph

* fix apply_esm_specific_options caching
* avoid resolve task for get_exports
* avoid analyse created items being turbo-tasks functions
* avoid reading created Vc
* determine module type per directory
* reduce resolve tasks
* make some functions in module processing non-turbo-tasks-functions, avoid reference type in apply_module_type
* add Option<Vc<T>>::to_resolved
* make esm_resolve a normal function
* make resolve_asset a non-turbo-tasks-function
2025-01-20 17:06:29 +01:00
Niklas Mischkulnig 4d9a9853c1 Turbopack: optimize NftJsonAsset (#75105) 2025-01-20 16:56:48 +01:00
Tobias Koppers 351fdbce4f [Turbopack] try to avoid too small chunks size when optimizing for production (#75048)
### What?

Merge small chunks if they are below the min chunk size of 20kb

---------

Co-authored-by: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
2025-01-20 14:17:35 +01:00
Tobias Koppers 169829a156 [Turbopack] Production Chunking (#75010)
### What?

Adds a very simple version of Production Chunking for ecmascript which creates chunks in a way that completely avoids duplicating modules.

Future PRs will add more heuristics to that like a minimum chunk size or a maximum number of requests.
2025-01-20 09:53:43 +01:00
Tobias Koppers 645ef165dd [Turbopack] implement chunking based on the module graph (#74979)
### What?

Visit the module graph by using the computed module graph during
chunking

---------

Co-authored-by: Will Binns-Smith <wbinnssmith@gmail.com>
Co-authored-by: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
2025-01-17 11:57:31 -08:00
Niklas Mischkulnig 9e2ba723cc Turbopack: don't search for layout segments in routes (#73847)
For routes, `find_server_entries` previously traversed over all modules trying to find server components and server utils, even though there weren't any.

I was not able to measure the total impact of this change. But this makes routes, for example

![Bildschirmfoto 2025-01-17 um 14 11 15](https://github.com/user-attachments/assets/c5b1c89f-b0a4-4902-9ab6-c89014916268)

![Bildschirmfoto 2025-01-17 um 14 31 36](https://github.com/user-attachments/assets/eeaf408f-ad93-4d41-a3fe-aeaf594c24b6)
2025-01-17 16:30:17 +01:00
Tobias Koppers ba7957e153 fix layout segment optimization (#75035)
### What?

fix layout segment optimization

more fixes for layout segment
2025-01-17 15:06:14 +01:00
Tobias Koppers 1baa63ae76 Turbopack: fix layout segment optimization (#75026)
### What?

#74859 regressed on the Layout Segment Optimization, this fixes it again
2025-01-17 14:26:56 +01:00
Hendrik Liebau 6090343956 Add next-custom-transforms tests to verify source maps (#75005) 2025-01-17 06:47:37 +01:00
Benjamin Woodruff 5e175e3700 refactor(turbopack/next-api): Make VcArc use OperationVc (#74479)
`VcArc` represents values passed through napi to next.js and back. Because these values exit the scope of turbo-tasks, similar to how `State` works, they must be operations, so that they're recomputed/"kept alive" when read.

These changes were extracted from @sokra's original `OperationVc` work (mostly preserved here: https://github.com/vercel/next.js/pull/72776), though adapted significantly for the removal of the `OperationVc::new()` constructor.
2025-01-16 20:19:56 -08:00
Benjamin Woodruff f6b783a857 Fix dhat support for turbo dev (#67166)
Build next-swc using the dhat feature:

```
pnpm pack-next --features __internal_dhat-heap
```

### Testing with dev

```
pnpm dev --turbo
```

Hit <kbd>ctrl</kbd>+<kbd>c</kbd>, and see the dhat output:

```
[dhat]: Teardown profiler
dhat: Total:     10,246,437 bytes in 84,934 blocks
dhat: At t-gmax: 4,129,935 bytes in 42,733 blocks
dhat: At t-end:  4,118,268 bytes in 42,728 blocks
dhat: The data has been saved to dhat-heap.json, and is viewable with dhat/dh_view.html
```

### Check that we didn't regress turbotrace

Modify a project's `next.config.mjs` to enable turbotrace:

```
  experimental: {
    turbotrace: {}
  }
```

run `pnpm build` and see that we get output from dhat at the end:

```
dhat: Total:     108,876,305 bytes in 716,123 blocks
dhat: At t-gmax: 53,145,524 bytes in 338,440 blocks
dhat: At t-end:  53,145,524 bytes in 338,440 blocks
dhat: The data has been saved to dhat-heap.json, and is viewable with dhat/dh_view.html
```
2025-01-16 19:31:48 -08:00
Niklas Mischkulnig 6615138992 Turbopack: compute chunk group info (#74947)
Compute which chunk group each module belongs to (i.e. `Map<Module, BitMap>`)
2025-01-16 22:10:59 +01:00
Will Binns-Smith 39f35f1457 Turbopack error logging: check NEXT_TEST_CI to use verbose behavior (#75001) 2025-01-16 12:09:58 -08:00
Niklas Mischkulnig 5c3c786d19 Turbopack: add server actions loader to module graph (#74951)
<!-- 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 #

-->
2025-01-16 13:13:18 +01:00
Niklas Mischkulnig 262a08d240 Turbopack: singular EcmascriptClientReferenceModule (#74946)
Merge `EcmascriptClientReferenceProxyModule` (which only had the proxy
codegen) and `EcmascriptClientReferenceModule` (which only had the
`Isolated` references) into a single module.
There was benefit to splitting them, and this caused problems for single
module graph chunking
2025-01-16 11:38:08 +01:00
Niklas Mischkulnig c2c62f0640 Turbopack: chunk_group_multiple (#74859)
Extracted from https://github.com/vercel/next.js/pull/73752

Adds `chunk_group_multiple` which takes a vector of modules, to get rid of the unnecessary `IncludeModulesModule`s that were used to work around that
2025-01-16 09:28:14 +01:00
Hendrik Liebau 847506f716 Emit build error if 'use action' directive is used (#74954) 2025-01-16 01:07:46 +01:00
Tobias Koppers ec13f064f6 [Turbopack] refactor to pass ModuleGraph to chunk_group (#74937) 2025-01-15 18:54:35 +01:00
Niklas Mischkulnig 83c9c3953e Turbopack: capture all references in module graph (#74873)
Everything should be in place now to have the module petgraph be 100% accurate even if its split across multiple graphs (i.e. in dev or because of the actions).

Nothing uses this yet, in the future we should measure if there's a perf impact of running next/dynamic, client references, server actions  on the whole graph as opposed to only on the change layout segments (which is what currently happens).

- [x] clean up/ensure that there are always all needed `entries` in the graph struct
2025-01-15 17:31:26 +01:00
Camille Barneaud cda3dbfdbd Fix missing await of params when metadata with an image file pt2 (#74193) 2025-01-15 12:48:07 +01:00
Benjamin Woodruff 5de932a442 feat(turbopack): Add a compile-time feature to turbopack to enable tokio-console support (#74675)
I'm trying to use this to debug a hang in turbo-tasks. There's some overlap with https://turbo-trace-viewer.vercel.app/, but this is more realtime, which is useful for finding hanging futures.

There's some similar functionality in turbopack-cli: https://github.com/vercel/next.js/blob/c51b53680de4b1c00cd66bb00b7ff6b878646583/turbopack/crates/turbopack-cli/src/dev/mod.rs#L349

Enable this by building with `--features next-swc-napi/tokio-console`.
2025-01-14 22:43:27 -08:00
Hendrik Liebau 6cc85deaea Add partial support for "use cache" in metadata route handlers (#74835)
Adds support for using `"use cache"` in the special metadata route
handlers like
[`sitemap.ts`](https://nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap#generating-a-sitemap-using-code-js-ts),
[`opengraph-image.tsx`](https://nextjs.org/docs/app/api-reference/file-conventions/metadata/opengraph-image#generate-images-using-code-js-ts-tsx),
[`icon.tsx`](https://nextjs.org/docs/app/api-reference/file-conventions/metadata/app-icons#generate-icons-using-code-js-ts-tsx),
and other [metadata
files](https://nextjs.org/docs/app/api-reference/file-conventions/metadata).

reverts #71225
fixes #74146
closes NAR-51

As a follow-up we need to ensure that opengraph image responses do not
bail out of static generation when `dynamicIO` is enabled.
2025-01-14 10:27:04 +01:00
Benjamin Woodruff d989f985dc refactor(turbopack): Rewrite CollectiblesSource callsites to use OperationVc (part 2/3) (#74168)
`OperationVc`s should be used with `CollectiblesSource` instead of `Vc`s because collectibles represent a side-effect or implicit extra return value of a function's execution.
2025-01-13 18:06:21 -08:00
Benjamin Woodruff 99c4dc3dc3 refactor(turbopack): Remove last known (non-test) usages of unresolved vcs in turbo_tasks::value (!!!) (#74713) 2025-01-13 14:48:55 -08:00
Benjamin Woodruff d710482cf1 refactor(turbopack/next-core): Use ResolvedVc in PackagesGlobs (#74712) 2025-01-13 14:06:56 -08:00
Benjamin Woodruff 521cdaef70 refactor(turbopack-core): Use ResolvedVc in ContextCondition (#74711) 2025-01-13 12:28:22 -08:00
Benjamin Woodruff 1df81bcea6 refactor(turbopack/next-core): Use ResolvedVc in VisitClientReferenceNodeState (#74710) 2025-01-13 11:09:39 -08:00
Hendrik Liebau 09e47152bf Fix output files warning by Turborepo (#74811)
The new version of Turborepo (rightfully) complains that two of our
packages don't have output files specified when running `pnpm build`:

```
 WARNING  no output files found for task @next/codemod#build. Please check your `outputs` key in `turbo.json`
 WARNING  no output files found for task @vercel/turbopack-next#build. Please check your `outputs` key in `turbo.json`
```

The package `@next/codemod` emits its `.js`, `.js.map`, and `.d.ts`
files into the same folder as the respective source files instead of
`dist` (as specified in the root `turbo.json`). And
`@vercel/turbopack-next` does not emit any output files. Both facts are
now reflected in the two added package-specific `turbo.json` files.
2025-01-13 11:52:51 +01:00
Tobias Koppers 5db5f136ae [Turbopack] make the single module graph a root task (#74778) 2025-01-13 09:58:23 +01:00
Benjamin Woodruff 3651a45072 refactor(turbopack-core): Remove remaining "local" value type opt-outs in turbopack-core (#74706) 2025-01-10 17:32:04 -08:00
Will Binns-Smith 4ef926bca5 Turbopack chunking: use ChunkableModules in chunk_content, not ChunkItems (#74040)
This:

- Replaces all use of chunk items in the interface for `chunk_content`
with ChunkableModules
- Moves `is_self_async` from `ChunkItem` to `Module`
- Renames `AvailableChunkItems` to `AvailableModules` and all associated
structs and methods, using modules in place of chunk items

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
2025-01-10 23:21:58 +00:00
Will Binns-Smith c6ac0b9f6c SingleModuleGraph: yield edge weights during traversal (#74620)
This adds the edge weight (the `ChunkingType`) of the edge during
`traverse_edges_from_entry` and `traverse_edges_from_entry_topological`,
rather than only providing only the originating node.

Test Plan: Added a log to verify edge types were provided to these
visitors.
2025-01-10 14:33:50 -08:00
Donny/강동윤 da3f782046 build: Update swc_core to v10.1.0 (#74580)
### What?

Update swc crates to https://github.com/swc-project/swc/releases/tag/swc_core%40v10.1.0



### Why?

To apply https://github.com/vercel/next.js/pull/73696 and parallelization improvements.

### How?
2025-01-10 16:21:06 +01:00
Sebastian "Sebbie" Silbermann 2514e133e7 Add experimental flag for View Transitions (#74659)
Will be stacked on next React sync which includes
`unstable_ViewTransition`.

Allows using React's `unstable_ViewTransition` Component behind the
`experimental.viewTransition` flag.

Right now it just opts into React's experimental build but may later
house Next.js specific features like Transition Types for navigations.
2025-01-10 09:49:47 +00:00
Luis Alvarez D. 6b7241a3cf Implement encryption key into turbopack as hash salt (#72933)
I checked that the encryption key is now being used as the hash_salt.
I'm not sure about how to add a test for this behavior so let me know
about any suggestions there. (I tried looking for tests that would use
the functions up the call tree without any luck)
2025-01-09 08:52:15 -08:00
Benjamin Woodruff 720518020c refactor(turbopack-ecmascript) Use ResolvedVc in EsmExport type (#74508)
This removes the `local` opt-out from `EsmExports`.
2025-01-08 21:54:46 -08:00
Benjamin Woodruff 2dab0ed222 refactor(turbopack): Use ResolvedVc for chunk graph related types (#74507)
Allows a few more `turbo_tasks::value`s to implement `NonLocalValue`, making them safe for use with local tasks.
2025-01-08 16:35:03 -08:00
Will Binns-Smith 13d5750a61 Relocate SingleModuleGraph to turbopack-core (#74571)
While many use case-specific graphs belong in Next.js, this primitive
should be kept in Turbopack as it will be used for other core usecases
like chunking.

Test Plan: CI
2025-01-07 09:41:29 -08:00
Tobias Koppers b0a0c92656 [Turbopack] fix shadow-rs build caching (#74579)
### What?

updates shadow-rs dependency, which fixes the build.rs caching problem: https://github.com/baoyachi/shadow-rs/pull/195/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R40

Also prints the git version during the build
2025-01-07 16:07:01 +01:00
Janka Uryga 15bb8ce520 fix: force module format for virtual client-proxy file (#74162)
Fixes #74062 (`jotai` ran into this error [when they added `"type":
"commonjs"` to their
package.json](https://github.com/pmndrs/jotai/discussions/2579#discussioncomment-9812492))

> this is a bug in the transform we do when a `'use client'` directive
is encountered. I think what's happening is that we're creating a
virtual file that uses ESM import/export syntax, but it's called
proxy.js (not proxy.mjs), so the `"type": "commonjs" `makes turbopack
"correctly" upset at the ESM syntax.
https://github.com/vercel/next.js/issues/74062#issuecomment-2555922867

The (slightly kludgy) solution is to use `proxy.mjs` or `proxy.cjs` to
force the module format, bypassing the issue where `proxy.js` changes
meaning depending on `package.json#type`.
2025-01-07 14:50:29 +01:00
Benjamin Woodruff 3a7301221a refactor(next-core): Remove local annotations from structs in next-core/src/app_structure.rs (#74200)
These only need trivial modifications to implement `NonLocalValue`.
2025-01-02 20:47:13 -05:00
Tobias Koppers 64efdadd0e [Turbopack] fix import.meta.url in monorepo (#72612)
### What?

fixes the computation of `import.meta.url` inside of monorepos (when the root_path != project_path)

### How?

We add a new argument to the ChunkingContext that represents the relative path from the output folder to the root of the monorepo. Since they are on different filesystems, we can't compute them from the values alone.
2024-12-20 16:34:51 +01:00
Hendrik Liebau 6fa146a45e Allow usage of this and arguments in nested function expression (#74179)
In #73059 we added build-time checks for the forbidden usage of `this`
and `arguments` in server functions. A nested function expression is
however allowed to use these expressions.

fixes #74181

To add a bit more context: `this` is forbidden in server actions because
the Next.js compiler hoists inline server actions out of their original
location into the module scope, so that they can be imported and invoked
by the action handler. Due to this hoisting, the `this` context gets
lost and is not available to the server action.

To prevent surprising runtime errors for such cases, we emit a build
error to provide feedback to developers as early as possible.

However, nested function declarations or function expressions create a
new `this` context, and in those it's allowed to access `this` and
`arguments`.

For consistency, and to prevent surprises when refactoring server
actions, we apply the same rules for module-level server actions.
2024-12-20 15:11:23 +01:00
Benjamin Woodruff ad2282df6e refactor(turbopack): Rewrite CollectiblesSource callsites to use OperationVc (part 1/3) (#74051)
`OperationVc`s should be used with `CollectiblesSource` instead of `Vc`s because collectibles represent a side-effect or implicit extra return value of a function's execution.

This PR doesn't convert all callsites, just a handful.
2024-12-19 20:09:24 -08:00
Benjamin Woodruff afddbe7a65 refactor(turbo-tasks): Make State require OperationValue (#73876)
I started writing this PR by requiring `OperationValue` in `State`:

```diff
  impl<T> State<T> {
-     pub fn new(value: T) -> Self {
+     pub fn new(value: T) -> Self
+    where
+        T: OperationValue,
+    {
```

And then went through changing every compilation error that occurred as a result.

## Hacks

**There are some hacks in a number of places here where we incorrectly convert `ResolvedVc` to `OperationValue`.**

Unfortunately these have to exist because it's impractical to convert everything to `OperationValue`. `OperationValues` are harder to construct as they must be created directly from the return value of an unresolved function call.

## Why does `State<T>` need `T: OperationValue`?

`OperationVc` types require that `.connect()` is called before their value can be read.

As an internally-mutable type, `State` allows `Vc`s to be passed in ways that we cannot track. `.connect()` re-connects the `Vc` to the dependency graph so that strong resolution is possible.

Without this, you might end up with stale results when reading a `Vc` inside of `State`, which can be very hard to debug.

Fundamentally, I think `State` is unsound, but this is a band-aid on it until we can migrate to a better solution (likely effect-style `Collectibles` for `VersionedContentMap`).
2024-12-19 19:12:03 -08:00