Skip to content

Commit fb3015b

Browse files
committed
refactor: split module file
1 parent 728f811 commit fb3015b

File tree

3 files changed

+247
-233
lines changed

3 files changed

+247
-233
lines changed

packages/nuxt/src/module.ts

Lines changed: 15 additions & 233 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { fileURLToPath } from 'node:url'
22
import { normalize } from 'node:path'
3-
import { readFile, stat } from 'node:fs/promises'
4-
import stripJsonComments from 'strip-json-comments'
53
import {
64
addImports,
75
addPlugin,
@@ -12,70 +10,11 @@ import {
1210
} from '@nuxt/kit'
1311
// cannot import from firebase/app because the build fails, maybe a nuxt bug?
1412
import type { FirebaseApp, FirebaseOptions } from '@firebase/app-types'
15-
import type {
16-
AppOptions,
17-
ServiceAccount,
18-
App as FirebaseAdminApp,
19-
} from 'firebase-admin/app'
13+
import type { AppOptions, App as FirebaseAdminApp } from 'firebase-admin/app'
2014
import { markRaw } from 'vue'
2115
import { consola } from 'consola'
22-
import type { NuxtVueFireAppCheckOptions } from './runtime/app-check'
23-
24-
export interface VueFireNuxtModuleOptions {
25-
/**
26-
* Should we add the `VueFireFirestoreOptionsAPI` and `VueFireRealtimeDatabaseOptionsAPI` modules?. Pass `true` to add
27-
* both, or `'firestore'` or `'realtime-database'` to add only one. Pass false to disable.
28-
* @default false
29-
*/
30-
optionsApiPlugin?: boolean | 'firestore' | 'database'
31-
32-
/**
33-
* Firebase Options passed to `firebase/app`'s `initializeApp()`.
34-
*/
35-
config?: FirebaseOptions
36-
37-
/**
38-
* Firebase Admin Options.
39-
*/
40-
admin?: {
41-
/**
42-
* Firebase Admin Options passed to `firebase-admin`'s `initializeApp()`. Required if you are using the auth, or the
43-
* app-check module.
44-
*/
45-
options?: Omit<AppOptions, 'credential'>
46-
47-
// TODO: remove, use env variables instead
48-
/**
49-
* Firebase Admin Service Account passed to `firebase-admin`'s `initializeApp()`. Required if you are adding an
50-
* adminConfig.
51-
* @deprecated use GOOGLE_APPLICATION_CREDENTIALS env variable instead with the service-account JSON content
52-
*/
53-
serviceAccount?: string | ServiceAccount
54-
}
55-
56-
/**
57-
* Enables AppCheck on the client and server. Note you only need to pass the options for the client, on the server,
58-
* the configuration will be handled automatically.
59-
*/
60-
appCheck?: NuxtVueFireAppCheckOptions
61-
62-
/**
63-
* Enables Authentication
64-
*/
65-
auth?: boolean
66-
67-
/**
68-
* Controls whether to use emulators or not. Pass `false` to disable emulators. When set to `true`, emulators are enabled when they are detected in the `firebase.json` file. You still need to run the emulators in parallel to your app.
69-
*/
70-
emulators?:
71-
| boolean
72-
| {
73-
/**
74-
* The host for the Firestore emulator. Defaults to `localhost`.
75-
*/
76-
host?: string
77-
}
78-
}
16+
import { VueFireNuxtModuleOptions } from './module/options'
17+
import { FirebaseEmulatorsToEnable, enableEmulators } from './module/emulators'
7918

8019
const logger = consola.withTag('nuxt-vuefire module')
8120

@@ -327,6 +266,13 @@ export type {
327266
NuxtVueFireAppCheckOptionsReCaptchaEnterprise,
328267
} from './runtime/app-check'
329268

269+
/**
270+
* Type Extensions
271+
*/
272+
273+
/**
274+
* Augments the Nuxt Runtime Config with the VueFire module options.
275+
*/
330276
interface VueFireRuntimeConfig {
331277
/**
332278
* Firebase Admin options passed to VueFire module. Only available on the server.
@@ -338,6 +284,11 @@ interface VueFireRuntimeConfig {
338284

339285
interface VueFirePublicRuntimeConfig {
340286
vuefire?: {
287+
/**
288+
* Emulators to enable.
289+
*
290+
* @internal
291+
*/
341292
emulators?: FirebaseEmulatorsToEnable
342293
}
343294
}
@@ -387,172 +338,3 @@ declare module '@vue/runtime-core' {
387338
$firebaseAdminApp: FirebaseAdminApp
388339
}
389340
}
390-
391-
async function enableEmulators(
392-
{ emulators: emulatorOptions, auth }: VueFireNuxtModuleOptions,
393-
firebaseJsonPath: string,
394-
logger: typeof consola
395-
) {
396-
const fileStats = await stat(firebaseJsonPath)
397-
if (!fileStats.isFile()) {
398-
return
399-
}
400-
let firebaseJson: FirebaseEmulatorsJSON
401-
try {
402-
firebaseJson = JSON.parse(
403-
stripJsonComments(await readFile(firebaseJsonPath, 'utf8'), {
404-
trailingCommas: true,
405-
})
406-
)
407-
} catch (err) {
408-
logger.error('Error parsing the `firebase.json` file', err)
409-
logger.error('Cannot enable Emulators')
410-
return
411-
}
412-
413-
if (!firebaseJson.emulators) {
414-
if (emulatorOptions === true) {
415-
logger.warn(
416-
'You enabled emulators but there is no `emulators` key in your `firebase.json` file. Emulators will not be enabled.'
417-
)
418-
}
419-
return
420-
}
421-
422-
const services = [
423-
'auth',
424-
'database',
425-
'firestore',
426-
'functions',
427-
'storage',
428-
] as const
429-
430-
const defaultHost =
431-
typeof emulatorOptions === 'object' ? emulatorOptions.host : 'localhost'
432-
433-
const emulatorsToEnable = services.reduce((acc, service) => {
434-
if (firebaseJson.emulators![service]) {
435-
// these env variables are automatically picked up by the admin SDK too
436-
// https://firebase.google.com/docs/emulator-suite/connect_rtdb?hl=en&authuser=0#admin_sdks
437-
const envKey =
438-
service === 'firestore'
439-
? 'FIRESTORE_EMULATOR_HOST'
440-
: `FIREBASE_${service.toUpperCase()}_EMULATOR_HOST`
441-
442-
if (process.env[envKey]) {
443-
try {
444-
const url = new URL(`http://${process.env[envKey]}`)
445-
acc[service] = {
446-
host: url.hostname,
447-
port: Number(url.port),
448-
}
449-
return acc
450-
} catch (err) {
451-
logger.error(
452-
`The "${envKey}" env variable is set but it is not a valid URL. It should be something like "localhost:8080" or "127.0.0.1:8080". It will be ignored.`
453-
)
454-
logger.error(`Cannot enable the ${service} Emulator.`)
455-
}
456-
}
457-
// take the values from the firebase.json file
458-
const serviceEmulatorConfig = firebaseJson.emulators![service]
459-
if (serviceEmulatorConfig?.host == null) {
460-
logger.warn(
461-
`The "${service}" emulator is enabled but there is no "host" key in the "emulators.${service}" key of your "firebase.json" file. It is recommended to set it to avoid mismatches between origins. Set it to "${defaultHost}".`
462-
)
463-
}
464-
465-
const host = serviceEmulatorConfig?.host || defaultHost
466-
const port = serviceEmulatorConfig?.port
467-
if (!host || !port) {
468-
logger.error(
469-
`The "${service}" emulator is enabled but there is no "host" or "port" key in the "emulators" key of your "firebase.json" file. You must specify *both*. It will be ignored.`
470-
)
471-
return acc
472-
}
473-
acc[service] = { host, port }
474-
}
475-
return acc
476-
}, {} as FirebaseEmulatorsToEnable)
477-
478-
// remove the emulator if auth is not enabled
479-
if (!auth) {
480-
// @ts-expect-error: cannot be deleted without ?: but that creates other errors
481-
delete emulatorsToEnable.auth
482-
}
483-
484-
return emulatorsToEnable
485-
}
486-
487-
/**
488-
* Extracted from as we cannot install firebase-tools just for the types
489-
* - https://github.com/firebase/firebase-tools/blob/master/src/firebaseConfig.ts#L183
490-
* - https://github.com/firebase/firebase-tools/blob/master/schema/firebase-config.json
491-
* @internal
492-
*/
493-
interface FirebaseEmulatorsJSON {
494-
emulators?: {
495-
auth?: {
496-
host?: string
497-
port?: number
498-
}
499-
database?: {
500-
host?: string
501-
port?: number
502-
}
503-
eventarc?: {
504-
host?: string
505-
port?: number
506-
}
507-
extensions?: {
508-
[k: string]: unknown
509-
}
510-
firestore?: {
511-
host?: string
512-
port?: number
513-
websocketPort?: number
514-
}
515-
functions?: {
516-
host?: string
517-
port?: number
518-
}
519-
hosting?: {
520-
host?: string
521-
port?: number
522-
}
523-
hub?: {
524-
host?: string
525-
port?: number
526-
}
527-
logging?: {
528-
host?: string
529-
port?: number
530-
}
531-
pubsub?: {
532-
host?: string
533-
port?: number
534-
}
535-
singleProjectMode?: boolean
536-
storage?: {
537-
host?: string
538-
port?: number
539-
}
540-
ui?: {
541-
enabled?: boolean
542-
host?: string
543-
port?: string | number
544-
}
545-
}
546-
}
547-
548-
type FirebaseEmulatorService =
549-
| 'auth'
550-
| 'database'
551-
| 'firestore'
552-
| 'functions'
553-
| 'storage'
554-
// | 'hosting' we are the hosting emulator
555-
556-
type FirebaseEmulatorsToEnable = {
557-
[key in FirebaseEmulatorService]: { host: string; port: number }
558-
}

0 commit comments

Comments
 (0)