From aec7ac2a8e5c97096a8959437a6f796d8830b34b Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Tue, 16 Jul 2024 18:06:12 +0200 Subject: [PATCH] fix lint --- src/build/advanced-api-routes.ts | 86 ++++++++++++++++---------------- src/build/content/server.ts | 48 +++++++++--------- src/build/functions/edge.ts | 6 +-- 3 files changed, 70 insertions(+), 70 deletions(-) diff --git a/src/build/advanced-api-routes.ts b/src/build/advanced-api-routes.ts index 6cd024424f..6e1c45f7a7 100644 --- a/src/build/advanced-api-routes.ts +++ b/src/build/advanced-api-routes.ts @@ -35,49 +35,6 @@ interface ApiBackgroundConfig { type ApiConfig = ApiStandardConfig | ApiScheduledConfig | ApiBackgroundConfig -export async function getAPIRoutesConfigs(ctx: PluginContext) { - const functionsConfigManifestPath = join( - ctx.publishDir, - 'server', - 'functions-config-manifest.json', - ) - if (!existsSync(functionsConfigManifestPath)) { - // before https://github.com/vercel/next.js/pull/60163 this file might not have been produced if there were no API routes at all - return [] - } - - const functionsConfigManifest = JSON.parse( - await readFile(functionsConfigManifestPath, 'utf-8'), - ) as FunctionsConfigManifest - - const appDir = ctx.resolveFromSiteDir('.') - const pagesDir = join(appDir, 'pages') - const srcPagesDir = join(appDir, 'src', 'pages') - const { pageExtensions } = ctx.requiredServerFiles.config - - return Promise.all( - Object.keys(functionsConfigManifest.functions).map(async (apiRoute) => { - const filePath = getSourceFileForPage(apiRoute, [pagesDir, srcPagesDir], pageExtensions) - - const sharedFields = { - apiRoute, - filePath, - config: {} as ApiConfig, - } - - if (filePath) { - const config = await extractConfigFromFile(filePath, appDir) - return { - ...sharedFields, - config, - } - } - - return sharedFields - }), - ) -} - // Next.js already defines a default `pageExtensions` array in its `required-server-files.json` file // In case it gets `undefined`, this is a fallback const SOURCE_FILE_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'] @@ -186,3 +143,46 @@ const extractConfigFromFile = async (apiFilePath: string, appDir: string): Promi return {} } } + +export async function getAPIRoutesConfigs(ctx: PluginContext) { + const functionsConfigManifestPath = join( + ctx.publishDir, + 'server', + 'functions-config-manifest.json', + ) + if (!existsSync(functionsConfigManifestPath)) { + // before https://github.com/vercel/next.js/pull/60163 this file might not have been produced if there were no API routes at all + return [] + } + + const functionsConfigManifest = JSON.parse( + await readFile(functionsConfigManifestPath, 'utf-8'), + ) as FunctionsConfigManifest + + const appDir = ctx.resolveFromSiteDir('.') + const pagesDir = join(appDir, 'pages') + const srcPagesDir = join(appDir, 'src', 'pages') + const { pageExtensions } = ctx.requiredServerFiles.config + + return Promise.all( + Object.keys(functionsConfigManifest.functions).map(async (apiRoute) => { + const filePath = getSourceFileForPage(apiRoute, [pagesDir, srcPagesDir], pageExtensions) + + const sharedFields = { + apiRoute, + filePath, + config: {} as ApiConfig, + } + + if (filePath) { + const config = await extractConfigFromFile(filePath, appDir) + return { + ...sharedFields, + config, + } + } + + return sharedFields + }), + ) +} diff --git a/src/build/content/server.ts b/src/build/content/server.ts index 39e11ea8c6..7417432d61 100644 --- a/src/build/content/server.ts +++ b/src/build/content/server.ts @@ -29,6 +29,30 @@ function isError(error: unknown): error is NodeJS.ErrnoException { return error instanceof Error } +/** + * Generates a copy of the middleware manifest without any middleware in it. We + * do this because we'll run middleware in an edge function, and we don't want + * to run it again in the server handler. + */ +const replaceMiddlewareManifest = async (sourcePath: string, destPath: string) => { + await mkdir(dirname(destPath), { recursive: true }) + + const data = await readFile(sourcePath, 'utf8') + const manifest = JSON.parse(data) + + // TODO: Check for `manifest.version` and write an error to the system log + // when we find a value that is not equal to 2. This will alert us in case + // Next.js starts using a new format for the manifest and we're writing + // one with the old version. + const newManifest = { + ...manifest, + middleware: {}, + } + const newData = JSON.stringify(newManifest) + + await writeFile(destPath, newData) +} + /** * Copy App/Pages Router Javascript needed by the server handler */ @@ -311,30 +335,6 @@ export const copyNextDependencies = async (ctx: PluginContext): Promise => }) } -/** - * Generates a copy of the middleware manifest without any middleware in it. We - * do this because we'll run middleware in an edge function, and we don't want - * to run it again in the server handler. - */ -const replaceMiddlewareManifest = async (sourcePath: string, destPath: string) => { - await mkdir(dirname(destPath), { recursive: true }) - - const data = await readFile(sourcePath, 'utf8') - const manifest = JSON.parse(data) - - // TODO: Check for `manifest.version` and write an error to the system log - // when we find a value that is not equal to 2. This will alert us in case - // Next.js starts using a new format for the manifest and we're writing - // one with the old version. - const newManifest = { - ...manifest, - middleware: {}, - } - const newData = JSON.stringify(newManifest) - - await writeFile(destPath, newData) -} - export const verifyHandlerDirStructure = async (ctx: PluginContext) => { const runConfig = JSON.parse(await readFile(join(ctx.serverHandlerDir, RUN_CONFIG), 'utf-8')) diff --git a/src/build/functions/edge.ts b/src/build/functions/edge.ts index b6629df28a..aa933d2ccd 100644 --- a/src/build/functions/edge.ts +++ b/src/build/functions/edge.ts @@ -53,6 +53,9 @@ const augmentMatchers = ( }) } +const getHandlerName = ({ name }: Pick): string => + `${EDGE_HANDLER_NAME}-${name.replace(/\W/g, '-')}` + const writeHandlerFile = async (ctx: PluginContext, { matchers, name, page }: NextDefinition) => { const nextConfig = ctx.buildConfig const handlerName = getHandlerName({ name }) @@ -149,9 +152,6 @@ const createEdgeHandler = async (ctx: PluginContext, definition: NextDefinition) await writeHandlerFile(ctx, definition) } -const getHandlerName = ({ name }: Pick): string => - `${EDGE_HANDLER_NAME}-${name.replace(/\W/g, '-')}` - const buildHandlerDefinition = ( ctx: PluginContext, { name, matchers, page }: NextDefinition,