1
- const { copyFile, ensureDir, writeFile, writeJSON } = require ( 'fs-extra' )
2
- const { join, relative } = require ( 'pathe' )
1
+ import { NetlifyConfig , NetlifyPluginConstants } from '@netlify/build'
2
+ import { copyFile , ensureDir , writeFile , writeJSON } from 'fs-extra'
3
+ import type { ImageConfigComplete } from 'next/dist/server/image-config'
4
+ import { join , relative } from 'pathe'
3
5
4
- const { HANDLER_FUNCTION_NAME , ODB_FUNCTION_NAME , IMAGE_FUNCTION_NAME } = require ( '../constants' )
5
- const getHandler = require ( '../templates/getHandler' )
6
- const { getPageResolver } = require ( '../templates/getPageResolver' )
6
+ import { HANDLER_FUNCTION_NAME , ODB_FUNCTION_NAME , IMAGE_FUNCTION_NAME , DEFAULT_FUNCTIONS_SRC } from '../constants'
7
+ import getHandler from '../templates/getHandler'
8
+ import { getPageResolver } from '../templates/getPageResolver'
7
9
8
- const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'
9
-
10
- exports . generateFunctions = async (
11
- { FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC , INTERNAL_FUNCTIONS_SRC , PUBLISH_DIR } ,
12
- appDir ,
13
- ) => {
10
+ export const generateFunctions = async (
11
+ { FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC , INTERNAL_FUNCTIONS_SRC , PUBLISH_DIR } : NetlifyPluginConstants ,
12
+ appDir : string ,
13
+ ) : Promise < void > => {
14
14
const functionsDir = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
15
15
const bridgeFile = require . resolve ( '@vercel/node/dist/bridge' )
16
16
17
17
const functionDir = join ( process . cwd ( ) , functionsDir , HANDLER_FUNCTION_NAME )
18
18
const publishDir = relative ( functionDir , join ( process . cwd ( ) , PUBLISH_DIR ) )
19
19
20
- const writeHandler = async ( func , isODB ) => {
20
+ const writeHandler = async ( func : string , isODB : boolean ) => {
21
21
const handlerSource = await getHandler ( { isODB, publishDir, appDir : relative ( functionDir , appDir ) } )
22
22
await ensureDir ( join ( functionsDir , func ) )
23
23
await writeFile ( join ( functionsDir , func , `${ func } .js` ) , handlerSource )
@@ -36,11 +36,15 @@ exports.generateFunctions = async (
36
36
* Writes a file in each function directory that contains references to every page entrypoint.
37
37
* This is just so that the nft bundler knows about them. We'll eventually do this better.
38
38
*/
39
- exports . generatePagesResolver = async ( {
39
+ export const generatePagesResolver = async ( {
40
40
constants : { INTERNAL_FUNCTIONS_SRC , FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC } ,
41
41
netlifyConfig,
42
42
target,
43
- } ) => {
43
+ } : {
44
+ constants : NetlifyPluginConstants
45
+ netlifyConfig : NetlifyConfig
46
+ target : string
47
+ } ) : Promise < void > => {
44
48
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
45
49
46
50
const jsSource = await getPageResolver ( {
@@ -53,12 +57,17 @@ exports.generatePagesResolver = async ({
53
57
}
54
58
55
59
// Move our next/image function into the correct functions directory
56
- exports . setupImageFunction = async ( {
60
+ export const setupImageFunction = async ( {
57
61
constants : { INTERNAL_FUNCTIONS_SRC , FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC } ,
58
62
imageconfig = { } ,
59
63
netlifyConfig,
60
64
basePath,
61
- } ) => {
65
+ } : {
66
+ constants : NetlifyPluginConstants
67
+ netlifyConfig : NetlifyConfig
68
+ basePath : string
69
+ imageconfig : Partial < ImageConfigComplete >
70
+ } ) : Promise < void > => {
62
71
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
63
72
const functionName = `${ IMAGE_FUNCTION_NAME } .js`
64
73
const functionDirectory = join ( functionsPath , IMAGE_FUNCTION_NAME )
0 commit comments