Skip to content

feat: Import custom components.[#729] #730

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
Closed
Show file tree
Hide file tree
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
19 changes: 14 additions & 5 deletions lib/prepare/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,25 @@ exports.genRoutesFile = async function ({
)
}

exports.genComponentRegistrationFile = async function ({ sourceDir }) {
function genImport (file) {
exports.genComponentRegistrationFile = async function ({ sourceDir, components, componentsDir }) {
function genFileImport (file) {
const name = fileToComponentName(file)
const baseDir = path.resolve(sourceDir, '.vuepress/components')
return genImport(name, file)
}

function genCustomImport (component) {
const { name, file } = component
return genImport(name, file, true)
}

function genImport (name, file, isCustomComponent = false) {
const baseDir = isCustomComponent ? path.resolve(sourceDir) : path.resolve(sourceDir, componentsDir)
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 componentsFromFolder = (await resolveComponents(sourceDir, componentsDir)) || []
return `import Vue from 'vue'\n` + componentsFromFolder.map(genFileImport).join('\n') + components.map(genCustomImport).join('\n')
}

8 changes: 8 additions & 0 deletions lib/prepare/resolveOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,20 @@ module.exports = async function resolveOptions (sourceDir) {
locales: siteConfig.locales
}

// resolve components dir
const componentsDir = siteConfig.componentsDir || '.vuepress/components'

// resolve custom components
const components = siteConfig.components || []

const options = {
siteConfig,
siteData,
sourceDir,
outDir,
componentsDir,
publicPath: base,
components,
pageFiles,
pagesData,
themePath,
Expand Down
4 changes: 2 additions & 2 deletions lib/prepare/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ exports.isIndexFile = function (file) {
return indexRE.test(file)
}

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