From 55fd613588da46f9f807c09879dc25bafe3c0536 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 13 May 2022 10:59:16 +0100 Subject: [PATCH 1/9] feat: enable API, SSR and DSG functions individually as required --- plugin/src/helpers/config.ts | 94 +++++++++++++++++++++++---------- plugin/src/helpers/functions.ts | 37 ++++++++----- plugin/src/index.ts | 52 +++++++++++------- 3 files changed, 121 insertions(+), 62 deletions(-) diff --git a/plugin/src/helpers/config.ts b/plugin/src/helpers/config.ts index 3b55de10..c0686ab2 100644 --- a/plugin/src/helpers/config.ts +++ b/plugin/src/helpers/config.ts @@ -3,7 +3,6 @@ import { EOL } from 'os' import path from 'path' import process from 'process' -import { stripIndent } from 'common-tags' import fs, { existsSync } from 'fs-extra' import type { GatsbyConfig, PluginRef } from 'gatsby' @@ -118,31 +117,49 @@ export function mutateConfig({ netlifyConfig, compiledFunctionsDir, cacheDir, + neededFunctions, }): void { /* eslint-disable no-underscore-dangle, no-param-reassign */ - netlifyConfig.functions.__api = { - included_files: [path.posix.join(compiledFunctionsDir, '**')], - external_node_modules: ['msgpackr-extract'], + if (neededFunctions.includes('API')) { + netlifyConfig.functions.__api = { + included_files: [path.posix.join(compiledFunctionsDir, '**')], + external_node_modules: ['msgpackr-extract'], + } } - netlifyConfig.functions.__dsg = { - included_files: [ - 'public/404.html', - 'public/500.html', - path.posix.join(cacheDir, 'data', '**'), - path.posix.join(cacheDir, 'query-engine', '**'), - path.posix.join(cacheDir, 'page-ssr', '**'), - '!**/*.js.map', - ], - external_node_modules: ['msgpackr-extract'], - node_bundler: 'esbuild', + if (neededFunctions.includes('DSG')) { + netlifyConfig.functions.__dsg = { + included_files: [ + 'public/404.html', + 'public/500.html', + path.posix.join(cacheDir, 'data', '**'), + path.posix.join(cacheDir, 'query-engine', '**'), + path.posix.join(cacheDir, 'page-ssr', '**'), + '!**/*.js.map', + ], + external_node_modules: ['msgpackr-extract'], + node_bundler: 'esbuild', + } } - netlifyConfig.functions.__ssr = { ...netlifyConfig.functions.__dsg } + if (neededFunctions.includes('SSR')) { + netlifyConfig.functions.__ssr = { + included_files: [ + 'public/404.html', + 'public/500.html', + path.posix.join(cacheDir, 'data', '**'), + path.posix.join(cacheDir, 'query-engine', '**'), + path.posix.join(cacheDir, 'page-ssr', '**'), + '!**/*.js.map', + ], + external_node_modules: ['msgpackr-extract'], + node_bundler: 'esbuild', + } + } /* eslint-enable no-underscore-dangle, no-param-reassign */ } -export function shouldSkipFunctions(cacheDir: string): boolean { +function shouldSupportFunctions(cacheDir: string) { if ( process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === 'true' || process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === '1' @@ -150,28 +167,47 @@ export function shouldSkipFunctions(cacheDir: string): boolean { console.log( 'Skipping Gatsby Functions and SSR/DSG support because the environment variable NETLIFY_SKIP_GATSBY_FUNCTIONS is set to true', ) - return true + return false } if (!existsSync(path.join(cacheDir, 'functions'))) { console.log( `Skipping Gatsby Functions and SSR/DSG support because the site's Gatsby version does not support them`, ) - return true + return false } - const skipFile = path.join(cacheDir, '.nf-skip-gatsby-functions') + return true +} - if (existsSync(skipFile)) { - console.log( - stripIndent` - Skipping Gatsby Functions and SSR/DSG support because gatsby-plugin-netlify reported that this site does not use them. - If this is incorrect, remove the file "${skipFile}" and try again.`, - ) - return true +export async function getNeededFunctions( + cacheDir: string, +): Promise> { + if (shouldSupportFunctions) { + try { + const skipReport = await fs.readJson( + path.join(cacheDir, '.nf-skip-gatsby-functions'), + ) + const funcs = Object.keys(skipReport).filter( + (name) => skipReport[name] === true, + ) + if (funcs.length !== 0) { + console.log(`Enabling support for ${funcs.join('/')}`) + return funcs + } + } catch (error) { + if (error.code === 'ENOENT') { + // no skip file + console.log(`Enabling support for Gatsby Functions and SSR/DSG.`) + return ['API', 'SSR', 'DSG'] + } + console.log( + // empty skip file (old plugin version) + `Skipping Gatsby Functions and SSR/DSG support because gatsby-plugin-netlify reported that this site does not use them.`, + ) + } } - - return false + return [] } export function getGatsbyRoot(publish: string): string { diff --git a/plugin/src/helpers/functions.ts b/plugin/src/helpers/functions.ts index 3525b135..d12c59b4 100644 --- a/plugin/src/helpers/functions.ts +++ b/plugin/src/helpers/functions.ts @@ -34,30 +34,39 @@ const writeApiFunction = async ({ appDir, functionDir }) => { export const writeFunctions = async ({ constants, netlifyConfig, + neededFunctions, }: { constants: NetlifyPluginConstants netlifyConfig: NetlifyConfig + neededFunctions: Array }): Promise => { const { PUBLISH_DIR, INTERNAL_FUNCTIONS_SRC } = constants const siteRoot = getGatsbyRoot(PUBLISH_DIR) const functionDir = resolve(INTERNAL_FUNCTIONS_SRC, '__api') const appDir = relative(functionDir, siteRoot) - await writeFunction({ - renderMode: 'SSR', - handlerName: '__ssr', - appDir, - functionsSrc: INTERNAL_FUNCTIONS_SRC, - }) + if (neededFunctions.includes('SSR')) { + await writeFunction({ + renderMode: 'SSR', + handlerName: '__ssr', + appDir, + functionsSrc: INTERNAL_FUNCTIONS_SRC, + }) + } + + if (neededFunctions.includes('DSG')) { + await writeFunction({ + renderMode: 'DSG', + handlerName: '__dsg', + appDir, + functionsSrc: INTERNAL_FUNCTIONS_SRC, + }) + } - await writeFunction({ - renderMode: 'DSG', - handlerName: '__dsg', - appDir, - functionsSrc: INTERNAL_FUNCTIONS_SRC, - }) - await setupImageCdn({ constants, netlifyConfig }) - await writeApiFunction({ appDir, functionDir }) + if (neededFunctions.includes('API')) { + await setupImageCdn({ constants, netlifyConfig }) + await writeApiFunction({ appDir, functionDir }) + } } export const setupImageCdn = async ({ diff --git a/plugin/src/index.ts b/plugin/src/index.ts index 09d96d0f..3a6fa24d 100644 --- a/plugin/src/index.ts +++ b/plugin/src/index.ts @@ -9,7 +9,7 @@ import { normalizedCacheDir, restoreCache, saveCache } from './helpers/cache' import { checkConfig, mutateConfig, - shouldSkipFunctions, + getNeededFunctions, spliceConfig, } from './helpers/config' import { patchFile, relocateBinaries } from './helpers/files' @@ -58,27 +58,36 @@ export async function onBuild({ The plugin no longer uses this and it should be deleted to avoid conflicts.\n`) } - if (shouldSkipFunctions(cacheDir)) { - await deleteFunctions(constants) - return - } - const compiledFunctionsDir = path.join(cacheDir, '/functions') + const neededFunctions = await getNeededFunctions(cacheDir) - await writeFunctions({ constants, netlifyConfig }) + await deleteFunctions(constants) - mutateConfig({ netlifyConfig, cacheDir, compiledFunctionsDir }) + const compiledFunctionsDir = path.join(cacheDir, '/functions') - const root = dirname(netlifyConfig.build.publish) - await patchFile(root) - await relocateBinaries(root) + await writeFunctions({ constants, netlifyConfig, neededFunctions }) - // Editing _redirects so it works with ntl dev - spliceConfig({ - startMarker: '# @netlify/plugin-gatsby redirects start', - endMarker: '# @netlify/plugin-gatsby redirects end', - contents: '/api/* /.netlify/functions/__api 200', - fileName: join(netlifyConfig.build.publish, '_redirects'), + mutateConfig({ + netlifyConfig, + cacheDir, + compiledFunctionsDir, + neededFunctions, }) + + if (neededFunctions.includes('DSG')) { + const root = dirname(netlifyConfig.build.publish) + await patchFile(root) + await relocateBinaries(root) + } + + if (neededFunctions.includes('API')) { + // Editing _redirects so it works with ntl dev + spliceConfig({ + startMarker: '# @netlify/plugin-gatsby redirects start', + endMarker: '# @netlify/plugin-gatsby redirects end', + contents: '/api/* /.netlify/functions/__api 200', + fileName: join(netlifyConfig.build.publish, '_redirects'), + }) + } } export async function onPostBuild({ @@ -86,7 +95,12 @@ export async function onPostBuild({ utils, }): Promise { await saveCache({ publish: PUBLISH_DIR, utils }) - for (const func of ['api', 'dsg', 'ssr']) { - await checkZipSize(path.join(FUNCTIONS_DIST, `__${func}.zip`)) + + const cacheDir = normalizedCacheDir(PUBLISH_DIR) + + const neededFunctions = await getNeededFunctions(cacheDir) + + for (const func of neededFunctions) { + await checkZipSize(path.join(FUNCTIONS_DIST, `__${func.toLowerCase()}.zip`)) } } From 4ba0b3438afcae4f786574fd9a9cb03922594ac5 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 13 May 2022 11:18:17 +0100 Subject: [PATCH 2/9] chore: edit variable names for clarity --- plugin/src/helpers/config.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/plugin/src/helpers/config.ts b/plugin/src/helpers/config.ts index c0686ab2..66f4a7bc 100644 --- a/plugin/src/helpers/config.ts +++ b/plugin/src/helpers/config.ts @@ -159,7 +159,7 @@ export function mutateConfig({ /* eslint-enable no-underscore-dangle, no-param-reassign */ } -function shouldSupportFunctions(cacheDir: string) { +function shouldEnableFunctions(cacheDir: string) { if ( process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === 'true' || process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === '1' @@ -183,17 +183,17 @@ function shouldSupportFunctions(cacheDir: string) { export async function getNeededFunctions( cacheDir: string, ): Promise> { - if (shouldSupportFunctions) { + if (shouldEnableFunctions) { try { - const skipReport = await fs.readJson( + const funcObj = await fs.readJson( path.join(cacheDir, '.nf-skip-gatsby-functions'), ) - const funcs = Object.keys(skipReport).filter( - (name) => skipReport[name] === true, + const funcArr = Object.keys(funcObj).filter( + (name) => funcObj[name] === true, ) - if (funcs.length !== 0) { - console.log(`Enabling support for ${funcs.join('/')}`) - return funcs + if (funcArr.length !== 0) { + console.log(`Enabling support for ${funcArr.join('/')}`) + return funcArr } } catch (error) { if (error.code === 'ENOENT') { From 8a61bc9354e6ff4274e39024c8fbe538625b0ac9 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 13 May 2022 16:32:35 +0100 Subject: [PATCH 3/9] fix: run setupImageCdn independently of functions --- plugin/src/helpers/functions.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugin/src/helpers/functions.ts b/plugin/src/helpers/functions.ts index d12c59b4..c6842760 100644 --- a/plugin/src/helpers/functions.ts +++ b/plugin/src/helpers/functions.ts @@ -6,6 +6,8 @@ import { makeApiHandler, makeHandler } from '../templates/handlers' import { getGatsbyRoot } from './config' +export type FunctionList = Array<'API' | 'SSR' | 'DSG'> + const writeFunction = async ({ renderMode, handlerName, @@ -38,7 +40,7 @@ export const writeFunctions = async ({ }: { constants: NetlifyPluginConstants netlifyConfig: NetlifyConfig - neededFunctions: Array + neededFunctions: FunctionList }): Promise => { const { PUBLISH_DIR, INTERNAL_FUNCTIONS_SRC } = constants const siteRoot = getGatsbyRoot(PUBLISH_DIR) @@ -63,8 +65,9 @@ export const writeFunctions = async ({ }) } + await setupImageCdn({ constants, netlifyConfig }) + if (neededFunctions.includes('API')) { - await setupImageCdn({ constants, netlifyConfig }) await writeApiFunction({ appDir, functionDir }) } } From f6a06fb55d155e4779b5cc7ed1637b16c634bbeb Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 13 May 2022 16:33:47 +0100 Subject: [PATCH 4/9] chore: abstract file modification functions to reduce complexity in onBuild --- plugin/src/helpers/files.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/plugin/src/helpers/files.ts b/plugin/src/helpers/files.ts index 714041a0..1e5646aa 100644 --- a/plugin/src/helpers/files.ts +++ b/plugin/src/helpers/files.ts @@ -1,6 +1,7 @@ import os from 'os' import process from 'process' +import { NetlifyConfig } from '@netlify/build' import { copyFile, ensureDir, @@ -12,6 +13,8 @@ import { import { dirname, join, resolve } from 'pathe' import semver from 'semver' +import type { FunctionList } from './functions' + const DEFAULT_LAMBDA_PLATFORM = 'linux' const DEFAULT_LAMBDA_ABI = '83' const DEFAULT_LAMBDA_ARCH = 'x64' @@ -23,6 +26,20 @@ const RELOCATABLE_BINARIES = [ `node.abi${DEFAULT_LAMBDA_ABI}.glibc.node`, ] +export const modifyFiles = async ({ + netlifyConfig, + neededFunctions, +}: { + netlifyConfig: NetlifyConfig + neededFunctions: FunctionList +}): Promise => { + if (neededFunctions.includes('SSR') || neededFunctions.includes('DSG')) { + const root = dirname(netlifyConfig.build.publish) + await patchFile(root) + await relocateBinaries(root) + } +} + /** * Manually patching the bundle to work around various incompatibilities in some versions. */ From 7863bb043771cf209b8b0c74d7d222b9b9905513 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 13 May 2022 16:34:37 +0100 Subject: [PATCH 5/9] chore: abstract config modification functions to reduce complexity in onBuild --- plugin/src/helpers/config.ts | 38 ++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/plugin/src/helpers/config.ts b/plugin/src/helpers/config.ts index 66f4a7bc..369fc5f1 100644 --- a/plugin/src/helpers/config.ts +++ b/plugin/src/helpers/config.ts @@ -3,10 +3,12 @@ import { EOL } from 'os' import path from 'path' import process from 'process' +import { NetlifyConfig } from '@netlify/build' import fs, { existsSync } from 'fs-extra' import type { GatsbyConfig, PluginRef } from 'gatsby' import { checkPackageVersion } from './files' +import type { FunctionList } from './functions' export async function spliceConfig({ startMarker, @@ -113,16 +115,41 @@ export async function checkConfig({ utils, netlifyConfig }): Promise { } } +export async function modifyConfig({ + netlifyConfig, + cacheDir, + neededFunctions, +}: { + netlifyConfig: NetlifyConfig + cacheDir: string + neededFunctions: FunctionList +}): Promise { + mutateConfig({ netlifyConfig, cacheDir, neededFunctions }) + + if (neededFunctions.includes('API')) { + // Editing _redirects so it works with ntl dev + await spliceConfig({ + startMarker: '# @netlify/plugin-gatsby redirects start', + endMarker: '# @netlify/plugin-gatsby redirects end', + contents: '/api/* /.netlify/functions/__api 200', + fileName: path.join(netlifyConfig.build.publish, '_redirects'), + }) + } +} + export function mutateConfig({ netlifyConfig, - compiledFunctionsDir, cacheDir, neededFunctions, +}: { + netlifyConfig: NetlifyConfig + cacheDir: string + neededFunctions: FunctionList }): void { /* eslint-disable no-underscore-dangle, no-param-reassign */ if (neededFunctions.includes('API')) { netlifyConfig.functions.__api = { - included_files: [path.posix.join(compiledFunctionsDir, '**')], + included_files: [path.posix.join(cacheDir, 'functions', '**')], external_node_modules: ['msgpackr-extract'], } } @@ -182,15 +209,18 @@ function shouldEnableFunctions(cacheDir: string) { export async function getNeededFunctions( cacheDir: string, -): Promise> { +): Promise { if (shouldEnableFunctions) { try { + // read skip file from gatsby-plugin-netlify const funcObj = await fs.readJson( path.join(cacheDir, '.nf-skip-gatsby-functions'), ) + // filter out true values into an array const funcArr = Object.keys(funcObj).filter( (name) => funcObj[name] === true, - ) + ) as FunctionList + // if functions are needed, return the list if (funcArr.length !== 0) { console.log(`Enabling support for ${funcArr.join('/')}`) return funcArr From 942584133b99fdacbfd60de994a227828825f01b Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 13 May 2022 16:35:03 +0100 Subject: [PATCH 6/9] chore: reduce complexity in onBuild handler --- plugin/src/index.ts | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/plugin/src/index.ts b/plugin/src/index.ts index 3a6fa24d..24c3f30e 100644 --- a/plugin/src/index.ts +++ b/plugin/src/index.ts @@ -1,4 +1,4 @@ -import path, { dirname, join } from 'path' +import path from 'path' import process from 'process' import { NetlifyPluginOptions } from '@netlify/build' @@ -6,13 +6,8 @@ import { stripIndent } from 'common-tags' import { existsSync } from 'fs-extra' import { normalizedCacheDir, restoreCache, saveCache } from './helpers/cache' -import { - checkConfig, - mutateConfig, - getNeededFunctions, - spliceConfig, -} from './helpers/config' -import { patchFile, relocateBinaries } from './helpers/files' +import { checkConfig, getNeededFunctions, modifyConfig } from './helpers/config' +import { modifyFiles } from './helpers/files' import { deleteFunctions, writeFunctions } from './helpers/functions' import { checkZipSize } from './helpers/verification' @@ -62,32 +57,11 @@ The plugin no longer uses this and it should be deleted to avoid conflicts.\n`) await deleteFunctions(constants) - const compiledFunctionsDir = path.join(cacheDir, '/functions') - await writeFunctions({ constants, netlifyConfig, neededFunctions }) - mutateConfig({ - netlifyConfig, - cacheDir, - compiledFunctionsDir, - neededFunctions, - }) - - if (neededFunctions.includes('DSG')) { - const root = dirname(netlifyConfig.build.publish) - await patchFile(root) - await relocateBinaries(root) - } + await modifyConfig({ netlifyConfig, cacheDir, neededFunctions }) - if (neededFunctions.includes('API')) { - // Editing _redirects so it works with ntl dev - spliceConfig({ - startMarker: '# @netlify/plugin-gatsby redirects start', - endMarker: '# @netlify/plugin-gatsby redirects end', - contents: '/api/* /.netlify/functions/__api 200', - fileName: join(netlifyConfig.build.publish, '_redirects'), - }) - } + await modifyFiles({ netlifyConfig, neededFunctions }) } export async function onPostBuild({ From e7e706573272fea801f1753c77d00e840fc9017a Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Mon, 16 May 2022 12:45:39 +0100 Subject: [PATCH 7/9] fix: missing function invocation in getNeededFunctions --- plugin/src/helpers/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/helpers/config.ts b/plugin/src/helpers/config.ts index 369fc5f1..8cbf705a 100644 --- a/plugin/src/helpers/config.ts +++ b/plugin/src/helpers/config.ts @@ -210,7 +210,7 @@ function shouldEnableFunctions(cacheDir: string) { export async function getNeededFunctions( cacheDir: string, ): Promise { - if (shouldEnableFunctions) { + if (shouldEnableFunctions(cacheDir)) { try { // read skip file from gatsby-plugin-netlify const funcObj = await fs.readJson( From 2cf166290054af38fa5309a7059aea0d1e600afb Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Tue, 17 May 2022 18:27:13 +0100 Subject: [PATCH 8/9] feat: add env vars for disabling api/ssr/dsg individually --- plugin/src/helpers/config.ts | 96 +++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 46 deletions(-) diff --git a/plugin/src/helpers/config.ts b/plugin/src/helpers/config.ts index 8cbf705a..e6af089f 100644 --- a/plugin/src/helpers/config.ts +++ b/plugin/src/helpers/config.ts @@ -186,58 +186,62 @@ export function mutateConfig({ /* eslint-enable no-underscore-dangle, no-param-reassign */ } -function shouldEnableFunctions(cacheDir: string) { - if ( - process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === 'true' || - process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === '1' - ) { - console.log( - 'Skipping Gatsby Functions and SSR/DSG support because the environment variable NETLIFY_SKIP_GATSBY_FUNCTIONS is set to true', - ) - return false - } +export async function getNeededFunctions( + cacheDir: string, +): Promise { + if (!existsSync(path.join(cacheDir, 'functions'))) return [] - if (!existsSync(path.join(cacheDir, 'functions'))) { - console.log( - `Skipping Gatsby Functions and SSR/DSG support because the site's Gatsby version does not support them`, - ) - return false + const neededFunctions = overrideNeededFunctions( + await readFunctionSkipFile(cacheDir), + ) + + const functionList = Object.keys(neededFunctions).filter( + (name) => neededFunctions[name] === true, + ) as FunctionList + + if (functionList.length === 0) { + console.log('Skipping Gatsby Functions and SSR/DSG support') + } else { + console.log(`Enabling Gatsby ${functionList.join('/')} support`) } - return true + return functionList } -export async function getNeededFunctions( - cacheDir: string, -): Promise { - if (shouldEnableFunctions(cacheDir)) { - try { - // read skip file from gatsby-plugin-netlify - const funcObj = await fs.readJson( - path.join(cacheDir, '.nf-skip-gatsby-functions'), - ) - // filter out true values into an array - const funcArr = Object.keys(funcObj).filter( - (name) => funcObj[name] === true, - ) as FunctionList - // if functions are needed, return the list - if (funcArr.length !== 0) { - console.log(`Enabling support for ${funcArr.join('/')}`) - return funcArr - } - } catch (error) { - if (error.code === 'ENOENT') { - // no skip file - console.log(`Enabling support for Gatsby Functions and SSR/DSG.`) - return ['API', 'SSR', 'DSG'] - } - console.log( - // empty skip file (old plugin version) - `Skipping Gatsby Functions and SSR/DSG support because gatsby-plugin-netlify reported that this site does not use them.`, - ) - } +async function readFunctionSkipFile(cacheDir: string) { + try { + // read skip file from gatsby-plugin-netlify + return await fs.readJson(path.join(cacheDir, '.nf-skip-gatsby-functions')) + } catch (error) { + // missing skip file = all functions needed + // empty or invalid skip file = no functions needed + return error.code === 'ENOENT' ? { API: true, SSR: true, DSG: true } : {} + } +} + +// eslint-disable-next-line complexity +function overrideNeededFunctions(neededFunctions) { + const skipAll = + process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === 'true' || + process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === '1' + + const skipAPI = + process.env.NETLIFY_SKIP_API_FUNCTION === 'true' || + process.env.NETLIFY_SKIP_API_FUNCTION === '1' + + const skipSSR = + process.env.NETLIFY_SKIP_SSR_FUNCTION === 'true' || + process.env.NETLIFY_SKIP_SSR_FUNCTION === '1' + + const skipDSG = + process.env.NETLIFY_SKIP_DSG_FUNCTION === 'true' || + process.env.NETLIFY_SKIP_DSG_FUNCTION === '1' + + return { + API: skipAll || skipAPI ? false : neededFunctions.API, + SSR: skipAll || skipSSR ? false : neededFunctions.SSR, + DSG: skipAll || skipDSG ? false : neededFunctions.DSG, } - return [] } export function getGatsbyRoot(publish: string): string { From 6d69ae078060be917f51f8ba4d8111a8f6232916 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Wed, 18 May 2022 10:52:33 +0100 Subject: [PATCH 9/9] chore: abstract function skip env var checks --- plugin/src/helpers/config.ts | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/plugin/src/helpers/config.ts b/plugin/src/helpers/config.ts index e6af089f..732b0a8b 100644 --- a/plugin/src/helpers/config.ts +++ b/plugin/src/helpers/config.ts @@ -221,21 +221,10 @@ async function readFunctionSkipFile(cacheDir: string) { // eslint-disable-next-line complexity function overrideNeededFunctions(neededFunctions) { - const skipAll = - process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === 'true' || - process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === '1' - - const skipAPI = - process.env.NETLIFY_SKIP_API_FUNCTION === 'true' || - process.env.NETLIFY_SKIP_API_FUNCTION === '1' - - const skipSSR = - process.env.NETLIFY_SKIP_SSR_FUNCTION === 'true' || - process.env.NETLIFY_SKIP_SSR_FUNCTION === '1' - - const skipDSG = - process.env.NETLIFY_SKIP_DSG_FUNCTION === 'true' || - process.env.NETLIFY_SKIP_DSG_FUNCTION === '1' + const skipAll = isEnvSet('NETLIFY_SKIP_GATSBY_FUNCTIONS') + const skipAPI = isEnvSet('NETLIFY_SKIP_API_FUNCTION') + const skipSSR = isEnvSet('NETLIFY_SKIP_SSR_FUNCTION') + const skipDSG = isEnvSet('NETLIFY_SKIP_DSG_FUNCTION') return { API: skipAll || skipAPI ? false : neededFunctions.API, @@ -244,6 +233,10 @@ function overrideNeededFunctions(neededFunctions) { } } +function isEnvSet(envVar: string) { + return process.env[envVar] === 'true' || process.env[envVar] === '1' +} + export function getGatsbyRoot(publish: string): string { return path.resolve(path.dirname(publish)) }