-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathdev.ts
46 lines (35 loc) · 1.75 KB
/
dev.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { resolve } from 'path'
import { OnPreBuild } from '@netlify/build'
import execa from 'execa'
import { unlink, existsSync } from 'fs-extra'
import { writeDevEdgeFunction } from './edge'
import { patchNextFiles } from './files'
// The types haven't been updated yet
export const onPreDev: OnPreBuild = async ({ constants, netlifyConfig }) => {
const base = netlifyConfig.build.base ?? process.cwd()
// Need to patch the files, because build might not have been run
await patchNextFiles(base)
// Clean up old functions
await unlink(resolve('.netlify', 'middleware.js')).catch(() => {
// Ignore if it doesn't exist
})
await writeDevEdgeFunction(constants)
let middlewareDetected = false
if (!existsSync(resolve(base, 'middleware.ts')) && !existsSync(resolve(base, 'middleware.js'))) {
console.log(
"No middleware found. If you did intend to use middleware, create a 'middleware.ts' or 'middleware.js' file in your project root to add custom middleware.",
)
} else {
console.log('Watching for changes in Next.js middleware...')
middlewareDetected = true
}
// Eventually we might want to do this via esbuild's API, but for now the CLI works fine
const common = [`--bundle`, `--outdir=${resolve('.netlify')}`, `--format=esm`, `--target=esnext`, '--watch']
const esbuildTSArgs = middlewareDetected ? [...common, resolve(base, 'middleware.ts')] : common
const esbuildJSArgs = middlewareDetected ? [...common, resolve(base, 'middleware.js')] : common
// TypeScript
execa(`esbuild`, esbuildTSArgs, { all: true }).all.pipe(process.stdout)
// JavaScript
execa(`esbuild`, esbuildJSArgs, { all: true }).all.pipe(process.stdout)
// Don't return the promise because we don't want to wait for the child process to finish
}