feat: add firebase preset (#100)

This commit is contained in:
Daniel Roe 2021-02-08 08:04:26 +00:00 committed by GitHub
parent c76bd35c29
commit 9390acce83
3 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,82 @@
import { join, relative, resolve } from 'upath'
import { existsSync, readJSONSync } from 'fs-extra'
import consola from 'consola'
import globby from 'globby'
import { writeFile } from '../utils'
import { NitroPreset, NitroContext } from '../context'
export const firebase: NitroPreset = {
inlineChunks: false,
entry: '{{ _internal.runtimeDir }}/entries/firebase',
hooks: {
async 'nitro:compiled' (ctx: NitroContext) {
await writeRoutes(ctx)
}
}
}
async function writeRoutes ({ output: { publicDir, serverDir }, _nuxt: { rootDir } }: NitroContext) {
if (!existsSync(join(rootDir, 'firebase.json'))) {
const firebase = {
functions: {
source: relative(rootDir, serverDir)
},
hosting: [
{
site: '<your_project_id>',
public: relative(rootDir, publicDir),
cleanUrls: true,
rewrites: [
{
source: '**',
function: 'server'
}
]
}
]
}
await writeFile(resolve(rootDir, 'firebase.json'), JSON.stringify(firebase))
}
const jsons = await globby(`${serverDir}/node_modules/**/package.json`)
const prefixLength = `${serverDir}/node_modules/`.length
const suffixLength = '/package.json'.length
const dependencies = jsons.reduce((obj, packageJson) => {
const dirname = packageJson.slice(prefixLength, -suffixLength)
if (!dirname.includes('node_modules')) {
obj[dirname] = require(packageJson).version
}
return obj
}, {} as Record<string, string>)
let nodeVersion = '12'
try {
const currentNodeVersion = readJSONSync(join(rootDir, 'package.json')).engines.node
if (['12', '10'].includes(currentNodeVersion)) {
nodeVersion = currentNodeVersion
}
} catch {}
await writeFile(
resolve(serverDir, 'package.json'),
JSON.stringify(
{
private: true,
main: './index.js',
dependencies,
devDependencies: {
'firebase-functions-test': 'latest',
'firebase-admin': require('firebase-admin/package.json').version,
'firebase-functions': require('firebase-functions/package.json')
.version
},
engines: { node: nodeVersion }
},
null,
2
)
)
consola.success('Ready to run `firebase deploy`')
}

View File

@ -2,6 +2,7 @@ export * from './azure_functions'
export * from './azure'
export * from './browser'
export * from './cloudflare'
export * from './firebase'
export * from './lambda'
export * from './netlify'
export * from './node'

View File

@ -0,0 +1,7 @@
import '~polyfill'
import { handle } from '../server'
const functions = require('firebase-functions')
export const server = functions.https.onRequest(handle)