1
- import { promises } from 'fs'
2
1
import { join } from 'path'
3
2
4
- import { OnPreBuild } from '@netlify/build'
5
- import { build } from '@netlify/esbuild'
6
- import { watch } from 'chokidar'
3
+ import type { OnPreBuild } from '@netlify/build'
4
+ import execa from 'execa'
7
5
8
6
import { writeDevEdgeFunction } from './edge'
9
7
import { patchNextFiles } from './files'
10
8
11
- const fileList = ( watched : Record < string , Array < string > > ) =>
12
- Object . entries ( watched ) . flatMap ( ( [ dir , files ] ) => files . map ( ( file ) => `${ dir } /${ file } ` ) )
13
-
14
- let watcher
15
-
16
9
// The types haven't been updated yet
17
10
export const onPreDev : OnPreBuild = async ( { constants, netlifyConfig } ) => {
18
11
const base = netlifyConfig . build . base ?? process . cwd ( )
@@ -21,72 +14,8 @@ export const onPreDev: OnPreBuild = async ({ constants, netlifyConfig }) => {
21
14
await patchNextFiles ( base )
22
15
23
16
await writeDevEdgeFunction ( constants )
24
-
25
- watcher = watch ( [ 'middleware.js' , 'middleware.ts' , 'src/middleware.js' , 'src/middleware.ts' ] , {
26
- persistent : true ,
27
- atomic : true ,
28
- ignoreInitial : true ,
29
- cwd : base ,
30
- } )
31
-
32
- const update = async ( initial = false ) => {
33
- try {
34
- await promises . unlink ( join ( base , '.netlify' , 'middleware.js' ) )
35
- } catch { }
36
-
37
- const watchedFiles = fileList ( watcher . getWatched ( ) )
38
- if ( watchedFiles . length === 0 ) {
39
- if ( ! initial ) {
40
- console . log ( 'No middleware found' )
41
- }
42
- return
43
- }
44
- if ( watchedFiles . length > 1 ) {
45
- console . log ( 'Multiple middleware files found:' )
46
- console . log ( watchedFiles . join ( '\n' ) )
47
- console . log ( 'This is not supported.' )
48
-
49
- return
50
- }
51
- console . log ( `${ initial ? 'Building' : 'Rebuilding' } middleware ${ watchedFiles [ 0 ] } ...` )
52
- try {
53
- await build ( {
54
- entryPoints : watchedFiles ,
55
- outfile : '.next/middleware.js' ,
56
- bundle : true ,
57
- format : 'esm' ,
58
- target : 'esnext' ,
59
- } )
60
- } catch ( error ) {
61
- console . error ( error )
62
- return
63
- }
64
-
65
- console . log ( '...done' )
66
- }
67
-
68
- watcher
69
- . on ( 'change' , ( path ) => {
70
- console . log ( `File ${ path } has been changed` )
71
- update ( )
72
- } )
73
- . on ( 'add' , ( path ) => {
74
- console . log ( `File ${ path } has been added` )
75
- update ( )
76
- } )
77
- . on ( 'unlink' , ( path ) => {
78
- console . log ( `File ${ path } has been removed` )
79
- update ( )
80
- } )
81
- . on ( 'ready' , ( ) => {
82
- console . log ( 'Initial scan complete. Ready for changes' )
83
- update ( true )
84
- } )
85
-
86
- const promise = new Promise < void > ( ( resolve ) => {
87
- process . on ( 'SIGINT' , ( ) => {
88
- watcher . close ( )
89
- resolve ( )
90
- } )
17
+ // Don't await this or it will never finish
18
+ execa . node ( join ( __dirname , 'watcher.js' ) , [ base ] , {
19
+ stdio : 'inherit' ,
91
20
} )
92
21
}
0 commit comments