Skip to content

Commit

Permalink
fix: watch edge function dependencies (#4576)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
eduardoboucas and kodiakhq[bot] authored May 4, 2022
1 parent 7c5f0e4 commit c44f2b6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
14 changes: 13 additions & 1 deletion src/lib/edge-functions/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ const handleProxyRequest = (req, proxyReq) => {
})
}

const initializeProxy = async ({ config, configPath, geolocationMode, getUpdatedConfig, offline, settings, state }) => {
const initializeProxy = async ({
config,
configPath,
geolocationMode,
getUpdatedConfig,
offline,
projectDir,
settings,
state,
}) => {
const { functions: internalFunctions, importMap, path: internalFunctionsPath } = await getInternalFunctions()
const { port: mainPort } = settings
const userFunctionsPath = config.build.edge_functions
Expand All @@ -60,6 +69,7 @@ const initializeProxy = async ({ config, configPath, geolocationMode, getUpdated
importMaps: [importMap].filter(Boolean),
internalFunctions,
port: isolatePort,
projectDir,
})
const hasEdgeFunctions = userFunctionsPath !== undefined || internalFunctions.length !== 0

Expand Down Expand Up @@ -125,6 +135,7 @@ const prepareServer = async ({
importMaps,
internalFunctions,
port,
projectDir,
}) => {
const bundler = await import('@netlify/edge-bundler')
const distImportMapPath = getPathInProject([DIST_IMPORT_MAP_PATH])
Expand All @@ -148,6 +159,7 @@ const prepareServer = async ({
directories,
getUpdatedConfig,
internalFunctions,
projectDir,
runIsolate,
})

Expand Down
37 changes: 29 additions & 8 deletions src/lib/edge-functions/registry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @ts-check
const { fileURLToPath } = require('url')

const { NETLIFYDEVERR, NETLIFYDEVLOG, chalk, log, warn, watchDebounced } = require('../../utils/command-helpers')

/**
Expand All @@ -24,9 +26,19 @@ class EdgeFunctionsRegistry {
* @param {string[]} opts.directories
* @param {() => Promise<object>} opts.getUpdatedConfig
* @param {EdgeFunction[]} opts.internalFunctions
* @param {string} opts.projectDir
* @param {(functions: EdgeFunction[]) => Promise<object>} opts.runIsolate
*/
constructor({ bundler, config, configPath, directories, getUpdatedConfig, internalFunctions, runIsolate }) {
constructor({
bundler,
config,
configPath,
directories,
getUpdatedConfig,
internalFunctions,
projectDir,
runIsolate,
}) {
/**
* @type {import('@netlify/edge-bundler')}
*/
Expand Down Expand Up @@ -87,7 +99,7 @@ class EdgeFunctionsRegistry {
*/
this.initialScan = this.scan(directories)

this.setupWatchers()
this.setupWatchers({ projectDir })
}

/**
Expand Down Expand Up @@ -283,7 +295,12 @@ class EdgeFunctionsRegistry {
const dependencyPaths = new Map()

graph.modules.forEach(({ dependencies = [], specifier }) => {
const functionMatch = functionPaths.get(specifier)
if (!specifier.startsWith('file://')) {
return
}

const path = fileURLToPath(specifier)
const functionMatch = functionPaths.get(path)

if (!functionMatch) {
return
Expand All @@ -301,9 +318,10 @@ class EdgeFunctionsRegistry {
}

const { specifier: dependencyURL } = dependency.code
const functions = dependencyPaths.get(dependencyURL) || []
const dependencyPath = fileURLToPath(dependencyURL)
const functions = dependencyPaths.get(dependencyPath) || []

dependencyPaths.set(dependencyURL, [...functions, functionMatch])
dependencyPaths.set(dependencyPath, [...functions, functionMatch])
})
})

Expand All @@ -323,7 +341,7 @@ class EdgeFunctionsRegistry {
return functions
}

async setupWatchers() {
async setupWatchers({ projectDir }) {
// Creating a watcher for the config file. When it changes, we update the
// declarations and see if we need to register or unregister any functions.
this.configWatcher = await watchDebounced(this.configPath, {
Expand All @@ -336,8 +354,11 @@ class EdgeFunctionsRegistry {
},
})

// Creating a watcher for each source directory.
await Promise.all(this.directories.map((directory) => this.setupWatcherForDirectory(directory)))
// While functions are guaranteed to be inside one of the configured
// directories, they might be importing files that are located in
// parent directories. So we watch the entire project directory for
// changes.
await this.setupWatcherForDirectory(projectDir)
}

async setupWatcherForDirectory(directory) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ const startProxy = async function ({
geolocationMode,
getUpdatedConfig,
offline,
projectDir,
settings,
state,
})
Expand Down

1 comment on commit c44f2b6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 274 MB

Please sign in to comment.