From 9390acce83b9f76dcd23aa9b547a824dc74e0c8d Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Feb 2021 08:04:26 +0000 Subject: [PATCH] feat: add firebase preset (#100) --- packages/nitro/src/presets/firebase.ts | 82 +++++++++++++++++++ packages/nitro/src/presets/index.ts | 1 + .../nitro/src/runtime/entries/firebase.ts | 7 ++ 3 files changed, 90 insertions(+) create mode 100644 packages/nitro/src/presets/firebase.ts create mode 100644 packages/nitro/src/runtime/entries/firebase.ts diff --git a/packages/nitro/src/presets/firebase.ts b/packages/nitro/src/presets/firebase.ts new file mode 100644 index 0000000000..536aa577fb --- /dev/null +++ b/packages/nitro/src/presets/firebase.ts @@ -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: '', + 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) + + 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`') +} diff --git a/packages/nitro/src/presets/index.ts b/packages/nitro/src/presets/index.ts index fed6b17a07..8f771fdc93 100644 --- a/packages/nitro/src/presets/index.ts +++ b/packages/nitro/src/presets/index.ts @@ -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' diff --git a/packages/nitro/src/runtime/entries/firebase.ts b/packages/nitro/src/runtime/entries/firebase.ts new file mode 100644 index 0000000000..ffbeba419f --- /dev/null +++ b/packages/nitro/src/runtime/entries/firebase.ts @@ -0,0 +1,7 @@ +import '~polyfill' + +import { handle } from '../server' + +const functions = require('firebase-functions') + +export const server = functions.https.onRequest(handle)