Skip to content

Commit

Permalink
✨ add hmr package
Browse files Browse the repository at this point in the history
  • Loading branch information
prazdevs committed May 9, 2023
1 parent 4833929 commit 78b80dc
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/hmr/CHANGELOG.md
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`
32 changes: 32 additions & 0 deletions packages/hmr/README.md
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.
33 changes: 33 additions & 0 deletions packages/hmr/package.json
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"
}
}
47 changes: 47 additions & 0 deletions packages/hmr/src/index.ts
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?.()
}
}
}
}

0 comments on commit 78b80dc

Please sign in to comment.