-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,16 @@ | ||
# Changelog | ||
|
||
<a name="0.2.0"></a> | ||
## 0.2.0 (2023-05-09) | ||
|
||
### Added | ||
|
||
- 🏷️ better type docs | ||
|
||
|
||
<a name="0.1.0"></a> | ||
## 0.1.0 (2023-05-09) | ||
|
||
### Added | ||
|
||
- 🚀 override Pinia's `acceptHMRUpdate` |
This file contains 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,32 @@ | ||
# `@pinia-plugin-persistedstate/hmr` | ||
|
||
> Override for Pinia's `acceptHMRUpdate` | ||
## 🚀 Quickstart | ||
|
||
1. Install with your favorite package manager: | ||
- **pnpm** : `pnpm i -D @pinia-plugin-persistedstate/hmr` | ||
- npm : `npm i -D @pinia-plugin-persistedstate/hmr` | ||
- yarn : `yarn add -D @pinia-plugin-persistedstate/hmr` | ||
|
||
2. Replace `acceptHMRUpdate` in your store definition file with: | ||
```ts | ||
import { defineStore } from 'pinia' | ||
import { acceptHMRUpdateWithHydration } from '@pinia-plugin-persistedstate/hmr' | ||
|
||
const useStore = defineStore('store', { | ||
// ... | ||
}) | ||
|
||
if (import.meta.hot) | ||
import.meta.hot.accept(acceptHMRUpdateWithHydration(useStore, import.meta.hot)) | ||
``` | ||
|
||
## ⚠️ Warning | ||
|
||
This is a copy/paste of Pinia's `acceptHMRUpdate` function that triggers `$hydrate` on HMR update. Use at your own risk. :) | ||
|
||
## 📝 License | ||
|
||
Copyright © 2022 [Sacha Bouillez](https://github.com/prazdevs). | ||
This project is under [MIT](https://github.com/prazdevs/pinia-plugin-persistedstate/blob/main/LICENSE) license. |
This file contains 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,33 @@ | ||
{ | ||
"name": "@pinia-plugin-persistedstate/hmr", | ||
"version": "0.2.0", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/prazdevs/pinia-plugin-persistedstate/tree/main/packages/hmr" | ||
}, | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.js", | ||
"import": "./dist/index.mjs" | ||
} | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "tsup --dts --format esm,cjs src/index.ts" | ||
}, | ||
"peerDependencies": { | ||
"pinia": "^2.0.0", | ||
"pinia-plugin-persistedstate": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"pinia": "^2.0.36", | ||
"pinia-plugin-persistedstate": "^3.1.0" | ||
} | ||
} |
This file contains 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,47 @@ | ||
import type { | ||
Pinia, | ||
StoreDefinition, | ||
StoreGeneric, | ||
} from 'pinia' | ||
|
||
function isUseStore(fn: any): fn is StoreDefinition { | ||
return typeof fn === 'function' && typeof fn.$id === 'string' | ||
} | ||
|
||
/** | ||
* Override of Pinia's `acceptHMRUpdate`. Use at your own discretion. | ||
* @param initialUseStore return of the `defineStore` to hot update | ||
* @param hot `import.meta.hot` | ||
*/ | ||
export function acceptHMRUpdateWithHydration(initialUseStore: any, hot: any) { | ||
return (newModule: any) => { | ||
const pinia: Pinia | undefined = hot.data.pinia || initialUseStore._pinia | ||
|
||
if (!pinia) | ||
return | ||
|
||
hot.data.pinia = pinia | ||
|
||
for (const exportName in newModule) { | ||
const useStore = newModule[exportName] | ||
// @ts-expect-error `_s is a stripped @internal` | ||
if (isUseStore(useStore) && pinia._s.has(useStore.$id)) { | ||
const id = useStore.$id | ||
|
||
if (id !== initialUseStore.$id) { | ||
console.warn( | ||
`The id of the store changed from "${initialUseStore.$id}" to "${id}". Reloading.`, | ||
) | ||
return hot.invalidate() | ||
} | ||
|
||
// @ts-expect-error `_s is a stripped @internal` | ||
const existingStore: StoreGeneric = pinia._s.get(id)! | ||
if (!existingStore) | ||
return | ||
|
||
useStore(pinia, existingStore).$hydrate?.() | ||
} | ||
} | ||
} | ||
} |