|
| 1 | +/* eslint-disable max-lines */ |
1 | 2 | import type { NetlifyConfig, NetlifyPluginConstants } from '@netlify/build'
|
2 | 3 | import bridgeFile from '@vercel/node-bridge'
|
| 4 | +import chalk from 'chalk' |
3 | 5 | import destr from 'destr'
|
4 |
| -import { copyFile, ensureDir, writeFile, writeJSON } from 'fs-extra' |
| 6 | +import { copyFile, ensureDir, existsSync, readJSON, writeFile, writeJSON } from 'fs-extra' |
5 | 7 | import type { ImageConfigComplete, RemotePattern } from 'next/dist/shared/lib/image-config'
|
| 8 | +import { outdent } from 'outdent' |
6 | 9 | import { join, relative, resolve } from 'pathe'
|
7 | 10 |
|
8 | 11 | import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME, DEFAULT_FUNCTIONS_SRC } from '../constants'
|
| 12 | +import { getApiHandler } from '../templates/getApiHandler' |
9 | 13 | import { getHandler } from '../templates/getHandler'
|
10 |
| -import { getPageResolver } from '../templates/getPageResolver' |
| 14 | +import { getPageResolver, getSinglePageResolver } from '../templates/getPageResolver' |
| 15 | + |
| 16 | +import { ApiConfig, ApiRouteType, extractConfigFromFile } from './analysis' |
| 17 | +import { getSourceFileForPage } from './files' |
| 18 | +import { getFunctionNameForPage } from './utils' |
| 19 | + |
| 20 | +export interface ApiRouteConfig { |
| 21 | + route: string |
| 22 | + config: ApiConfig |
| 23 | + compiled: string |
| 24 | +} |
11 | 25 |
|
12 | 26 | export const generateFunctions = async (
|
13 | 27 | { FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR }: NetlifyPluginConstants,
|
14 | 28 | appDir: string,
|
| 29 | + apiRoutes: Array<ApiRouteConfig>, |
15 | 30 | ): Promise<void> => {
|
16 |
| - const functionsDir = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC |
17 |
| - const functionDir = join(process.cwd(), functionsDir, HANDLER_FUNCTION_NAME) |
18 |
| - const publishDir = relative(functionDir, resolve(PUBLISH_DIR)) |
| 31 | + const publish = resolve(PUBLISH_DIR) |
| 32 | + const functionsDir = resolve(INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC) |
| 33 | + console.log({ functionsDir }) |
| 34 | + const functionDir = join(functionsDir, HANDLER_FUNCTION_NAME) |
| 35 | + const publishDir = relative(functionDir, publish) |
19 | 36 |
|
20 |
| - const writeHandler = async (func: string, isODB: boolean) => { |
| 37 | + for (const { route, config, compiled } of apiRoutes) { |
| 38 | + const apiHandlerSource = await getApiHandler({ |
| 39 | + page: route, |
| 40 | + config, |
| 41 | + }) |
| 42 | + const functionName = getFunctionNameForPage(route, config.type === ApiRouteType.BACKGROUND) |
| 43 | + await ensureDir(join(functionsDir, functionName)) |
| 44 | + await writeFile(join(functionsDir, functionName, `${functionName}.js`), apiHandlerSource) |
| 45 | + await copyFile(bridgeFile, join(functionsDir, functionName, 'bridge.js')) |
| 46 | + await copyFile( |
| 47 | + join(__dirname, '..', '..', 'lib', 'templates', 'handlerUtils.js'), |
| 48 | + join(functionsDir, functionName, 'handlerUtils.js'), |
| 49 | + ) |
| 50 | + const resolverSource = await getSinglePageResolver({ |
| 51 | + functionsDir, |
| 52 | + sourceFile: join(publish, 'server', compiled), |
| 53 | + }) |
| 54 | + await writeFile(join(functionsDir, functionName, 'pages.js'), resolverSource) |
| 55 | + } |
| 56 | + |
| 57 | + const writeHandler = async (functionName: string, isODB: boolean) => { |
21 | 58 | const handlerSource = await getHandler({ isODB, publishDir, appDir: relative(functionDir, appDir) })
|
22 |
| - await ensureDir(join(functionsDir, func)) |
23 |
| - await writeFile(join(functionsDir, func, `${func}.js`), handlerSource) |
24 |
| - await copyFile(bridgeFile, join(functionsDir, func, 'bridge.js')) |
| 59 | + await ensureDir(join(functionsDir, functionName)) |
| 60 | + await writeFile(join(functionsDir, functionName, `${functionName}.js`), handlerSource) |
| 61 | + await copyFile(bridgeFile, join(functionsDir, functionName, 'bridge.js')) |
25 | 62 | await copyFile(
|
26 | 63 | join(__dirname, '..', '..', 'lib', 'templates', 'handlerUtils.js'),
|
27 |
| - join(functionsDir, func, 'handlerUtils.js'), |
| 64 | + join(functionsDir, functionName, 'handlerUtils.js'), |
28 | 65 | )
|
29 | 66 | }
|
30 | 67 |
|
@@ -124,3 +161,58 @@ export const setupImageFunction = async ({
|
124 | 161 | })
|
125 | 162 | }
|
126 | 163 | }
|
| 164 | + |
| 165 | +/** |
| 166 | + * Look for API routes, and extract the config from the source file. |
| 167 | + */ |
| 168 | +export const getApiRouteConfigs = async (publish: string, baseDir: string): Promise<Array<ApiRouteConfig>> => { |
| 169 | + const pages = await readJSON(join(publish, 'server', 'pages-manifest.json')) |
| 170 | + const apiRoutes = Object.keys(pages).filter((page) => page.startsWith('/api/')) |
| 171 | + const pagesDir = join(baseDir, 'pages') |
| 172 | + return Promise.all( |
| 173 | + apiRoutes.map(async (apiRoute) => { |
| 174 | + const filePath = getSourceFileForPage(apiRoute, pagesDir) |
| 175 | + return { route: apiRoute, config: await extractConfigFromFile(filePath), compiled: pages[apiRoute] } |
| 176 | + }), |
| 177 | + ) |
| 178 | +} |
| 179 | + |
| 180 | +interface FunctionsManifest { |
| 181 | + functions: Array<{ name: string; schedule?: string }> |
| 182 | +} |
| 183 | + |
| 184 | +/** |
| 185 | + * Warn the user of the caveats if they're using background or scheduled API routes |
| 186 | + */ |
| 187 | + |
| 188 | +export const warnOnApiRoutes = async ({ |
| 189 | + FUNCTIONS_DIST, |
| 190 | +}: Pick<NetlifyPluginConstants, 'FUNCTIONS_DIST'>): Promise<void> => { |
| 191 | + const functionsManifestPath = join(FUNCTIONS_DIST, 'manifest.json') |
| 192 | + if (!existsSync(functionsManifestPath)) { |
| 193 | + return |
| 194 | + } |
| 195 | + |
| 196 | + const { functions }: FunctionsManifest = await readJSON(functionsManifestPath) |
| 197 | + |
| 198 | + if (functions.some((func) => func.name.endsWith('-background'))) { |
| 199 | + console.warn( |
| 200 | + outdent` |
| 201 | + ${chalk.yellowBright`Using background API routes`} |
| 202 | + If your account type does not support background functions, the deploy will fail. |
| 203 | + During local development, background API routes will run as regular API routes, but in production they will immediately return an empty "202 Accepted" response. |
| 204 | + `, |
| 205 | + ) |
| 206 | + } |
| 207 | + |
| 208 | + if (functions.some((func) => func.schedule)) { |
| 209 | + console.warn( |
| 210 | + outdent` |
| 211 | + ${chalk.yellowBright`Using scheduled API routes`} |
| 212 | + These are run on a schedule when deployed to production. |
| 213 | + You can test them locally by loading them in your browser but this will not be available when deployed, and any returned value is ignored. |
| 214 | + `, |
| 215 | + ) |
| 216 | + } |
| 217 | +} |
| 218 | +/* eslint-enable max-lines */ |
0 commit comments