Nuxt/packages/nitro/src/presets/netlify.ts

39 lines
1.3 KiB
TypeScript
Raw Normal View History

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'
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: {
dir: '{{ _nuxt.rootDir }}/netlify/functions',
2020-11-20 01:49:43 +00:00
publicDir: '{{ _nuxt.rootDir }}/dist'
},
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'
]
})
// eslint-disable-next-line
export const netlify_builder: NitroPreset = extendPreset(netlify, {
entry: '{{ _internal.runtimeDir }}/entries/netlify_builder'
})