--- /dev/null
+import path from 'path'
+
+/** @type {import('@nuxt/types').Module<{ disableVuex?: boolean}>} */
+export default function NuxtPiniaModule(options) {
+ const disableStore = 'disableVuex' in options ? options.disableVuex : true
+
+ // Disable default Vuex store (options.features only exists in Nuxt v2.10+)
+ if (this.options.features && disableStore) {
+ this.options.features.store = false
+ }
+
+ this.addPlugin({
+ src: path.resolve(__dirname, 'plugin.js'),
+ fileName: 'pinia.js',
+ })
+
+ this.options.build.transpile = this.options.build.transpile || []
+
+ // transpile pinia if @vue/composition-api is transpiled because we must use the same instance
+ if (
+ !this.options.dev &&
+ !this.options.build.transpile.includes('pinia') &&
+ this.options.build.transpile.includes('@vue/composition-api')
+ ) {
+ this.options.build.transpile.push('pinia')
+ }
+}
--- /dev/null
+// @ts-check
+/// <reference types="./types" />
+import Vue from 'vue'
+// @ts-expect-error: this must be pinia to load the local module
+import { setActivePinia, PiniaPlugin, createPinia } from 'pinia'
+
+Vue.use(PiniaPlugin)
+
+/** @type {import('@nuxt/types').Plugin} */
+const myPlugin = (context, inject) => {
+ // console.log(context)
+
+ /** @type {import('../src').Pinia} */
+ const pinia = createPinia()
+ inject('pinia', pinia)
+ // simulate the injection ofr `new Vue({ pinia })`
+ context.app.pinia = pinia
+ // we also inject it without the $ to be able to use it without it
+ context.pinia = pinia
+ setActivePinia(pinia)
+
+ // we bypass warnings
+ // @ts-expect-error
+ pinia._p.push(() => ({ $nuxt: context }))
+
+ if (process.server) {
+ context.beforeNuxtRender(({ nuxtState }) => {
+ nuxtState.pinia = pinia.state.value
+ })
+ } else if (context.nuxtState && context.nuxtState.pinia) {
+ pinia.state.value = context.nuxtState.pinia
+ }
+}
+
+export default myPlugin
--- /dev/null
+import '@nuxt/types'
+import 'pinia'
+import { Pinia } from 'pinia'
+import { Context } from '@nuxt/types'
+
+declare module '@nuxt/types' {
+ export interface Context {
+ pinia: Pinia
+ $pinia: Pinia
+ }
+}
+
+declare module 'pinia' {
+ export interface PiniaCustomProperties {
+ $nuxt: Context
+ }
+}