Skip to content

Commit

Permalink
fix: prioritize vue installed in user project root
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 6, 2022
1 parent 1cb8a38 commit 9b3243b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/node/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@ export const DEFAULT_THEME_PATH = path.join(DIST_CLIENT_PATH, 'theme-default')
export const SITE_DATA_ID = '@siteData'
export const SITE_DATA_REQUEST_PATH = '/' + SITE_DATA_ID

export function resolveAliases(themeDir: string): AliasOptions {
const vueRuntimePath = 'vue/dist/vue.runtime.esm-bundler.js'

export function resolveAliases(root: string, themeDir: string): AliasOptions {
const paths: Record<string, string> = {
'/@theme': themeDir,
'/@shared': SHARED_PATH,
[SITE_DATA_ID]: SITE_DATA_REQUEST_PATH
}

// prioritize vue installed in project root and fallback to
// vue that comes with vitepress itself.
let vuePath
try {
vuePath = require.resolve(vueRuntimePath, { paths: [root] })
} catch (e) {
vuePath = require.resolve(vueRuntimePath)
}

const aliases: Alias[] = [
...Object.keys(paths).map((p) => ({
find: p,
Expand All @@ -40,7 +51,7 @@ export function resolveAliases(themeDir: string): AliasOptions {
// vitepress itself
{
find: /^vue$/,
replacement: require.resolve('vue/dist/vue.runtime.esm-bundler.js')
replacement: vuePath
}
]

Expand Down
14 changes: 13 additions & 1 deletion src/node/build/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ export async function renderPage(
const routePath = `/${page.replace(/\.md$/, '')}`
const siteData = resolveSiteDataByRoute(config.site, routePath)
router.go(routePath)

// lazy require server-renderer for production build
const content = await require('vue/server-renderer').renderToString(app)
// prioritize project root over vitepress' own dep
let rendererPath
try {
rendererPath = require.resolve('vue/server-renderer', {
paths: [config.root]
})
} catch (e) {
rendererPath = require.resolve('vue/server-renderer')
}

// render page
const content = await require(rendererPath).renderToString(app)

const pageName = page.replace(/\//g, '_')
// server build doesn't need hash
Expand Down
2 changes: 1 addition & 1 deletion src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export async function resolveConfig(
outDir,
tempDir: resolve(root, '.temp'),
markdown: userConfig.markdown,
alias: resolveAliases(themeDir),
alias: resolveAliases(root, themeDir),
vue: userConfig.vue,
vite: userConfig.vite,
shouldPreload: userConfig.shouldPreload,
Expand Down

0 comments on commit 9b3243b

Please sign in to comment.