Skip to content

Commit

Permalink
refactor: vike干活了
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Oct 24, 2023
1 parent 766664b commit b8bb175
Show file tree
Hide file tree
Showing 20 changed files with 11,002 additions and 6,407 deletions.
16,900 changes: 10,769 additions & 6,131 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"scripts": {
"start": "nest start",
"dev": "nest start --watch",
"dev": "npm -w @bangbang93/freyja-server run dev",
"pm2": "pm2 startOrReload process.json",
"build": "npm run build:server && npm run build:client",
"build:server": "nest build",
Expand Down
2 changes: 1 addition & 1 deletion packages/home/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
</head>
<body>
<app id="app"><!--vue-ssr-outlet--></app>
<script type="module" src="/packages/server/src/main.ts"></script>
<script type="module" src="src/main.ts"></script>
</body>
</html>
8 changes: 5 additions & 3 deletions packages/home/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@
"@types/prismjs": "^1.26.0",
"@vitejs/plugin-vue": "^4.4.0",
"sass": "^1.69.4",
"unplugin-element-plus": "^0.7.0",
"vike": "^0.4.144",
"vite": "^4.5.0",
"vue-tsc": "^1.8.20",
"unplugin-element-plus": "^0.7.0"
"vue-tsc": "^1.8.20"
},
"dependencies": {
"@element-plus/icons-vue": "^2.1.0",
"@sindresorhus/is": "^6.0.1",
"@vue/server-renderer": "^3.2.45",
"@vue/shared": "^3.3.4",
"@sindresorhus/is": "^6.0.1",
"animate.css": "^3.7.2",
"core-js": "^3.8.3",
"element-plus": "^2.4.1",
"font-awesome": "^4.7.0",
"http-errors": "^2.0.0",
"ky": "^0.33.0",
"lozad": "^1.14.0",
"markdown-it": "^12.3.2",
Expand Down
49 changes: 49 additions & 0 deletions packages/home/src/_default.page.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Created by bangbang93 on 2017/8/25.
*/
'use strict'
import {App} from 'vue'
import {createHome} from './entries'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

import './utils/prism.ts'
import 'prismjs/themes/prism-okaidia.css'
import 'element-plus/dist/index.css'

export async function render(): Promise<App> {
const {app, router, store} = createHome()
if (window.__INITIAL_STATE__) {
// We initialize the store state with the data injected from the server
store.replaceState(window.__INITIAL_STATE__)
}
await router.isReady()
router.beforeResolve((to, _, next) => {
const matched = to.matched.flatMap((e) => {
if (e.components) {
return Object.values(e.components)
}
return []
})
// 这里如果有加载指示器(loading indicator),就触发
Promise.all(
matched.map((c) => {
if ('asyncData' in c && c?.asyncData) {
return c.asyncData({store, route: to})
}
return null
}),
)
.then(() => {
// 停止加载指示器(loading indicator)
next()
})
.catch(next)
})

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component)
}

app.mount('app')
return app
}
84 changes: 84 additions & 0 deletions packages/home/src/_default.page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Created by bangbang93 on 2017/8/25.
*/
'use strict'
import {renderToString} from '@vue/server-renderer'
import * as HttpErrors from 'http-errors'
import {dangerouslySkipEscape, escapeInject} from 'vike'
import {PageContext} from 'vike/types'
import {createHome} from './entries'

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Vike {
interface PageContext {
origin: string
referer: string
state: Record<string, unknown>
status: object
}
}
}

export async function render(pageContext: PageContext) {
const {app, router, store} = createHome()
await router.push(pageContext.urlOriginal)

await router.isReady()
const matchedComponents = router.currentRoute.value.matched.flatMap((e) => {
if (e.components) {
return Object.values(e.components)
}
return []
})
if (!matchedComponents.length) {
throw new HttpErrors.NotFound('no such route')
}
await Promise.all(
matchedComponents.map((Component) => {
if (!Component) return Promise.resolve()
if ('asyncData' in Component && Component.asyncData) {
store.commit('setOrigin', pageContext.origin)
store.commit('setReferer', pageContext.referer)
return Component.asyncData({
store,
route: router.currentRoute.value,
})
}
return null
}),
)

pageContext.state = store.state
if (router.currentRoute.value.meta.status) {
pageContext.status = router.currentRoute.value.meta.status
}

const appHtml = await renderToString(app)
const documentProps = pageContext.exports.documentProps as Record<string, string | undefined>
const title = documentProps?.title || 'Vite SSR app'
const desc = documentProps?.description || 'App using Vite + Vike'

const documentHtml = escapeInject`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="${desc}" />
<title>${title}</title>
</head>
<body>
<div id="app">${dangerouslySkipEscape(appHtml)}</div>
</body>
</html>`

return {
documentHtml,
pageContext: {
// We can add some `pageContext` here,
// which is useful if we want to do page redirection
// https://vike.dev/page-redirection
},
}
}
12 changes: 7 additions & 5 deletions packages/home/src/components/home/nav-menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@
</div>
</template>

<script>
export default {
<script lang="ts">
import {defineComponent} from 'vue'
export default defineComponent({
name: 'FreyjaNavMenu',
data() {
return {
keyword: '',
}
},
methods: {
onSearch() {
this.$router.push({
async onSearch() {
return this.$router.push({
name: 'search',
query: {
keyword: this.keyword,
Expand All @@ -69,7 +71,7 @@ export default {
})
},
},
}
})
</script>

<style lang="scss">
Expand Down
43 changes: 0 additions & 43 deletions packages/home/src/entries/entry-server.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/home/src/entries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'font-awesome/scss/font-awesome.scss'
import 'prismjs'
import {createSSRApp, App as VueApp} from 'vue'
import VueFetch from 'vue-fetch'
import App from '../pages/index.vue'
import App from '../pages/index.page.vue'
import {createHomeRouter} from '../router'
import '../scss/style.scss'
import {createStore} from '../store'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@
</div>
</template>

<script>
<script lang="ts">
import 'animate.css'
import {ElCol as Col} from 'element-plus'
import {defineComponent} from 'vue'
import FreyjaNavMenu from '../components/home/nav-menu.vue'
export default {
export default defineComponent({
name: 'PageIndex',
components: {
FreyjaNavMenu,
Expand All @@ -77,21 +78,21 @@ export default {
},
mounted() {
const mq = window.matchMedia('(max-width: 999px)')
mq.addListener(this.onWidthChange.bind(this))
mq.addEventListener('change', this.onWidthChange.bind(this))
this.onWidthChange(mq)
this.isShowMenu = true
this.$store.state.origin = ''
},
methods: {
onWidthChange(mq) {
onWidthChange(mq: MediaQueryListEvent | MediaQueryList) {
if (mq.matches) {
this.menuWidth = 1
} else {
this.menuWidth = 4
}
},
},
}
})
</script>

<style>
Expand Down
5 changes: 3 additions & 2 deletions packages/home/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import vuePlugin from '@vitejs/plugin-vue'
import {fileURLToPath, URL} from 'node:url'
import {fileURLToPath} from 'node:url'
import {defineConfig} from 'vite'
import vike from 'vike/plugin'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vuePlugin(),
vike(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
base: '/admin/',
})
27 changes: 17 additions & 10 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
"name": "@bangbang93/freyja-server",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "nest start --watch"
},
"dependencies": {
"@bangbang93/utils": "^5.5.0",
"@nestjs/common": "^9.2.0",
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^9.2.0",
"@nestjs/mongoose": "^9.2.1",
"@nestjs/platform-express": "^9.2.0",
"@nestjs/swagger": "^6.1.3",
"@sindresorhus/is": "^5.3.0",
"@nestjs/common": "^10.2.7",
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.2.7",
"@nestjs/mongoose": "^10.0.1",
"@nestjs/platform-express": "^10.2.7",
"@nestjs/swagger": "^7.1.14",
"@sindresorhus/is": "^4.6.0",
"@vue/server-renderer": "^3.3.6",
"bcrypt": "^5.0.1",
"bluebird": "^3.7.2",
"body-parser": "^1.19.0",
Expand Down Expand Up @@ -47,10 +51,11 @@
"randomstring": "^1.1.5",
"serve-favicon": "^2.5.0",
"tslib": "^2.4.1",
"umzug": "^3.2.1"
"umzug": "^3.2.1",
"vike": "^0.4.144"
},
"devDependencies": {
"@types/markdown-it": "^12.0.0",
"@nestjs/cli": "^10.2.0",
"@types/bcrypt": "^3.0.0",
"@types/bluebird": "^3.5.37",
"@types/bunyan": "^1.8.8",
Expand All @@ -64,10 +69,12 @@
"@types/http-errors": "^1.6.3",
"@types/lodash": "^4.14.149",
"@types/lru-cache": "^5.1.0",
"@types/markdown-it": "^12.0.0",
"@types/morgan": "^1.9.3",
"@types/ms": "^0.7.31",
"@types/multer": "^1.4.7",
"@types/nodemailer": "^6.4.0",
"@types/serve-favicon": "^2.5.0"
"@types/serve-favicon": "^2.5.0",
"vite": "^4.5.0"
}
}
Loading

0 comments on commit b8bb175

Please sign in to comment.