Skip to content

Commit 1bdc972

Browse files
authored
Turbopack build: Move Turbopack marker to SERVER_FILES_MANIFEST (#77711)
### What? As recommended by @ijjk. Replaces the `IS_TURBOPACK_BUILD_FILE` marker with a `turbopack` boolean property in the required server files manifest. Would like a review of this because it does mean we have to ship the require server files manifest into the standalone output which seemingly did not happen before. If we'd want to avoid that the current approach of writing a small file is better. ### How? - Added a `turbopack` boolean property to the `RequiredServerFilesManifest` interface - Removed the `IS_TURBOPACK_BUILD_FILE` constant and related file operations - Updated the server to check the manifest property instead of looking for the marker file - Updated tests to verify the manifest property instead of the marker file
1 parent c41991b commit 1bdc972

File tree

5 files changed

+14
-17
lines changed

5 files changed

+14
-17
lines changed

Diff for: packages/next/src/build/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ import {
8181
UNDERSCORE_NOT_FOUND_ROUTE,
8282
DYNAMIC_CSS_MANIFEST,
8383
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST,
84-
IS_TURBOPACK_BUILD_FILE,
8584
} from '../shared/lib/constants'
8685
import {
8786
getSortedRoutes,
@@ -1453,7 +1452,6 @@ export default async function build(
14531452
let shutdownPromise = Promise.resolve()
14541453
if (!isGenerateMode) {
14551454
if (turboNextBuild) {
1456-
await writeFileUtf8(path.join(distDir, IS_TURBOPACK_BUILD_FILE), '')
14571455
const {
14581456
duration: compilerDuration,
14591457
shutdownPromise: p,
@@ -2279,6 +2277,7 @@ export default async function build(
22792277

22802278
// @ts-expect-error internal field TODO: fix this, should use a separate mechanism to pass the info.
22812279
isExperimentalCompile: isCompileMode,
2280+
isTurbopackBuild: turboNextBuild,
22822281
},
22832282
},
22842283
appDir: dir,
@@ -2334,7 +2333,6 @@ export default async function build(
23342333
]
23352334
: []),
23362335
BUILD_ID_FILE,
2337-
turboNextBuild ? IS_TURBOPACK_BUILD_FILE : null,
23382336
path.join(SERVER_DIRECTORY, NEXT_FONT_MANIFEST + '.js'),
23392337
path.join(SERVER_DIRECTORY, NEXT_FONT_MANIFEST + '.json'),
23402338
...instrumentationHookEntryFiles,

Diff for: packages/next/src/server/next-server.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import {
4242
PHASE_PRODUCTION_BUILD,
4343
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY,
4444
FUNCTIONS_CONFIG_MANIFEST,
45-
IS_TURBOPACK_BUILD_FILE,
4645
} from '../shared/lib/constants'
4746
import { findDir } from '../lib/find-pages-dir'
4847
import { NodeNextRequest, NodeNextResponse } from './base-http/node'
@@ -193,9 +192,9 @@ export default class NextNodeServer extends BaseServer<
193192
this.isDev = isDev
194193
this.sriEnabled = Boolean(options.conf.experimental?.sri?.algorithm)
195194

196-
const isTurbopackBuild = this.isTurbopackBuild()
197-
198-
if (!isDev) {
195+
// @ts-expect-error internal field not publicly exposed
196+
const isTurbopackBuild = this.nextConfig.experimental?.isTurbopackBuild
197+
if (!isDev && typeof isTurbopackBuild !== 'undefined') {
199198
if (process.env.TURBOPACK && !isTurbopackBuild) {
200199
throw new Error(
201200
`Invariant: --turbopack is set but the build used Webpack`
@@ -526,11 +525,6 @@ export default class NextNodeServer extends BaseServer<
526525
}
527526
}
528527

529-
private isTurbopackBuild(): boolean {
530-
const isTurbopackBuildFile = join(this.distDir, IS_TURBOPACK_BUILD_FILE)
531-
return fs.existsSync(isTurbopackBuildFile)
532-
}
533-
534528
protected getEnabledDirectories(dev: boolean): NextEnabledDirectories {
535529
const dir = dev ? this.dir : this.serverDistDir
536530

Diff for: packages/next/src/server/next.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,18 @@ export class NextServer implements NextWrapperServer {
231231
)
232232

233233
// check serialized build config when available
234-
if (process.env.NODE_ENV === 'production') {
234+
if (!this.options.dev) {
235235
try {
236236
const serializedConfig = require(
237-
path.join(dir, '.next', SERVER_FILES_MANIFEST)
237+
path.join(dir, config.distDir, SERVER_FILES_MANIFEST)
238238
).config
239239

240240
// @ts-expect-error internal field
241241
config.experimental.isExperimentalCompile =
242242
serializedConfig.experimental.isExperimentalCompile
243+
// @ts-expect-error internal field
244+
config.experimental.isTurbopackBuild =
245+
serializedConfig.experimental.isTurbopackBuild
243246
} catch (_) {
244247
// if distDir is customized we don't know until we
245248
// load the config so fallback to loading the config

Diff for: packages/next/src/shared/lib/constants.ts

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export const CONFIG_FILES = [
5656
'next.config.ts',
5757
]
5858
export const BUILD_ID_FILE = 'BUILD_ID'
59-
export const IS_TURBOPACK_BUILD_FILE = 'IS_TURBOPACK_BUILD'
6059
export const BLOCKED_PAGES = ['/_document', '/_app', '/_error']
6160
export const CLIENT_PUBLIC_FILES_PATH = 'public'
6261
export const CLIENT_STATIC_FILES_PATH = 'static'

Diff for: test/production/app-dir/turbopack-build-marker/turbopack-build-marker.test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ describe('turbopack-build-marker', () => {
55
})
66

77
it('should have Turbopack build marker', async () => {
8-
expect(await next.hasFile('.next/IS_TURBOPACK_BUILD')).toBe(
9-
!!process.env.TURBOPACK
8+
const requiredServerFilesManifest = await next.readJSON(
9+
'.next/required-server-files.json'
1010
)
11+
expect(
12+
requiredServerFilesManifest.config.experimental.isTurbopackBuild
13+
).toBe(!!process.env.TURBOPACK)
1114
})
1215
})

0 commit comments

Comments
 (0)