From: Eduardo San Martin Morote Date: Wed, 18 Aug 2021 22:10:39 +0000 (+0200) Subject: refactor: create playground package X-Git-Tag: @pinia/nuxt@0.0.1~16 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5c11aae9fc62e3c442dbf7c250ad51311f7deba1;p=thirdparty%2Fvuejs%2Fpinia.git refactor: create playground package --- diff --git a/jest.config.js b/jest.config.js index ace2e0b2..c4647470 100644 --- a/jest.config.js +++ b/jest.config.js @@ -3,7 +3,7 @@ module.exports = { collectCoverage: true, coverageDirectory: 'coverage', coverageReporters: ['html', 'lcov', 'text'], - collectCoverageFrom: ['packages/*/src/**/*.ts'], + collectCoverageFrom: ['packages/pinia/src/**/*.ts'], coveragePathIgnorePatterns: [ '/node_modules/', 'src/index.ts', @@ -13,7 +13,7 @@ module.exports = { 'src/deprecated.ts', 'src/vue2-plugin.ts', ], - testMatch: ['/packages/*/__tests__/**/*.spec.ts'], + testMatch: ['/packages/pinia/__tests__/**/*.spec.ts'], transform: { '^.+\\.tsx?$': '@sucrase/jest-plugin', }, diff --git a/package.json b/package.json index cb9e7e56..16ad8115 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "release": "node TODO: scripts/release.mjs", "size": "lerna run size", "build": "lerna run build --parallel", + "play": "lerna run play", "build:dts": "lerna run build:dts --parallel", "lint": "prettier -c --parser typescript \"packages/*/{src,__tests__,e2e}/**/*.[jt]s?(x)\"", "lint:fix": "yarn run lint --write", diff --git a/packages/pinia/package.json b/packages/pinia/package.json index 5cc89af5..c4df7c56 100644 --- a/packages/pinia/package.json +++ b/packages/pinia/package.json @@ -30,7 +30,6 @@ "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . -l pinia -r 1", "test:dts": "tsc -p ./test-dts/tsconfig.json", "dev": "yarn run test:unit --watchAll", - "play": "yarn vite", "test": "yarn run build && yarn run build:dts && yarn test:dts" }, "files": [ @@ -62,18 +61,11 @@ "@microsoft/api-extractor": "7.18.5", "@nuxt/app": "^0.5.0", "@nuxt/kit": "^0.6.4", - "@vitejs/plugin-vue": "^1.4.0", "@vue/compiler-sfc": "^3.2.2", "@vue/server-renderer": "^3.2.2", "@vue/test-utils": "^2.0.0-rc.12", - "@vueuse/core": "^6.0.0", - "mande": "^1.0.0", - "swrv": "^1.0.0-beta.8", - "vite": "^2.5.0", "vitepress": "^0.16.1", "vue": "^3.2.2", - "vue-promised": "^2.1.0", - "vue-router": "^4.0.11", "vue2": "npm:vue@2" }, "dependencies": { diff --git a/packages/pinia/vite.config.ts b/packages/pinia/vite.config.ts deleted file mode 100644 index 064353e1..00000000 --- a/packages/pinia/vite.config.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { defineConfig } from 'vite' -import vue from '@vitejs/plugin-vue' - -// https://vitejs.dev/config/ -export default defineConfig({ - plugins: [vue()], - define: { - __DEV__: 'true', - __BROWSER__: 'true', - __TEST__: 'false', - }, - optimizeDeps: { - exclude: ['vue-demi', '@vueuse/shared', '@vueuse/core'], - }, -}) diff --git a/packages/pinia/playground/.gitignore b/packages/playground/.gitignore similarity index 100% rename from packages/pinia/playground/.gitignore rename to packages/playground/.gitignore diff --git a/packages/pinia/index.html b/packages/playground/index.html similarity index 93% rename from packages/pinia/index.html rename to packages/playground/index.html index 3d7f1bbc..71f6a63b 100644 --- a/packages/pinia/index.html +++ b/packages/playground/index.html @@ -37,6 +37,6 @@
- + diff --git a/packages/playground/package.json b/packages/playground/package.json new file mode 100644 index 00000000..f5d922b7 --- /dev/null +++ b/packages/playground/package.json @@ -0,0 +1,23 @@ +{ + "name": "@pinia/playground", + "version": "0.0.0", + "private": true, + "scripts": { + "play": "vite", + "play:build": "vite build", + "serve": "vite preview" + }, + "devDependencies": { + "@vitejs/plugin-vue": "^1.4.0", + "vite": "^2.4.0" + }, + "dependencies": { + "@vueuse/core": "^6.0.0", + "mande": "^1.0.0", + "swrv": "^1.0.0-beta.8", + "vue": "^3.2.2", + "@vue/compiler-sfc": "^3.2.2", + "vue-promised": "^2.1.0", + "vue-router": "^4.0.11" + } +} diff --git a/packages/pinia/playground/src/App.vue b/packages/playground/src/App.vue similarity index 100% rename from packages/pinia/playground/src/App.vue rename to packages/playground/src/App.vue diff --git a/packages/pinia/playground/src/api/jokes.ts b/packages/playground/src/api/jokes.ts similarity index 100% rename from packages/pinia/playground/src/api/jokes.ts rename to packages/playground/src/api/jokes.ts diff --git a/packages/pinia/playground/src/api/nasa.ts b/packages/playground/src/api/nasa.ts similarity index 100% rename from packages/pinia/playground/src/api/nasa.ts rename to packages/playground/src/api/nasa.ts diff --git a/packages/pinia/playground/src/main.ts b/packages/playground/src/main.ts similarity index 80% rename from packages/pinia/playground/src/main.ts rename to packages/playground/src/main.ts index 3de1a062..cb71b2fd 100644 --- a/packages/pinia/playground/src/main.ts +++ b/packages/playground/src/main.ts @@ -1,11 +1,25 @@ -import { computed, createApp, markRaw } from 'vue' +import { computed, createApp, markRaw, Ref } from 'vue' import App from './App.vue' -import { createPinia } from '../../src' +import { createPinia } from 'pinia' import { router } from './router' +import { + RouteLocationNormalized, + RouteLocationNormalizedLoaded, +} from 'vue-router' const pinia = createPinia() +declare module 'pinia' { + export interface PiniaCustomProperties { + set route( + value: RouteLocationNormalizedLoaded | Ref + ) + get route(): RouteLocationNormalized + } +} + pinia.use(() => ({ + // @ts-expect-error: WHY? route: computed(() => markRaw(router.currentRoute.value)), })) diff --git a/packages/pinia/playground/src/router.ts b/packages/playground/src/router.ts similarity index 100% rename from packages/pinia/playground/src/router.ts rename to packages/playground/src/router.ts diff --git a/packages/pinia/playground/src/shims-vue.d.ts b/packages/playground/src/shims-vue.d.ts similarity index 100% rename from packages/pinia/playground/src/shims-vue.d.ts rename to packages/playground/src/shims-vue.d.ts diff --git a/packages/pinia/playground/src/stores/cart.ts b/packages/playground/src/stores/cart.ts similarity index 95% rename from packages/pinia/playground/src/stores/cart.ts rename to packages/playground/src/stores/cart.ts index a1fe4bbb..1adeaba7 100644 --- a/packages/pinia/playground/src/stores/cart.ts +++ b/packages/playground/src/stores/cart.ts @@ -1,4 +1,4 @@ -import { defineStore } from '../../../src' +import { defineStore } from 'pinia' import { useUserStore } from './user' export const useCartStore = defineStore({ diff --git a/packages/pinia/playground/src/stores/counter.ts b/packages/playground/src/stores/counter.ts similarity index 95% rename from packages/pinia/playground/src/stores/counter.ts rename to packages/playground/src/stores/counter.ts index bc1d0c9c..9e1841bf 100644 --- a/packages/pinia/playground/src/stores/counter.ts +++ b/packages/playground/src/stores/counter.ts @@ -1,4 +1,4 @@ -import { acceptHMRUpdate, defineStore } from '../../../src' +import { acceptHMRUpdate, defineStore } from 'pinia' const delay = (t: number) => new Promise((r) => setTimeout(r, t)) diff --git a/packages/pinia/playground/src/stores/counterSetup.ts b/packages/playground/src/stores/counterSetup.ts similarity index 95% rename from packages/pinia/playground/src/stores/counterSetup.ts rename to packages/playground/src/stores/counterSetup.ts index fadca0a0..4c5330da 100644 --- a/packages/pinia/playground/src/stores/counterSetup.ts +++ b/packages/playground/src/stores/counterSetup.ts @@ -1,5 +1,5 @@ import { computed, toRefs, reactive } from 'vue' -import { acceptHMRUpdate, defineStore } from '../../../src' +import { acceptHMRUpdate, defineStore } from 'pinia' const delay = (t: number) => new Promise((r) => setTimeout(r, t)) diff --git a/packages/pinia/playground/src/stores/jokes-swrv.ts b/packages/playground/src/stores/jokes-swrv.ts similarity index 76% rename from packages/pinia/playground/src/stores/jokes-swrv.ts rename to packages/playground/src/stores/jokes-swrv.ts index 3656bfbc..3ce07d9f 100644 --- a/packages/pinia/playground/src/stores/jokes-swrv.ts +++ b/packages/playground/src/stores/jokes-swrv.ts @@ -1,5 +1,5 @@ -import { ref, toRaw, unref, watch } from 'vue' -import { acceptHMRUpdate, defineStore } from '../../../src' +import { ref, toRaw, watch } from 'vue' +import { acceptHMRUpdate, defineStore } from 'pinia' import { getRandomJoke, Joke } from '../api/jokes' import useSWRV from 'swrv' @@ -7,7 +7,7 @@ export const useJokesSetup = defineStore('jokes-swrv-setup', () => { // const current = ref(null) const history = ref([]) - const { data, error, isValidating, mutate } = useSWRV('jokes', getRandomJoke) + const { data, error, mutate } = useSWRV('jokes', getRandomJoke) watch(data, (joke) => { console.log('changed from within the store', joke) diff --git a/packages/pinia/playground/src/stores/jokes.ts b/packages/playground/src/stores/jokes.ts similarity index 95% rename from packages/pinia/playground/src/stores/jokes.ts rename to packages/playground/src/stores/jokes.ts index 340d7f50..e344a2f5 100644 --- a/packages/pinia/playground/src/stores/jokes.ts +++ b/packages/playground/src/stores/jokes.ts @@ -1,5 +1,5 @@ import { ref, unref } from 'vue' -import { acceptHMRUpdate, defineStore } from '../../../src' +import { acceptHMRUpdate, defineStore } from 'pinia' import { getRandomJoke, Joke } from '../api/jokes' export const useJokes = defineStore('jokes', { diff --git a/packages/pinia/playground/src/stores/jokesUsePromised.ts b/packages/playground/src/stores/jokesUsePromised.ts similarity index 93% rename from packages/pinia/playground/src/stores/jokesUsePromised.ts rename to packages/playground/src/stores/jokesUsePromised.ts index d3648e38..c7a4820e 100644 --- a/packages/pinia/playground/src/stores/jokesUsePromised.ts +++ b/packages/playground/src/stores/jokesUsePromised.ts @@ -1,4 +1,4 @@ -import { acceptHMRUpdate, defineStore } from '../../../src' +import { acceptHMRUpdate, defineStore } from 'pinia' import { getRandomJoke, Joke } from '../api/jokes' import { usePromise } from 'vue-promised' import { ref, watch } from 'vue' @@ -85,6 +85,6 @@ export const useSetupJokes = defineStore('jokes-setup-vue-promised', () => { }) if (import.meta.hot) { - // import.meta.hot.accept(acceptHMRUpdate(useJokes, import.meta.hot)) + import.meta.hot.accept(acceptHMRUpdate(useJokes, import.meta.hot)) // import.meta.hot.accept(acceptHMRUpdate(useSetupJokes, import.meta.hot)) } diff --git a/packages/pinia/playground/src/stores/nasa.ts b/packages/playground/src/stores/nasa.ts similarity index 87% rename from packages/pinia/playground/src/stores/nasa.ts rename to packages/playground/src/stores/nasa.ts index 705d712a..a491c0ea 100644 --- a/packages/pinia/playground/src/stores/nasa.ts +++ b/packages/playground/src/stores/nasa.ts @@ -1,10 +1,8 @@ import useSWRV from 'swrv' -import { computed, ref } from 'vue' -import { acceptHMRUpdate, defineStore } from '../../../src' +import { ref } from 'vue' +import { acceptHMRUpdate, defineStore } from 'pinia' import { getNASAPOD } from '../api/nasa' -import LocalStorageCache from 'swrv/dist/cache/adapters/localStorage' - export const useNasaStore = defineStore('nasa-pod-swrv', () => { // can't go past today const today = new Date().toISOString().slice(0, 10) diff --git a/packages/pinia/playground/src/stores/user.ts b/packages/playground/src/stores/user.ts similarity index 83% rename from packages/pinia/playground/src/stores/user.ts rename to packages/playground/src/stores/user.ts index 32970ecd..d8c1afd3 100644 --- a/packages/pinia/playground/src/stores/user.ts +++ b/packages/playground/src/stores/user.ts @@ -1,7 +1,6 @@ -import { defineStore } from '../../../src' +import { defineStore } from 'pinia' -export const useUserStore = defineStore({ - id: 'user', +export const useUserStore = defineStore('user', { state: () => ({ name: 'Eduardo', isAdmin: true, @@ -13,13 +12,13 @@ export const useUserStore = defineStore({ async login(user: string, password: string) { const userData = await apiLogin(user, password) - this.patch({ + this.$patch({ name: user, ...userData, }) }, logout() { - this.patch({ + this.$patch({ name: '', isAdmin: false, }) diff --git a/packages/pinia/playground/src/test.ts b/packages/playground/src/test.ts similarity index 100% rename from packages/pinia/playground/src/test.ts rename to packages/playground/src/test.ts diff --git a/packages/pinia/playground/src/views/404.vue b/packages/playground/src/views/404.vue similarity index 100% rename from packages/pinia/playground/src/views/404.vue rename to packages/playground/src/views/404.vue diff --git a/packages/pinia/playground/src/views/About.vue b/packages/playground/src/views/About.vue similarity index 100% rename from packages/pinia/playground/src/views/About.vue rename to packages/playground/src/views/About.vue diff --git a/packages/pinia/playground/src/views/AllStores.vue b/packages/playground/src/views/AllStores.vue similarity index 100% rename from packages/pinia/playground/src/views/AllStores.vue rename to packages/playground/src/views/AllStores.vue diff --git a/packages/pinia/playground/src/views/CounterSetupStore.vue b/packages/playground/src/views/CounterSetupStore.vue similarity index 100% rename from packages/pinia/playground/src/views/CounterSetupStore.vue rename to packages/playground/src/views/CounterSetupStore.vue diff --git a/packages/pinia/playground/src/views/CounterStore.vue b/packages/playground/src/views/CounterStore.vue similarity index 100% rename from packages/pinia/playground/src/views/CounterStore.vue rename to packages/playground/src/views/CounterStore.vue diff --git a/packages/pinia/playground/src/views/Jokes.vue b/packages/playground/src/views/Jokes.vue similarity index 100% rename from packages/pinia/playground/src/views/Jokes.vue rename to packages/playground/src/views/Jokes.vue diff --git a/packages/pinia/playground/src/views/JokesPromised.vue b/packages/playground/src/views/JokesPromised.vue similarity index 100% rename from packages/pinia/playground/src/views/JokesPromised.vue rename to packages/playground/src/views/JokesPromised.vue diff --git a/packages/pinia/playground/src/views/NasaPOD-swrv.vue b/packages/playground/src/views/NasaPOD-swrv.vue similarity index 100% rename from packages/pinia/playground/src/views/NasaPOD-swrv.vue rename to packages/playground/src/views/NasaPOD-swrv.vue diff --git a/packages/pinia/playground/src/views/swrv.vue b/packages/playground/src/views/swrv.vue similarity index 100% rename from packages/pinia/playground/src/views/swrv.vue rename to packages/playground/src/views/swrv.vue diff --git a/packages/pinia/playground/src/vite-env.d.ts b/packages/playground/src/vite-env.d.ts similarity index 100% rename from packages/pinia/playground/src/vite-env.d.ts rename to packages/playground/src/vite-env.d.ts diff --git a/packages/pinia/playground/tsconfig.json b/packages/playground/tsconfig.json similarity index 68% rename from packages/pinia/playground/tsconfig.json rename to packages/playground/tsconfig.json index 5b6c24df..181bbeb2 100644 --- a/packages/pinia/playground/tsconfig.json +++ b/packages/playground/tsconfig.json @@ -1,4 +1,5 @@ { + "extends": "../../tsconfig.json", "compilerOptions": { "target": "esnext", "module": "esnext", @@ -9,6 +10,6 @@ "resolveJsonModule": true, "esModuleInterop": true, "lib": ["esnext", "dom"] - }, - "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"] + } + // "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"] } diff --git a/packages/playground/vite.config.ts b/packages/playground/vite.config.ts new file mode 100644 index 00000000..1998ab78 --- /dev/null +++ b/packages/playground/vite.config.ts @@ -0,0 +1,46 @@ +import { defineConfig, Plugin } from 'vite' +import vue from '@vitejs/plugin-vue' +import _fs from 'fs' +import path from 'path' + +const fs = _fs.promises + +// https://vitejs.dev/config/ +export default defineConfig({ + plugins: [vue(), copyPiniaPlugin()], + define: { + // __DEV__: 'true', + // __BROWSER__: 'true', + __TEST__: 'false', + }, + resolve: { + // alias: { + // '@vue/composition-api': 'vue-demi', + // }, + dedupe: ['vue-demi', 'vue'], + }, + optimizeDeps: { + exclude: ['vue-demi', '@vueuse/shared', '@vueuse/core', 'pinia'], + }, +}) + +function copyPiniaPlugin(): Plugin { + return { + name: 'copy-pinia', + async generateBundle() { + const filePath = path.resolve( + __dirname, + '../pinia/dist/pinia.esm-bundler.js' + ) + + // throws if file doesn't exist + await fs.access(filePath) + + this.emitFile({ + type: 'asset', + fileName: 'pinia.esm-bundler.js', + source: await fs.readFile(filePath, 'utf-8'), + }) + }, + } +} diff --git a/yarn.lock b/yarn.lock index 182543f5..cd35d4ae 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7778,7 +7778,7 @@ verror@1.10.0: core-util-is "1.0.2" extsprintf "^1.2.0" -vite@^2.4.4, vite@^2.5.0: +vite@^2.4.0, vite@^2.4.4: version "2.5.0" resolved "https://registry.yarnpkg.com/vite/-/vite-2.5.0.tgz#111ba3679432d426e44566acf480005a7914cbd6" integrity sha512-Dn4B+g54PJsMG5WCc4QeFy1ygMXRdTtFrUPegqfk4+vzVQcbF/DqqmI/1bxezArzbujBJg/67QeT5wz8edfJVQ==