mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat: add firebase preset (#100)
This commit is contained in:
parent
c76bd35c29
commit
9390acce83
82
packages/nitro/src/presets/firebase.ts
Normal file
82
packages/nitro/src/presets/firebase.ts
Normal 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`')
|
||||
}
|
@ -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'
|
||||
|
7
packages/nitro/src/runtime/entries/firebase.ts
Normal file
7
packages/nitro/src/runtime/entries/firebase.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import '~polyfill'
|
||||
|
||||
import { handle } from '../server'
|
||||
|
||||
const functions = require('firebase-functions')
|
||||
|
||||
export const server = functions.https.onRequest(handle)
|
Loading…
Reference in New Issue
Block a user