Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jul 16, 2024
1 parent 4817063 commit aec7ac2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 70 deletions.
86 changes: 43 additions & 43 deletions src/build/advanced-api-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
}),
)
}
48 changes: 24 additions & 24 deletions src/build/content/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -311,30 +335,6 @@ export const copyNextDependencies = async (ctx: PluginContext): Promise<void> =>
})
}

/**
* 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'))

Expand Down
6 changes: 3 additions & 3 deletions src/build/functions/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const augmentMatchers = (
})
}

const getHandlerName = ({ name }: Pick<NextDefinition, 'name'>): 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 })
Expand Down Expand Up @@ -149,9 +152,6 @@ const createEdgeHandler = async (ctx: PluginContext, definition: NextDefinition)
await writeHandlerFile(ctx, definition)
}

const getHandlerName = ({ name }: Pick<NextDefinition, 'name'>): string =>
`${EDGE_HANDLER_NAME}-${name.replace(/\W/g, '-')}`

const buildHandlerDefinition = (
ctx: PluginContext,
{ name, matchers, page }: NextDefinition,
Expand Down

0 comments on commit aec7ac2

Please sign in to comment.