refactor: duplicate teh shared code for better dx

pull/112/head
Kia Ishii 5 years ago
parent af4b015864
commit 50798bdb76

@ -27,13 +27,12 @@
}, },
"homepage": "https://github.com/vuejs/vitepress/tree/master/#readme", "homepage": "https://github.com/vuejs/vitepress/tree/master/#readme",
"scripts": { "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": "tsc -w -p src/client",
"dev-client-copy": "node scripts/watchAndCopy", "dev-client-copy": "node scripts/watchAndCopy",
"dev-node": "tsc -w -p src/node", "dev-node": "tsc -w -p src/node",
"dev-shared": "tsc -w -p src/shared",
"release": "bash scripts/release.sh", "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" "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s"
}, },
"engines": { "engines": {

@ -1,10 +1,75 @@
import { computed } from 'vue' import { computed } from 'vue'
import { resolveSiteDataByRoute } from '/@shared/config' import { SiteData } from '../../../../types/shared'
import { siteDataRef } from './siteData'
import { useRoute } from '../router' import { useRoute } from '../router'
import { siteDataRef } from './siteData'
const inBrowser = typeof window !== 'undefined'
export function useSiteDataByRoute(route = useRoute()) { export function useSiteDataByRoute(route = useRoute()) {
return computed(() => { return computed(() => {
return resolveSiteDataByRoute(siteDataRef.value, route.path) 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<T>(
locales: Record<string, T>,
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<any>(
(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)
}

@ -2,7 +2,7 @@
"extends": "../../tsconfig.base.json", "extends": "../../tsconfig.base.json",
"compilerOptions": { "compilerOptions": {
"baseUrl": ".", "baseUrl": ".",
"outDir": "../../dist", "outDir": "../../dist/client",
"module": "esnext", "module": "esnext",
"lib": ["ESNext", "DOM"], "lib": ["ESNext", "DOM"],
"types": ["vite"], "types": ["vite"],
@ -16,8 +16,5 @@
"include": [ "include": [
".", ".",
"../../types/shared.d.ts", "../../types/shared.d.ts",
],
"exclude": [
"../shared"
] ]
} }

@ -2,24 +2,13 @@ import path from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
import chalk from 'chalk' import chalk from 'chalk'
import globby from 'globby' import globby from 'globby'
import { createResolver, APP_PATH } from './resolver'
import { Resolver } from 'vite' import { Resolver } from 'vite'
import { SiteData, HeadConfig, LocaleConfig } from '../../types/shared' import { SiteData, HeadConfig, LocaleConfig } from '../../types/shared'
export { resolveSiteDataByRoute } from '../shared/config' import { createResolver, APP_PATH } from './resolver'
const debug = require('debug')('vitepress:config') const debug = require('debug')('vitepress:config')
export interface UserConfig<ThemeConfig = any> { const inBrowser = typeof window !== 'undefined'
lang?: string
base?: string
title?: string
description?: string
head?: HeadConfig[]
themeConfig?: ThemeConfig
locales?: Record<string, LocaleConfig>
alias?: Record<string, string>
// TODO locales support etc.
}
export interface SiteConfig<ThemeConfig = any> { export interface SiteConfig<ThemeConfig = any> {
root: string root: string
@ -32,6 +21,17 @@ export interface SiteConfig<ThemeConfig = any> {
pages: string[] pages: string[]
} }
export interface UserConfig<ThemeConfig = any> {
lang?: string
base?: string
title?: string
description?: string
head?: HeadConfig[]
themeConfig?: ThemeConfig
locales?: Record<string, LocaleConfig>
alias?: Record<string, string>
}
const resolve = (root: string, file: string) => const resolve = (root: string, file: string) =>
path.resolve(root, `.vitepress`, file) path.resolve(root, `.vitepress`, file)
@ -90,3 +90,68 @@ export async function resolveSiteData(root: string): Promise<SiteData> {
locales: userConfig.locales || {} 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<any>(
(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<T>(
locales: Record<string, T>,
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)
}

@ -1,10 +1,14 @@
{ {
"extends": "../../tsconfig.base.json", "extends": "../../tsconfig.base.json",
"compilerOptions": { "compilerOptions": {
"outDir": "../../dist", "baseUrl": ".",
"outDir": "../../dist/node",
"module": "commonjs", "module": "commonjs",
"lib": ["ESNext", "DOM"], "lib": ["ESNext", "DOM"],
"sourceMap": true "sourceMap": true
}, },
"include": [".", "../shared", "../../types/shared.d.ts"] "include": [
".",
"../../types/shared.d.ts"
]
} }

@ -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<T>(
locales: Record<string, T>,
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<any>(
(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)
}

@ -1,13 +0,0 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "../../dist/client/shared",
"module": "esnext",
"lib": ["ESNext", "DOM"],
},
"include": [
".",
"../../types/shared.d.ts",
]
}
Loading…
Cancel
Save