mirror of
https://github.com/wahyd4/vuepress.git
synced 2026-08-23 04:05:55 +10:00
42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
const logger = require('../util/logger')
|
|
const chalk = require('chalk')
|
|
|
|
exports.resolvePlugin = function (pluginRaw) {
|
|
if (typeof pluginRaw === 'function' || typeof pluginRaw === 'object') {
|
|
return pluginRaw
|
|
}
|
|
if (typeof pluginRaw === 'string') {
|
|
try {
|
|
return require(pluginRaw.startsWith('vuepress-plugin-')
|
|
? pluginRaw
|
|
: `vuepress-plugin-${pluginRaw}`
|
|
)
|
|
} catch (err) {
|
|
try {
|
|
return require(pluginRaw.startsWith('@vuepress/plugin-')
|
|
? pluginRaw
|
|
: `@vuepress/plugin-${pluginRaw}`
|
|
)
|
|
} catch (e) {
|
|
console.error(chalk.red(logger.error(`\n[vuepress] Cannot resolve plugin: ${pluginRaw}\n`, false)))
|
|
throw new Error(err)
|
|
}
|
|
}
|
|
}
|
|
logger.warn(`\n[vuepress] Invalid plugin usage: ${chalk.yellow(pluginRaw)}\n`)
|
|
}
|
|
|
|
exports.inferPluginName = function (pluginRaw, pluginConfig) {
|
|
if (pluginConfig.name) {
|
|
return pluginConfig.name
|
|
}
|
|
if (typeof pluginRaw === 'string') {
|
|
if (pluginRaw.startsWith('vuepress-plugin-')) {
|
|
return pluginRaw.slice(16)
|
|
}
|
|
return pluginRaw
|
|
}
|
|
// ensure each plugin have a unique name.
|
|
return Date.now().toString(16)
|
|
}
|