|
1 | 1 | import type { NetlifyConfig, NetlifyPluginConstants } from '@netlify/build'
|
2 | 2 | import bridgeFile from '@vercel/node-bridge'
|
3 |
| -import { copyFile, ensureDir, writeFile, writeJSON } from 'fs-extra' |
| 3 | +import { copyFile, ensureDir, readJSON, writeFile, writeJSON } from 'fs-extra' |
4 | 4 | import type { ImageConfigComplete, RemotePattern } from 'next/dist/shared/lib/image-config'
|
5 | 5 | import { join, relative, resolve } from 'pathe'
|
6 | 6 |
|
7 | 7 | import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME, DEFAULT_FUNCTIONS_SRC } from '../constants'
|
| 8 | +import { getApiHandler } from '../templates/getApiHandler' |
8 | 9 | import { getHandler } from '../templates/getHandler'
|
9 |
| -import { getPageResolver } from '../templates/getPageResolver' |
| 10 | +import { getPageResolver, getSinglePageResolver } from '../templates/getPageResolver' |
| 11 | + |
| 12 | +import { ApiConfig, extractConfigFromFile } from './analysis' |
| 13 | +import { getSourceFileForPage } from './files' |
| 14 | +import { getFunctionNameForPage } from './utils' |
| 15 | + |
| 16 | +export interface ApiRouteConfig { |
| 17 | + route: string |
| 18 | + config: ApiConfig |
| 19 | + compiled: string |
| 20 | +} |
10 | 21 |
|
11 | 22 | export const generateFunctions = async (
|
12 | 23 | { FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR }: NetlifyPluginConstants,
|
13 | 24 | appDir: string,
|
| 25 | + apiRoutes: Array<ApiRouteConfig>, |
14 | 26 | ): Promise<void> => {
|
15 |
| - const functionsDir = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC |
16 |
| - const functionDir = join(process.cwd(), functionsDir, HANDLER_FUNCTION_NAME) |
17 |
| - const publishDir = relative(functionDir, resolve(PUBLISH_DIR)) |
| 27 | + const publish = resolve(PUBLISH_DIR) |
| 28 | + const functionsDir = resolve(INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC) |
| 29 | + console.log({ functionsDir }) |
| 30 | + const functionDir = join(functionsDir, HANDLER_FUNCTION_NAME) |
| 31 | + const publishDir = relative(functionDir, publish) |
| 32 | + |
| 33 | + for (const { route, config, compiled } of apiRoutes) { |
| 34 | + const apiHandlerSource = await getApiHandler({ |
| 35 | + page: route, |
| 36 | + config, |
| 37 | + }) |
| 38 | + const functionName = getFunctionNameForPage(route, config.type === 'experimental-background') |
| 39 | + await ensureDir(join(functionsDir, functionName)) |
| 40 | + await writeFile(join(functionsDir, functionName, `${functionName}.js`), apiHandlerSource) |
| 41 | + await copyFile(bridgeFile, join(functionsDir, functionName, 'bridge.js')) |
| 42 | + await copyFile( |
| 43 | + join(__dirname, '..', '..', 'lib', 'templates', 'handlerUtils.js'), |
| 44 | + join(functionsDir, functionName, 'handlerUtils.js'), |
| 45 | + ) |
| 46 | + const resolverSource = await getSinglePageResolver({ |
| 47 | + functionsDir, |
| 48 | + sourceFile: join(publish, 'server', compiled), |
| 49 | + }) |
| 50 | + await writeFile(join(functionsDir, functionName, 'pages.js'), resolverSource) |
| 51 | + } |
18 | 52 |
|
19 |
| - const writeHandler = async (func: string, isODB: boolean) => { |
| 53 | + const writeHandler = async (functionName: string, isODB: boolean) => { |
20 | 54 | const handlerSource = await getHandler({ isODB, publishDir, appDir: relative(functionDir, appDir) })
|
21 |
| - await ensureDir(join(functionsDir, func)) |
22 |
| - await writeFile(join(functionsDir, func, `${func}.js`), handlerSource) |
23 |
| - await copyFile(bridgeFile, join(functionsDir, func, 'bridge.js')) |
| 55 | + await ensureDir(join(functionsDir, functionName)) |
| 56 | + await writeFile(join(functionsDir, functionName, `${functionName}.js`), handlerSource) |
| 57 | + await copyFile(bridgeFile, join(functionsDir, functionName, 'bridge.js')) |
24 | 58 | await copyFile(
|
25 | 59 | join(__dirname, '..', '..', 'lib', 'templates', 'handlerUtils.js'),
|
26 |
| - join(functionsDir, func, 'handlerUtils.js'), |
| 60 | + join(functionsDir, functionName, 'handlerUtils.js'), |
27 | 61 | )
|
28 | 62 | }
|
29 | 63 |
|
@@ -105,3 +139,18 @@ export const setupImageFunction = async ({
|
105 | 139 | })
|
106 | 140 | }
|
107 | 141 | }
|
| 142 | + |
| 143 | +/** |
| 144 | + * Look for API routes, and extract the config from the source file. |
| 145 | + */ |
| 146 | +export const getApiRouteConfigs = async (publish: string, baseDir: string): Promise<Array<ApiRouteConfig>> => { |
| 147 | + const pages = await readJSON(join(publish, 'server', 'pages-manifest.json')) |
| 148 | + const apiRoutes = Object.keys(pages).filter((page) => page.startsWith('/api/')) |
| 149 | + const pagesDir = join(baseDir, 'pages') |
| 150 | + return Promise.all( |
| 151 | + apiRoutes.map(async (apiRoute) => { |
| 152 | + const filePath = getSourceFileForPage(apiRoute, pagesDir) |
| 153 | + return { route: apiRoute, config: await extractConfigFromFile(filePath), compiled: pages[apiRoute] } |
| 154 | + }), |
| 155 | + ) |
| 156 | +} |
0 commit comments