1
1
// @ts -check
2
+ const { fileURLToPath } = require ( 'url' )
3
+
2
4
const { NETLIFYDEVERR , NETLIFYDEVLOG , chalk, log, warn, watchDebounced } = require ( '../../utils/command-helpers' )
3
5
4
6
/**
@@ -24,9 +26,19 @@ class EdgeFunctionsRegistry {
24
26
* @param {string[] } opts.directories
25
27
* @param {() => Promise<object> } opts.getUpdatedConfig
26
28
* @param {EdgeFunction[] } opts.internalFunctions
29
+ * @param {string } opts.projectDir
27
30
* @param {(functions: EdgeFunction[]) => Promise<object> } opts.runIsolate
28
31
*/
29
- constructor ( { bundler, config, configPath, directories, getUpdatedConfig, internalFunctions, runIsolate } ) {
32
+ constructor ( {
33
+ bundler,
34
+ config,
35
+ configPath,
36
+ directories,
37
+ getUpdatedConfig,
38
+ internalFunctions,
39
+ projectDir,
40
+ runIsolate,
41
+ } ) {
30
42
/**
31
43
* @type {import('@netlify/edge-bundler') }
32
44
*/
@@ -87,7 +99,7 @@ class EdgeFunctionsRegistry {
87
99
*/
88
100
this . initialScan = this . scan ( directories )
89
101
90
- this . setupWatchers ( )
102
+ this . setupWatchers ( { projectDir } )
91
103
}
92
104
93
105
/**
@@ -283,7 +295,12 @@ class EdgeFunctionsRegistry {
283
295
const dependencyPaths = new Map ( )
284
296
285
297
graph . modules . forEach ( ( { dependencies = [ ] , specifier } ) => {
286
- const functionMatch = functionPaths . get ( specifier )
298
+ if ( ! specifier . startsWith ( 'file://' ) ) {
299
+ return
300
+ }
301
+
302
+ const path = fileURLToPath ( specifier )
303
+ const functionMatch = functionPaths . get ( path )
287
304
288
305
if ( ! functionMatch ) {
289
306
return
@@ -301,9 +318,10 @@ class EdgeFunctionsRegistry {
301
318
}
302
319
303
320
const { specifier : dependencyURL } = dependency . code
304
- const functions = dependencyPaths . get ( dependencyURL ) || [ ]
321
+ const dependencyPath = fileURLToPath ( dependencyURL )
322
+ const functions = dependencyPaths . get ( dependencyPath ) || [ ]
305
323
306
- dependencyPaths . set ( dependencyURL , [ ...functions , functionMatch ] )
324
+ dependencyPaths . set ( dependencyPath , [ ...functions , functionMatch ] )
307
325
} )
308
326
} )
309
327
@@ -323,7 +341,7 @@ class EdgeFunctionsRegistry {
323
341
return functions
324
342
}
325
343
326
- async setupWatchers ( ) {
344
+ async setupWatchers ( { projectDir } ) {
327
345
// Creating a watcher for the config file. When it changes, we update the
328
346
// declarations and see if we need to register or unregister any functions.
329
347
this . configWatcher = await watchDebounced ( this . configPath , {
@@ -336,8 +354,11 @@ class EdgeFunctionsRegistry {
336
354
} ,
337
355
} )
338
356
339
- // Creating a watcher for each source directory.
340
- await Promise . all ( this . directories . map ( ( directory ) => this . setupWatcherForDirectory ( directory ) ) )
357
+ // While functions are guaranteed to be inside one of the configured
358
+ // directories, they might be importing files that are located in
359
+ // parent directories. So we watch the entire project directory for
360
+ // changes.
361
+ await this . setupWatcherForDirectory ( projectDir )
341
362
}
342
363
343
364
async setupWatcherForDirectory ( directory ) {
0 commit comments