2021-09-27 12:49:36 +00:00
|
|
|
import { join } from 'pathe'
|
2021-06-14 22:10:17 +00:00
|
|
|
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-07-26 10:37:39 +00:00
|
|
|
dir: '{{ _nuxt.rootDir }}/.netlify/functions-internal',
|
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) {
|
2021-07-01 12:49:42 +00:00
|
|
|
const redirectsPath = join(ctx.output.publicDir, '_redirects')
|
2021-06-14 22:10:17 +00:00
|
|
|
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)
|
2021-07-26 10:37:39 +00:00
|
|
|
},
|
|
|
|
'nitro:rollup:before' (ctx: NitroContext) {
|
|
|
|
ctx.rollupConfig.output.entryFileNames = 'server.ts'
|
2021-06-14 22:10:17 +00:00
|
|
|
}
|
|
|
|
},
|
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'
|
|
|
|
})
|