-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat!(nuxt): upgrade nuxt module #1435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b5674b9
feat(nuxt)!: rewrite with nuxt/kit
pi0 e97bcb0
auto imports
pi0 d816163
update deps
pi0 2e75652
lint
pi0 a6534d3
use nuxi to typecheck nuxt
pi0 5919835
add vue-tsc devDependency
pi0 1a85c24
fix build script
pi0 0af4ebd
remove vue-demi
pi0 894d81d
simplify injections
pi0 45b9318
feat: usePinia composable
pi0 d49defb
refactor: use resolver
pi0 5d53d34
fix lint issues
pi0 ab14389
chore: add back the name and missing configs
posva 826c7ec
wip: add incomplete missing types
posva 21f4d05
ci: prepare types
posva 873f79c
chore: format
posva 607f76a
test(ts): add src folder
posva 6fde230
chore: fix tsconfig
posva 8c3b34e
test: remove failing types
posva 74713bf
add missing peer dep
pi0 9849aed
feat(nuxt): remove wrong `$nuxt` in Nuxt 3
posva d630cb0
feat(nuxt): deprecate old `$nuxt` context
posva a15cf09
style: format comment
posva 1ae5c41
docs: remove outdated nuxt section
posva 1a01da6
docs: updated nuxt instructions
posva 3d7b930
feat(nuxt): add `autoImports` option in module
posva 08b8ccb
fix(nuxt): correct type for `$nuxt`
posva e0a1210
docs: update version
posva cbaf9de
chore: ok
posva 5286aee
chore: up lock
posva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
shamefully-hoist=true | ||
strict-peer-dependencies=false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.nuxt | ||
.output |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script lang="ts" setup> | ||
const counter = useCounter() | ||
|
||
if (process.server) { | ||
counter.increment() | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<p>Count: {{ counter.$state.count }}</p> | ||
<button @click="counter.increment()">+</button> | ||
</div> | ||
</template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const useCounter = definePiniaStore('counter', { | ||
state: () => ({ | ||
count: 100, | ||
}), | ||
actions: { | ||
increment() { | ||
this.count++ | ||
}, | ||
}, | ||
getters: { | ||
getCount: (state) => state.count, | ||
}, | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineNuxtConfig } from 'nuxt' | ||
import piniaModule from '../src/module' | ||
|
||
export default defineNuxtConfig({ | ||
modules: [piniaModule], | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"private": true, | ||
"name": "pinia-nuxt-playground" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
declare namespace NodeJS { | ||
export interface Process { | ||
server: boolean | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,83 @@ | ||
/** | ||
* @module @pinia/nuxt | ||
*/ | ||
// import { isVue2 } from 'vue-demi' | ||
import type { Pinia } from 'pinia' | ||
import type { Context, Module } from '@nuxt/types' | ||
import { | ||
defineNuxtModule, | ||
addPlugin, | ||
isNuxt2, | ||
addAutoImport, | ||
createResolver, | ||
} from '@nuxt/kit' | ||
|
||
export interface PiniaNuxtOptions { | ||
export interface ModuleOptions { | ||
/** | ||
* Pinia disables Vuex by default, set this option to `false` to avoid it and | ||
* use Pinia alongside Vuex. | ||
* use Pinia alongside Vuex (Nuxt 2 only) | ||
* | ||
* @default `true` | ||
*/ | ||
disableVuex?: boolean | ||
} | ||
|
||
const DEFAULTS = { | ||
disableVuex: true, | ||
/** | ||
* Array of auto imports to be added to the nuxt.config.js file. | ||
* | ||
* @example | ||
* ```js | ||
* autoImports: [ | ||
* // automatically import `defineStore` | ||
* 'defineStore', | ||
* // automatically import `defineStore` as `definePiniaStore` | ||
* ['defineStore', 'definePiniaStore', | ||
* ] | ||
* ``` | ||
* | ||
*/ | ||
autoImports?: Array<string | [string, string]> | ||
} | ||
|
||
export default <Module>function (_options) { | ||
const nuxt = this.nuxt | ||
const options = { | ||
...DEFAULTS, | ||
...(_options || {}), | ||
...(nuxt.options.pinia || {}), | ||
} | ||
export default defineNuxtModule<ModuleOptions>({ | ||
meta: { | ||
name: 'pinia', | ||
configKey: 'pinia', | ||
compatibility: { | ||
nuxt: '^2.0.0 || ^3.0.0', | ||
bridge: true, | ||
}, | ||
}, | ||
defaults: { | ||
disableVuex: true, | ||
autoImports: [], | ||
}, | ||
setup(options, nuxt) { | ||
const resolver = createResolver(import.meta.url) | ||
|
||
// Disable default Vuex store (options.features only exists in Nuxt v2.10+) | ||
if (nuxt.options.features && options.disableVuex) { | ||
nuxt.options.features.store = false | ||
} | ||
// Disable default Vuex store (Nuxt v2.10+ only) | ||
if (nuxt.options.features && options.disableVuex && isNuxt2()) { | ||
nuxt.options.features.store = false | ||
} | ||
|
||
// make sure we use the mjs for pinia so node doesn't complain about using a module js with an extension that is js | ||
// but doesn't have the type: module in its packages.json file | ||
nuxt.options.alias.pinia = 'pinia/dist/pinia.mjs' | ||
// Transpile runtime | ||
nuxt.options.build.transpile.push(resolver.resolve('./runtime')) | ||
|
||
this.addPlugin({ src: require.resolve('./plugin.mjs') }) | ||
// Make sure we use the mjs build for pinia | ||
nuxt.options.alias.pinia = 'pinia/dist/pinia.mjs' | ||
|
||
// transpile pinia for nuxt 2 and nuxt bridge | ||
// if (isVue2 && !nuxt.options.build.transpile.includes('pinia')) { | ||
// nuxt.options.build.transpile.push('pinia') | ||
// } | ||
} | ||
|
||
declare module '@nuxt/types' { | ||
pi0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export interface Context { | ||
/** | ||
* Pinia instance attached to the app. | ||
* | ||
* @deprecated: use context.$pinia instead | ||
*/ | ||
pinia: Pinia | ||
// Add runtime plugin | ||
if (isNuxt2()) { | ||
addPlugin(resolver.resolve('./runtime/plugin.vue2')) | ||
} else { | ||
addPlugin(resolver.resolve('./runtime/plugin.vue3')) | ||
} | ||
pi0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Pinia instance attached to the app. | ||
*/ | ||
$pinia: Pinia | ||
} | ||
} | ||
|
||
declare module 'pinia' { | ||
export interface PiniaCustomProperties { | ||
/** | ||
* Nuxt context. | ||
*/ | ||
$nuxt: Context | ||
} | ||
} | ||
// Add auto imports | ||
const composables = resolver.resolve('./runtime/composables') | ||
addAutoImport([ | ||
{ from: composables, name: 'usePinia' }, | ||
...options.autoImports.map((imports) => | ||
typeof imports === 'string' | ||
? { from: composables, name: imports } | ||
: { from: composables, name: imports[0], as: imports[1] } | ||
), | ||
]) | ||
}, | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { useNuxtApp } from '#imports' | ||
export * from 'pinia' | ||
|
||
export const usePinia = () => useNuxtApp().$pinia | ||
pi0 marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.