From bb8e5fe9d00546bd68238604f7415d307dcbf458 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Wed, 12 Oct 2022 13:04:34 +0200 Subject: [PATCH] add more lazy modules --- packages/next/server/api-utils/node.ts | 10 ++++++++-- packages/next/server/post-process.ts | 22 +++++++--------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/packages/next/server/api-utils/node.ts b/packages/next/server/api-utils/node.ts index 3900fc214a03a..4c875076d66b9 100644 --- a/packages/next/server/api-utils/node.ts +++ b/packages/next/server/api-utils/node.ts @@ -11,8 +11,6 @@ import type { CookieSerializeOptions } from 'next/dist/compiled/cookie' import type { PreviewData } from 'next/types' import bytes from 'next/dist/compiled/bytes' -import jsonwebtoken from 'next/dist/compiled/jsonwebtoken' -import { decryptWithSecret, encryptWithSecret } from '../crypto-utils' import { generateETag } from '../lib/etag' import { sendEtagResponse } from '../send-payload' import { Stream } from 'stream' @@ -88,6 +86,8 @@ export function tryGetPreviewData( data: string } try { + const jsonwebtoken = + require('next/dist/compiled/jsonwebtoken') as typeof import('next/dist/compiled/jsonwebtoken') encryptedPreviewData = jsonwebtoken.verify( tokenPreviewData, options.previewModeSigningKey @@ -98,6 +98,8 @@ export function tryGetPreviewData( return false } + const { decryptWithSecret } = + require('../crypto-utils') as typeof import('../crypto-utils') const decryptedPreviewData = decryptWithSecret( Buffer.from(options.previewModeEncryptionKey), encryptedPreviewData.data @@ -286,6 +288,10 @@ function setPreviewData( throw new Error('invariant: invalid previewModeSigningKey') } + const jsonwebtoken = + require('next/dist/compiled/jsonwebtoken') as typeof import('next/dist/compiled/jsonwebtoken') + const { encryptWithSecret } = + require('../crypto-utils') as typeof import('../crypto-utils') const payload = jsonwebtoken.sign( { data: encryptWithSecret( diff --git a/packages/next/server/post-process.ts b/packages/next/server/post-process.ts index 220e3e1c048a7..491c41e2b701a 100644 --- a/packages/next/server/post-process.ts +++ b/packages/next/server/post-process.ts @@ -4,21 +4,6 @@ import type { HTMLElement } from 'next/dist/compiled/node-html-parser' import { OPTIMIZED_FONT_PROVIDERS } from '../shared/lib/constants' import { nonNullable } from '../lib/non-nullable' -let optimizeAmp: typeof import('./optimize-amp').default | undefined -let getFontDefinitionFromManifest: - | typeof import('./font-utils').getFontDefinitionFromManifest - | undefined -let parse: typeof import('next/dist/compiled/node-html-parser').parse - -if (process.env.NEXT_RUNTIME !== 'edge') { - optimizeAmp = require('./optimize-amp').default - getFontDefinitionFromManifest = - require('./font-utils').getFontDefinitionFromManifest - parse = ( - require('next/dist/compiled/node-html-parser') as typeof import('next/dist/compiled/node-html-parser') - ).parse -} - type postProcessOptions = { optimizeFonts: any } @@ -56,6 +41,9 @@ async function processHTML( if (!middlewareRegistry[0]) { return html } + + const { parse } = + require('next/dist/compiled/node-html-parser') as typeof import('next/dist/compiled/node-html-parser') const root: HTMLElement = parse(html) let document = html @@ -190,6 +178,8 @@ async function postProcessHTML( const postProcessors: Array<(html: string) => Promise> = [ process.env.NEXT_RUNTIME !== 'edge' && inAmpMode ? async (html: string) => { + const optimizeAmp = require('./optimize-amp') + .default as typeof import('./optimize-amp').default html = await optimizeAmp!(html, renderOpts.ampOptimizerConfig) if (!renderOpts.ampSkipValidation && renderOpts.ampValidator) { await renderOpts.ampValidator(html, pathname) @@ -201,6 +191,8 @@ async function postProcessHTML( ? async (html: string) => { const getFontDefinition = (url: string): string => { if (renderOpts.fontManifest) { + const { getFontDefinitionFromManifest } = + require('./font-utils') as typeof import('./font-utils') return getFontDefinitionFromManifest!( url, renderOpts.fontManifest