Skip to content

Commit fc340cf

Browse files
committed
refactor(nuxt): work locally with workaround vite
1 parent 95056cf commit fc340cf

File tree

10 files changed

+274
-102
lines changed

10 files changed

+274
-102
lines changed

packages/nuxt/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"prepack": "nuxt-module-build",
2727
"dev": "nuxi dev playground",
2828
"dev:build": "nuxi build playground",
29-
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground"
29+
"stub": "nuxt-module-build --stub"
3030
},
3131
"dependencies": {
3232
"@nuxt/kit": "^3.0.0"

packages/nuxt/playground/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
node_modules
2+
*.log*
3+
.nuxt
4+
.nitro
5+
.cache
6+
.output
7+
.env
8+
dist

packages/nuxt/playground/nuxt.config.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { fileURLToPath } from 'node:url'
2-
import { defineNuxtConfig } from 'nuxt/config'
3-
import VueFire from '../'
2+
import { resolve } from 'node:path'
3+
import VueFire from 'nuxt-vuefire'
4+
5+
// we need the root node modules where packages are hoisted
6+
const nodeModules = fileURLToPath(
7+
new URL('../../../node_modules', import.meta.url)
8+
)
49

510
export default defineNuxtConfig({
611
app: {
@@ -9,8 +14,8 @@ export default defineNuxtConfig({
914
},
1015

1116
alias: {
17+
// import the dev version directly
1218
vuefire: fileURLToPath(new URL('../../../src/index.ts', import.meta.url)),
13-
'nuxt-vuefire': fileURLToPath(new URL('../src/module.ts', import.meta.url)),
1419
},
1520

1621
modules: [
@@ -34,4 +39,27 @@ export default defineNuxtConfig({
3439
},
3540
],
3641
],
42+
43+
// NOTE: temporary workaround that cannot be put within the nuxt-vuefire module
44+
hooks: {
45+
// cannot be added in nuxt's resolve.alias
46+
'vite:extendConfig': (config, { isServer }) => {
47+
if (isServer) {
48+
config.resolve ??= {}
49+
config.resolve.alias ??= {}
50+
// @ts-ignore
51+
config.resolve.alias['firebase/firestore'] = resolve(
52+
nodeModules,
53+
'firebase/firestore/dist/index.mjs'
54+
)
55+
// @ts-ignore
56+
config.resolve.alias['@firebase/firestore'] = resolve(
57+
nodeModules,
58+
'@firebase/firestore/dist/index.node.mjs'
59+
)
60+
61+
// add any other firebase alias you need
62+
}
63+
},
64+
},
3765
})

packages/nuxt/playground/package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
{
22
"private": true,
3-
"name": "my-module-playground"
3+
"scripts": {
4+
"build": "nuxt build",
5+
"dev": "nuxt dev",
6+
"generate": "nuxt generate",
7+
"preview": "nuxt preview",
8+
"postinstall": "nuxt prepare"
9+
},
10+
"devDependencies": {
11+
"nuxt": "^3.0.0"
12+
},
13+
"dependencies": {
14+
"@firebase/app-types": "^0.8.1",
15+
"firebase": "^9.14.0",
16+
"nuxt-vuefire": "workspace:*",
17+
"vuefire": "workspace:*"
18+
}
419
}

packages/nuxt/playground/pages/firestore/config.vue

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@ const configRef = doc(db, 'configs', 'jORwjIykFo2NmkdzTkhU')
99
const isDoneFetching = ref(false)
1010
const isAllDoneFetching = ref(false)
1111
12-
getDoc(configRef).then((data) => {
13-
console.log('got data once', data)
14-
})
15-
16-
const { data: config, promise } = useDocument(configRef, { wait: true })
12+
const { data: config, promise } = useDocument(configRef)
1713
// const { data: hey } = useDocument(configRef)
1814
19-
promise.value.then((data) => {
20-
console.log('one', data)
21-
isDoneFetching.value = true
22-
})
15+
onMounted(() => {
16+
promise.value.then((data) => {
17+
if (process.client) {
18+
console.log(data)
19+
}
20+
isDoneFetching.value = true
21+
})
2322
24-
usePendingPromises().then((data) => {
25-
console.log(data)
26-
isAllDoneFetching.value = true
23+
usePendingPromises().then((data) => {
24+
if (process.client) {
25+
console.log(data)
26+
}
27+
isAllDoneFetching.value = true
28+
})
2729
})
2830
</script>
2931

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script lang="ts" setup>
2+
import { doc, getDoc } from 'firebase/firestore'
3+
import { onServerPrefetch, ref } from 'vue'
4+
import { useFirestore } from 'vuefire'
5+
6+
const db = useFirestore()
7+
const configRef = doc(db, 'configs', 'jORwjIykFo2NmkdzTkhU')
8+
9+
onServerPrefetch(async () => {
10+
const snapshot = await getDoc(configRef)
11+
console.log('got snapshot once', snapshot.data()?.amount)
12+
})
13+
</script>
14+
15+
<template>
16+
<div>Hello</div>
17+
</template>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
// https://nuxt.com/docs/guide/concepts/typescript
3+
"extends": "./.nuxt/tsconfig.json"
4+
}

packages/nuxt/templates/plugin.ejs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import {
2020
export default defineNuxtPlugin((nuxtApp) => {
2121
const appConfig = useAppConfig()
2222
const firebaseConfig = toRaw(appConfig).firebaseConfig
23-
console.log('Initializing Firebase app...', firebaseConfig)
2423

2524
const firebaseApp = initializeApp(firebaseConfig)
26-
console.log('initialized firebase app', !!firebaseApp)
2725

2826
// connectFirestoreEmulator(getFirestore(firebaseApp), 'localhost', 8080)
2927
// connectDatabaseEmulator(getDatabase(firebaseApp), 'localhost', 8081)

0 commit comments

Comments
 (0)