diff --git a/packages/next/src/utilities/getPayloadHMR.ts b/packages/next/src/utilities/getPayloadHMR.ts index bdb8ba1b5cf..a3f60f7f414 100644 --- a/packages/next/src/utilities/getPayloadHMR.ts +++ b/packages/next/src/utilities/getPayloadHMR.ts @@ -53,22 +53,21 @@ export const getPayloadHMR = async (options: InitOptions): Promise => { if (cached.payload) { const config = await options.config // TODO: check if we can move this inside the cached.reload === true condition - // Generate types on startup - if (config.typescript.autoGenerate !== false) { - const configHash = quickHash(config) - - if (cached.hashedConfig !== configHash) { - cached.payload.logger.info(`Config has changed, regenerating types... (${configHash})`) - cached.hashedConfig = configHash - await generateTypes(config) - } - } - if (cached.reload === true) { let resolve cached.reload = new Promise((res) => (resolve = res)) + // Generate types on startup + if (config.typescript.autoGenerate !== false) { + const configHash = quickHash(config) + + if (cached.hashedConfig !== configHash) { + cached.hashedConfig = configHash + void generateTypes(config, { log: false }) + } + } + await reload(config, cached.payload) resolve() diff --git a/packages/payload/src/bin/generateTypes.ts b/packages/payload/src/bin/generateTypes.ts index 5aee6c1d713..7d85a84beb9 100644 --- a/packages/payload/src/bin/generateTypes.ts +++ b/packages/payload/src/bin/generateTypes.ts @@ -6,11 +6,16 @@ import type { SanitizedConfig } from '../config/types.js' import { configToJSONSchema } from '../utilities/configToJSONSchema.js' import Logger from '../utilities/logger.js' -export async function generateTypes(config: SanitizedConfig): Promise { +export async function generateTypes( + config: SanitizedConfig, + options?: { log: boolean }, +): Promise { const logger = Logger() const outputFile = process.env.PAYLOAD_TS_OUTPUT_PATH || config.typescript.outputFile - logger.info('Compiling TS types for Collections and Globals...') + const shouldLog = options?.log ?? true + + if (shouldLog) logger.info('Compiling TS types for Collections and Globals...') const jsonSchema = configToJSONSchema(config, config.db.defaultIDType) @@ -37,5 +42,5 @@ export async function generateTypes(config: SanitizedConfig): Promise { } } fs.writeFileSync(outputFile, compiled) - logger.info(`Types written to ${outputFile}`) + if (shouldLog) logger.info(`Types written to ${outputFile}`) } diff --git a/packages/payload/src/config/schema.ts b/packages/payload/src/config/schema.ts index abfb0ffd61d..0526e0e16d9 100644 --- a/packages/payload/src/config/schema.ts +++ b/packages/payload/src/config/schema.ts @@ -189,8 +189,8 @@ export default joi.object({ sharp: joi.any(), telemetry: joi.boolean(), typescript: joi.object({ + autoGenerate: joi.boolean(), declare: joi.alternatives().try(joi.boolean(), joi.object({ ignoreTSError: joi.boolean() })), - generateTypesOnStartup: joi.boolean(), outputFile: joi.string(), }), upload: joi.object(), diff --git a/test/fields/config.ts b/test/fields/config.ts index fb16c7e44eb..ab4ff6b1119 100644 --- a/test/fields/config.ts +++ b/test/fields/config.ts @@ -51,10 +51,10 @@ export const collectionSlugs: CollectionConfig[] = [ type: 'checkbox', defaultValue: true, }, - { - name: 'asdf', - type: 'text', - }, + // { + // name: 'asdf', + // type: 'text', + // }, ], }, ArrayFields, diff --git a/test/fields/payload-types.ts b/test/fields/payload-types.ts index 5f99b4fc5db..f0682a72e78 100644 --- a/test/fields/payload-types.ts +++ b/test/fields/payload-types.ts @@ -261,7 +261,6 @@ export interface LexicalLocalizedField { export interface User { id: string; canViewConditionalField?: boolean | null; - asdf?: string | null; updatedAt: string; createdAt: string; email: string; @@ -1424,5 +1423,6 @@ export interface LexicalBlocksRadioButtonsBlock { declare module 'payload' { + // @ts-ignore export interface GeneratedTypes extends Config {} } \ No newline at end of file