|
1 | 1 | const path = require('path')
|
2 | 2 | const createMarkdown = require('../markdown/index')
|
3 | 3 | const loadConfig = require('./loadConfig')
|
| 4 | +const loadTheme = require('./loadTheme') |
4 | 5 | const { fs, logger, chalk, globby, sort } = require('@vuepress/shared-utils')
|
5 | 6 |
|
6 | 7 | const Page = require('./Page')
|
@@ -87,7 +88,7 @@ module.exports = class AppContext {
|
87 | 88 | // user plugin
|
88 | 89 | .useByPluginsConfig(this._options.plugins)
|
89 | 90 | .useByPluginsConfig(this.siteConfig.plugins)
|
90 |
| - .useByPluginsConfig(this.themeplugins) |
| 91 | + .useByPluginsConfig(this.themePlugins) |
91 | 92 | // built-in plugins
|
92 | 93 | .use('@vuepress/last-updated', shouldUseLastUpdated)
|
93 | 94 | .use('@vuepress/register-components', {
|
@@ -168,70 +169,7 @@ module.exports = class AppContext {
|
168 | 169 | */
|
169 | 170 | async resolveTheme () {
|
170 | 171 | const theme = this.siteConfig.theme || this._options.theme
|
171 |
| - const requireResolve = (target) => { |
172 |
| - return require.resolve(target, { |
173 |
| - paths: [ |
174 |
| - path.resolve(__dirname, '../../node_modules'), |
175 |
| - path.resolve(this.sourceDir) |
176 |
| - ] |
177 |
| - }) |
178 |
| - } |
179 |
| - |
180 |
| - // resolve theme |
181 |
| - const localThemePath = path.resolve(this.vuepressDir, 'theme') |
182 |
| - const useLocalTheme = await fs.exists(localThemePath) |
183 |
| - |
184 |
| - let themePath = null |
185 |
| - let themeLayoutPath = null |
186 |
| - let themeNotFoundPath = null |
187 |
| - let themeIndexFile = null |
188 |
| - let themePlugins = [] |
189 |
| - |
190 |
| - if (useLocalTheme) { |
191 |
| - logger.tip(`\nApply theme located at ${localThemePath}...`) |
192 |
| - |
193 |
| - // use local custom theme |
194 |
| - themePath = localThemePath |
195 |
| - themeLayoutPath = path.resolve(localThemePath, 'Layout.vue') |
196 |
| - themeNotFoundPath = path.resolve(localThemePath, 'NotFound.vue') |
197 |
| - if (!fs.existsSync(themeLayoutPath)) { |
198 |
| - throw new Error(`[vuepress] Cannot resolve Layout.vue file in .vuepress/theme.`) |
199 |
| - } |
200 |
| - if (!fs.existsSync(themeNotFoundPath)) { |
201 |
| - themeNotFoundPath = path.resolve(__dirname, '../app/components/NotFound.vue') |
202 |
| - } |
203 |
| - } else if (theme) { |
204 |
| - // use external theme |
205 |
| - try { |
206 |
| - // backward-compatible 0.x.x. |
207 |
| - themeLayoutPath = requireResolve(`vuepress-theme-${theme}/Layout.vue`) |
208 |
| - themePath = path.dirname(themeLayoutPath) |
209 |
| - themeNotFoundPath = path.resolve(themeLayoutPath, 'NotFound.vue') |
210 |
| - } catch (e) { |
211 |
| - try { |
212 |
| - themeIndexFile = requireResolve(`vuepress-theme-${theme}/index.js`) |
213 |
| - } catch (e) { |
214 |
| - try { |
215 |
| - themeIndexFile = requireResolve(`@vuepress/theme-${theme}`) |
216 |
| - themePath = path.dirname(themeIndexFile) |
217 |
| - themeIndexFile = require(themeIndexFile) |
218 |
| - themeLayoutPath = themeIndexFile.layout |
219 |
| - themeNotFoundPath = themeIndexFile.notFound |
220 |
| - themePlugins = themeIndexFile.plugins |
221 |
| - } catch (e) { |
222 |
| - throw new Error(`[vuepress] Failed to load custom theme "${theme}". File vuepress-theme-${theme}/Layout.vue does not exist.`) |
223 |
| - } |
224 |
| - } |
225 |
| - } |
226 |
| - logger.tip(`\nApply theme ${chalk.gray(theme)}`) |
227 |
| - } else { |
228 |
| - 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`) |
229 |
| - } |
230 |
| - |
231 |
| - this.themePath = themePath |
232 |
| - this.themeLayoutPath = themeLayoutPath |
233 |
| - this.themeNotFoundPath = themeNotFoundPath |
234 |
| - this.themeplugins = themePlugins |
| 172 | + Object.assign(this, (await loadTheme(theme, this.sourceDir, this.vuepressDir))) |
235 | 173 | }
|
236 | 174 |
|
237 | 175 | /**
|
|
0 commit comments