From 6095d6ab36d5d17347953f4a3bbeb0b01c94d2db Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 26 Jun 2023 18:21:05 +0200 Subject: [PATCH] Only require nextjs-require-cache-hot-reloader related API in dev mode --- packages/next/src/server/lib/render-server.ts | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/next/src/server/lib/render-server.ts b/packages/next/src/server/lib/render-server.ts index 5f4c72feeeddd..35f22c7792cd2 100644 --- a/packages/next/src/server/lib/render-server.ts +++ b/packages/next/src/server/lib/render-server.ts @@ -10,12 +10,8 @@ import '../require-hook' import next from '../next' import { warn } from '../../build/output/log' -import { - deleteCache as _deleteCache, - deleteAppClientCache as _deleteAppClientCache, -} from '../../build/webpack/plugins/nextjs-require-cache-hot-reloader' -import { clearModuleContext as _clearModuleContext } from '../web/sandbox/context' import { getFreePort } from '../lib/worker-utils' + export const WORKER_SELF_EXIT_CODE = 77 const MAXIMUM_HEAP_SIZE_ALLOWED = @@ -28,16 +24,26 @@ let result: hostname: string } +let sandboxContext: undefined | typeof import('../web/sandbox/context') +let requireCacheHotReloader: + | undefined + | typeof import('../../build/webpack/plugins/nextjs-require-cache-hot-reloader') + +if (process.env.NODE_ENV !== 'production') { + sandboxContext = require('../web/sandbox/context') + requireCacheHotReloader = require('../../build/webpack/plugins/nextjs-require-cache-hot-reloader') +} + export function clearModuleContext(target: string, content: string) { - _clearModuleContext(target, content) + sandboxContext?.clearModuleContext(target, content) } export function deleteAppClientCache() { - _deleteAppClientCache() + requireCacheHotReloader?.deleteAppClientCache() } export function deleteCache(filePath: string) { - _deleteCache(filePath) + requireCacheHotReloader?.deleteCache(filePath) } export async function initialize(opts: {