Skip to content

Commit c2c7267

Browse files
committed
fix(emulators): activate without enabled option in dev
1 parent fd5db49 commit c2c7267

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

packages/nuxt/src/module.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
3636

3737
defaults: {
3838
optionsApiPlugin: false,
39-
emulators: true,
39+
emulators: { enabled: true },
4040
},
4141

4242
async setup(options, nuxt) {
@@ -51,16 +51,6 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
5151
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
5252
const templatesDir = fileURLToPath(new URL('../templates', import.meta.url))
5353

54-
// we need this to avoid some warnings about missing credentials and ssr
55-
const emulatorsConfig = await willUseEmulators(
56-
options,
57-
resolve(nuxt.options.rootDir, 'firebase.json'),
58-
logger
59-
)
60-
61-
// to handle TimeStamp and GeoPoints objects
62-
addPlugin(resolve(runtimeDir, 'payload-plugin'))
63-
6454
// TODO: I don't think the appConfig is the right place to store these as it makes things reactive
6555
// Let plugins and the user access the firebase config within the app
6656
nuxt.options.appConfig.firebaseConfig = markRaw(options.config)
@@ -73,13 +63,23 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
7363
emulators: {
7464
enabled:
7565
typeof options.emulators === 'object'
76-
? options.emulators.enabled
66+
? options.emulators.enabled ?? true // allows user to comment out enabled: false
7767
: !!options.emulators,
7868
...(typeof options.emulators === 'object' ? options.emulators : {}),
7969
},
8070
},
8171
}
8272

73+
// we need this to avoid some warnings about missing credentials and ssr
74+
const emulatorsConfig = await willUseEmulators(
75+
nuxt.options.runtimeConfig.vuefire.options!,
76+
resolve(nuxt.options.rootDir, 'firebase.json'),
77+
logger
78+
)
79+
80+
// to handle TimeStamp and GeoPoints objects
81+
addPlugin(resolve(runtimeDir, 'payload-plugin'))
82+
8383
nuxt.options.build.transpile.push(runtimeDir)
8484
nuxt.options.build.transpile.push(templatesDir)
8585

@@ -194,7 +194,11 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
194194
// Emulators must be enabled after the app is initialized but before some APIs like auth.signinWithCustomToken() are called
195195

196196
if (emulatorsConfig) {
197-
const emulators = detectEmulators(options, emulatorsConfig, logger)
197+
const emulators = detectEmulators(
198+
nuxt.options.runtimeConfig.vuefire.options!,
199+
emulatorsConfig,
200+
logger
201+
)
198202
// add the option to disable the warning. It only exists in Auth
199203
if (emulators?.auth) {
200204
emulators.auth.options =

packages/nuxt/src/module/emulators.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { readFile, stat, access, constants } from 'node:fs/promises'
1+
import { readFile, access, constants } from 'node:fs/promises'
22
import stripJsonComments from 'strip-json-comments'
33
import type { ConsolaInstance } from 'consola'
4-
import type { VueFireNuxtModuleOptions } from './options'
4+
import type { VueFireNuxtModuleOptionsResolved } from './options'
55

66
export async function willUseEmulators(
7-
{ emulators }: VueFireNuxtModuleOptions,
7+
{ emulators }: VueFireNuxtModuleOptionsResolved,
88
firebaseJsonPath: string,
99
logger: ConsolaInstance
1010
): Promise<NonNullable<FirebaseEmulatorsJSON['emulators']> | null> {
1111
const isEmulatorEnabled =
12-
(typeof emulators === 'object' ? emulators.enabled : !!emulators) &&
12+
// emulators is always defined
13+
emulators.enabled &&
1314
// Disable emulators on production unless the user explicitly enables them
1415
(process.env.NODE_ENV !== 'production' || process.env.VUEFIRE_EMULATORS)
1516

@@ -26,10 +27,6 @@ export async function willUseEmulators(
2627
return null
2728
}
2829

29-
const fileStats = await stat(firebaseJsonPath)
30-
if (!fileStats.isFile()) {
31-
return null
32-
}
3330
let firebaseJson: FirebaseEmulatorsJSON | null = null
3431
try {
3532
firebaseJson = JSON.parse(
@@ -54,31 +51,34 @@ export async function willUseEmulators(
5451
* @param logger - The logger instance
5552
*/
5653
export function detectEmulators(
57-
{ emulators: _emulatorsOptions, auth }: VueFireNuxtModuleOptions,
58-
emulators: NonNullable<FirebaseEmulatorsJSON['emulators']>,
54+
{
55+
emulators: _vuefireEmulatorsOptions,
56+
auth,
57+
}: VueFireNuxtModuleOptionsResolved,
58+
firebaseEmulatorsConfig: NonNullable<FirebaseEmulatorsJSON['emulators']>,
5959
logger: ConsolaInstance
6060
) {
6161
// normalize the emulators option
62-
const emulatorsOptions =
63-
typeof _emulatorsOptions === 'object'
64-
? _emulatorsOptions
62+
const vuefireEmulatorsOptions =
63+
typeof _vuefireEmulatorsOptions === 'object'
64+
? _vuefireEmulatorsOptions
6565
: {
66-
enabled: _emulatorsOptions,
66+
enabled: _vuefireEmulatorsOptions,
6767
}
6868

69-
if (!emulators) {
70-
if (emulatorsOptions.enabled !== false) {
69+
if (!firebaseEmulatorsConfig) {
70+
if (vuefireEmulatorsOptions.enabled !== false) {
7171
logger.warn(
7272
'You enabled emulators but there is no `emulators` key in your `firebase.json` file. Emulators will not be enabled.'
7373
)
7474
}
7575
return
7676
}
7777

78-
const defaultHost: string = emulatorsOptions.host || '127.0.0.1'
78+
const defaultHost: string = vuefireEmulatorsOptions.host || '127.0.0.1'
7979

8080
const emulatorsToEnable = services.reduce((acc, service) => {
81-
if (emulators[service]) {
81+
if (firebaseEmulatorsConfig[service]) {
8282
// these env variables are automatically picked up by the admin SDK too
8383
// https://firebase.google.com/docs/emulator-suite/connect_rtdb?hl=en&authuser=0#admin_sdks
8484
// Also, Firestore is the only one that has a different env variable
@@ -109,7 +109,7 @@ export function detectEmulators(
109109
}
110110

111111
// take the values from the firebase.json file
112-
const emulatorsServiceConfig = emulators[service]
112+
const emulatorsServiceConfig = firebaseEmulatorsConfig[service]
113113
// they might be picked up from the environment variables
114114
host ??= emulatorsServiceConfig?.host || defaultHost
115115
port ??= emulatorsServiceConfig?.port

packages/nuxt/src/module/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,5 @@ export interface VueFireNuxtModuleOptions {
7777

7878
export interface VueFireNuxtModuleOptionsResolved
7979
extends Omit<VueFireNuxtModuleOptions, 'emulators'> {
80-
emulators: Exclude<VueFireNuxtModuleOptions['emulators'], boolean>
80+
emulators: Exclude<VueFireNuxtModuleOptions['emulators'], boolean | undefined>
8181
}

0 commit comments

Comments
 (0)