Skip to content

Commit 5602c96

Browse files
committed
fix: bundle edge functions if they exist on deploy --no-build
1 parent dabc560 commit 5602c96

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

src/commands/deploy/deploy.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { SiteInfo } from '../../utils/types.js'
5353
import type { DeployOptionValues } from './option_values.js'
5454
import boxen from 'boxen'
5555
import terminalLink from 'terminal-link'
56+
import { anyEdgeFunctionsDirectoryExists } from '../../lib/edge-functions/get-directories.js'
5657

5758
const triggerDeploy = async ({
5859
api,
@@ -867,10 +868,11 @@ const prepAndRunDeploy = async ({
867868
const functionsFolder = getFunctionsFolder({ workingDir, options, config, site, siteData })
868869
const { configPath } = site
869870

870-
const edgeFunctionsConfig = command.netlify.config.edge_functions
871-
872-
// build flag wasn't used and edge functions exist
873-
if (!options.build && edgeFunctionsConfig && edgeFunctionsConfig.length !== 0) {
871+
// build flag wasn't used and edge functions directories exist
872+
if (!options.build && (await anyEdgeFunctionsDirectoryExists(command))) {
873+
// for the case of directories existing but not containing any edge functions,
874+
// there is early bail in edge functions bundling after scanning for edge functions
875+
// for this case and to avoid replicating scanning logic here, we defer to the bundling step
874876
await bundleEdgeFunctions(options, command)
875877
}
876878

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { join } from 'path'
2+
3+
import { getPathInProject } from '../settings.js'
4+
import { INTERNAL_EDGE_FUNCTIONS_FOLDER } from './consts.js'
5+
import BaseCommand from '../../commands/base-command.js'
6+
import { fileExistsAsync } from '../fs.js'
7+
8+
export const getUserEdgeFunctionsDirectory = (command: BaseCommand): string | undefined => {
9+
return command.netlify.config.build.edge_functions
10+
}
11+
12+
export const getInternalEdgeFunctionsDirectory = (command: BaseCommand): string => {
13+
return join(command.workingDir, getPathInProject([INTERNAL_EDGE_FUNCTIONS_FOLDER]))
14+
}
15+
16+
export const getFrameworkEdgeFunctionsDirectory = (command: BaseCommand): string => {
17+
return command.netlify.frameworksAPIPaths.edgeFunctions.path
18+
}
19+
20+
const getAllEdgeFunctionsDirectories = (command: BaseCommand) => {
21+
return [
22+
getUserEdgeFunctionsDirectory(command),
23+
getInternalEdgeFunctionsDirectory(command),
24+
getFrameworkEdgeFunctionsDirectory(command),
25+
].filter(Boolean) as string[]
26+
}
27+
28+
export const anyEdgeFunctionsDirectoryExists = async (command: BaseCommand): Promise<boolean> => {
29+
const directoriesToCheck = getAllEdgeFunctionsDirectories(command)
30+
31+
return (await Promise.all(directoriesToCheck.map(fileExistsAsync))).some(Boolean)
32+
}

src/lib/edge-functions/proxy.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ export const initializeProxy = async ({
117117
siteInfo: $TSFixMe
118118
state: LocalState
119119
}) => {
120-
const userFunctionsPath = config.build.edge_functions
121120
const isolatePort = await getAvailablePort()
122121
const runtimeFeatureFlags = ['edge_functions_bootstrap_failure_mode', 'edge_functions_bootstrap_populate_environment']
123122
const protocol = settings.https ? 'https' : 'http'
@@ -132,7 +131,6 @@ export const initializeProxy = async ({
132131
config,
133132
configPath,
134133
debug,
135-
directory: userFunctionsPath,
136134
env: configEnv,
137135
featureFlags: buildFeatureFlags,
138136
getUpdatedConfig,
@@ -207,7 +205,6 @@ const prepareServer = async ({
207205
config,
208206
configPath,
209207
debug,
210-
directory,
211208
env: configEnv,
212209
featureFlags,
213210
getUpdatedConfig,
@@ -221,7 +218,6 @@ const prepareServer = async ({
221218
config: NormalizedCachedConfigConfig
222219
configPath: string
223220
debug: boolean
224-
directory?: string
225221
env: Record<string, { sources: string[]; value: string }>
226222
featureFlags: FeatureFlags
227223
getUpdatedConfig: () => Promise<NormalizedCachedConfigConfig>
@@ -261,7 +257,6 @@ const prepareServer = async ({
261257
config,
262258
configPath,
263259
debug,
264-
directories: directory ? [directory] : [],
265260
env: configEnv,
266261
featureFlags,
267262
getUpdatedConfig,

src/lib/edge-functions/registry.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ import type { FeatureFlags } from '../../utils/feature-flags.js'
2222
import { MultiMap } from '../../utils/multimap.js'
2323
import { getPathInProject } from '../settings.js'
2424

25-
import { DIST_IMPORT_MAP_PATH, INTERNAL_EDGE_FUNCTIONS_FOLDER } from './consts.js'
25+
import { DIST_IMPORT_MAP_PATH } from './consts.js'
26+
import {
27+
getFrameworkEdgeFunctionsDirectory,
28+
getInternalEdgeFunctionsDirectory,
29+
getUserEdgeFunctionsDirectory,
30+
} from './get-directories.js'
2631

2732
type DependencyCache = Record<string, string[]>
2833
type EdgeFunctionEvent = 'buildError' | 'loaded' | 'reloaded' | 'reloading' | 'removed'
@@ -38,7 +43,6 @@ interface EdgeFunctionsRegistryOptions {
3843
config: NormalizedCachedConfigConfig
3944
configPath: string
4045
debug: boolean
41-
directories: string[]
4246
env: Record<string, { sources: string[]; value: string }>
4347
featureFlags: FeatureFlags
4448
getUpdatedConfig: () => Promise<NormalizedCachedConfigConfig>
@@ -105,7 +109,6 @@ export class EdgeFunctionsRegistry {
105109
// Mapping file URLs to names of functions that use them as dependencies.
106110
private dependencyPaths = new MultiMap<string, string>()
107111

108-
private directories: string[]
109112
private directoryWatchers = new Map<string, import('chokidar').FSWatcher>()
110113
private env: Record<string, string>
111114
private featureFlags: FeatureFlags
@@ -133,7 +136,6 @@ export class EdgeFunctionsRegistry {
133136
command,
134137
config,
135138
configPath,
136-
directories,
137139
env,
138140
featureFlags,
139141
getUpdatedConfig,
@@ -146,7 +148,6 @@ export class EdgeFunctionsRegistry {
146148
this.command = command
147149
this.bundler = bundler
148150
this.configPath = configPath
149-
this.directories = directories
150151
this.featureFlags = featureFlags
151152
this.getUpdatedConfig = getUpdatedConfig
152153
this.runIsolate = runIsolate
@@ -563,16 +564,12 @@ export class EdgeFunctionsRegistry {
563564
return { functionsConfig, graph, success }
564565
}
565566

566-
private get internalDirectory() {
567-
return join(this.projectDir, getPathInProject([INTERNAL_EDGE_FUNCTIONS_FOLDER]))
568-
}
569-
570567
private get internalImportMapPath() {
571568
return join(this.projectDir, getPathInProject([DIST_IMPORT_MAP_PATH]))
572569
}
573570

574571
private async readDeployConfig() {
575-
const manifestPath = join(this.internalDirectory, 'manifest.json')
572+
const manifestPath = join(getInternalEdgeFunctionsDirectory(this.command), 'manifest.json')
576573
try {
577574
const contents = await readFile(manifestPath, 'utf8')
578575
const manifest = JSON.parse(contents)
@@ -592,15 +589,16 @@ export class EdgeFunctionsRegistry {
592589

593590
this.declarationsFromDeployConfig = deployConfig.functions
594591
this.importMapFromDeployConfig = deployConfig.import_map
595-
? join(this.internalDirectory, deployConfig.import_map)
592+
? join(getInternalEdgeFunctionsDirectory(this.command), deployConfig.import_map)
596593
: undefined
597594
}
598595

599596
private async scanForFunctions() {
597+
const userFunctionDirectory = getUserEdgeFunctionsDirectory(this.command)
600598
const [frameworkFunctions, integrationFunctions, userFunctions] = await Promise.all([
601-
this.usesFrameworksAPI ? this.bundler.find([this.command.netlify.frameworksAPIPaths.edgeFunctions.path]) : [],
602-
this.bundler.find([this.internalDirectory]),
603-
this.bundler.find(this.directories),
599+
this.usesFrameworksAPI ? this.bundler.find([getFrameworkEdgeFunctionsDirectory(this.command)]) : [],
600+
this.bundler.find([getInternalEdgeFunctionsDirectory(this.command)]),
601+
userFunctionDirectory ? this.bundler.find([userFunctionDirectory]) : [],
604602
this.scanForDeployConfig(),
605603
])
606604
const internalFunctions = [...frameworkFunctions, ...integrationFunctions]

0 commit comments

Comments
 (0)