From 814d09deba6be4c9cdbe89aa51836fbcdc451d3c Mon Sep 17 00:00:00 2001 From: taty2010 Date: Tue, 7 Feb 2023 14:05:17 -0600 Subject: [PATCH 1/6] fix: skip writeFunctions if there are no functions --- plugin/src/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugin/src/index.ts b/plugin/src/index.ts index 84e73b41..fed16a42 100644 --- a/plugin/src/index.ts +++ b/plugin/src/index.ts @@ -69,9 +69,11 @@ The plugin no longer uses this and it should be deleted to avoid conflicts.\n`) console.log('Creating site data metadata file') await createMetadataFileAndCopyDatastore(PUBLISH_DIR, cacheDir) } - - await writeFunctions({ constants, netlifyConfig, neededFunctions }) - +// Gatsby V2 does not have functions so we can skip this step + if (neededFunctions.length !== 0) { + const helperFunctions = await import('./helpers/functions') + await helperFunctions.writeFunctions({ constants, netlifyConfig, neededFunctions }) + } await modifyConfig({ netlifyConfig, cacheDir, neededFunctions }) await modifyFiles({ netlifyConfig, neededFunctions }) From 6517f70ac2ea286e1756411fc0780617979488e5 Mon Sep 17 00:00:00 2001 From: taty2010 Date: Tue, 7 Feb 2023 14:15:54 -0600 Subject: [PATCH 2/6] fix: removed top level writeFunctions import --- plugin/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/index.ts b/plugin/src/index.ts index fed16a42..23f43461 100644 --- a/plugin/src/index.ts +++ b/plugin/src/index.ts @@ -16,7 +16,7 @@ import { shouldSkipBundlingDatastore, } from './helpers/config' import { modifyFiles } from './helpers/files' -import { deleteFunctions, writeFunctions } from './helpers/functions' +import { deleteFunctions } from './helpers/functions' import { checkZipSize } from './helpers/verification' const DEFAULT_FUNCTIONS_SRC = 'netlify/functions' From d926e87b303d12abac116a3470ce7115f245b9e8 Mon Sep 17 00:00:00 2001 From: taty2010 Date: Tue, 7 Feb 2023 14:18:07 -0600 Subject: [PATCH 3/6] fix: prettier reformat --- plugin/src/index.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugin/src/index.ts b/plugin/src/index.ts index 23f43461..2a66c8bb 100644 --- a/plugin/src/index.ts +++ b/plugin/src/index.ts @@ -69,10 +69,14 @@ The plugin no longer uses this and it should be deleted to avoid conflicts.\n`) console.log('Creating site data metadata file') await createMetadataFileAndCopyDatastore(PUBLISH_DIR, cacheDir) } -// Gatsby V2 does not have functions so we can skip this step + // Gatsby V2 does not have functions so we can skip this step if (neededFunctions.length !== 0) { const helperFunctions = await import('./helpers/functions') - await helperFunctions.writeFunctions({ constants, netlifyConfig, neededFunctions }) + await helperFunctions.writeFunctions({ + constants, + netlifyConfig, + neededFunctions, + }) } await modifyConfig({ netlifyConfig, cacheDir, neededFunctions }) From 4f32179cf1607776c3359bd17843d601b3c888fa Mon Sep 17 00:00:00 2001 From: taty2010 Date: Mon, 13 Feb 2023 14:51:39 -0600 Subject: [PATCH 4/6] fix: moved deleteFunction under dynamic import --- plugin/src/index.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugin/src/index.ts b/plugin/src/index.ts index 2a66c8bb..8638115c 100644 --- a/plugin/src/index.ts +++ b/plugin/src/index.ts @@ -16,7 +16,6 @@ import { shouldSkipBundlingDatastore, } from './helpers/config' import { modifyFiles } from './helpers/files' -import { deleteFunctions } from './helpers/functions' import { checkZipSize } from './helpers/verification' const DEFAULT_FUNCTIONS_SRC = 'netlify/functions' @@ -63,8 +62,6 @@ The plugin no longer uses this and it should be deleted to avoid conflicts.\n`) const neededFunctions = await getNeededFunctions(cacheDir) - await deleteFunctions(constants) - if (shouldSkipBundlingDatastore()) { console.log('Creating site data metadata file') await createMetadataFileAndCopyDatastore(PUBLISH_DIR, cacheDir) @@ -72,6 +69,7 @@ The plugin no longer uses this and it should be deleted to avoid conflicts.\n`) // Gatsby V2 does not have functions so we can skip this step if (neededFunctions.length !== 0) { const helperFunctions = await import('./helpers/functions') + await helperFunctions.deleteFunctions(constants) await helperFunctions.writeFunctions({ constants, netlifyConfig, From 8b93a71b0666c40d5f50c6f155605de255b7d7d4 Mon Sep 17 00:00:00 2001 From: pieh Date: Thu, 16 Feb 2023 17:57:38 +0100 Subject: [PATCH 5/6] fix: use javascript tagged template for api function handler implementation Co-authored-by: taty2010 --- plugin/src/index.ts | 16 ++++++---------- plugin/src/templates/handlers.ts | 17 +++++------------ 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/plugin/src/index.ts b/plugin/src/index.ts index 8638115c..84e73b41 100644 --- a/plugin/src/index.ts +++ b/plugin/src/index.ts @@ -16,6 +16,7 @@ import { shouldSkipBundlingDatastore, } from './helpers/config' import { modifyFiles } from './helpers/files' +import { deleteFunctions, writeFunctions } from './helpers/functions' import { checkZipSize } from './helpers/verification' const DEFAULT_FUNCTIONS_SRC = 'netlify/functions' @@ -62,20 +63,15 @@ The plugin no longer uses this and it should be deleted to avoid conflicts.\n`) const neededFunctions = await getNeededFunctions(cacheDir) + await deleteFunctions(constants) + if (shouldSkipBundlingDatastore()) { console.log('Creating site data metadata file') await createMetadataFileAndCopyDatastore(PUBLISH_DIR, cacheDir) } - // Gatsby V2 does not have functions so we can skip this step - if (neededFunctions.length !== 0) { - const helperFunctions = await import('./helpers/functions') - await helperFunctions.deleteFunctions(constants) - await helperFunctions.writeFunctions({ - constants, - netlifyConfig, - neededFunctions, - }) - } + + await writeFunctions({ constants, netlifyConfig, neededFunctions }) + await modifyConfig({ netlifyConfig, cacheDir, neededFunctions }) await modifyFiles({ netlifyConfig, neededFunctions }) diff --git a/plugin/src/templates/handlers.ts b/plugin/src/templates/handlers.ts index b2dd1cb9..7822c298 100644 --- a/plugin/src/templates/handlers.ts +++ b/plugin/src/templates/handlers.ts @@ -1,9 +1,4 @@ -import type { - Handler, - HandlerContext, - HandlerEvent, - HandlerResponse, -} from '@netlify/functions' +import type { Handler, HandlerEvent } from '@netlify/functions' import { stripIndent as javascript } from 'common-tags' import type { GatsbyFunctionRequest } from 'gatsby' import type { @@ -19,8 +14,6 @@ const { join } = require('path') const etag = require('etag') -const { gatsbyFunction } = require('./api/gatsbyFunction') -const { createRequestObject, createResponseObject } = require('./api/utils') const { getPagePathFromPageDataPath, getGraphQLEngine, @@ -142,7 +135,7 @@ const getHandler = (renderMode: RenderMode, appDir: string): Handler => { * Generate an API handler */ -const getApiHandler = (appDir: string): Handler => +const apiHandler = javascript`(appDir: string): Handler => function handler( event: HandlerEvent, context: HandlerContext, @@ -158,11 +151,11 @@ const getApiHandler = (appDir: string): Handler => // Try to call the actual function gatsbyFunction(req, res, event, appDir) } catch (error) { - console.error(`Error executing ${event.path}`, error) + console.error(\`Error executing \${event.path}\`, error) resolve({ statusCode: 500 }) } }) - } + }` export const makeHandler = (appDir: string, renderMode: RenderMode): string => // This is a string, but if you have the right editor plugin it should format as js @@ -188,5 +181,5 @@ export const makeApiHandler = (appDir: string): string => const { resolve } = require("path"); const pageRoot = resolve(__dirname, "${appDir}"); - exports.handler = ((${getApiHandler.toString()})(pageRoot)) + exports.handler = ((${apiHandler})(pageRoot)) ` From db5a3ef0e6847455638134321c83d2398cc12947 Mon Sep 17 00:00:00 2001 From: pieh Date: Fri, 17 Feb 2023 11:13:11 +0100 Subject: [PATCH 6/6] fix: use JavaScript instead of TypeScript in apiHandler template --- plugin/src/templates/handlers.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugin/src/templates/handlers.ts b/plugin/src/templates/handlers.ts index 7822c298..85f40290 100644 --- a/plugin/src/templates/handlers.ts +++ b/plugin/src/templates/handlers.ts @@ -135,11 +135,11 @@ const getHandler = (renderMode: RenderMode, appDir: string): Handler => { * Generate an API handler */ -const apiHandler = javascript`(appDir: string): Handler => +const apiHandler = javascript`(appDir) => function handler( - event: HandlerEvent, - context: HandlerContext, - ): Promise { + event, + context, + ) { // Create a fake Gatsby/Express Request object const req = createRequestObject({ event, context })