From 50f64b473f50978f2dc5848a78242d7f3dbb7a00 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Sun, 2 Jun 2019 21:31:49 +0800 Subject: [PATCH] fix($core): cannot retrieve the correct theme name when them path is a local absolute path linked to a javascript file. --- packages/@vuepress/core/lib/node/loadTheme.js | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/@vuepress/core/lib/node/loadTheme.js b/packages/@vuepress/core/lib/node/loadTheme.js index 11d9d295a5..f05269d2db 100755 --- a/packages/@vuepress/core/lib/node/loadTheme.js +++ b/packages/@vuepress/core/lib/node/loadTheme.js @@ -32,11 +32,15 @@ const ThemeAPI = require('./theme-api') module.exports = function loadTheme (ctx) { const themeResolver = getThemeResolver() - const theme = resolveTheme(ctx, themeResolver) + if (!theme.path) { - 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`) + 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' + ) } + let applyTip = `Apply theme ${chalk.magenta(theme.name)}` theme.entry.name = '@vuepress/internal-theme-entry-file' @@ -46,6 +50,7 @@ module.exports = function loadTheme (ctx) { parentTheme.entry.name = '@vuepress/internal-parent-theme-entry-file' applyTip += chalk.gray(` (extends ${chalk.magenta(parentTheme.name)})`) } + logger.tip(applyTip + ' ...') logger.debug('theme', theme.name, theme.path) @@ -74,7 +79,9 @@ function resolveTheme (ctx, resolver, ignoreLocal, theme) { let shortcut let entry = {} - // 1. From local + /** + * 1. From `.vuepress/theme` directory. + */ if (!ignoreLocal && !fs.existsSync(theme) && fs.existsSync(localThemePath) @@ -84,15 +91,31 @@ function resolveTheme (ctx, resolver, ignoreLocal, theme) { name = shortcut = 'local' logger.tip(`Apply local theme at ${chalk.gray(path)}...`) - // 2. From dep + /** + * 2. From deps or custom local path. + * - vuepress-plugin-foo + * - /path/to/a-theme/index.js + */ } else if (isString(theme)) { + /** + * To let theme resolver get the correct theme name. + */ + if (theme.endsWith('/index.js')) { + theme = theme.replace(/\/index\.js$/, '') + } + const resolved = resolver.resolve(theme, sourceDir) if (resolved.entry === null) { throw new Error(`Cannot resolve theme: ${theme}.`) } + path = normalizeThemePath(resolved) name = resolved.name shortcut = resolved.shortcut + + /** + * 3. fallback + */ } else { return {} }