From 63da97bc73724ae8b2c513e45e76d85fad00ae7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=B1=E5=90=B9=E8=89=B2=E5=BE=A1=E5=AE=88?= <85992002+KazariEX@users.noreply.github.com> Date: Mon, 29 Apr 2024 11:38:37 +0800 Subject: [PATCH] feat: add ``vue-router:extendRoute`` hook (#387) * refactor: passing ``valaxyApp`` for better context * feat: add ``vue-router:extendRoute`` hook --- packages/valaxy/node/build.ts | 10 ++++++---- packages/valaxy/node/cli/build.ts | 4 ++-- packages/valaxy/node/cli/dev.ts | 6 +++--- packages/valaxy/node/cli/utils/cli.ts | 11 +++++++---- packages/valaxy/node/plugins/preset.ts | 8 +++++--- packages/valaxy/node/plugins/vueRouter.ts | 9 ++++++--- packages/valaxy/node/server.ts | 9 ++++++--- 7 files changed, 35 insertions(+), 22 deletions(-) diff --git a/packages/valaxy/node/build.ts b/packages/valaxy/node/build.ts index f39ad07d0..a97b70150 100644 --- a/packages/valaxy/node/build.ts +++ b/packages/valaxy/node/build.ts @@ -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'], diff --git a/packages/valaxy/node/cli/build.ts b/packages/valaxy/node/cli/build.ts index 4b3ff1c80..8884c5abc 100644 --- a/packages/valaxy/node/cli/build.ts +++ b/packages/valaxy/node/cli/build.ts @@ -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) { @@ -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) { diff --git a/packages/valaxy/node/cli/dev.ts b/packages/valaxy/node/cli/dev.ts index a39b21e30..edfe5887b 100644 --- a/packages/valaxy/node/cli/dev.ts +++ b/packages/valaxy/node/cli/dev.ts @@ -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 @@ -74,7 +74,7 @@ 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 = [ @@ -82,7 +82,7 @@ export function registerDevCommand(cli: yargs.Argv) { name: 'r', fullName: 'restart', action() { - initServer(options, viteConfig) + initServer(valaxyApp, viteConfig) }, }, { diff --git a/packages/valaxy/node/cli/utils/cli.ts b/packages/valaxy/node/cli/utils/cli.ts index 8539a3e26..f6d159ea8 100644 --- a/packages/valaxy/node/cli/utils/cli.ts +++ b/packages/valaxy/node/cli/utils/cli.ts @@ -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' @@ -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 @@ -92,7 +95,7 @@ export async function initServer(options: ResolvedValaxyOptions, viteConfig: Inl // } if (reload) - initServer(options, viteConfig) + initServer(valaxyApp, viteConfig) }, }) await server.listen() diff --git a/packages/valaxy/node/plugins/preset.ts b/packages/valaxy/node/plugins/preset.ts index aa5389411..8b4c7f30a 100644 --- a/packages/valaxy/node/plugins/preset.ts +++ b/packages/valaxy/node/plugins/preset.ts @@ -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' @@ -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) @@ -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({ diff --git a/packages/valaxy/node/plugins/vueRouter.ts b/packages/valaxy/node/plugins/vueRouter.ts index f075bd8ec..9b438209f 100644 --- a/packages/valaxy/node/plugins/vueRouter.ts +++ b/packages/valaxy/node/plugins/vueRouter.ts @@ -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' @@ -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({ @@ -188,6 +189,8 @@ export function createRouterPlugin(options: ResolvedValaxyOptions) { valaxyConfig.extendMd?.(ctx) } + valaxyApp.hooks.callHook('vue-router:extendRoute', route) + return valaxyConfig.router?.extendRoute?.(route) }, }) diff --git a/packages/valaxy/node/server.ts b/packages/valaxy/node/server.ts index 8986e2419..39bfc810b 100644 --- a/packages/valaxy/node/server.ts +++ b/packages/valaxy/node/server.ts @@ -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 = [