From 50798bdb762dabe8fba5a1e99d31afaf48e6ea83 Mon Sep 17 00:00:00 2001 From: Kia Ishii Date: Mon, 2 Nov 2020 18:18:22 +0900 Subject: [PATCH] refactor: duplicate teh shared code for better dx --- package.json | 5 +- src/client/app/composables/siteDataByRoute.ts | 69 +++++++++++++- src/client/tsconfig.json | 7 +- src/node/config.ts | 91 ++++++++++++++++--- src/node/tsconfig.json | 8 +- src/shared/config.ts | 66 -------------- src/shared/tsconfig.json | 13 --- 7 files changed, 155 insertions(+), 104 deletions(-) delete mode 100644 src/shared/config.ts delete mode 100644 src/shared/tsconfig.json diff --git a/package.json b/package.json index 4cad34e6..f0bf3156 100644 --- a/package.json +++ b/package.json @@ -27,13 +27,12 @@ }, "homepage": "https://github.com/vuejs/vitepress/tree/master/#readme", "scripts": { - "dev": "run-p dev-client dev-client-copy dev-node dev-shared", + "dev": "run-p dev-client dev-client-copy dev-node", "dev-client": "tsc -w -p src/client", "dev-client-copy": "node scripts/watchAndCopy", "dev-node": "tsc -w -p src/node", - "dev-shared": "tsc -w -p src/shared", "release": "bash scripts/release.sh", - "build": "rimraf -rf dist && tsc -p src/client && tsc -p src/node && tsc -p src/shared && node scripts/copy", + "build": "rimraf -rf dist && tsc -p src/client && tsc -p src/node && node scripts/copy", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s" }, "engines": { diff --git a/src/client/app/composables/siteDataByRoute.ts b/src/client/app/composables/siteDataByRoute.ts index f7bcb3a0..49e4117e 100644 --- a/src/client/app/composables/siteDataByRoute.ts +++ b/src/client/app/composables/siteDataByRoute.ts @@ -1,10 +1,75 @@ import { computed } from 'vue' -import { resolveSiteDataByRoute } from '/@shared/config' -import { siteDataRef } from './siteData' +import { SiteData } from '../../../../types/shared' import { useRoute } from '../router' +import { siteDataRef } from './siteData' + +const inBrowser = typeof window !== 'undefined' export function useSiteDataByRoute(route = useRoute()) { return computed(() => { return resolveSiteDataByRoute(siteDataRef.value, route.path) }) } + +function findMatchRoot(route: string, roots: string[]) { + // first match to the routes with the most deep level. + roots.sort((a, b) => { + const levelDelta = b.split('/').length - a.split('/').length + if (levelDelta !== 0) { + return levelDelta + } else { + return b.length - a.length + } + }) + + for (const r of roots) { + if (route.startsWith(r)) return r + } + return undefined +} + +function resolveLocales( + locales: Record, + route: string +): T | undefined { + const localeRoot = findMatchRoot(route, Object.keys(locales)) + return localeRoot ? locales[localeRoot] : undefined +} + +// this merges the locales data to the main data by the route +export function resolveSiteDataByRoute(siteData: SiteData, route: string) { + route = cleanRoute(siteData, route) + + const localeData = resolveLocales(siteData.locales || {}, route) || {} + const localeThemeConfig = + resolveLocales( + (siteData.themeConfig && siteData.themeConfig.locales) || {}, + route + ) || {} + + return { + ...siteData, + ...localeData, + themeConfig: { + ...siteData.themeConfig, + ...localeThemeConfig, + // clean the locales to reduce the bundle size + locales: {} + }, + locales: {} + } +} + +/** + * Clean up the route by removing the `base` path if it's set in config. + */ +function cleanRoute(siteData: SiteData, route: string): string { + if (!inBrowser) { + return route + } + + const base = siteData.base + const baseWithoutSuffix = base.endsWith('/') ? base.slice(0, -1) : base + + return route.slice(baseWithoutSuffix.length) +} diff --git a/src/client/tsconfig.json b/src/client/tsconfig.json index b004aa66..c87486c1 100644 --- a/src/client/tsconfig.json +++ b/src/client/tsconfig.json @@ -2,7 +2,7 @@ "extends": "../../tsconfig.base.json", "compilerOptions": { "baseUrl": ".", - "outDir": "../../dist", + "outDir": "../../dist/client", "module": "esnext", "lib": ["ESNext", "DOM"], "types": ["vite"], @@ -14,10 +14,7 @@ } }, "include": [ - ".", + ".", "../../types/shared.d.ts", - ], - "exclude": [ - "../shared" ] } diff --git a/src/node/config.ts b/src/node/config.ts index 3df6646c..46ce25c4 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -2,24 +2,13 @@ import path from 'path' import fs from 'fs-extra' import chalk from 'chalk' import globby from 'globby' -import { createResolver, APP_PATH } from './resolver' import { Resolver } from 'vite' import { SiteData, HeadConfig, LocaleConfig } from '../../types/shared' -export { resolveSiteDataByRoute } from '../shared/config' +import { createResolver, APP_PATH } from './resolver' const debug = require('debug')('vitepress:config') -export interface UserConfig { - lang?: string - base?: string - title?: string - description?: string - head?: HeadConfig[] - themeConfig?: ThemeConfig - locales?: Record - alias?: Record - // TODO locales support etc. -} +const inBrowser = typeof window !== 'undefined' export interface SiteConfig { root: string @@ -32,6 +21,17 @@ export interface SiteConfig { pages: string[] } +export interface UserConfig { + lang?: string + base?: string + title?: string + description?: string + head?: HeadConfig[] + themeConfig?: ThemeConfig + locales?: Record + alias?: Record +} + const resolve = (root: string, file: string) => path.resolve(root, `.vitepress`, file) @@ -90,3 +90,68 @@ export async function resolveSiteData(root: string): Promise { locales: userConfig.locales || {} } } + +/** + * This method merges the locales data to the main data by the route. + */ +export function resolveSiteDataByRoute(siteData: SiteData, route: string) { + route = cleanRoute(siteData, route) + + const localeData = resolveLocales(siteData.locales || {}, route) || {} + const localeThemeConfig = + resolveLocales( + (siteData.themeConfig && siteData.themeConfig.locales) || {}, + route + ) || {} + + return { + ...siteData, + ...localeData, + themeConfig: { + ...siteData.themeConfig, + ...localeThemeConfig, + // clean the locales to reduce the bundle size + locales: {} + }, + locales: {} + } +} + +function resolveLocales( + locales: Record, + route: string +): T | undefined { + const localeRoot = findMatchRoot(route, Object.keys(locales)) + return localeRoot ? locales[localeRoot] : undefined +} + +function findMatchRoot(route: string, roots: string[]) { + // first match to the routes with the most deep level. + roots.sort((a, b) => { + const levelDelta = b.split('/').length - a.split('/').length + if (levelDelta !== 0) { + return levelDelta + } else { + return b.length - a.length + } + }) + + for (const r of roots) { + if (route.startsWith(r)) return r + } + return undefined +} + +/** + * Clean up the route by removing the `base` path if it's set in config. + */ +function cleanRoute(siteData: SiteData, route: string): string { + if (!inBrowser) { + return route + } + + const base = siteData.base + const baseWithoutSuffix = base.endsWith('/') ? base.slice(0, -1) : base + + return route.slice(baseWithoutSuffix.length) +} diff --git a/src/node/tsconfig.json b/src/node/tsconfig.json index e67dbbc7..e6cd4d2a 100644 --- a/src/node/tsconfig.json +++ b/src/node/tsconfig.json @@ -1,10 +1,14 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { - "outDir": "../../dist", + "baseUrl": ".", + "outDir": "../../dist/node", "module": "commonjs", "lib": ["ESNext", "DOM"], "sourceMap": true }, - "include": [".", "../shared", "../../types/shared.d.ts"] + "include": [ + ".", + "../../types/shared.d.ts" + ] } diff --git a/src/shared/config.ts b/src/shared/config.ts deleted file mode 100644 index 2946c23d..00000000 --- a/src/shared/config.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { SiteData } from '../../types/shared' - -const inBrowser = typeof window !== 'undefined' - -function findMatchRoot(route: string, roots: string[]) { - // first match to the routes with the most deep level. - roots.sort((a, b) => { - const levelDelta = b.split('/').length - a.split('/').length - if (levelDelta !== 0) { - return levelDelta - } else { - return b.length - a.length - } - }) - - for (const r of roots) { - if (route.startsWith(r)) return r - } - return undefined -} - -function resolveLocales( - locales: Record, - route: string -): T | undefined { - const localeRoot = findMatchRoot(route, Object.keys(locales)) - return localeRoot ? locales[localeRoot] : undefined -} - -// this merges the locales data to the main data by the route -export function resolveSiteDataByRoute(siteData: SiteData, route: string) { - route = cleanRoute(siteData, route) - - const localeData = resolveLocales(siteData.locales || {}, route) || {} - const localeThemeConfig = - resolveLocales( - (siteData.themeConfig && siteData.themeConfig.locales) || {}, - route - ) || {} - - return { - ...siteData, - ...localeData, - themeConfig: { - ...siteData.themeConfig, - ...localeThemeConfig, - // clean the locales to reduce the bundle size - locales: {} - }, - locales: {} - } -} - -/** - * Clean up the route by removing the `base` path if it's set in config. - */ -function cleanRoute(siteData: SiteData, route: string): string { - if (!inBrowser) { - return route - } - - const base = siteData.base - const baseWithoutSuffix = base.endsWith('/') ? base.slice(0, -1) : base - - return route.slice(baseWithoutSuffix.length) -} diff --git a/src/shared/tsconfig.json b/src/shared/tsconfig.json deleted file mode 100644 index 34954d27..00000000 --- a/src/shared/tsconfig.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "extends": "../../tsconfig.base.json", - "compilerOptions": { - "baseUrl": ".", - "outDir": "../../dist/client/shared", - "module": "esnext", - "lib": ["ESNext", "DOM"], - }, - "include": [ - ".", - "../../types/shared.d.ts", - ] -}