Skip to content

Commit d78700c

Browse files
piehmrstork
andauthored
fix: handle empty cases in edge bundling (#6659)
Co-authored-by: Mateusz Bocian <[email protected]>
1 parent f01bed3 commit d78700c

File tree

4 files changed

+54
-36
lines changed
  • packages/build
    • src/plugins_core/edge_functions
    • tests/edge_functions

4 files changed

+54
-36
lines changed

packages/build/src/plugins_core/edge_functions/index.ts

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,24 @@ const coreStep = async function ({
7272

7373
const sourcePaths = [...generatedFunctionPaths, srcPath].filter(Boolean) as string[]
7474

75-
logFunctions({ frameworksAPISrcPath, internalSrcDirectory, internalSrcPath, logs, srcDirectory, srcPath })
75+
const [userFunctionsSrcExists, userFunctions, internalFunctions, frameworkFunctions] = await Promise.all([
76+
srcPath ? pathExists(srcPath) : Promise.resolve(false),
77+
srcPath ? find([srcPath]) : Promise.resolve([]),
78+
find([internalSrcPath]),
79+
frameworksAPISrcPath ? find([frameworksAPISrcPath]) : Promise.resolve([]),
80+
])
81+
82+
logFunctionsToBundle({
83+
logs,
84+
userFunctions: userFunctions.map(({ name }) => name),
85+
userFunctionsSrc: srcDirectory,
86+
userFunctionsSrcExists,
87+
internalFunctions: internalFunctions.map(({ name }) => name),
88+
internalFunctionsSrc: internalSrcDirectory,
89+
frameworkFunctions: frameworkFunctions.map(({ name }) => name),
90+
type: 'Edge Functions',
91+
generatedFunctions: {},
92+
})
7693

7794
// If we're running in buildbot, we set the Deno cache dir to a directory
7895
// that is persisted between builds.
@@ -86,6 +103,10 @@ const coreStep = async function ({
86103
// no-op
87104
}
88105

106+
if (userFunctions.length === 0 && internalFunctions.length === 0 && frameworkFunctions.length === 0) {
107+
return {}
108+
}
109+
89110
let vendorDirectory: string | undefined
90111

91112
// If we're building locally, set a vendor directory in `internalSrcPath`.
@@ -175,41 +196,6 @@ const hasEdgeFunctionsDirectories = async function ({
175196
return await pathExists(frameworkFunctionsSrc)
176197
}
177198

178-
const logFunctions = async ({
179-
frameworksAPISrcPath,
180-
internalSrcDirectory,
181-
internalSrcPath,
182-
logs,
183-
srcDirectory: userFunctionsSrc,
184-
srcPath,
185-
}: {
186-
frameworksAPISrcPath?: string
187-
internalSrcDirectory: string
188-
internalSrcPath: string
189-
logs: any
190-
srcDirectory?: string
191-
srcPath?: string
192-
}): Promise<void> => {
193-
const [userFunctionsSrcExists, userFunctions, internalFunctions, frameworkFunctions] = await Promise.all([
194-
srcPath ? pathExists(srcPath) : Promise.resolve(false),
195-
srcPath ? find([srcPath]) : Promise.resolve([]),
196-
find([internalSrcPath]),
197-
frameworksAPISrcPath ? find([frameworksAPISrcPath]) : Promise.resolve([]),
198-
])
199-
200-
logFunctionsToBundle({
201-
logs,
202-
userFunctions: userFunctions.map(({ name }) => name),
203-
userFunctionsSrc,
204-
userFunctionsSrcExists,
205-
internalFunctions: internalFunctions.map(({ name }) => name),
206-
internalFunctionsSrc: internalSrcDirectory,
207-
frameworkFunctions: frameworkFunctions.map(({ name }) => name),
208-
type: 'Edge Functions',
209-
generatedFunctions: {},
210-
})
211-
}
212-
213199
export const bundleEdgeFunctions = {
214200
event: 'onBuild',
215201
coreStep,

packages/build/tests/edge_functions/fixtures/functions_empty_directory/.netlify/edge-functions/.gitkeep

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"functions": [],
3+
"version": 1
4+
}

packages/build/tests/edge_functions/tests.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,31 @@ test.serial(
289289
])
290290
},
291291
)
292+
293+
test('skip bundling when edge function directories exist, contain no functions', async (t) => {
294+
await new Fixture('./fixtures/functions_empty_directory').runWithBuild()
295+
296+
const manifestPath = join(
297+
FIXTURES_DIR,
298+
'functions_empty_directory',
299+
'.netlify',
300+
'edge-functions-dist',
301+
'manifest.json',
302+
)
303+
304+
t.false(await pathExists(manifestPath))
305+
})
306+
307+
test('skip bundling when edge function directories exist, contain no functions, contain empty manifest', async (t) => {
308+
await new Fixture('./fixtures/functions_empty_manifest').runWithBuild()
309+
310+
const manifestPath = join(
311+
FIXTURES_DIR,
312+
'functions_empty_manifest',
313+
'.netlify',
314+
'edge-functions-dist',
315+
'manifest.json',
316+
)
317+
318+
t.false(await pathExists(manifestPath))
319+
})

0 commit comments

Comments
 (0)