Server entries no longer load 'use client' modules
The main entry and next/middleware re-exported module barrels that also export React client components (LanguageSwitcher, AlternateLinks*, AccessibilityWidget), so payload.config, the CLI and the proxy bundle evaluated React UI. They now import concrete files; i18n client components moved to modules/i18n/client.ts. test/entryGraph guards it. Also lint fixes in tests added on this branch. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e5d86b8afe
commit
14fd2c497f
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* The main entry (payload.config, the payload CLI, server code) and the proxy
|
||||
* entry must not load 'use client' modules: in plain Node every re-exported
|
||||
* module is evaluated, and the proxy bundle should stay free of React UI.
|
||||
*/
|
||||
import { existsSync, readFileSync } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { describe, expect, test } from 'vitest'
|
||||
|
||||
const root = path.resolve(import.meta.dirname, '..')
|
||||
|
||||
function resolve(from: string, spec: string): null | string {
|
||||
if (!spec.startsWith('.')) {return null}
|
||||
const base = path.resolve(path.dirname(from), spec).replace(/\.js$/, '')
|
||||
for (const ext of ['.ts', '.tsx', '/index.ts']) {
|
||||
if (existsSync(base + ext)) {return base + ext}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/** Runtime (non-type) imports and re-exports reachable from `entry`; returns the 'use client' ones. */
|
||||
function clientModulesReachable(entry: string): string[] {
|
||||
const seen = new Set<string>()
|
||||
const hits: string[] = []
|
||||
const stack = [path.join(root, entry)]
|
||||
while (stack.length) {
|
||||
const file = stack.pop()!
|
||||
if (seen.has(file)) {continue}
|
||||
seen.add(file)
|
||||
const src = readFileSync(file, 'utf8')
|
||||
if (/^\s*['"]use client['"]/.test(src)) {hits.push(path.relative(root, file))}
|
||||
for (const m of src.matchAll(/^(?:import|export) (?!type\b)[^'"]*?from ['"]([^'"]+)['"]/gm)) {
|
||||
const next = resolve(file, m[1])
|
||||
if (next) {stack.push(next)}
|
||||
}
|
||||
}
|
||||
return hits.sort()
|
||||
}
|
||||
|
||||
describe('server entries load no client modules', () => {
|
||||
test.each(['src/exports/next-middleware.ts', 'src/index.ts'])('%s', (entry) => {
|
||||
expect(clientModulesReachable(entry)).toEqual([])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user