/** * 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() 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([]) }) })