Skip to content

Commit b7ced3b

Browse files
authored
refactor(theme): improve theme options (#463)
1 parent 6442ffa commit b7ced3b

File tree

9 files changed

+80
-53
lines changed

9 files changed

+80
-53
lines changed

theme/src/node/autoFrontmatter/generator.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import type {
33
AutoFrontmatterArray,
44
AutoFrontmatterMarkdownFile,
55
AutoFrontmatterObject,
6-
AutoFrontmatterOptions,
7-
PlumeThemeLocaleOptions,
86
} from '../../shared/index.js'
97
import { isArray, isEmptyObject, promiseParallel, toArray } from '@pengzhanbo/utils'
108
import chokidar from 'chokidar'
@@ -34,10 +32,11 @@ export interface Generate {
3432

3533
let generate: Generate | null = null
3634

37-
export function initAutoFrontmatter(
38-
localeOptions: PlumeThemeLocaleOptions,
39-
autoFrontmatter: AutoFrontmatterOptions = {},
40-
) {
35+
export function initAutoFrontmatter() {
36+
const { localeOptions, autoFrontmatter = {} } = getThemeConfig()
37+
if (autoFrontmatter === false)
38+
return
39+
4140
const { include, exclude, frontmatter = {} } = resolveOptions(localeOptions, autoFrontmatter)
4241

4342
const globFilter = createFilter(include, exclude, { resolve: false })

theme/src/node/config/extendsBundlerOptions.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ export function extendsBundlerOptions(bundlerOptions: any, app: App): void {
1515
},
1616
})
1717

18-
addViteOptimizeDepsInclude(bundlerOptions, app, '@vueuse/core', true)
18+
addViteOptimizeDepsInclude(
19+
bundlerOptions,
20+
app,
21+
['@vueuse/core', 'bcrypt-ts/browser', '@vuepress/helper/client', '@iconify/vue', '@iconify/vue/offline'],
22+
)
1923
addViteOptimizeDepsExclude(bundlerOptions, app, '@theme')
2024

2125
addViteSsrNoExternal(bundlerOptions, app, [

theme/src/node/config/templateBuildRenderer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { TemplateRendererContext } from 'vuepress/utils'
2-
import type { PlumeThemeLocaleOptions } from '../../shared/index.js'
32
import { templateRenderer } from 'vuepress/utils'
3+
import { getThemeConfig } from '../loadConfig/index.js'
44
import { getThemePackage } from '../utils/index.js'
55

66
export function templateBuildRenderer(
77
template: string,
88
context: TemplateRendererContext,
9-
options: PlumeThemeLocaleOptions,
109
) {
10+
const { localeOptions: options } = getThemeConfig()
1111
const pkg = getThemePackage()
1212
template = template
1313
.replace('{{ themeVersion }}', pkg.version || '')

theme/src/node/pages/createPages.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
import type { App, Page } from 'vuepress/core'
2-
import type { PlumeThemeLocaleOptions } from '../../shared/index.js'
3-
import { getRootLang } from '@vuepress/helper'
42
import { createPage } from 'vuepress/core'
3+
import { getThemeConfig } from '../loadConfig/index.js'
54
import { perfLog, perfMark, withBase } from '../utils/index.js'
65

7-
export async function createPages(app: App, localeOption: PlumeThemeLocaleOptions) {
8-
if (localeOption.blog === false)
6+
function getRootLang(app: App): string {
7+
// infer from siteLocale
8+
const siteLocales = app.siteData.locales
9+
10+
if (siteLocales['/']?.lang)
11+
return siteLocales['/'].lang
12+
13+
return app.siteData.lang
14+
}
15+
16+
export async function createPages(app: App) {
17+
const { localeOptions } = getThemeConfig()
18+
19+
if (localeOptions.blog === false)
920
return
1021

1122
perfMark('create:blog-pages')
1223
const pageList: Promise<Page>[] = []
13-
const locales = localeOption.locales || {}
24+
const locales = localeOptions.locales || {}
1425
const rootLang = getRootLang(app)
1526

16-
const blog = localeOption.blog || {}
27+
const blog = localeOptions.blog || {}
1728
const link = blog.link || '/blog/'
1829

1930
for (const localePath of Object.keys(locales)) {
@@ -24,31 +35,31 @@ export async function createPages(app: App, localeOption: PlumeThemeLocaleOption
2435
if (blog.postList !== false) {
2536
pageList.push(createPage(app, {
2637
path: withBase(link, localePath),
27-
frontmatter: { lang, _pageLayout: 'blog', title: opt.blogText || localeOption.blogText || 'Blog' },
38+
frontmatter: { lang, _pageLayout: 'blog', title: opt.blogText || localeOptions.blogText || 'Blog' },
2839
}))
2940
}
3041

3142
// 添加 标签页
3243
if (blog.tags !== false) {
3344
pageList.push(createPage(app, {
3445
path: withBase(blog.tagsLink || `${link}/tags/`, localePath),
35-
frontmatter: { lang, _pageLayout: 'blog-tags', title: opt.tagText || localeOption.tagText || 'Tags' },
46+
frontmatter: { lang, _pageLayout: 'blog-tags', title: opt.tagText || localeOptions.tagText || 'Tags' },
3647
}))
3748
}
3849

3950
// 添加归档页
4051
if (blog.archives !== false) {
4152
pageList.push(createPage(app, {
4253
path: withBase(blog.archivesLink || `${link}/archives/`, localePath),
43-
frontmatter: { lang, _pageLayout: 'blog-archives', title: opt.archiveText || localeOption.archiveText || 'Archives' },
54+
frontmatter: { lang, _pageLayout: 'blog-archives', title: opt.archiveText || localeOptions.archiveText || 'Archives' },
4455
}))
4556
}
4657

4758
// 添加分类页
4859
if (blog.categories !== false) {
4960
pageList.push(createPage(app, {
5061
path: withBase(blog.categoriesLink || `${link}/categories/`, localePath),
51-
frontmatter: { lang, _pageLayout: 'blog-categories', title: opt.categoryText || localeOption.categoryText || 'Categories' },
62+
frontmatter: { lang, _pageLayout: 'blog-categories', title: opt.categoryText || localeOptions.categoryText || 'Categories' },
5263
}))
5364
}
5465
}

theme/src/node/pages/extendsPage.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { Page } from 'vuepress/core'
2-
import type { PlumeThemeLocaleOptions, PlumeThemePageData } from '../../shared/index.js'
2+
import type { PlumeThemePageData } from '../../shared/index.js'
3+
import { getThemeConfig } from '../loadConfig/index.js'
34
import { autoCategory } from './autoCategory.js'
45
import { enableBulletin } from './pageBulletin.js'
56

67
export function extendsPageData(
78
page: Page<PlumeThemePageData>,
8-
localeOptions: PlumeThemeLocaleOptions,
99
) {
10+
const { localeOptions } = getThemeConfig()
1011
cleanPageData(page)
1112
autoCategory(page, localeOptions)
1213
enableBulletin(page, localeOptions)

theme/src/node/plugins/getPlugins.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { SeoPluginOptions } from '@vuepress/plugin-seo'
22
import type { SitemapPluginOptions } from '@vuepress/plugin-sitemap'
33
import type { App, PluginConfig } from 'vuepress/core'
4-
import type { PlumeThemeLocaleOptions, PlumeThemePluginOptions } from '../../shared/index.js'
4+
import type { PlumeThemePluginOptions } from '../../shared/index.js'
55
import { contentUpdatePlugin } from '@vuepress-plume/plugin-content-update'
66
import { fontsPlugin } from '@vuepress-plume/plugin-fonts'
77
import { searchPlugin } from '@vuepress-plume/plugin-search'
@@ -23,6 +23,7 @@ import { sitemapPlugin } from '@vuepress/plugin-sitemap'
2323
import { watermarkPlugin } from '@vuepress/plugin-watermark'
2424
import { mdEnhancePlugin } from 'vuepress-plugin-md-enhance'
2525
import { markdownPowerPlugin } from 'vuepress-plugin-md-power'
26+
import { getThemeConfig } from '../loadConfig/index.js'
2627

2728
export interface SetupPluginOptions {
2829
app: App
@@ -31,16 +32,14 @@ export interface SetupPluginOptions {
3132
cache?: false | 'memory' | 'filesystem'
3233
}
3334

34-
export function getPlugins(
35-
options: PlumeThemeLocaleOptions,
36-
{
37-
app,
38-
pluginOptions,
39-
hostname,
40-
cache,
41-
}: SetupPluginOptions,
42-
): PluginConfig {
35+
export function getPlugins({
36+
app,
37+
pluginOptions,
38+
hostname,
39+
cache,
40+
}: SetupPluginOptions): PluginConfig {
4341
const isProd = app.env.isBuild
42+
const { localeOptions: options } = getThemeConfig()
4443

4544
const plugins: PluginConfig = [
4645
fontsPlugin(),

theme/src/node/prepare/prepareThemeData.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { FSWatcher } from 'chokidar'
22
import type { App } from 'vuepress'
3-
import type { PlumeThemeData, PlumeThemeLocaleOptions, PlumeThemePluginOptions } from '../../shared/index.js'
3+
import type { PlumeThemeData, PlumeThemePluginOptions } from '../../shared/index.js'
44
import fs from 'node:fs/promises'
55
import process from 'node:process'
66
import { watch } from 'chokidar'
77
import { resolveImageSize } from 'vuepress-plugin-md-power'
88
import { hash } from 'vuepress/utils'
99
import { resolveThemeData } from '../config/resolveThemeData.js'
10+
import { getThemeConfig } from '../loadConfig/index.js'
1011
import { perfLog, perfMark, resolveContent, writeTemp } from '../utils/index.js'
1112

1213
let bulletinFileWatcher: FSWatcher | null = null
@@ -16,10 +17,10 @@ process.on('exit', () => bulletinFileWatcher?.close())
1617

1718
export async function prepareThemeData(
1819
app: App,
19-
localeOptions: PlumeThemeLocaleOptions,
2020
pluginOptions: PlumeThemePluginOptions,
2121
): Promise<void> {
2222
perfMark('prepare:theme-data')
23+
const { localeOptions } = getThemeConfig()
2324
const resolvedThemeData = resolveThemeData(app, localeOptions)
2425

2526
await resolveProfileImage(app, resolvedThemeData, pluginOptions)

theme/src/node/theme.ts

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
import type { Page, Theme } from 'vuepress/core'
22
import type { PlumeThemeOptions, PlumeThemePageData } from '../shared/index.js'
33
import { sleep } from '@pengzhanbo/utils'
4-
import { generateAutoFrontmatter, initAutoFrontmatter, watchAutoFrontmatter } from './autoFrontmatter/index.js'
5-
import { extendsBundlerOptions, resolveAlias, resolveProvideData, resolveThemeOptions, templateBuildRenderer } from './config/index.js'
6-
import { getThemeConfig, initConfigLoader, waitForConfigLoaded, watchConfigFile } from './loadConfig/index.js'
4+
import {
5+
generateAutoFrontmatter,
6+
initAutoFrontmatter,
7+
watchAutoFrontmatter,
8+
} from './autoFrontmatter/index.js'
9+
import {
10+
extendsBundlerOptions,
11+
resolveAlias,
12+
resolveProvideData,
13+
resolveThemeOptions,
14+
templateBuildRenderer,
15+
} from './config/index.js'
16+
import { initConfigLoader, waitForConfigLoaded, watchConfigFile } from './loadConfig/index.js'
717
import { createPages, extendsPageData } from './pages/index.js'
818
import { getPlugins } from './plugins/index.js'
919
import { prepareData, watchPrepare } from './prepare/index.js'
1020
import { prepareThemeData } from './prepare/prepareThemeData.js'
1121
import { resolve, templates, THEME_NAME } from './utils/index.js'
1222

1323
export function plumeTheme(options: PlumeThemeOptions = {}): Theme {
14-
const { localeOptions, pluginOptions, hostname, configFile, cache } = resolveThemeOptions(options)
24+
const {
25+
localeOptions,
26+
pluginOptions,
27+
hostname,
28+
configFile,
29+
cache,
30+
} = resolveThemeOptions(options)
1531

1632
return (app) => {
1733
initConfigLoader(app, localeOptions, {
1834
configFile,
19-
onChange: ({ localeOptions, autoFrontmatter }) => {
20-
if (autoFrontmatter !== false)
21-
initAutoFrontmatter(localeOptions, autoFrontmatter)
22-
},
35+
onChange: initAutoFrontmatter,
2336
})
2437

2538
return {
@@ -33,40 +46,39 @@ export function plumeTheme(options: PlumeThemeOptions = {}): Theme {
3346

3447
alias: resolveAlias(),
3548

36-
plugins: getPlugins(getThemeConfig().localeOptions, { app, pluginOptions, hostname, cache }),
49+
plugins: getPlugins({ app, pluginOptions, hostname, cache }),
3750

3851
extendsBundlerOptions,
3952

40-
templateBuildRenderer: (template, context) =>
41-
templateBuildRenderer(template, context, getThemeConfig().localeOptions),
53+
templateBuildRenderer,
4254

4355
extendsMarkdown: async (_, app) => {
44-
const { autoFrontmatter, localeOptions } = await waitForConfigLoaded()
56+
const { autoFrontmatter } = await waitForConfigLoaded()
4557
if (autoFrontmatter !== false) {
46-
initAutoFrontmatter(localeOptions, autoFrontmatter)
58+
initAutoFrontmatter()
4759
await generateAutoFrontmatter(app)
4860
// wait for autoFrontmatter generated
4961
// i/o performance
5062
await sleep(100)
5163
}
5264
},
5365

54-
extendsPage: async (page) => {
55-
extendsPageData(page as Page<PlumeThemePageData>, getThemeConfig().localeOptions)
66+
extendsPage: (page) => {
67+
extendsPageData(page as Page<PlumeThemePageData>)
5668
},
5769

5870
onInitialized: async (app) => {
59-
await createPages(app, getThemeConfig().localeOptions)
71+
await createPages(app)
6072
},
6173

6274
onPrepared: async (app) => {
63-
await prepareThemeData(app, getThemeConfig().localeOptions, pluginOptions)
75+
await prepareThemeData(app, pluginOptions)
6476
await prepareData(app)
6577
},
6678

6779
onWatched: (app, watchers) => {
68-
watchConfigFile(app, watchers, async ({ localeOptions }) => {
69-
await prepareThemeData(app, localeOptions, pluginOptions)
80+
watchConfigFile(app, watchers, async () => {
81+
await prepareThemeData(app, pluginOptions)
7082
await prepareData(app)
7183
})
7284
watchAutoFrontmatter(app, watchers)

theme/src/node/utils/package.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ export function getPackage() {
1616
}
1717

1818
export function getThemePackage() {
19-
return readJsonFileAsync(resolve('.../../package.json'))
19+
return readJsonFileAsync(resolve('../../package.json'))
2020
}

0 commit comments

Comments
 (0)