Skip to content

Commit

Permalink
feat: add vue-router:extendRoute hook (#387)
Browse files Browse the repository at this point in the history
* refactor: passing ``valaxyApp`` for better context

* feat: add ``vue-router:extendRoute`` hook
  • Loading branch information
KazariEX authored Apr 29, 2024
1 parent 49e6bd7 commit 63da97b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 22 deletions.
10 changes: 6 additions & 4 deletions packages/valaxy/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@ import generateSitemap from 'vite-ssg-sitemap'

import fs from 'fs-extra'
import consola from 'consola'
import type { ValaxyNode } from './types'
import type { ResolvedValaxyOptions } from './options'
import { ViteValaxyPlugins } from './plugins/preset'
import { collectRedirects, writeRedirectFiles } from './utils/clientRedirects'

export async function build(
options: ResolvedValaxyOptions,
valaxyApp: ValaxyNode,
viteConfig: InlineConfig = {},
) {
const inlineConfig = mergeViteConfig(viteConfig, {
plugins: await ViteValaxyPlugins(options),
plugins: await ViteValaxyPlugins(valaxyApp),
})

await viteBuild(inlineConfig)
}

export async function ssgBuild(
options: ResolvedValaxyOptions,
valaxyApp: ValaxyNode,
viteConfig: InlineConfig = {},
) {
const { options } = valaxyApp
const defaultConfig: InlineConfig = {
plugins: await ViteValaxyPlugins(options),
plugins: await ViteValaxyPlugins(valaxyApp),
ssr: {
// TODO: workaround until they support native ESM
noExternal: ['workbox-window', /vue-i18n/, '@vue/devtools-api'],
Expand Down
4 changes: 2 additions & 2 deletions packages/valaxy/node/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function registerBuildCommand(cli: yargs.Argv) {
consola.info(`use ${yellow('vite-ssg')} to do ssg build...`)

try {
await ssgBuild(options, viteConfig)
await ssgBuild(valaxyApp, viteConfig)
await postProcessForSSG(options)
}
catch (e) {
Expand All @@ -109,7 +109,7 @@ export function registerBuildCommand(cli: yargs.Argv) {
}
else {
consola.info('use vite do spa build...')
await build(options, viteConfig)
await build(valaxyApp, viteConfig)
}
}
catch (e) {
Expand Down
6 changes: 3 additions & 3 deletions packages/valaxy/node/cli/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function registerDevCommand(cli: yargs.Argv) {
const port = userPort || await findFreePort(4859)
const options = await resolveOptions({ userRoot: root })

createValaxyNode(options)
const valaxyApp = createValaxyNode(options)

const viteConfig: InlineConfig = mergeConfig({
// avoid load userRoot/vite.config.ts repeatedly
Expand All @@ -74,15 +74,15 @@ export function registerDevCommand(cli: yargs.Argv) {
logLevel: log as LogLevel,
}, options.config.vite || {})

await initServer(options, viteConfig)
await initServer(valaxyApp, viteConfig)
printInfo(options, port, remote)

const SHORTCUTS = [
{
name: 'r',
fullName: 'restart',
action() {
initServer(options, viteConfig)
initServer(valaxyApp, viteConfig)
},
},
{
Expand Down
11 changes: 7 additions & 4 deletions packages/valaxy/node/cli/utils/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import consola from 'consola'
import type { InlineConfig, ViteDevServer } from 'vite'
import { mergeConfig } from 'vite'
import { version } from 'valaxy/package.json'
import type { ValaxyNode } from '../../types'
import { createServer } from '../../server'
import type { ResolvedValaxyOptions } from '../../options'
import { mergeViteConfigs } from '../../common'
Expand Down Expand Up @@ -57,23 +58,25 @@ export function printInfo(options: ResolvedValaxyOptions, port?: number, remote?
// 'extendMd',
// ]

export async function initServer(options: ResolvedValaxyOptions, viteConfig: InlineConfig) {
export async function initServer(valaxyApp: ValaxyNode, viteConfig: InlineConfig) {
if (server) {
vLogger.info('close server...')
await server.close()
}

const { options } = valaxyApp

const viteConfigs: InlineConfig = mergeConfig(
await mergeViteConfigs(options, 'serve'),
viteConfig,
)

try {
server = await createServer(options, viteConfigs, {
server = await createServer(valaxyApp, viteConfigs, {
async onConfigReload(newConfig, config, force = false) {
if (force) {
vLogger.info(`${yellow('force')} reload the server`)
initServer(options, viteConfig)
initServer(valaxyApp, viteConfig)
}

let reload = false
Expand All @@ -92,7 +95,7 @@ export async function initServer(options: ResolvedValaxyOptions, viteConfig: Inl
// }

if (reload)
initServer(options, viteConfig)
initServer(valaxyApp, viteConfig)
},
})
await server.listen()
Expand Down
8 changes: 5 additions & 3 deletions packages/valaxy/node/plugins/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import VueI18n from '@intlify/unplugin-vue-i18n/vite'
import UnheadVite from '@unhead/addons/vite'

import { resolve } from 'pathe'
import type { ResolvedValaxyOptions, ValaxyServerOptions } from '../options'
import type { ValaxyServerOptions } from '../options'
import type { ValaxyNode } from '../types'

import { customElements } from '../constants'
import { createUnocssPlugin } from './unocss'
Expand All @@ -21,9 +22,10 @@ import { createValaxyLoader } from './valaxy'
import { createMarkdownPlugin } from './markdown'

export async function ViteValaxyPlugins(
options: ResolvedValaxyOptions,
valaxyApp: ValaxyNode,
serverOptions: ValaxyServerOptions = {},
): Promise<(PluginOption | PluginOption[])[]> {
const { options } = valaxyApp
const { roots, config: valaxyConfig } = options

const MarkdownPlugin = await createMarkdownPlugin(options)
Expand Down Expand Up @@ -55,7 +57,7 @@ export async function ViteValaxyPlugins(
UnheadVite(),

// https://github.com/posva/unplugin-vue-router
createRouterPlugin(options),
createRouterPlugin(valaxyApp),

// https://github.com/JohnCampionJr/vite-plugin-vue-layouts
Layouts({
Expand Down
9 changes: 6 additions & 3 deletions packages/valaxy/node/plugins/vueRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { convert } from 'html-to-text'
import type { ExcerptType, Page } from 'valaxy/types'
import type { RouteMeta } from 'vue-router'
import MarkdownIt from 'markdown-it'
import type { ResolvedValaxyOptions } from '../options'
import type { ValaxyNode } from '../types'
import { EXCERPT_SEPARATOR } from '../constants'

import { presetStatistics } from './presets/statistics'
Expand Down Expand Up @@ -36,9 +36,10 @@ function getExcerptByType(excerpt = '', type: ExcerptType = 'html') {

/**
* @see https://github.com/posva/unplugin-vue-router
* @param options
* @param valaxyApp
*/
export function createRouterPlugin(options: ResolvedValaxyOptions) {
export function createRouterPlugin(valaxyApp: ValaxyNode) {
const { options } = valaxyApp
const { roots, config: valaxyConfig } = options

return VueRouter({
Expand Down Expand Up @@ -188,6 +189,8 @@ export function createRouterPlugin(options: ResolvedValaxyOptions) {
valaxyConfig.extendMd?.(ctx)
}

valaxyApp.hooks.callHook('vue-router:extendRoute', route)

return valaxyConfig.router?.extendRoute?.(route)
},
})
Expand Down
9 changes: 6 additions & 3 deletions packages/valaxy/node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import process from 'node:process'
import type { InlineConfig } from 'vite'
import { createServer as createViteServer, mergeConfig as mergeViteConfig } from 'vite'

import type { ResolvedValaxyOptions, ValaxyServerOptions } from './options'
import type { ValaxyNode } from './types'
import type { ValaxyServerOptions } from './options'
import { ViteValaxyPlugins } from './plugins/preset'

export async function createServer(
options: ResolvedValaxyOptions,
valaxyApp: ValaxyNode,
viteConfig: InlineConfig = {},
serverOptions: ValaxyServerOptions = {},
) {
// default editor vscode
process.env.EDITOR = process.env.EDITOR || 'code'

const plugins = await ViteValaxyPlugins(options, serverOptions)
const { options } = valaxyApp

const plugins = await ViteValaxyPlugins(valaxyApp, serverOptions)
// dynamic import to avoid bundle it in build
const enableDevtools = options.mode === 'dev' && options.config.devtools
const vitePlugins = [
Expand Down

1 comment on commit 63da97b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://yun.valaxy.site as production
🚀 Deployed on https://662f169a0acda64d33436206--valaxy.netlify.app

Please sign in to comment.