Skip to content
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

Chris/vike pinia #65

Merged
merged 14 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ jobs:
- examples/onBeforeRender
- examples/ssr-spa
- examples/with-vue-plugin
- examples/with-vike-pinia

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
- run: pnpm install

- name: Build vike-vue
- name: Build packages
run: pnpm run build

- name: Test building example
Expand Down
2 changes: 2 additions & 0 deletions examples/with-vike-pinia/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/dist/
36 changes: 36 additions & 0 deletions examples/with-vike-pinia/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions examples/with-vike-pinia/components/Counter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<button type="button" @click="counterStore.increment">Counter {{ counterStore.count }}</button>
</template>

<script lang="ts" setup>
import { useCounterStore } from '../stores/useCounterStore';

const counterStore = useCounterStore();
</script>
25 changes: 25 additions & 0 deletions examples/with-vike-pinia/components/Link.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<a :class="{ active: isActive }">
<slot />
</a>
</template>
<style scoped>
a {
padding: 2px 10px;
margin-left: -10px;
}
a.active {
background-color: #eee;
}
</style>
<script lang="ts" setup>
import { useAttrs, computed } from 'vue'
import { usePageContext } from 'vike-vue/usePageContext'

const pageContext = usePageContext()
const { href } = useAttrs() as { href: string }
const isActive = computed(() => {
const { urlPathname } = pageContext
return href === '/' ? urlPathname === href : urlPathname.startsWith(href)
})
</script>
3 changes: 3 additions & 0 deletions examples/with-vike-pinia/layouts/HeadDefault.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</template>
61 changes: 61 additions & 0 deletions examples/with-vike-pinia/layouts/LayoutDefault.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<template>
<div class="layout">
<div class="navigation">
<a href="/" class="logo">
<img src="../assets/logo.svg" height="64" width="64" />
</a>
<Link href="/">Welcome</Link>
<Link href="/about">About</Link>
</div>
<div class="content"><slot /></div>
</div>
</template>

<script lang="ts" setup>
import Link from '../components/Link.vue'
</script>

<style>
body {
margin: 0;
font-family: sans-serif;
}
* {
box-sizing: border-box;
}
a {
text-decoration: none;
}
</style>

<style scoped>
.layout {
display: flex;
max-width: 900px;
margin: auto;
}
.content {
padding: 20px;
padding-bottom: 50px;
min-height: 100vh;
flex-grow: 1;
}
.navigation {
padding: 20px;
flex-shrink: 0;
display: flex;
flex-direction: column;
line-height: 1.8em;
border-right: 2px solid #eee;
}
.logo {
margin-top: 20px;
margin-bottom: 10px;
}
.content {
transition: opacity 0.1s ease-in;
}
.content.page-transition {
opacity: 0;
}
</style>
19 changes: 19 additions & 0 deletions examples/with-vike-pinia/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"test": "tsc --noEmit"
},
"dependencies": {
"@vitejs/plugin-vue": "^5.0.2",
"pinia": "^2.1.7",
"typescript": "^5.3.3",
"vike": "0.4.156",
"vike-pinia": "0.0.1",
"vike-vue": "0.5.3",
"vite": "^5.0.11",
"vue": "^3.4.7"
},
"type": "module"
}
19 changes: 19 additions & 0 deletions examples/with-vike-pinia/pages/+config.h.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Config } from 'vike/types'
import logoUrl from '../assets/logo.svg'
import Layout from '../layouts/LayoutDefault.vue'
import Head from '../layouts/HeadDefault.vue'
import vikeVue from 'vike-vue'
import vikePinia from 'vike-pinia'

// Default configs (can be overridden by pages)
export default {
Layout,
Head,
// <title>
title: 'My Vike + Vue + Pinia App',
// <meta name="description">
description: 'Demo showcasing Vike + Vue + Pinia',
// <link rel="icon" href="${favicon}" />
favicon: logoUrl,
extends: [vikeVue, vikePinia]
} satisfies Config
25 changes: 25 additions & 0 deletions examples/with-vike-pinia/pages/_error/+Page.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<div class="center">
<p>{{ abortReason }}</p>
</div>
</template>

<script lang="ts" setup>
import { usePageContext } from 'vike-vue/usePageContext'

const ctx = usePageContext()
let { is404, abortReason } = ctx
if (!abortReason) {
abortReason = is404 ? 'Page not found.' : 'Something went wrong.'
}
</script>

<style>
.center {
height: calc(100vh - 100px);
display: flex;
font-size: 1.3em;
justify-content: center;
align-items: center;
}
</style>
4 changes: 4 additions & 0 deletions examples/with-vike-pinia/pages/about/+Page.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<template>
<h1>About</h1>
<p>Example of using Vike.</p>
</template>
5 changes: 5 additions & 0 deletions examples/with-vike-pinia/pages/about/+config.h.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Config } from 'vike/types'

export default {
title: 'About - My Vike + Vue App'
} satisfies Config
24 changes: 24 additions & 0 deletions examples/with-vike-pinia/pages/index/+Page.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<h1>My Vike + Vue app</h1>
This page is:
<ul>
<li>Rendered to HTML.</li>
<li>Interactive.</li>
</ul>
<h2>Counters synced via shared store</h2>
<Counter />
<hr>
<Counter />
</template>

<script lang="ts" setup>
import { onServerPrefetch } from 'vue';
import Counter from '../../components/Counter.vue'
import { useCounterStore } from '../../stores/useCounterStore'

const { increment } = useCounterStore()
onServerPrefetch(() => {
// Show that hydration works - the counter should start at 1
increment()
})
</script>
8 changes: 8 additions & 0 deletions examples/with-vike-pinia/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Example of using [Pinia](https://pinia.vuejs.org/) with the `vike-pinia` extension.

```bash
git clone [email protected]:vikejs/vike-vue
cd vike-vue/examples/with-vue-plugin/
npm install
npm run dev
```
10 changes: 10 additions & 0 deletions examples/with-vike-pinia/stores/useCounterStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useCounterStore = defineStore("counter", () => {
const count = ref(0)

const increment = () => count.value++

return { count, increment }
})
12 changes: 12 additions & 0 deletions examples/with-vike-pinia/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"strict": true,
"module": "ES2020",
"moduleResolution": "Node",
"target": "ES2020",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"types": ["vite/client"],
"skipLibCheck": true,
"esModuleInterop": true
}
}
14 changes: 14 additions & 0 deletions examples/with-vike-pinia/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import vue from '@vitejs/plugin-vue'
import vike from 'vike/plugin'
import { UserConfig } from 'vite'

const config: UserConfig = {
plugins: [
vike({ prerender: true }),
vue({
include: [/\.vue$/, /\.md$/]
})
]
}

export default config
11 changes: 11 additions & 0 deletions examples/with-vike-pinia/vue-shim.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Without this file, `tsc` will fail with such errors:
// pages/+config.h.ts:3:20 - error TS2307: Cannot find module '../layouts/LayoutDefault.vue' or its corresponding type declarations.
// import Layout from "../layouts/LayoutDefault.vue"
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// See https://stackoverflow.com/questions/71477277/typescript-cannot-find-module-in-vue-project

declare module "*.vue" {
import Vue from "vue";
export default Vue;
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"scripts": {
"dev": "cd vike-vue/ && pnpm run dev",
"build": "cd vike-vue/ && pnpm run build",
"build": "cd vike-vue/ && pnpm run build && cd ../packages/vike-pinia/ && pnpm run build",
"release": "cd vike-vue/ && pnpm run release",
"========= Clean": "",
"clean": "git clean -Xdf",
Expand All @@ -11,7 +11,8 @@
},
"pnpm": {
"overrides": {
"vike-vue": "link:./vike-vue/"
"vike-vue": "link:./vike-vue/",
"vike-pinia": "link:./packages/vike-pinia/"
}
},
"packageManager": "[email protected]"
Expand Down
2 changes: 2 additions & 0 deletions packages/vike-pinia/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist/
/node_modules/
Empty file added packages/vike-pinia/README.md
Empty file.
54 changes: 54 additions & 0 deletions packages/vike-pinia/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "vike-pinia",
"version": "0.0.1",
"description": "",
"main": "./dist/+config.js",
"types": "./dist/+config.d.ts",
"type": "module",
"scripts": {
"dev": "tsc --watch",
"build": "rm -rf dist && tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"exports": {
".": "./dist/+config.js",
"./renderer/installPinia": "./dist/installPinia.js",
"./renderer/dehydratePinia": "./dist/dehydratePinia.js",
"./renderer/hydratePinia": "./dist/hydratePinia.js"
},
"typesVersions": {
"*": {
".": [
"./dist/+config.d.ts"
],
"renderer/installPinia": [
"./dist/installPinia.d.ts"
],
"renderer/dehydratePinia": [
"./dist/dehydratePinia.d.ts"
],
"renderer/hydratePinia": [
"./dist/hydratePinia.d.ts"
]
}
},
"peerDependencies": {
"vike": "^0.4.152",
"pinia": "^2.0.0",
"vike-vue": "^0.5.3",
"vue": "^3.0.0"
},
"devDependencies": {
"@brillout/release-me": "^0.1.14",
"@types/node": "^20.10.7",
"pinia": "^2.1.7",
"typescript": "^5.3.3",
"vike": "0.4.156",
"vike-vue": "0.5.3",
"vue": "^3.4.7"
},
"files": [
"dist"
],
"license": "MIT"
}
Loading