Skip to content

Commit

Permalink
hot-reloader-turbopack refactors (#61929)
Browse files Browse the repository at this point in the history
## What?

Follow-up to #61917. Splitting out more code from the development
handling so that it can be used for builds too in a later 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 #

-->


Closes NEXT-2429
  • Loading branch information
timneutkens authored Feb 12, 2024
1 parent cc1d40f commit 94c0152
Show file tree
Hide file tree
Showing 3 changed files with 423 additions and 381 deletions.
48 changes: 42 additions & 6 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ import { isEdgeRuntime } from '../lib/is-edge-runtime'
import { recursiveCopy } from '../lib/recursive-copy'
import { recursiveReadDir } from '../lib/recursive-readdir'
import {
loadBindings,
lockfilePatchPromise,
teardownTraceSubscriber,
teardownHeapProfiler,
createDefineEnv,
} from './swc'
import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex'
import { getFilesInDir } from '../lib/get-files-in-dir'
Expand Down Expand Up @@ -164,6 +166,7 @@ import { hasCustomExportOutput } from '../export/utils'
import { interopDefault } from '../lib/interop-default'
import { formatDynamicImportPath } from '../lib/format-dynamic-import-path'
import { isInterceptionRouteAppPath } from '../server/future/helpers/interception-routes'
import { getTurbopackJsConfig } from '../server/dev/turbopack-utils'
import { buildCustomRoute } from '../lib/build-custom-route'

interface ExperimentalBypassForInfo {
Expand Down Expand Up @@ -720,6 +723,13 @@ export default async function build(
.traceAsyncFn(() => loadCustomRoutes(config))

const { headers, rewrites, redirects } = customRoutes
const combinedRewrites: Rewrite[] = [
...rewrites.beforeFiles,
...rewrites.afterFiles,
...rewrites.fallback,
]
const hasRewrites = combinedRewrites.length > 0

NextBuildContext.originalRewrites = config._originalRewrites
NextBuildContext.originalRedirects = config._originalRedirects

Expand Down Expand Up @@ -1132,12 +1142,6 @@ export default async function build(
}
}

const combinedRewrites: Rewrite[] = [
...rewrites.beforeFiles,
...rewrites.afterFiles,
...rewrites.fallback,
]

if (config.experimental.clientRouterFilter) {
const nonInternalRedirects = (config._originalRedirects || []).filter(
(r: any) => !r.internal
Expand Down Expand Up @@ -1306,6 +1310,38 @@ export default async function build(
})

async function turbopackBuild(): Promise<never> {
if (!process.env.TURBOPACK || !process.env.TURBOPACK_BUILD) {
throw new Error("next build doesn't support turbopack yet")
}
const bindings = await loadBindings(config?.experimental?.useWasmBinary)
const project = await bindings.turbo.createProject({
projectPath: dir,
rootPath: config.experimental.outputFileTracingRoot || dir,
nextConfig: config.nextConfig,
jsConfig: await getTurbopackJsConfig(dir, config),
watch: false,
env: process.env as Record<string, string>,
defineEnv: createDefineEnv({
isTurbopack: true,
allowedRevalidateHeaderKeys: undefined,
clientRouterFilters: undefined,
config,
dev: false,
distDir,
fetchCacheKeyPrefix: undefined,
hasRewrites,
middlewareMatchers: undefined,
previewModeId: undefined,
}),
// TODO: fix
// serverAddr: `127.0.0.1:${opts.port}`,
serverAddr: `127.0.0.1:3000`,
})

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const iter = project.entrypointsSubscribe()
// for await (const entrypoints of iter) {
// }
throw new Error("next build doesn't support turbopack yet")
}
let buildTraceContext: undefined | BuildTraceContext
Expand Down
Loading

0 comments on commit 94c0152

Please sign in to comment.