Skip to content

Commit fe266e2

Browse files
committed
feat: enhancement and make it work
1 parent 814a9ca commit fe266e2

File tree

18 files changed

+93
-104
lines changed

18 files changed

+93
-104
lines changed

packages/@vuepress/core/__tests__/theme-api/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const parent = {
2121
describe('ThemeAPI', () => {
2222
test('extend', async () => {
2323
const themeAPI = new ThemeAPI(theme, parent)
24-
console.log(themeAPI.theme.entryFile)
24+
console.log(themeAPI.theme.entry)
2525
})
2626
// loadTheme('vuepress-theme-child')
2727
})

packages/@vuepress/core/lib/internal-plugins/enhanceApp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ module.exports = (options, context) => ({
66
enhanceAppFiles () {
77
const { sourceDir, themeAPI } = context
88
const enhanceAppPath = path.resolve(sourceDir, '.vuepress/enhanceApp.js')
9-
const themeEnhanceAppPath = path.resolve(themeAPI.themePath, 'enhanceApp.js')
9+
const themeEnhanceAppPath = path.resolve(themeAPI.theme.path, 'enhanceApp.js')
1010
const files = [
1111
enhanceAppPath,
1212
themeEnhanceAppPath
1313
]
1414
if (themeAPI.existsParentTheme) {
15-
files.push(path.resolve(themeAPI.parentThemePath, 'enhanceApp.js'))
15+
files.push(path.resolve(themeAPI.parentTheme.path, 'enhanceApp.js'))
1616
}
1717
return files
1818
}
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
module.exports = (options, ctx) => {
2-
const { layoutComponentMap } = ctx
3-
const componentNames = Object.keys(layoutComponentMap)
4-
52
return {
63
name: '@vuepress/internal-layout-components',
74

85
async clientDynamicModules () {
6+
const componentNames = Object.keys(ctx.themeAPI.layoutComponentMap)
97
const code = `export default {\n${componentNames
10-
.map(name => ` ${JSON.stringify(name)}: () => import(${JSON.stringify(layoutComponentMap[name].path)})`)
8+
.map(name => ` ${JSON.stringify(name)}: () => import(${JSON.stringify(ctx.themeAPI.layoutComponentMap[name].path)})`)
119
.join(',\n')} \n}`
1210
return { name: 'layout-components.js', content: code, dirname: 'internal' }
13-
},
14-
15-
chainWebpack (config, isServer) {
16-
const setAlias = (alias, raw) => config.resolve.alias.set(alias, raw)
17-
componentNames.forEach(name => {
18-
setAlias(`@${name}`, layoutComponentMap[name].path)
19-
})
2011
}
2112
}
2213
}

packages/@vuepress/core/lib/internal-plugins/palette/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = (options, ctx) => ({
2020
// 2. write palette.styl
2121
const { sourceDir, writeTemp } = ctx
2222

23-
const themePalette = path.resolve(ctx.themePath, 'styles/palette.styl')
23+
const themePalette = path.resolve(ctx.themeAPI.theme.path, 'styles/palette.styl')
2424
const userPalette = path.resolve(sourceDir, '.vuepress/styles/palette.styl')
2525

2626
const themePaletteContent = fs.existsSync(themePalette)
@@ -34,8 +34,8 @@ module.exports = (options, ctx) => ({
3434
// user's palette can override theme's palette.
3535
let paletteContent = themePaletteContent + userPaletteContent
3636

37-
if (ctx.parentThemePath) {
38-
const parentThemePalette = path.resolve(ctx.parentThemePath, 'styles/palette.styl')
37+
if (ctx.themeAPI.existsParentTheme) {
38+
const parentThemePalette = path.resolve(ctx.themeAPI.parentTheme.path, 'styles/palette.styl')
3939
const parentThemePaletteContent = fs.existsSync(parentThemePalette)
4040
? `@import(${JSON.stringify(parentThemePalette.replace(/[\\]+/g, '/'))})`
4141
: ''

packages/@vuepress/core/lib/internal-plugins/style/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
const { fs, path, logger, chalk } = require('@vuepress/shared-utils')
22

3+
/**
4+
* @param options
5+
* @param {AppContext} ctx
6+
*/
37
module.exports = (options, ctx) => ({
48
name: '@vuepress/internal-style',
59

@@ -15,7 +19,7 @@ module.exports = (options, ctx) => ({
1519
logger.tip(`${chalk.magenta('override.styl')} has been deprecated from v1.0.0, using ${chalk.cyan('.vuepress/styles/palette.styl')} instead.\n`)
1620
}
1721

18-
const themeStyle = path.resolve(themeAPI.themePath, 'styles/index.styl')
22+
const themeStyle = path.resolve(themeAPI.theme.path, 'styles/index.styl')
1923
const userStyle = path.resolve(sourceDir, '.vuepress/styles/index.styl')
2024

2125
const themeStyleContent = fs.existsSync(themeStyle)
@@ -29,7 +33,7 @@ module.exports = (options, ctx) => ({
2933
let styleContent = themeStyleContent + userStyleContent
3034

3135
if (themeAPI.existsParentTheme) {
32-
const parentThemeStyle = path.resolve(themeAPI.parentThemePath, 'styles/index.styl')
36+
const parentThemeStyle = path.resolve(themeAPI.parentTheme.path, 'styles/index.styl')
3337
const parentThemeStyleContent = fs.existsSync(parentThemeStyle)
3438
? `@import(${JSON.stringify(parentThemeStyle.replace(/[\\]+/g, '/'))})`
3539
: ''

packages/@vuepress/core/lib/prepare/AppContext.js

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ module.exports = class AppContext {
9494
this.resolveConfigAndInitialize()
9595
this.resolveCacheLoaderOptions()
9696
this.normalizeHeadTagUrls()
97-
/**
98-
* @type {ThemeAPI}
99-
*/
10097
this.themeAPI = loadTheme(this)
10198
this.resolveTemplates()
10299
this.resolveGlobalLayout()
@@ -156,8 +153,8 @@ module.exports = class AppContext {
156153
.use('@vuepress/register-components', {
157154
componentsDir: [
158155
path.resolve(this.sourceDir, '.vuepress/components'),
159-
path.resolve(this.themeAPI.themePath, 'global-components'),
160-
this.themeAPI.existsParentTheme && path.resolve(this.themeAPI.parentThemePath, 'global-components')
156+
path.resolve(this.themeAPI.theme.path, 'global-components'),
157+
this.themeAPI.existsParentTheme && path.resolve(this.themeAPI.parentTheme.path, 'global-components')
161158
]
162159
})
163160
}
@@ -171,10 +168,11 @@ module.exports = class AppContext {
171168
applyUserPlugins () {
172169
this.pluginAPI.useByPluginsConfig(this.cliOptions.plugins)
173170
if (this.themeAPI.existsParentTheme) {
174-
this.pluginAPI.use(this.themeAPI.parentThemePath)
171+
this.pluginAPI.use(this.themeAPI.parentTheme.entry)
175172
}
176173
this.pluginAPI
177-
.use(this.themeAPI.themePath)
174+
.use(this.themeAPI.theme.entry)
175+
.use(this.themeAPI.vuepressPlugin)
178176
.use(Object.assign({}, this.siteConfig, { name: '@vuepress/internal-site-config' }))
179177
}
180178

@@ -224,41 +222,26 @@ module.exports = class AppContext {
224222
*/
225223

226224
resolveTemplates () {
227-
const { siteSsrTemplate, siteDevTemplate } = this.siteConfig
228-
229-
const templateDir = path.resolve(this.vuepressDir, 'templates')
230-
const siteSsrTemplate2 = path.resolve(templateDir, 'ssr.html')
231-
const siteDevTemplate2 = path.resolve(templateDir, 'dev.html')
232-
233-
const themeSsrTemplate = path.resolve(this.themeAPI.themePath, 'templates/ssr.html')
234-
const themeDevTemplate = path.resolve(this.themeAPI.themePath, 'templates/dev.html')
235-
236-
const parentThemeSsrTemplate = path.resolve(this.themeAPI.parentThemePath, 'templates/ssr.html')
237-
const parentThemeDevTemplate = path.resolve(this.themeAPI.parentThemePath, 'templates/dev.html')
238-
239-
const defaultSsrTemplate = path.resolve(__dirname, '../app/index.ssr.html')
240-
const defaultDevTemplate = path.resolve(__dirname, '../app/index.dev.html')
241-
242-
const ssrTemplate = fsExistsFallback([
243-
siteSsrTemplate,
244-
siteSsrTemplate2,
245-
themeSsrTemplate,
246-
parentThemeSsrTemplate,
247-
defaultSsrTemplate
248-
])
225+
this.devTemplate = this.resolveCommonAgreementFilePath(
226+
'devTemplate',
227+
{
228+
defaultValue: path.resolve(__dirname, '../app/index.dev.html'),
229+
siteAgreement: 'templates/dev.html',
230+
themeAgreement: 'templates/dev.html'
231+
}
232+
)
249233

250-
const devTemplate = fsExistsFallback([
251-
siteDevTemplate,
252-
siteDevTemplate2,
253-
themeDevTemplate,
254-
parentThemeDevTemplate,
255-
defaultDevTemplate
256-
])
234+
this.ssrTemplate = this.resolveCommonAgreementFilePath(
235+
'ssrTemplate',
236+
{
237+
defaultValue: path.resolve(__dirname, '../app/index.ssr.html'),
238+
siteAgreement: 'templates/ssr.html',
239+
themeAgreement: 'templates/ssr.html'
240+
}
241+
)
257242

258-
logger.debug('SSR Template File: ' + chalk.gray(ssrTemplate))
259-
logger.debug('DEV Template File: ' + chalk.gray(devTemplate))
260-
this.devTemplate = devTemplate
261-
this.ssrTemplate = ssrTemplate
243+
logger.debug('SSR Template File: ' + chalk.gray(this.ssrTemplate))
244+
logger.debug('DEV Template File: ' + chalk.gray(this.devTemplate))
262245
}
263246

264247
/**
@@ -366,7 +349,8 @@ module.exports = class AppContext {
366349
*/
367350

368351
getThemeConfigValue (key) {
369-
return this.themeEntryFile[key] || this.parentThemeEntryFile[key]
352+
return this.themeAPI.theme.entry[key]
353+
|| this.themeAPI.existsParentTheme && this.themeAPI.parentTheme.entry[key]
370354
}
371355

372356
/**
@@ -378,12 +362,12 @@ module.exports = class AppContext {
378362
*/
379363

380364
resolveThemeAgreementFile (filepath) {
381-
const current = path.resolve(this.themeAPI.themePath, filepath)
365+
const current = path.resolve(this.themeAPI.theme.path, filepath)
382366
if (fs.existsSync(current)) {
383367
return current
384368
}
385369
if (this.themeAPI.existsParentTheme) {
386-
const parent = path.resolve(this.themeAPI.parentThemePath, filepath)
370+
const parent = path.resolve(this.themeAPI.theme.path, filepath)
387371
if (fs.existsSync(parent)) {
388372
return parent
389373
}

packages/@vuepress/core/lib/prepare/loadTheme.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ const ThemeAPI = require('../theme-api')
3333
module.exports = function loadTheme (ctx) {
3434
const themeResolver = getThemeResolver()
3535

36-
const theme = resolveTheme(themeResolver, ctx)
36+
const theme = resolveTheme(ctx, themeResolver)
3737
if (!theme.path) {
3838
throw new Error(`[vuepress] You must specify a theme, or create a local custom theme. \n For more details, refer to https://vuepress.vuejs.org/guide/custom-themes.html#custom-themes. \n`)
3939
}
40-
theme.entryFile.name = '@vuepress/internal-theme-entry-file'
40+
theme.entry.name = '@vuepress/internal-theme-entry-file'
4141

4242
let parentTheme = {}
43-
if (theme.entryFile.extend) {
44-
parentTheme = resolveTheme(themeResolver, ctx, true, theme.entryFile.extend)
45-
parentTheme.entryFile.name = '@vuepress/internal-parent-theme-entry-file'
43+
if (theme.entry.extend) {
44+
parentTheme = resolveTheme(ctx, themeResolver, true, theme.entry.extend)
45+
parentTheme.entry.name = '@vuepress/internal-parent-theme-entry-file'
4646
}
4747

4848
return new ThemeAPI(theme, parentTheme, ctx)
@@ -63,12 +63,12 @@ function normalizeThemePath (resolved) {
6363
function resolveTheme (ctx, resolver, ignoreLocal, theme) {
6464
const { siteConfig, cliOptions, sourceDir, vuepressDir, pluginAPI } = ctx
6565
const localThemePath = resolve(vuepressDir, 'theme')
66-
theme = siteConfig.theme || cliOptions.theme
66+
theme = theme || siteConfig.theme || cliOptions.theme
6767

6868
let path
6969
let name
7070
let shortcut
71-
let entryFile = {}
71+
let entry = {}
7272

7373
// 1. From local
7474
if (!ignoreLocal
@@ -95,10 +95,10 @@ function resolveTheme (ctx, resolver, ignoreLocal, theme) {
9595
}
9696

9797
try {
98-
entryFile = pluginAPI.normalizePlugin(path, ctx.themeConfig)
98+
entry = pluginAPI.normalizePlugin(path, ctx.themeConfig)
9999
} catch (error) {
100-
entryFile = {}
100+
entry = {}
101101
}
102102

103-
return { path, name, shortcut, entryFile }
103+
return { path, name, shortcut, entry }
104104
}

packages/@vuepress/core/lib/theme-api/index.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,28 @@ const { logger, fs, path: { resolve }} = require('@vuepress/shared-utils')
22
const readdirSync = dir => fs.existsSync(dir) && fs.readdirSync(dir) || []
33

44
module.exports = class ThemeAPI {
5-
constructor (theme, parentTheme, ctx) {
5+
constructor (theme, parentTheme) {
66
this.theme = theme
77
this.parentTheme = parentTheme || {}
8-
this.ctx = ctx
98
this.existsParentTheme = !!this.parentTheme.path
9+
this.vuepressPlugin = {
10+
name: '@vuepress/internal-theme-api',
11+
alias: {}
12+
}
1013
this.init()
1114
}
1215

1316
setAlias (alias) {
14-
this.theme.entryFile.alias = {
15-
...(this.theme.entryFile.alias || {}),
16-
alias
17+
this.vuepressPlugin.alias = {
18+
...this.vuepressPlugin.alias,
19+
...alias
1720
}
1821
}
1922

20-
get themePath () {
21-
return this.theme.path
22-
}
23-
24-
get parentThemePath () {
25-
return this.parentTheme.path
26-
}
27-
2823
init () {
2924
const alias = {
3025
'@current-theme': this.theme.path
3126
}
32-
this.setAlias()
3327
if (this.existsParentTheme) {
3428
alias['@parent-theme'] = this.parentTheme.path
3529
}
@@ -101,6 +95,13 @@ module.exports = class ThemeAPI {
10195
}
10296
}
10397

98+
/**
99+
* Resolve Vue SFCs, return a Map
100+
*
101+
* @param dirs
102+
* @returns {*}
103+
*/
104+
104105
function resolveSFCs (dirs) {
105106
return dirs.map(
106107
layoutDir => readdirSync(layoutDir)
@@ -125,9 +126,11 @@ function resolveSFCs (dirs) {
125126

126127
/**
127128
* normalize component name
129+
*
128130
* @param {strin} filename
129131
* @returns {string}
130132
*/
133+
131134
function getComponentName (filename) {
132135
filename = filename.slice(0, -4)
133136
if (filename === '404') {
@@ -136,6 +139,13 @@ function getComponentName (filename) {
136139
return filename
137140
}
138141

142+
/**
143+
* Whether it's agreed layout component
144+
*
145+
* @param name
146+
* @returns {boolean}
147+
*/
148+
139149
function isInternal (name) {
140150
return name === 'Layout' || name === 'NotFound'
141151
}

packages/@vuepress/theme-default/components/DropdownLink.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
<script>
5353
import NavLink from '@theme/components/NavLink.vue'
54-
import DropdownTransition from './DropdownTransition.vue'
54+
import DropdownTransition from '@theme/components/DropdownTransition.vue'
5555
5656
export default {
5757
components: { NavLink, DropdownTransition },

packages/@vuepress/theme-default/components/Home.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
</template>
5151

5252
<script>
53-
import NavLink from './NavLink.vue'
53+
import NavLink from '@theme/components/NavLink.vue'
5454
5555
export default {
5656
components: { NavLink },

packages/@vuepress/theme-default/components/NavLinks.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
</template>
3535

3636
<script>
37-
import DropdownLink from './DropdownLink.vue'
37+
import DropdownLink from '@theme/components/DropdownLink.vue'
3838
import { resolveNavLinkItem } from '../util'
39-
import NavLink from './NavLink.vue'
39+
import NavLink from '@theme/components/NavLink.vue'
4040
4141
export default {
4242
components: { NavLink, DropdownLink },

packages/@vuepress/theme-default/components/Navbar.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
</template>
3838

3939
<script>
40-
import SidebarButton from './SidebarButton.vue'
4140
import AlgoliaSearchBox from '@AlgoliaSearchBox'
4241
import SearchBox from '@SearchBox'
43-
import NavLinks from './NavLinks.vue'
42+
import SidebarButton from '@theme/components/SidebarButton.vue'
43+
import NavLinks from '@theme/components/NavLinks.vue'
4444
4545
export default {
4646
components: { SidebarButton, NavLinks, SearchBox, AlgoliaSearchBox },

0 commit comments

Comments
 (0)