Unverified Commit 8586cdf1 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: add separate turbopackPersistentCachingForBuild/ForDev flags (#84215)

### What?

Move persistent caching for `next build` into a separate config option: `turbopackPersistentCachingForBuild`.
And `next dev` to `turbopackPersistentCachingForDev`.
parent 08773fa0
...@@ -7,7 +7,8 @@ const nextConfig = { ...@@ -7,7 +7,8 @@ const nextConfig = {
ignoreBuildErrors: true, ignoreBuildErrors: true,
}, },
experimental: { experimental: {
turbopackPersistentCaching: process.env.TURBO_CACHE === '1', turbopackPersistentCachingForDev: process.env.TURBO_CACHE === '1',
turbopackPersistentCachingForBuild: process.env.TURBO_CACHE === '1',
}, },
} }
......
...@@ -18,7 +18,10 @@ import type { NextConfig } from 'next' ...@@ -18,7 +18,10 @@ import type { NextConfig } from 'next'
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
experimental: { experimental: {
turbopackPersistentCaching: true, // Enable persistent caching for `next dev`
turbopackPersistentCachingForDev: true,
// Enable persistent caching for `next build`
turbopackPersistentCachingForBuild: true,
}, },
} }
...@@ -29,7 +32,10 @@ export default nextConfig ...@@ -29,7 +32,10 @@ export default nextConfig
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
experimental: { experimental: {
turbopackPersistentCaching: true, // Enable persistent caching for `next dev`
turbopackPersistentCachingForDev: true,
// Enable persistent caching for `next build`
turbopackPersistentCachingForBuild: true,
}, },
} }
...@@ -40,4 +46,5 @@ module.exports = nextConfig ...@@ -40,4 +46,5 @@ module.exports = nextConfig
| Version | Changes | | Version | Changes |
| --------- | ------------------------------------------- | | --------- | ------------------------------------------- |
| `v16.0.0` | Separate flags for build and dev. |
| `v15.5.0` | Persistent caching released as experimental | | `v15.5.0` | Persistent caching released as experimental |
...@@ -140,7 +140,7 @@ We are planning to offer an equivalent to the `innerGraph` optimization in Turbo ...@@ -140,7 +140,7 @@ We are planning to offer an equivalent to the `innerGraph` optimization in Turbo
### Build Caching ### Build Caching
Webpack supports [disk build caching](https://webpack.js.org/configuration/cache/#cache) to speed up builds. We are planning to support an analogous feature in Turbopack but it is not ready yet. On the `next@canary` release you can experiment with our solution by enabling the [`experimental.turbopackPersistentCaching` flag](/docs/app/api-reference/config/next-config-js/turbopackPersistentCaching). Webpack supports [disk build caching](https://webpack.js.org/configuration/cache/#cache) to speed up builds. We are planning to support an analogous feature in Turbopack but it is not ready yet. On the `next@canary` release you can experiment with our solution by enabling the [`experimental.turbopackPersistentCachingForDev`/`experimental.turbopackPersistentCachingForBuild` flags](/docs/app/api-reference/config/next-config-js/turbopackPersistentCaching).
> **Good to know:** For this reason, when comparing webpack and Turbopack performance, make sure to delete the `.next` folder between builds to see a fair comparison. > **Good to know:** For this reason, when comparing webpack and Turbopack performance, make sure to delete the `.next` folder between builds to see a fair comparison.
......
...@@ -331,7 +331,8 @@ export function getDefineEnv({ ...@@ -331,7 +331,8 @@ export function getDefineEnv({
// In the worst case we'll show an option to clear the cache, but it'll be a // In the worst case we'll show an option to clear the cache, but it'll be a
// no-op that just restarts the development server. // no-op that just restarts the development server.
'process.env.__NEXT_BUNDLER_HAS_PERSISTENT_CACHE': 'process.env.__NEXT_BUNDLER_HAS_PERSISTENT_CACHE':
!isTurbopack || (config.experimental.turbopackPersistentCaching ?? false), !isTurbopack ||
(config.experimental.turbopackPersistentCachingForDev ?? false),
'process.env.__NEXT_REACT_DEBUG_CHANNEL': 'process.env.__NEXT_REACT_DEBUG_CHANNEL':
config.experimental.reactDebugChannel ?? false, config.experimental.reactDebugChannel ?? false,
} }
......
...@@ -206,7 +206,7 @@ import { ...@@ -206,7 +206,7 @@ import {
} from '../server/lib/router-utils/build-prefetch-segment-data-route' } from '../server/lib/router-utils/build-prefetch-segment-data-route'
import { turbopackBuild } from './turbopack-build' import { turbopackBuild } from './turbopack-build'
import { isPersistentCachingEnabled } from '../shared/lib/turbopack/utils' import { isPersistentCachingEnabledForBuild } from '../shared/lib/turbopack/utils'
import { inlineStaticEnv } from '../lib/inline-static-env' import { inlineStaticEnv } from '../lib/inline-static-env'
import { populateStaticEnv } from '../lib/static-env' import { populateStaticEnv } from '../lib/static-env'
import { durationToString } from './duration-to-string' import { durationToString } from './duration-to-string'
...@@ -2756,7 +2756,7 @@ export default async function build( ...@@ -2756,7 +2756,7 @@ export default async function build(
}, },
{ {
featureName: 'turbopackPersistentCaching', featureName: 'turbopackPersistentCaching',
invocationCount: isPersistentCachingEnabled(config) ? 1 : 0, invocationCount: isPersistentCachingEnabledForBuild(config) ? 1 : 0,
}, },
] ]
telemetry.record( telemetry.record(
......
...@@ -2,7 +2,7 @@ import path from 'path' ...@@ -2,7 +2,7 @@ import path from 'path'
import { validateTurboNextConfig } from '../../lib/turbopack-warning' import { validateTurboNextConfig } from '../../lib/turbopack-warning'
import { import {
formatIssue, formatIssue,
isPersistentCachingEnabled, isPersistentCachingEnabledForBuild,
isRelevantWarning, isRelevantWarning,
} from '../../shared/lib/turbopack/utils' } from '../../shared/lib/turbopack/utils'
import { NextBuildContext } from '../build-context' import { NextBuildContext } from '../build-context'
...@@ -50,7 +50,7 @@ export async function turbopackBuild(): Promise<{ ...@@ -50,7 +50,7 @@ export async function turbopackBuild(): Promise<{
const supportedBrowsers = getSupportedBrowsers(dir, dev) const supportedBrowsers = getSupportedBrowsers(dir, dev)
const persistentCaching = isPersistentCachingEnabled(config) const persistentCaching = isPersistentCachingEnabledForBuild(config)
const rootPath = config.turbopack?.root || config.outputFileTracingRoot || dir const rootPath = config.turbopack?.root || config.outputFileTracingRoot || dir
const project = await bindings.turbo.createProject( const project = await bindings.turbo.createProject(
{ {
......
...@@ -436,7 +436,8 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -436,7 +436,8 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
webpackMemoryOptimizations: z.boolean().optional(), webpackMemoryOptimizations: z.boolean().optional(),
turbopackMemoryLimit: z.number().optional(), turbopackMemoryLimit: z.number().optional(),
turbopackMinify: z.boolean().optional(), turbopackMinify: z.boolean().optional(),
turbopackPersistentCaching: z.boolean().optional(), turbopackPersistentCachingForDev: z.boolean().optional(),
turbopackPersistentCachingForBuild: z.boolean().optional(),
turbopackSourceMaps: z.boolean().optional(), turbopackSourceMaps: z.boolean().optional(),
turbopackTreeShaking: z.boolean().optional(), turbopackTreeShaking: z.boolean().optional(),
turbopackRemoveUnusedExports: z.boolean().optional(), turbopackRemoveUnusedExports: z.boolean().optional(),
......
...@@ -423,9 +423,14 @@ export interface ExperimentalConfig { ...@@ -423,9 +423,14 @@ export interface ExperimentalConfig {
turbopackScopeHoisting?: boolean turbopackScopeHoisting?: boolean
/** /**
* Enable persistent caching for the turbopack dev server and build. * Enable persistent caching for the turbopack dev server.
*/ */
turbopackPersistentCaching?: boolean turbopackPersistentCachingForDev?: boolean
/**
* Enable persistent caching for the turbopack build.
*/
turbopackPersistentCachingForBuild?: boolean
/** /**
* Enable source maps. Defaults to true. * Enable source maps. Defaults to true.
......
...@@ -133,17 +133,31 @@ describe('loadConfig', () => { ...@@ -133,17 +133,31 @@ describe('loadConfig', () => {
) )
}) })
it('errors when using persistentCaching if not in canary', async () => { it('errors when using persistentCachingForDev if not in canary', async () => {
await expect( await expect(
loadConfig(PHASE_PRODUCTION_BUILD, __dirname, { loadConfig(PHASE_PRODUCTION_BUILD, __dirname, {
customConfig: { customConfig: {
experimental: { experimental: {
turbopackPersistentCaching: true, turbopackPersistentCachingForDev: true,
}, },
}, },
}) })
).rejects.toThrow( ).rejects.toThrow(
/The experimental feature "experimental.turbopackPersistentCaching" can only be enabled when using the latest canary version of Next.js./ /The experimental feature "experimental.turbopackPersistentCachingForDev" can only be enabled when using the latest canary version of Next.js./
)
})
it('errors when using persistentCachingForBuild if not in canary', async () => {
await expect(
loadConfig(PHASE_PRODUCTION_BUILD, __dirname, {
customConfig: {
experimental: {
turbopackPersistentCachingForBuild: true,
},
},
})
).rejects.toThrow(
/The experimental feature "experimental.turbopackPersistentCachingForBuild" can only be enabled when using the latest canary version of Next.js./
) )
}) })
}) })
......
...@@ -354,9 +354,13 @@ function assignDefaultsAndValidate( ...@@ -354,9 +354,13 @@ function assignDefaultsAndValidate(
throw new CanaryOnlyError({ feature: 'experimental.ppr' }) throw new CanaryOnlyError({ feature: 'experimental.ppr' })
} else if (result.experimental?.cacheComponents) { } else if (result.experimental?.cacheComponents) {
throw new CanaryOnlyError({ feature: 'experimental.cacheComponents' }) throw new CanaryOnlyError({ feature: 'experimental.cacheComponents' })
} else if (result.experimental?.turbopackPersistentCaching) { } else if (result.experimental?.turbopackPersistentCachingForDev) {
throw new CanaryOnlyError({ throw new CanaryOnlyError({
feature: 'experimental.turbopackPersistentCaching', feature: 'experimental.turbopackPersistentCachingForDev',
})
} else if (result.experimental?.turbopackPersistentCachingForBuild) {
throw new CanaryOnlyError({
feature: 'experimental.turbopackPersistentCachingForBuild',
}) })
} }
} }
......
...@@ -84,7 +84,7 @@ import { setBundlerFindSourceMapImplementation } from '../patch-error-inspect' ...@@ -84,7 +84,7 @@ import { setBundlerFindSourceMapImplementation } from '../patch-error-inspect'
import { getNextErrorFeedbackMiddleware } from '../../next-devtools/server/get-next-error-feedback-middleware' import { getNextErrorFeedbackMiddleware } from '../../next-devtools/server/get-next-error-feedback-middleware'
import { import {
formatIssue, formatIssue,
isPersistentCachingEnabled, isPersistentCachingEnabledForDev,
isWellKnownError, isWellKnownError,
processIssues, processIssues,
renderStyledStringToErrorAnsi, renderStyledStringToErrorAnsi,
...@@ -273,7 +273,7 @@ export async function createHotReloaderTurbopack( ...@@ -273,7 +273,7 @@ export async function createHotReloaderTurbopack(
currentNodeJsVersion, currentNodeJsVersion,
}, },
{ {
persistentCaching: isPersistentCachingEnabled(opts.nextConfig), persistentCaching: isPersistentCachingEnabledForDev(opts.nextConfig),
memoryLimit: opts.nextConfig.experimental?.turbopackMemoryLimit, memoryLimit: opts.nextConfig.experimental?.turbopackMemoryLimit,
isShortSession: false, isShortSession: false,
} }
......
...@@ -69,7 +69,7 @@ import { normalizeMetadataPageToRoute } from '../../../lib/metadata/get-metadata ...@@ -69,7 +69,7 @@ import { normalizeMetadataPageToRoute } from '../../../lib/metadata/get-metadata
import { JsConfigPathsPlugin } from '../../../build/webpack/plugins/jsconfig-paths-plugin' import { JsConfigPathsPlugin } from '../../../build/webpack/plugins/jsconfig-paths-plugin'
import { store as consoleStore } from '../../../build/output/store' import { store as consoleStore } from '../../../build/output/store'
import { import {
isPersistentCachingEnabled, isPersistentCachingEnabledForDev,
ModuleBuildError, ModuleBuildError,
} from '../../../shared/lib/turbopack/utils' } from '../../../shared/lib/turbopack/utils'
import { getDefineEnv } from '../../../build/define-env' import { getDefineEnv } from '../../../build/define-env'
...@@ -1239,7 +1239,9 @@ export async function setupDevBundler(opts: SetupOpts) { ...@@ -1239,7 +1239,9 @@ export async function setupDevBundler(opts: SetupOpts) {
eventName: EVENT_BUILD_FEATURE_USAGE, eventName: EVENT_BUILD_FEATURE_USAGE,
payload: { payload: {
featureName: 'turbopackPersistentCaching', featureName: 'turbopackPersistentCaching',
invocationCount: isPersistentCachingEnabled(opts.nextConfig) ? 1 : 0, invocationCount: isPersistentCachingEnabledForDev(opts.nextConfig)
? 1
: 0,
}, },
}) })
......
...@@ -314,8 +314,14 @@ export function renderStyledStringToErrorAnsi(string: StyledString): string { ...@@ -314,8 +314,14 @@ export function renderStyledStringToErrorAnsi(string: StyledString): string {
} }
} }
export function isPersistentCachingEnabled( export function isPersistentCachingEnabledForDev(
config: NextConfigComplete config: NextConfigComplete
): boolean { ): boolean {
return config.experimental?.turbopackPersistentCaching || false return config.experimental?.turbopackPersistentCachingForDev || false
}
export function isPersistentCachingEnabledForBuild(
config: NextConfigComplete
): boolean {
return config.experimental?.turbopackPersistentCachingForBuild || false
} }
...@@ -16,7 +16,8 @@ const nextConfig = { ...@@ -16,7 +16,8 @@ const nextConfig = {
}, },
}, },
experimental: { experimental: {
turbopackPersistentCaching: true, turbopackPersistentCachingForDev: true,
turbopackPersistentCachingForBuild: true,
}, },
env: { env: {
NEXT_PUBLIC_CONFIG_ENV: 'hello world', NEXT_PUBLIC_CONFIG_ENV: 'hello world',
......
module.exports = { module.exports = {
experimental: { experimental: {
turbopackPersistentCaching: true turbopackPersistentCachingForDev: true,
turbopackPersistentCachingForBuild: true,
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment