Skip to content

feat: register globally theme components (close #281) #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,22 @@ async function resolveOptions (sourceDir) {
return options
}

async function genComponentRegistrationFile ({ sourceDir }) {
function genImport (file) {
async function genComponentRegistrationFile ({ sourceDir, themePath }) {
function genImport (file, ...componentDir) {
const name = fileToComponentName(file)
const baseDir = path.resolve(sourceDir, '.vuepress/components')
const baseDir = path.resolve(...componentDir)
const absolutePath = path.resolve(baseDir, file)
const code = `Vue.component(${JSON.stringify(name)}, () => import(${JSON.stringify(absolutePath)}))`
return code
}
const components = (await resolveComponents(sourceDir)) || []
return `import Vue from 'vue'\n` + components.map(genImport).join('\n')
const components = (await resolveComponents(sourceDir, '.vuepress/components')) || []
const themeComponents = (await resolveComponents(themePath, 'components')) || []
const componentsStr =
components.map(f => genImport(f, sourceDir, '.vuepress/components')).join('\n')
const themeComponentsStr =
themeComponents.map(f => genImport(f, themePath, 'components')).join('\n')

return `import Vue from 'vue'\n${componentsStr}\n${themeComponentsStr}`
}

const indexRE = /(^|.*\/)(index|readme)\.md$/i
Expand Down Expand Up @@ -287,8 +293,8 @@ function isIndexFile (file) {
return indexRE.test(file)
}

async function resolveComponents (sourceDir) {
const componentDir = path.resolve(sourceDir, '.vuepress/components')
async function resolveComponents (...sourceDir) {
const componentDir = path.resolve(...sourceDir)
if (!fs.existsSync(componentDir)) {
return
}
Expand Down