]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor: preparing for migration
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 9 Feb 2021 17:12:03 +0000 (18:12 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 9 Feb 2021 17:12:03 +0000 (18:12 +0100)
15 files changed:
README.md
__tests__/actions.spec.ts
__tests__/getters.spec.ts
__tests__/pinia/stores/cart.ts
__tests__/pinia/stores/user.ts
__tests__/rootState.spec.ts
__tests__/ssr/app/store.ts
__tests__/tds/store.test-d.ts
nuxt/plugin.js
rollup.config.js
src/deprecated.ts [new file with mode: 0644]
src/env.ts [new file with mode: 0644]
src/global.d.ts [new file with mode: 0644]
src/index.ts
src/store.ts

index 0243b0cfc826a97d922e7efff0e584776fabe7a3..9fdf7c730d22e1be3104232b5fc52d5840d637ae 100644 (file)
--- a/README.md
+++ b/README.md
@@ -8,11 +8,11 @@
 
 ## 👉 [Demo](https://vcuiu.csb.app/)
 
-⚠️⚠️⚠️ This project is experimental, it's an exploration of what a _Store_ could be like using [the composition api](https://vue-composition-api-rfc.netlify.com). It works for Vue 2 by using the [official library](https://github.com/vuejs/composition-api).
+⚠️⚠️⚠️ This project is experimental, it's an exploration of what a _Store_ could be like using [the composition api](https://vue-composition-api-rfc.netlify.com). It works for Vue 2 by using the [official plugin](https://github.com/vuejs/composition-api) to add Composition API support to Vue 2.
 
 **If you are looking for the version compatible with Vue 3.x, check the [`v2` branch](https://github.com/posva/pinia/tree/v2)**
 
-What I want is to inspire others to think about ways to improve Vuex and come up with something that works very well with the composition api. Ideally it could also be used without it. **@vue/composition-api is necessary**.
+What I want is to inspire others to think about ways to improve Vuex and come up with something that works very well with the composition api. Ideally it could also be used without it. Note **@vue/composition-api must be installed and used by your application** for Pinia to work.
 
 There are the core principles that I try to achieve with this experiment:
 
@@ -89,9 +89,9 @@ Note: **The Vue Composition API plugin must be installed for Pinia to work**
 You can create as many stores as you want, and they should each exist in different files:
 
 ```ts
-import { createStore } from 'pinia'
+import { defineStore } from 'pinia'
 
-export const useMainStore = createStore({
+export const useMainStore = defineStore({
   // name of the store
   // it is used in devtools and allows restoring state
   id: 'main',
@@ -120,7 +120,7 @@ export const useMainStore = createStore({
 })
 ```
 
-`createStore` returns a function that has to be called to get access to the store:
+`defineStore` returns a function that has to be called to get access to the store:
 
 ```ts
 import { useMainStore } from '@/stores/main'
@@ -351,12 +351,12 @@ setStateProvider(() => window.__PINIA_STATE__)
 
 ### Accessing other Stores
 
-You can `useOtherStore` inside a store `actions` and `getters`:
+You can `useOtherStore()` inside a store `actions` and `getters`:
 
 Actions are simply function that contain business logic. As with components, they **must call `useStore`** to retrieve the store:
 
 ```ts
-createStore({
+defineStore({
   id: 'cart',
   state: () => ({ items: [] }),
   getters: {
@@ -391,11 +391,11 @@ If you need to compute a value based on the `state` and/or `getters` of multiple
 To prevent this, **we follow the rule above** and we create a new file with a new store:
 
 ```ts
-import { createStore } from 'pinia'
+import { defineStore } from 'pinia'
 import { useUserStore } from './user'
 import { useCartStore } from './cart'
 
-export const useSharedStore = createStore({
+export const useSharedStore = defineStore({
   id: 'shared',
   getters: {
     summary() {
@@ -413,11 +413,11 @@ export const useSharedStore = createStore({
 When an actions needs to use multiple stores, we do the same, we create a new file with a new store:
 
 ```ts
-import { createStore } from 'pinia'
+import { defineStore } from 'pinia'
 import { useUserStore } from './user'
 import { useCartStore } from './cart'
 
-export const useSharedStore = createStore({
+export const useSharedStore = defineStore({
   id: 'shared',
   state: () => ({}),
   actions: {
@@ -436,42 +436,6 @@ export const useSharedStore = createStore({
 })
 ```
 
-#### Creating _Pinias_
-
-_Not implemented_. Still under discussion, needs more feedback as this doesn't seem necessary because it can be replaced by shared stores as shown above.
-
-Combine multiple _stores_ (gajos) into a new one:
-
-```ts
-import { pinia } from 'pinia'
-import { useUserStore } from './user'
-import { useCartStore, emptyCart } from './cart'
-
-export const useCartUserStore = pinia(
-  {
-    user: useUserStore,
-    cart: useCartStore,
-  },
-  {
-    getters: {
-      combinedGetter () {
-        return `Hi ${this.user.name}, you have ${this.cart.list.length} items in your cart. It costs ${this.cart.price}.`,
-      }
-    },
-    actions: {
-      async orderCart() {
-        try {
-          await apiOrderCart(this.user.token, this.cart.items)
-          this.cart.emptyCart()
-        } catch (err) {
-          displayError(err)
-        }
-      },
-    },
-  }
-)
-```
-
 ## TypeScript
 
 Pinia is conceived to make typing automatic, benefiting both, JS and, TS users. There are however different ways of handling types when using TS
@@ -486,7 +450,7 @@ interface MainState {
   name: string
 }
 
-export const useMainStore = createStore({
+export const useMainStore = defineStore({
   id: 'main',
   state: (): MainState => ({
     counter: 0,
index fb7aee81687633b93a9b1b58c16578f6b3cc5f29..76b56cb9a9d42ecc8d5de283205425becc00eb0e 100644 (file)
@@ -41,12 +41,12 @@ describe('Actions', () => {
     })()
   }
 
-  const useB = createStore({
+  const useB = defineStore({
     id: 'B',
     state: () => ({ b: 'b' }),
   })
 
-  const useA = createStore({
+  const useA = defineStore({
     id: 'A',
     state: () => ({ a: 'a' }),
     actions: {
index 3fce3aa43b44611cb1b0d248670279962122750b..3617c7faa51cd95192c2a382a811e4f8df872583 100644 (file)
@@ -40,12 +40,12 @@ describe('Getters', () => {
     })()
   }
 
-  const useB = createStore({
+  const useB = defineStore({
     id: 'B',
     state: () => ({ b: 'b' }),
   })
 
-  const useA = createStore({
+  const useA = defineStore({
     id: 'A',
     state: () => ({ a: 'a' }),
     getters: {
index 98942645d7ab30b78e8b5d257abed394d9bc7ed4..f2b3480bcdfb8c6bb85251fcf1779a167d546cf9 100644 (file)
@@ -1,8 +1,8 @@
-import { createStore } from '../../../src'
+import { defineStore } from '../../../src'
 import { useUserStore, UserStore } from './user'
 import { PiniaStore, ExtractGettersFromStore } from 'src/store'
 
-export const useCartStore = createStore({
+export const useCartStore = defineStore({
   id: 'cart',
   state: () => ({
     rawItems: [] as string[],
index 2dc94425a3fbf4ab9f1d1c99952212bf1b81c2a8..33f15fdf33c95486077145820058b34a2b840750 100644 (file)
@@ -1,11 +1,11 @@
-import { createStore, WrapStoreWithId } from 'src/store'
+import { defineStore, WrapStoreWithId } from 'src/store'
 
 function apiLogin(a: string, p: string) {
   if (a === 'ed' && p === 'ed') return Promise.resolve({ isAdmin: true })
   return Promise.reject(new Error('invalid credentials'))
 }
 
-export const useUserStore = createStore({
+export const useUserStore = defineStore({
   id: 'user',
   state: () => ({
     name: 'Eduardo',
index d3ee12f79a8ca6cc35b52da33176213a4c229156..5c9519ae67bd314ee00d95b4c99c5f89c2c2368c 100644 (file)
@@ -1,12 +1,12 @@
-import { createStore, getRootState } from '../src'
+import { defineStore, getRootState } from '../src'
 
 describe('Root State', () => {
-  const useA = createStore({
+  const useA = defineStore({
     id: 'a',
     state: () => ({ a: 'a' }),
   })
 
-  const useB = createStore({
+  const useB = defineStore({
     id: 'b',
     state: () => ({ b: 'b' }),
   })
index 4a40174c3048e64441e5ece28f16469d9d3df41b..708e63acd06a862b6aa53a5bd2862abd953f3ead 100644 (file)
@@ -1,6 +1,6 @@
-import { createStore } from '../../../src'
+import { defineStore } from '../../../src'
 
-export const useStore = createStore({
+export const useStore = defineStore({
   id: 'main',
   state: () => ({
     counter: 0,
index a6d032314b8a44035658edeac7b3735f1d688026..ebee55bac485059a0dc367d2eea747342eb21c0a 100644 (file)
@@ -1,7 +1,7 @@
-import { createStore } from '../../src'
+import { defineStore } from '../../src'
 import { expectType, expectError } from 'tsd'
 
-const useStore = createStore({
+const useStore = defineStore({
   id: 'name',
   state: () => ({ a: 'on' as 'on' | 'off' }),
   getters: {
index 58900058644cf09d5963902efcff5fe241dc3534..c00ea7a9ae548e9fd22e447d7291de512045ad2c 100644 (file)
@@ -8,7 +8,7 @@ if (process.server) {
 }
 
 /** @type {import('@nuxt/types').Plugin} */
-const myPlugin = context => {
+const myPlugin = (context) => {
   // console.log('🍍 Pinia Nuxt plugin installed')
 
   if (process.server) {
index a802a89cd469cfcc1b52e3d3ce7e2aa36fc4ead6..ee8bb1f0dbabef751c02d22bd319aad286cfc4e8 100644 (file)
@@ -2,7 +2,6 @@ import replace from '@rollup/plugin-replace'
 import resolve from 'rollup-plugin-node-resolve'
 import commonjs from 'rollup-plugin-commonjs'
 import ts from 'rollup-plugin-typescript2'
-import alias from '@rollup/plugin-alias'
 import { terser } from 'rollup-plugin-terser'
 import path from 'path'
 import rimraf from 'rimraf'
@@ -33,16 +32,23 @@ function createEntry({
   // force production mode when minifying
   if (minify) env = 'production'
 
+  const isBundlerESMBuild = format == 'es'
+  const isNodeBuild = format == 'cjs'
+  const isProduction = env == 'production'
+
   const config = {
     input,
     plugins: [
       replace({
         __VERSION__: pkg.version,
+        __BROWSER__: JSON.stringify(isBrowser),
         'process.env.NODE_ENV': `'${env}'`,
-      }),
-      alias({
-        resolve: ['.ts', '.js'],
-        // entries: [{ find: 'firebase', replacement: path.join(__dirname, './stub') }],
+        __DEV__:
+          isBundlerESMBuild || (isNodeBuild && !isProduction)
+            ? // preserve to be handled by bundlers
+              `(process.env.NODE_ENV !== 'production')`
+            : // hard coded dev/prod builds
+              JSON.stringify(!isProduction),
       }),
     ],
     output: {
diff --git a/src/deprecated.ts b/src/deprecated.ts
new file mode 100644 (file)
index 0000000..e79cb52
--- /dev/null
@@ -0,0 +1,12 @@
+import { defineStore } from './store'
+
+/**
+ * {@inheritDoc defineStore}
+ * @deprecated Use {@link defineStore}
+ */
+export const createStore = ((options: any) => {
+  console.warn(
+    '[🍍]: "defineStore" has been deprecated and will be removed on the sable release, use "defineStore" instead.'
+  )
+  return defineStore(options)
+}) as typeof defineStore
diff --git a/src/env.ts b/src/env.ts
new file mode 100644 (file)
index 0000000..53538e8
--- /dev/null
@@ -0,0 +1 @@
+export const IS_CLIENT = typeof window !== 'undefined'
diff --git a/src/global.d.ts b/src/global.d.ts
new file mode 100644 (file)
index 0000000..516fe4d
--- /dev/null
@@ -0,0 +1,5 @@
+// Global compile-time constants
+declare let __DEV__: boolean
+declare let __TEST__: boolean
+declare let __BROWSER__: boolean
+declare let __CI__: boolean
index f1c3086cb700c31ab1ffb430415b87a1b40ca314..56b883ff6e1d35ac81ebd5622cbbd89a4dadb9ee 100644 (file)
@@ -8,3 +8,5 @@ export {
   StoreWithState,
 } from './types'
 export { PiniaSsr } from './ssrPlugin'
+
+export { createStore } from './deprecated'
index 876c7f7adcffcac1e796c60bb69e2a1052e9c9ba..78708e4521905e8912ce253c637c34ba404230fc 100644 (file)
@@ -174,10 +174,12 @@ export function buildStore<
 }
 
 /**
- * Creates a `useStore` function that retrieves the store instance
+ * Defines a `useStore()` function that creates or retrieves the store instance
+ * when called.
+ *
  * @param options
  */
-export function createStore<
+export function defineStore<
   Id extends string,
   S extends StateTree,
   G /* extends Record<string, StoreGetterThis> */,