2021-06-14 22:10:17 +00:00
|
|
|
import { join } from 'upath'
|
|
|
|
import { existsSync, readFile, writeFile } from 'fs-extra'
|
|
|
|
import consola from 'consola'
|
2020-11-20 00:16:31 +00:00
|
|
|
import { extendPreset } from '../utils'
|
2021-06-14 22:10:17 +00:00
|
|
|
import { NitroContext, NitroPreset } from '../context'
|
2020-11-20 00:16:31 +00:00
|
|
|
import { lambda } from './lambda'
|
|
|
|
|
2021-01-22 19:55:59 +00:00
|
|
|
export const netlify: NitroPreset = extendPreset(lambda, {
|
2020-11-20 01:49:43 +00:00
|
|
|
output: {
|
2021-06-14 22:10:17 +00:00
|
|
|
dir: '{{ _nuxt.rootDir }}/netlify/functions',
|
2020-11-20 01:49:43 +00:00
|
|
|
publicDir: '{{ _nuxt.rootDir }}/dist'
|
|
|
|
},
|
2021-06-14 22:10:17 +00:00
|
|
|
hooks: {
|
|
|
|
async 'nitro:compiled' (ctx: NitroContext) {
|
|
|
|
const redirectsPath = join(ctx._nuxt.rootDir, '_redirects')
|
|
|
|
let contents = '/* /.netlify/functions/server 200'
|
|
|
|
if (existsSync(redirectsPath)) {
|
|
|
|
const currentRedirects = await readFile(redirectsPath, 'utf-8')
|
|
|
|
if (currentRedirects.match(/^\/\* /m)) {
|
|
|
|
consola.info('Not adding Nitro fallback to `_redirects` (as an existing fallback was found).')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
consola.info('Adding Nitro fallback to `_redirects` to handle all unmatched routes.')
|
|
|
|
contents = currentRedirects + '\n' + contents
|
|
|
|
}
|
|
|
|
await writeFile(redirectsPath, contents)
|
|
|
|
}
|
|
|
|
},
|
2020-11-20 00:16:31 +00:00
|
|
|
ignore: [
|
|
|
|
'netlify.toml',
|
|
|
|
'_redirects'
|
|
|
|
]
|
|
|
|
})
|
2021-04-08 14:53:54 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
export const netlify_builder: NitroPreset = extendPreset(netlify, {
|
|
|
|
entry: '{{ _internal.runtimeDir }}/entries/netlify_builder'
|
|
|
|
})
|