Skip to content

Commit f302be2

Browse files
committed
refactor(core): refine resolveThemeInfo
1 parent 397dc22 commit f302be2

File tree

6 files changed

+63
-56
lines changed

6 files changed

+63
-56
lines changed

packages/@vuepress/core/__tests__/app/resolveThemeApi.spec.ts renamed to packages/@vuepress/core/__tests__/app/resolveThemeInfo.spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
1-
import { createBaseApp, resolveThemeApi } from '@vuepress/core'
1+
import { createBaseApp, resolveThemeInfo } from '@vuepress/core'
22
import { path } from '@vuepress/utils'
33

44
const fixtures = (...args: string[]) =>
55
path.resolve(__dirname, '../__fixtures__/', ...args)
66

7-
describe('core > app > resolveThemeApi', () => {
7+
describe('core > app > resolveThemeInfo', () => {
88
describe('layouts', () => {
9-
it('should resolve theme api without layouts correctly', () => {
9+
it('should resolve theme info without layouts correctly', () => {
1010
const app = createBaseApp({
1111
source: path.resolve(__dirname, 'fake-source'),
1212
theme: fixtures('themes/empty.js'),
1313
})
1414

15-
expect(resolveThemeApi(app, app.options.theme).layouts).toEqual({})
15+
expect(resolveThemeInfo(app, app.options.theme).layouts).toEqual({})
1616
})
1717

18-
it('should resolve theme api with layouts correctly', () => {
18+
it('should resolve theme info with layouts correctly', () => {
1919
const app = createBaseApp({
2020
source: path.resolve(__dirname, 'fake-source'),
2121
theme: fixtures('themes/has-layouts.js'),
2222
})
2323

24-
expect(resolveThemeApi(app, app.options.theme).layouts).toEqual({
24+
expect(resolveThemeInfo(app, app.options.theme).layouts).toEqual({
2525
Layout: fixtures('layouts/Layout.vue'),
2626
404: fixtures('layouts/404.vue'),
2727
})
2828
})
2929
})
3030

3131
describe('plugins', () => {
32-
it('should resolve theme api without plugins correctly', () => {
32+
it('should resolve theme info without plugins correctly', () => {
3333
const app = createBaseApp({
3434
source: path.resolve(__dirname, 'fake-source'),
3535
theme: fixtures('themes/empty.js'),
3636
})
3737

38-
expect(resolveThemeApi(app, app.options.theme).plugins).toEqual([
38+
expect(resolveThemeInfo(app, app.options.theme).plugins).toEqual([
3939
require(fixtures('themes/empty.js')),
4040
])
4141
})
4242

43-
it('should resolve theme api with plugins correctly', () => {
43+
it('should resolve theme info with plugins correctly', () => {
4444
const app = createBaseApp({
4545
source: path.resolve(__dirname, 'fake-source'),
4646
theme: fixtures('themes/has-plugins.js'),
4747
})
4848

49-
expect(resolveThemeApi(app, app.options.theme).plugins).toEqual([
49+
expect(resolveThemeInfo(app, app.options.theme).plugins).toEqual([
5050
require(fixtures('themes/has-plugins.js')),
5151
require(fixtures('plugins/obj.js')),
5252
])
5353
})
5454
})
5555

5656
describe('extends', () => {
57-
it('should resolve theme api with parent theme correctly', () => {
57+
it('should resolve theme info with parent theme correctly', () => {
5858
const app = createBaseApp({
5959
source: path.resolve(__dirname, 'fake-source'),
6060
theme: fixtures('themes/extends-parent.js'),
6161
})
6262

63-
expect(resolveThemeApi(app, app.options.theme)).toEqual({
63+
expect(resolveThemeInfo(app, app.options.theme)).toEqual({
6464
plugins: [
6565
require(fixtures('themes/has-layouts-and-plugins.js')),
6666
require(fixtures('plugins/obj.js')),
@@ -75,13 +75,13 @@ describe('core > app > resolveThemeApi', () => {
7575
})
7676
})
7777

78-
it('should resolve theme api with grandparent theme correctly', () => {
78+
it('should resolve theme info with grandparent theme correctly', () => {
7979
const app = createBaseApp({
8080
source: path.resolve(__dirname, 'fake-source'),
8181
theme: fixtures('themes/extends-grandparent.js'),
8282
})
8383

84-
expect(resolveThemeApi(app, app.options.theme)).toEqual({
84+
expect(resolveThemeInfo(app, app.options.theme)).toEqual({
8585
plugins: [
8686
require(fixtures('themes/has-layouts-and-plugins.js')),
8787
require(fixtures('plugins/obj.js')),

packages/@vuepress/core/src/app/createBaseApp.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { createAppSiteData } from './createAppSiteData'
1111
import { createAppVersion } from './createAppVersion'
1212
import { createAppWriteTemp } from './createAppWriteTemp'
1313
import { resolvePluginsFromConfig } from './resolvePluginsFromConfig'
14-
import { resolveThemeApi } from './resolveThemeApi'
14+
import { resolveThemeInfo } from './resolveThemeInfo'
1515

1616
/**
1717
* Create vuepress app
@@ -40,10 +40,10 @@ export const createBaseApp = (config: AppConfig, isBuild = false): App => {
4040
prepare: () => appPrepare(app),
4141
} as App
4242

43-
// resolve theme plugins and layouts
44-
const themeApi = resolveThemeApi(app, options.theme)
45-
themeApi.plugins.forEach((plugin) => app.use(plugin))
46-
app.layouts = themeApi.layouts
43+
// resolve theme info and use theme plugins
44+
const themeInfo = resolveThemeInfo(app, options.theme)
45+
themeInfo.plugins.forEach((plugin) => app.use(plugin))
46+
app.layouts = themeInfo.layouts
4747

4848
// resolve plugins
4949
const plugins = resolvePluginsFromConfig(app, options.plugins)

packages/@vuepress/core/src/app/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ export * from './resolvePlugin'
1818
export * from './resolvePluginModule'
1919
export * from './resolvePluginsFromConfig'
2020
export * from './resolveTheme'
21-
export * from './resolveThemeApi'
21+
export * from './resolveThemeInfo'
2222
export * from './resolveThemeLayouts'

packages/@vuepress/core/src/app/resolveThemeApi.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { App, ThemeInfo } from '../types'
2+
import { resolvePluginsFromConfig } from './resolvePluginsFromConfig'
3+
import { resolveTheme } from './resolveTheme'
4+
import { resolveThemeLayouts } from './resolveThemeLayouts'
5+
6+
/**
7+
* Resolve theme info and its parent theme info
8+
*/
9+
export const resolveThemeInfo = (app: App, themeName: string): ThemeInfo => {
10+
// resolve current theme info
11+
const theme = resolveTheme(app, themeName)
12+
const themeInfo = {
13+
layouts: resolveThemeLayouts(theme.layouts),
14+
plugins: [theme, ...resolvePluginsFromConfig(app, theme.plugins)],
15+
}
16+
17+
// return if current theme does not have a parent theme
18+
if (!theme.extends) {
19+
return themeInfo
20+
}
21+
22+
// resolve parent theme info recursively
23+
const parentThemeInfo = resolveThemeInfo(app, theme.extends)
24+
return {
25+
layouts: { ...parentThemeInfo.layouts, ...themeInfo.layouts },
26+
plugins: [...parentThemeInfo.plugins, ...themeInfo.plugins],
27+
}
28+
}

packages/@vuepress/core/src/types/theme.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,18 @@ export interface ThemeObject extends PluginObject {
5151
* @remark suffix `Config` means this is for user config
5252
*/
5353
export type ThemeConfig = Record<string, any>
54+
55+
/**
56+
* Resolved theme info
57+
*/
58+
export interface ThemeInfo {
59+
/**
60+
* Layout components
61+
*/
62+
layouts: Record<string, string>
63+
64+
/**
65+
* Plugins, including theme itself and plugins used by theme
66+
*/
67+
plugins: PluginObject[]
68+
}

0 commit comments

Comments
 (0)