Commit graph

1094 commits

Author SHA1 Message Date
Justin Ridgewell
d7818d21b6
turbopack: Update the next/dist/esm rewrite ResolvePlugin (#55689)
### What?

This updates `NextSharedRuntimeResolvePlugin` to match on `next/dist/esm` pattern instead of matching on `node_modules/next/dist`. While the old code is more correct, it prevents the plugin from working when we're developing Next.js locally, because the imported path is resolved to `packages/next/dist/esm/`, not `node_modules/next/dist/esm/`

### Why?

So that we can develop locally without bugs.

### How?

A simple glob change.

Closes NEXT-
Fixes #

-->


Closes WEB-1616
2023-09-20 22:04:29 +00:00
Tobias Koppers
628a19393b
only put css in entry_css_files (#55585)
### What?

Fixes the error:

```
Refused to apply style from '...'  because its MIME type ('application/javascript') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
```

Closes WEB-1606
2023-09-20 17:32:09 +00:00
Shu Ding
dff48cb8a7
Fix importing name multiple times with the named_import transform (#55664)
Specific case: https://twitter.com/shuding_/status/1704220735726633205


> import { User, User as foo } from 'lucide-react'
In your codebase so `User` got imported twice
2023-09-20 15:22:38 +00:00
OJ Kwon
a429e04fec
test(turbopack) migrate api tests, few image tests (#55552)
### What?

PR migrates next-dev's api tests to use next.js integration tests, as well as enabling few more image tests.

Closes WEB-1599
2023-09-19 20:14:53 +00:00
Tobias Koppers
deb81cf246
fix styled-jsx alias (#55581)
### What?

makes sure that styled-jsx is always bundled as it contains `react` and
it being external would break the react vendoring
2023-09-19 15:51:14 +02:00
Shu Ding
8881c413a3
Fix useState function initialiser case for optimize_server_react transform (#55551)
This addresses the case mentioned in https://github.com/vercel/next.js/pull/54925#pullrequestreview-1616315332. We can't optimize the case where we can't statically determine the type of the `useState` argument as it can be a function: `useState(() => ...)`.
2023-09-19 13:32:52 +00:00
Jimmy Lai
aa4efc11d9
build: add aliases for react-dom/server usage in server bundles (#55536)
Some people are using `ReactDOM/server` in some contexts like App Routes and Server Actions. The changes in #55362 made it so that there were no more aliases if they wanted to use it so it would fallback to their versions of React, which might not include the APIs you would expect from canary.

Also reverting the decision to proxy `ReactDOM/server` to `ReactDOM/server.edge` in the SSR layer as the APIs are not the same.
2023-09-18 18:02:14 +00:00
OJ Kwon
f393f55678
fix(next-core): enable image extensions (#55460)
### What?

Enables the image extensions supported by turbopack.

Also fixes test fixture to assert error overlay header.


Closes WEB-1587
2023-09-18 17:30:27 +00:00
Jiachi Liu
ffc0e54102
Allow server-only in server targets and client-only in client components targets to be available (#55394)
Users want to use `server-only` to restrict the middleware / app routes / pages api, but now it's failing as we're treating them as different webpack layers, but validating the `server-only` only with server components layers.

Here we modify the rules a bit to let everyone can use "server-only" for the bundles that targeting server-side.

For next-swc transformer, we introduce the new option `bundleType` which only has `"server" | "client" | "default"` 3 values:
* - `server`  for server-side targets, like server components, app routes, pages api, middleware
* - `client`   for client components targets such as client components app pages, or page routes under pages directory.
* - `default` for environment like jest, we don't validate module graph with swc, replaced the `disable_checks` introduced  [#54891](https://github.com/vercel/next.js/pull/54891).

Refactor a bit webpack-config to adapt to the new rules, after that `server-only` will be able to used in the server-side targets conventions like middleware and `pages/api`

Fixes #43700
Fixes #54549
Fixes #52833

Closes NEXT-1616
Closes NEXT-1607
Closes NEXT-1385
2023-09-18 12:27:45 +00:00
Tobias Koppers
eaa4d02ac9
update turbopack (#55515)
And sync deps so they don't conflict

* https://github.com/vercel/turbo/pull/5959 <!-- Tobias Koppers - reduce
number of task in async_module -->
* https://github.com/vercel/turbo/pull/5960 <!-- Tobias Koppers - reduce
unneccessary fs tasks -->
* https://github.com/vercel/turbo/pull/5955 <!-- Tobias Koppers -
bumping minor or major should reset patch/minor -->

Closes WEB-1592

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-09-18 11:48:36 +02:00
Andrew Clark
0bfd4801e4
Restrict useFormState in Server Components (#55417)
I based my changes on #49331, which did this for useFormStatus.

While I was editing the file, I noticed that useOptimistic was incorrectly categorized as a `react-dom` package export, but it's actually exported from `react`. So I fixed that, too.

Co-authored-by: Zack Tanner <1939140+ztanner@users.noreply.github.com>
2023-09-16 16:53:51 +00:00
Tim Neutkens
16886136f2
Skip JSON.parse in loadClientReferenceManifest (#55457)
While investigating the `next start` CPU profile noticed that this
function was called many times and that the JSON.parse happened for each
of them. This replaces that to no longer be needed and leverages the
require cache instead. The reason I didn't go for memoizing this
function is that it would add further complexity and potential for
leaking memory compared to the approach implemented in this PR.

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

-->
2023-09-16 12:33:34 +02:00
Will Binns-Smith
8ee89e5f31
Don't log error about root_task_dispose (#55458)
Closes WEB-1585
2023-09-15 21:19:16 +00:00
Jimmy Lai
bd705537a3
server: bundle vendored react (#55362)
## What

This PR changes Next.js to bundle its vendored React libraries so that the App Router pages can use those built-in versions.

## Why

Next.js supports both Pages and App Router and we've gone through a lot of iteration to make sure that Next.js stays flexible wrt to the version of React used: in Pages, we want to use the React provided by the user and in the App Router, to be able to use it, we need to use the canary version of React, which we've built into Next.js for convenience.

The problem stems from the fact that you can't run two different instances of React (by design).

Previously we have a dual worker setup, where we would separate completely each Next.js versions (App and Pages) so that they would not overlap with each other, however this approach was not great performance and memory wise.

We've recently tried using an ESM loader and a single process, but this change would still opt you into the React canary version if you had an app page, which breaks some assumptions.

## How

A list of the changes in this PR:

### New versions of the Next.js runtime

Since we now compile a runtime per type of page (app/route/api/pages), in order to bundle the two versions of React that we vendored, we introduced a new type of bundle suffixed by `-experimental`. This bundle will have the bleeding edge React needed for Server Actions and Next.js will opt you in into that runtime automatically.

For internal contributors, it means that we now run a compiler for 10 subparts of Next.js:
- next_bundle_server
- next_bundle_pages_prod
- next_bundle_pages_turbo
- next_bundle_pages_dev
- next_bundle_app_turbo_experimental
- next_bundle_app_prod
- next_bundle_app_prod_experimental
- next_bundle_app_turbo
- next_bundle_app_dev_experimental
- next_bundle_app_dev

![image](https://github.com/vercel/next.js/assets/11064311/f340417d-845e-45b9-8e86-5b287a295c82)

### Simplified require-hook

Since the versions of React are correctly re-routed at build time for app pages, we don't need the require hook anymore

### Turbopack changes

The bundling logic in Turbopack has been addressed to properly follow the new logic

### Changes to the shared contexts system

Some context files need to have a shared instance between the rendering runtime and the user code, like the one that powers the `next/image` component. In general, the aliasing setup takes care of that but we need the require hook for code that is not compiled to reroute to the correct runtime. This only happens for pages node_modules.

A new Turbopack resolving plugin has been added to handle that logic in Turbopack.

### Misc changes

- `runtime-config` (that powers `next/config`) has been converted to an `.external` file, as it should have been
- there are some rules that have been added to the aliases to support the usage of `react-dom/server` in a server-components. We can do that now since the runtime takes care of separating the versions of React.



Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2023-09-15 19:49:39 +00:00
Tobias Koppers
b3a916cab4
fix server-side changes events (#55437)
### What?

fix the problems that caused sideSideChanges to be emitted for client
side changes

### Why?

### How?


Closes WEB-1578
2023-09-15 17:57:01 +02:00
Donny/강동윤
ad79325a9f
Update swc_core to v0.83.12 (#55216)
### What?

I made `styled-jsx` configurable. It uses `swc_css` by default, and with
`useLigntningcss: true`, it uses it.

https://github.com/swc-project/plugins/pull/207

### Why?

### How?

Closes NEXT-
Fixes #



Closes WEB-1532
2023-09-15 17:41:05 +02:00
Justin Ridgewell
36afda4479
Turbopack: remap next/head on client in App Pages (#55419)
### What?

Remaps `next/head` to `next/dist/client/components/noop-head` on the client inside an App Page.

### Why?

Because webpack bundles do it.

### How?

We were just missing an import remap. 🤦 

Closes WEB-1573
2023-09-15 04:10:05 +00:00
Justin Ridgewell
d2641bd072
Update turbopack (#55402)
* https://github.com/vercel/turbo/pull/5952 
* https://github.com/vercel/turbo/pull/5938 

Closes WEB-1566
2023-09-14 22:11:12 +00:00
Tobias Koppers
67bc262b67
Revert "Revert "feat(turbopack): support next.js specific node polyfills"" (#55392)
Reverts vercel/next.js#55385

Closes WEB-1565
2023-09-14 17:32:29 +02:00
Tobias Koppers
129f56d3e7
more turbopack HMR fixes and test case (#55368)
### What?

improve test case to check HMR
reenable deduplication for server side hmr events
add aggregation of client side HMR events

### Turbopack Changes

* https://github.com/vercel/turbo/pull/5936 <!-- Will Binns-Smith -
Update http mock and clap -->
* https://github.com/vercel/turbo/pull/5948 <!-- Tobias Koppers - CSS
HMR fixes -->



Closes WEB-1557

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-09-14 15:50:59 +02:00
Tobias Koppers
9953db4a30
Revert "feat(turbopack): support next.js specific node polyfills" (#55385)
Reverts vercel/next.js#55343

Closes WEB-1562

it fails CI with:

```
  ● node builtins › should support node.js builtins in server component
  
      SyntaxError: Unexpected end of JSON input
          at JSON.parse (<anonymous>)
  
        124 |
        125 |       const data = $('#node-browser-polyfills').text()
      > 126 |       const parsedData = JSON.parse(data)
            |                               ^
        127 |
        128 |       expect(parsedData.vm).toBe(105)
        129 |       expect(parsedData.hash).toBe(
  
        at Object.<anonymous> (development/basic/node-builtins.test.ts:126:31)
  
    ● node builtins › should support node.js builtins prefixed by node: in server component
  
      SyntaxError: Unexpected end of JSON input
          at JSON.parse (<anonymous>)
  
        149 |
        150 |       const data = $('#node-browser-polyfills').text()
      > 151 |       const parsedData = JSON.parse(data)
            |                               ^
        152 |
        153 |       expect(parsedData.vm).toBe(105)
        154 |       expect(parsedData.hash).toBe(
  
        at Object.<anonymous> (development/basic/node-builtins.test.ts:151:31)
  
  ```
  
  @kwonoj
2023-09-14 15:16:33 +02:00
Justin Ridgewell
2c1043ebc4
Turbopack: Allow Pages entrypoint template to import user's .ts files (#55296)
### What?

This adds an exception for the entrypoint templates directory which will
prevent the restricted resolve options from taking over. So, the
templates entrypoint will be able to import with the user's default
resolve options, which enables a `_app.ts` import.

### Why?

The entrypoint templates need to be able to import the user's code as if
the template were use code itself.

### How?

The [Pages
entrypoint](85d30b62db/packages/next/src/build/templates/pages.ts (L9-L10))
template [imports the user's `_app`
file](85d30b62db/packages/next-swc/crates/next-core/src/next_pages/page_entry.rs (L83-L84))
via a special `@vercel/turbopack-next/pages/_app` which is [import
mapped](85d30b62db/packages/next-swc/crates/next-core/src/next_import_map.rs (L404-L411))
to the user's `/pages/_app` file. If the user is using `/pages/_app.js`
file, everything is OK. But a problem pops up if the user is using a
`.ts` (or similar non-standard extension).

Why? In order to prevent a foobar node modules from importing TS/CSS/etc
files, we apply a restricted set of `ResolveOptions` when the base file
(the file that is doing the import) is inside a `node_modules`
directory. In this case, we stop allowing `.ts`/`.tsx`/`.css` file
extension imports and only allow `.js`/`.json`/`.node` (the standard
node extensions).

Well, the shared entrypoint templates are _technically_ inside the
`node_modules/next/dist/esm/build/templates/` directory, ie they are
node modules. So we applied the restricted resolve options when it was
importing `@vercel/turbopack-next/pages/_app`. Even though we're able to
correctly read the user's `/pages` directory, we ignore a
`/pages/_app.ts` file that would otherwise be found if the user code did
the import.

Depends on https://github.com/vercel/turbo/pull/5938.

Closes WEB-1545
2023-09-14 14:54:44 +02:00
OJ Kwon
e8adcd8527
feat(turbopack): support next.js specific node polyfills (#55343)
- feat(turbopack): support next.js specific node polyfills
- test(turbopack): migrate polyfill tests

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



### Why?

### How?

Closes NEXT-
Fixes #

-->

### What?

This PR fulfills gap for the node.js polyfill between next.js to
turbopack, then enable corresponding integration tests.


Closes WEB-1552
2023-09-14 14:52:16 +02:00
Will Binns-Smith
17ba84b7a7
WEB-1135: Update examples and schema for Turbopack loaders config change (#54584)
This addresses inconsistencies with existing examples and config schema
from changes in #49535.


Closes WEB-1451
2023-09-14 14:50:02 +02:00
Tobias Koppers
1ffc40ac6d
fix turbopack HMR, fix disconnect detection (#55361)
### What?

Fix reload in turbopack on every change
Fix loosing of events due to debouncing
Fix writing of CSS files for CSS HMR

### Why?

Since we removed the pong event, the websocket impl would cause reconnects every 5 seconds loosing HMR events...

### How?


Closes WEB-1555
2023-09-14 09:03:52 +00:00
simonri
2ea7290824
Fix next swc windows bug (#55294)
### What?
Updated regex to support Windows paths in `is_layout_or_page`, resolving inconsistent behavior between OSes.

Previously, the regex was tailored for Linux paths, causing certain checks to fail on Windows such as:
- Server entry can't export `generateMetadata` and `metadata` together.
- Client entry can't export `generateMetadata` or `metadata`.

Fixes #55267
2023-09-13 23:22:09 +00:00
Jiachi Liu
14e94e69c5
fix duplicated error logging when start server (#55328)
There're few places calling `loadConfig` to access nextjs config for different purpose, and every of them will do validation of next config scheme and could log the warnings potentially to cause duplicated warnings. To address that issue this PR limit the places when should the warnings be logged in certain places, mainly the `next` command entry. Refactor the API here to make it more explicit

Previous logging redeisgn also might trigger duplicated compilation, for instance you compiled page A but then editing another component it might still displaying page A is recompiled. Now we always display `"Compiled .."` message when there's a recompilation but avoid the unnecessary ones such as initial edge compilor ready but there's no modules inside. Then when it recompiles or recovers from error nextjs server will tell "Compiled .." instead of the specific page which might be not related.

Also refactors a minor issue: When `silent` option is set to `true`, auto recorrect next config option from `loadConfig` API like `traillingSlash` is not working

Closes NEXT-1610

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2023-09-13 22:25:41 +00:00
OJ Kwon
4a6a2ca258
feat(turbopack): attach type metadata for static metadata item (#55340)
### What?

Trying to mimic 32e066ff6c/packages/next/src/build/webpack/loaders/next-metadata-image-loader.ts (L160C7-L160C12), when there's a static metadata turbopack does not includes `type` properties. This partially fulfills existing metadata test cases for checking those values.

Closes WEB-1551
2023-09-13 22:05:33 +00:00
Michael Novotny
fe797c1074
Updates Mozilla links to not include language preference (#55326)
Internal suggestion to remove `en-US` from Mozilla urls since MDN is
available in multiple languages nowadays it will automatically redirect
to the viewer’s language preference.

Closes
[DX-2076](https://linear.app/vercel/issue/DX-2076/make-external-mozilla-links-language-agnostic-in-nextjs-docs)
2023-09-13 11:06:29 -05:00
OJ Kwon
6111845e11
test(next-dev): clean up 404 tests (#55292)
### What?

Previous change migrated 404 tests https://github.com/vercel/next.js/blob/canary/test/turbopack-tests-manifest.js#L112-L115 from next.js.

Closes WEB-1542
2023-09-13 06:35:32 +00:00
OJ Kwon
45fbd4db14
refactor(next-font): use next-transform-font (#55252)
Closes WEB-1539

### What?

It looks like there are duplicated codes between `next-font` and `next-transform-font`, and the prior is not being referenced anywhere.
2023-09-12 17:48:58 +00:00
OJ Kwon
0bf5cbb901
feat(next-dev): reenable turbopack beta message (#55116)
### What?
Minor PR brings back turbopack beta message. We'll need to improve the actual checks per progress, but that might need auditing what's working vs not later.


Closes WEB-1517
2023-09-12 16:39:26 +00:00
Wyatt Johnson
d3656cc42b
reafactor: renamed pathname -> page (#55282) 2023-09-12 16:02:43 +00:00
OJ Kwon
ddc26c9904
test(turbopack): migrate 404-related tests from next-dev (#55243)
Closes WEB-1535
2023-09-11 21:16:51 +00:00
OJ Kwon
360142d409
tests(next-dev): migrate basic tailwind tests for turbopack (#55118)
Closes WEB-1518

### What?

This PR refactors test cases from next-dev, tests basic tailwind rendering with turbopack. It still runs against plain next.js too. While checking, noticed there's a stub fixture (css-fixture/with-tailwindcss) but those fixture seems not being used in the test. Moved those and utilized. Since this test runs against next.js / turbopack both anyway, moving fixture should not be a huge regression I believe.
2023-09-11 15:44:39 +00:00
Leah
6e3ef5b01c
feat: support expanding urls in nexturbo (and fix static files) (#55147)
Closes WEB-1525
2023-09-08 21:35:33 +00:00
Will Binns-Smith
8f30255922
Revert swc versions to one that doesn't use lightningcss (#55148)
Using lightningcss results in a breaking change to Next.js, so let's revert swc for now.


Closes WEB-1526
2023-09-08 20:02:51 +00:00
Leah
b5d752667a
feat(turbopack): add dynamic metadata support (#54995)
Closes NEXT-1435
Closes WEB-1435
2023-09-08 18:25:13 +00:00
Jimmy Lai
5217e7eb06
server: re-land bundled runtimes (#55139)
see https://github.com/vercel/next.js/pull/52997

also added a fix by @jridgewell to fix turbopack





Co-authored-by: Justin Ridgewell <112982+jridgewell@users.noreply.github.com>
2023-09-08 16:05:29 +00:00
JJ Kasper
7267538e00
Revert "perf: add bundled rendering runtimes (#52997)" (#55117)
This reverts commit a5b7c77c1f.

Our E2E tests are failing with this change this reverts to allow investigating async 

x-ref: https://github.com/vercel/next.js/actions/runs/6112149126/job/16589769954
2023-09-07 21:07:53 +00:00
Balázs Orbán
068002bb3a
chore: upgrade to TypeScript 5.2.2 (#55105)
### What?

Upgrade TypeScript to the latest version as of this PR. **This does not affect users, as the change is only for our repository.**

### Why?

Part of some upcoming PRs to try to clean up cookie handling, now that `getSetCookie` is available. Since we use `undici`, which [implements it](https://github.com/nodejs/undici/pull/1915), we can get rid of some code to rely more on the platform.

This PR is needed to get the types for `Headers#getSetCookie` which was added in 5.2

### How?

I needed to update some dependency types to get build to pass, but other than that, only needed to bump from `5.1.6` to `5.2.2`, so hopefully all is fine.
2023-09-07 18:46:05 +00:00
Leah
a8fea15b50
chore: add structured app page path type (#55070)
Makes it easier to reason about the path and easy to convert to the router pathname

Closes WEB-1507
2023-09-07 17:19:32 +00:00
Jimmy Lai
a5b7c77c1f
perf: add bundled rendering runtimes (#52997)
## What?

In Next, rendering a route involves 3 layers:
- the routing layer, which will direct the request to the correct route to render
- the rendering layer, which will take a route and render it appropriately
- the user layer, which contains the user code 

In #51831, in order to optimise the boot time of Next.js, I introduced a change that allowed the routing layer to be bundled. In this PR, I'm doing the same for the rendering layer. This is building up on @wyattjoh's work that initially split the routing and the rendering layer into separate entry-points.

The benefits of having this approach is that this allows us to compartmentalise the different part of Next, optimise them individually and making sure that serving a request is as efficient as possible, e.g. rendering a `pages` route should not need code from the `app router` to be used.

There are now 4 different rendering runtimes, depending on the route type:
- app pages: for App Router pages
- app routes: for App Router route handlers
- pages: for legacy pages
- pages api: for legacy API routes

This change should be transparent to the end user, beside faster cold boots.

## Notable changes

Doing this change required a lot of changes for Next.js under the hood in order to make the different layers play well together.

### New conventions for externals/shared modules

The big issue of bundling the rendering runtimes is that the user code needs to be able to reference an instance of a module/value created in Next during the render. This is the case when the user wants to access the router context during SSR via `next/link` for example; when you call `useContext(value)` the value needs to be the exact same reference to one as the one created by `createContext` earlier.

Previously, we were handling this case by making all files from Next that were affected by this `externals`, meaning that we were marking them not to be bundled.

**Why not keep it this way?**

The goal of this PR as stated previously was to make the rendering process as efficient as possible, so I really wanted to avoid extraneous fs reads to unoptimised code. 

In order to "fix" it, I introduced two new conventions to the codebase:
- all files that explicitly need to be shared between a rendering runtime and the user code must be suffixed by `.shared-runtime` and exposed via adding a reference in the relevant `externals` file. At compilation time, a reference to a file ending with this will get re-written to the appropriate runtime.
- all files that need to be truly externals need to be suffixed by `.external`. At compilation time, a reference to it will stay as-is. This special case is needed mostly only for the async local storages that need to be shared with all three layers of Next.

As a side effect, we should be bundling more of the Next code in the user bundles, so it should be slightly more efficient.

### App route handlers are compiled on their own layer

App route handlers should be compiled in their own layer, this allows us to separate more cleanly the compilation logic here (we don't need to run the RSC logic for example).

### New rendering bundles

We now generate a prod and a dev bundle for:
- the routing server
- the app/pages SSR rendering process
- the API routes process

The development bundle is needed because:
- there is code in Next that relies on NODE_ENV
- because we opt out of the logic referencing the correct rendering runtime in dev for a `shared-runtime` file. This is because we don't need to and that Turbopack does not support rewriting an external to something that looks like this `require('foo').bar.baz` yet. We will need to fix that when Turbopack build ships.

### New development pipeline

Bundling Next is now required when developing on the repo so I extended the taskfile setup to account for that. The webpack config for Next itself lives in `webpack.config.js` and contains the logic for all the new bundles generated.

### Misc changes

There are some misc reshuffling in the code to better use the tree shaking abilities that we can now use.

fixes NEXT-1573

Co-authored-by: Alex Kirszenberg <1621758+alexkirsz@users.noreply.github.com>
2023-09-07 15:51:49 +00:00
OJ Kwon
4f1be5d999
test(next-dev): migrate styled-jsx integration test (#55079)
### What?

There are tests under `next-dev-tests` which used native binary to run tests for Turbopack. This should belong to next.js integration tests, and also indeed there are overlaps.

As a first step, PR removes duplicated styled-jsx test and mark existing test under turbopack test filter as enabled.


Closes WEB-1510
2023-09-07 02:51:20 +00:00
Will Binns-Smith
10b186564d
Next SWC: Constrain Vc cell values with Send (#55077)
This addresses failures from the latest versions of turbo-tasks.

Test Plan: `cargo check` when building against turbo.


Closes WEB-1509
2023-09-06 19:18:05 +00:00
Tobias Koppers
15980a66d4
remove --turbo, use --experimental-turbo as --turbo (#55063)
### What?

Switch the default for `--turbo` to the new `--experimental-turbo`, remove the old code in next.js

### Why?

The new approach will be used in future


Closes WEB-1506
2023-09-06 17:46:54 +00:00
Shu Ding
a9b5174e68
Experimental server optimization (#54925)
Server React API optimization of `useEffect`, `useLayoutEffect` and `useState` under a `experimental.optimizePackageImports` flag.
2023-09-05 22:22:24 +00:00
Jiachi Liu
e117c000e4
Redesign nextjs logging (#54713)
The current logging styles has been existed for a while, this PR gives a fresh impression for the logging output from Next.js.
We want to achieve few new goals that makes the output clean, modernized, sweet 🍫 .

Few goals are addressed with this redesign:

## Refresh Impression & Simplification

The new design of logging is much more information centralized and streamlined.

* Given a `ready` message at the begining when compilers are bootstrapped.
* Only show `compiled` event with green check mark indicating succesful compilation, this will merge the unclear `compiling` event which shows `(client and server)` before, now tell you the route compilation info in one line.

hello world app

### `next dev`

#### After vs Before


<img src="https://github.com/vercel/next.js/assets/4800338/9649b340-8241-4756-a2b3-a989f0b74003" height="120"> 
<img src="https://github.com/vercel/next.js/assets/4800338/ee181263-3dd4-40d0-9ffc-819a56b45900" height="120">  

 


 

### `next build`

#### After vs Before


<img src="https://github.com/vercel/next.js/assets/4800338/5db9829a-9ffc-49f0-b030-93ee92f5c248" width="360"> 
<img src="https://github.com/vercel/next.js/assets/4800338/b9527b83-27c8-4426-9c0d-c0d4072b7d58" width="360">





### error status

#### After vs Before

<img src="https://github.com/vercel/next.js/assets/4800338/00455226-ace7-468b-8d90-0d36bf038489" height="120"> 
<img src="https://github.com/vercel/next.js/assets/4800338/1be8c451-d3f0-465c-9ef7-6b0dde7cff85" height="120"> 



## Streamlization

If you have customized envs and experiments Next.js will give the brief in the early summary about your network information, env vars, and enabled experimental features

<img src="https://github.com/vercel/next.js/assets/4800338/ca1a7409-1532-46cb-850f-687e61e587b2" width="400">


## Polish

### fetching logging structure 

#### After vs Before
<img src="https://github.com/vercel/next.js/assets/4800338/97526397-dffe-4736-88ed-e5cbe5e945bd" width="400">
<img src="https://github.com/vercel/next.js/assets/4800338/ab77c907-5ab5-48bb-8347-6146d2e60932" width="400">


### Dedupe Duplicates

The logging is moved from `@next/env` to `next` itself, `@next/env` will only notify the invoker that the env is reloaded. Then the duplicated logs for the env reloading cases can be avoid.

#### After vs Before
<img src="https://github.com/vercel/next.js/assets/4800338/04799295-e739-4035-87aa-61cec962fc39" width="400">
<img src="https://github.com/vercel/next.js/assets/4800338/e29020c9-0031-4bf3-a21b-8b64633f43a2" width="400"> 


### Different indicators

Use unicode text icons for different situation: 
* passed -> check mark
* warning -> warning
* error -> red cross
* loading -> circle

<img src="https://github.com/vercel/next.js/assets/4800338/715c34bd-298f-4990-a5d7-e12e455ead44" width="400">



Co-authored-by: Tim Neutkens <6324199+timneutkens@users.noreply.github.com>
2023-09-05 11:40:00 +00:00
Damien Simonin Feugas
3338dd0b31
fix(next-swc): skips client/server only checks when running with Jest to unblock testing (#54891)
Co-authored-by: Jiachi Liu <inbox@huozhi.im>
2023-09-04 13:53:41 +02:00
Shu Ding
ef6140723a
Ensure that barrel files behind wildcards are transformed into shortpath (#54951)
This fix ensures the case that if you `import { foo } from './index'`,
where:

index.js:

```js
export * from './subpkg'
```

subpkg.js:

```js

const unrelatedExpressions = ...

export { foo } from './foo'
export { bar } from './bar'
```

Previously we'll transform the proxy to the second module to `export {
foo } from './subpkg'`. With this fix it will be correctly optimized as
`export { foo } from './foo'` which is a shorter path.
2023-09-03 13:02:14 -07:00