diff --git a/.npmrc b/.npmrc
index bf2e7648b0..cf04042455 100644
--- a/.npmrc
+++ b/.npmrc
@@ -1 +1,2 @@
shamefully-hoist=true
+strict-peer-dependencies=false
diff --git a/packages/docs/ssr/nuxt.md b/packages/docs/ssr/nuxt.md
index 72e5814d5e..05ee3af96b 100644
--- a/packages/docs/ssr/nuxt.md
+++ b/packages/docs/ssr/nuxt.md
@@ -1,15 +1,13 @@
# Nuxt.js
-Using Pinia with [Nuxt.js](https://nuxtjs.org/) is easier since Nuxt takes care of a lot of things when it comes to _server side rendering_. For instance, **you don't need to care about serialization nor XSS attacks**.
+Using Pinia with [Nuxt.js](https://nuxtjs.org/) is easier since Nuxt takes care of a lot of things when it comes to _server side rendering_. For instance, **you don't need to care about serialization nor XSS attacks**. Pinia supports Nuxt Bridge and Nuxt 3, for bare Nuxt 2 support, [See below](#nuxt-2-without-bridge).
## Installation
-Make sure to install [`@nuxtjs/composition-api`](https://composition-api.nuxtjs.org/) alongside `pinia`:
-
```bash
-yarn add pinia @pinia/nuxt @nuxtjs/composition-api
+yarn add @pinia/nuxt
# or with npm
-npm install pinia @pinia/nuxt @nuxtjs/composition-api
+npm install @pinia/nuxt
```
We supply a _module_ to handle everything for you, you only need to add it to `buildModules` in your `nuxt.config.js` file:
@@ -19,9 +17,7 @@ We supply a _module_ to handle everything for you, you only need to add it to `b
export default {
// ... other options
buildModules: [
- // Nuxt 2 only:
- // https://composition-api.nuxtjs.org/getting-started/setup#quick-start
- '@nuxtjs/composition-api/module',
+ // ...
'@pinia/nuxt',
],
}
@@ -43,37 +39,28 @@ export default {
}
```
-## Using the Nuxt context in stores
+## Auto imports
-You can also use [the context](https://nuxtjs.org/docs/2.x/internals-glossary/context) in any store by using the injected property `$nuxt`:
-
-```js
-import { useUserStore } from '~/stores/userStore'
-
-defineStore('cart', {
- actions: {
- purchase() {
- const user = useUserStore()
- if (!user.isAuthenticated()) {
- this.$nuxt.redirect('/login')
- }
- },
- },
-})
-```
-
-## Using Pinia alongside Vuex
-
-It is recommended to **avoid using both Pinia and Vuex** but if you need to use both, you need to tell pinia to not disable it:
+By default `@pinia/nuxt` exposes one single auto import: `usePinia()`, which is similar to `getActivePinia()` but works better with Nuxt. You can add auto imports to make your life easier:
```js
// nuxt.config.js
export default {
+ // ... other options
buildModules: [
- '@nuxtjs/composition-api/module',
- ['@pinia/nuxt', { disableVuex: false }],
+ // ...
+ [
+ '@pinia/nuxt',
+ {
+ autoImports: [
+ // automatically imports `usePinia()`
+ 'defineStore',
+ // automatically imports `usePinia()` as `usePiniaStore()`
+ ['defineStore', 'definePiniaStore'],
+ ],
+ },
+ ],
],
- // ... other options
}
```
@@ -91,3 +78,43 @@ If you are using TypeScript or have a `jsconfig.json`, you should also add the t
```
This will also ensure you have autocompletion 😉 .
+
+## Nuxt 2 without bridge
+
+Pinia supports Nuxt 2 until `@pinia/nuxt` v0.2.1. Make sure to also install [`@nuxtjs/composition-api`](https://composition-api.nuxtjs.org/) alongside `pinia`:
+
+```bash
+yarn add pinia @pinia/nuxt@0.2.1 @nuxtjs/composition-api
+# or with npm
+npm install pinia @pinia/nuxt@0.2.1 @nuxtjs/composition-api
+```
+
+We supply a _module_ to handle everything for you, you only need to add it to `buildModules` in your `nuxt.config.js` file:
+
+```js
+// nuxt.config.js
+export default {
+ // ... other options
+ buildModules: [
+ // Nuxt 2 only:
+ // https://composition-api.nuxtjs.org/getting-started/setup#quick-start
+ '@nuxtjs/composition-api/module',
+ '@pinia/nuxt',
+ ],
+}
+```
+
+### Using Pinia alongside Vuex
+
+It is recommended to **avoid using both Pinia and Vuex** but if you need to use both, you need to tell pinia to not disable it:
+
+```js
+// nuxt.config.js
+export default {
+ buildModules: [
+ '@nuxtjs/composition-api/module',
+ ['@pinia/nuxt', { disableVuex: false }],
+ ],
+ // ... other options
+}
+```
diff --git a/packages/nuxt/.gitignore b/packages/nuxt/.gitignore
new file mode 100644
index 0000000000..a8ef4f1d9c
--- /dev/null
+++ b/packages/nuxt/.gitignore
@@ -0,0 +1,2 @@
+.nuxt
+.output
diff --git a/packages/nuxt/build.config.ts b/packages/nuxt/build.config.ts
deleted file mode 100644
index 922465bfda..0000000000
--- a/packages/nuxt/build.config.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-import { defineBuildConfig } from 'unbuild'
-
-export default defineBuildConfig({
- declaration: true,
- rollup: {
- emitCJS: false,
- cjsBridge: true,
- },
- entries: ['src/module', 'src/templates/plugin'],
- externals: ['pinia', '@nuxt/kit-edge', '@nuxt/types'],
-})
diff --git a/packages/nuxt/module.cjs b/packages/nuxt/module.cjs
deleted file mode 100644
index 5df57566c4..0000000000
--- a/packages/nuxt/module.cjs
+++ /dev/null
@@ -1,6 +0,0 @@
-// CommonJS proxy to bypass jiti transforms from nuxt 2 and using native ESM
-module.exports = function(...args) {
- return import('./dist/module.mjs').then(m => m.default.call(this, ...args))
-}
-
-module.exports.meta = require('./package.json')
diff --git a/packages/nuxt/package.json b/packages/nuxt/package.json
index 984d8a7e59..b6f27d3ecb 100644
--- a/packages/nuxt/package.json
+++ b/packages/nuxt/package.json
@@ -24,35 +24,35 @@
"email": "posva13@gmail.com"
},
"sideEffects": false,
+ "type": "module",
"exports": {
".": {
"import": "./dist/module.mjs",
- "require": "./module.cjs"
- },
- "./dist/*": "./dist/*"
+ "require": "./dist/module.cjs"
+ }
},
- "main": "./module.cjs",
- "types": "./dist/module.d.ts",
+ "main": "./dist/module.cjs",
+ "types": "./dist/types.d.ts",
"files": [
- "module.cjs",
- "dist/*.js",
- "dist/*.mjs",
- "dist/*.d.ts"
+ "dist"
],
"scripts": {
- "build": "unbuild",
+ "build": "pnpm dev:prepare && nuxt-module-build",
+ "dev": "nuxi dev playground",
+ "dev:build": "nuxi build playground",
+ "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
+ "test:types": "pnpm dev:prepare && nuxi typecheck",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . -l @pinia/nuxt -r 1"
},
"dependencies": {
- "vue-demi": "*"
+ "@nuxt/kit": "^3.0.0-rc.4",
+ "pinia": ">=2.0.14"
},
"devDependencies": {
- "@nuxt/types": "^2.15.8",
- "pinia": "^2.0.0",
- "unbuild": "^0.7.4"
- },
- "peerDependencies": {
- "pinia": ">=2.0.16"
+ "@nuxt/module-builder": "latest",
+ "nuxt": "^3.0.0-rc.4",
+ "typescript": "^4.6.3",
+ "vue-tsc": "^0.38.3"
},
"publishConfig": {
"access": "public"
diff --git a/packages/nuxt/playground/app.vue b/packages/nuxt/playground/app.vue
new file mode 100644
index 0000000000..b36025b412
--- /dev/null
+++ b/packages/nuxt/playground/app.vue
@@ -0,0 +1,14 @@
+
+
+
+