Skip to content

Commit 15f4e50

Browse files
committed
fix: add aliases to workaround vite issue
1 parent b51a3f5 commit 15f4e50

File tree

2 files changed

+47
-30
lines changed

2 files changed

+47
-30
lines changed

packages/nuxt/src/firebaseAliases.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// handle aliases of firebase packages until https://github.com/vitejs/vite/issues/11114 is fixed
2+
3+
import { resolvePath } from '@nuxt/kit'
4+
5+
/**
6+
* Adds an alias to the config.resolve.alias if it doesn't exist.
7+
*
8+
* @param aliases - config.resolve.alias
9+
* @param libToCheck - the name of the library to check for e.g. 'firebase/firestore'
10+
* @param filename - file after the `dist` folder e.g. 'index.mjs' -> 'firebase/firestore/dist/index.mjs'
11+
*/
12+
export async function addMissingAlias(
13+
aliases: { [find: string]: string },
14+
libToCheck: string,
15+
filename: string
16+
) {
17+
// skip adding it if the alias is already set in user config
18+
if (!aliases[libToCheck]) {
19+
// this gives an absolute path which is needed for the alias to work since the firebase package is not including the dist folder in exports
20+
const resolvedLibFolder = await resolvePath(libToCheck)
21+
const resolvedLibFile =
22+
resolvedLibFolder.slice(0, resolvedLibFolder.lastIndexOf('dist')) +
23+
'dist/' +
24+
filename
25+
aliases[libToCheck] = resolvedLibFile
26+
}
27+
}

packages/nuxt/src/module.ts

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
} from 'firebase-admin/app'
2020
import { markRaw } from 'vue'
2121
import type { NuxtVueFireAppCheckOptions } from './runtime/app-check'
22+
import { addMissingAlias } from './firebaseAliases'
2223

2324
export interface VueFireNuxtModuleOptions {
2425
/**
@@ -206,38 +207,27 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
206207
hooks: {
207208
// Resolve the correct firebase/firestore path on server only since vite is resolving the wrong one in dev
208209
'vite:extendConfig': async (config, { isServer }) => {
209-
if (isServer) {
210-
config.resolve ??= {}
211-
config.resolve.alias ??= {}
212-
213-
// skip the whole thing if the alias is already set in user config
214-
// @ts-ignore
215-
if (!config.resolve.alias['firebase/firestore']) {
216-
// this gives an absolute path which is needed for the alias to work since the firebase package is not including the dist folder in exports
217-
const resolvedFirestore = await resolvePath('firebase/firestore')
218-
const resolvedFirestoreMJS =
219-
resolvedFirestore.slice(
220-
0,
221-
resolvedFirestore.lastIndexOf('dist')
222-
) + 'dist/index.mjs'
223-
// @ts-ignore
224-
config.resolve.alias['firebase/firestore'] = resolvedFirestoreMJS
225-
226-
const resolvedNamespacedFirestore = await resolvePath(
227-
'@firebase/firestore'
228-
)
229-
const resolvedNamespacedFirestoreMJS =
230-
resolvedNamespacedFirestore.slice(
231-
0,
232-
resolvedNamespacedFirestore.lastIndexOf('dist')
233-
) + 'dist/index.node.mjs'
234-
// @ts-ignore
235-
config.resolve.alias['@firebase/firestore'] =
236-
resolvedNamespacedFirestoreMJS
237-
}
210+
config.resolve ??= {}
211+
config.resolve.alias ??= {}
212+
const aliases: Record<string, string> = config.resolve.alias as Record<
213+
string,
214+
string
215+
>
238216

239-
// add any other firebase alias you need
217+
const promises: Promise<void>[] = []
218+
219+
if (isServer) {
220+
promises.push(
221+
addMissingAlias(aliases, 'firebase/firestore', 'index.mjs')
222+
)
223+
promises.push(
224+
addMissingAlias(aliases, '@firebase/firestore', 'index.node.mjs')
225+
)
240226
}
227+
228+
promises.push(addMissingAlias(aliases, 'firebase/app', 'index.mjs'))
229+
230+
await Promise.all(promises)
241231
},
242232
},
243233
})

0 commit comments

Comments
 (0)