Skip to content
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

handle error on loading module, fix #1296 #1322

Closed
wants to merge 3 commits into from
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
7 changes: 3 additions & 4 deletions packages/@vuepress/core/lib/node/loadTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,10 @@ function resolveTheme (ctx, resolver, ignoreLocal, theme) {
path = localThemePath
name = shortcut = 'local'
logger.tip(`Apply local theme at ${chalk.gray(path)}...`)

// 2. From dep
} else if (isString(theme)) {
// 2. From dep
const resolved = resolver.resolve(theme, sourceDir)
if (resolved.entry === null) {
if (!resolved.entry) {
throw new Error(`Cannot resolve theme: ${theme}.`)
}
path = normalizeThemePath(resolved)
Expand All @@ -108,7 +107,7 @@ function resolveTheme (ctx, resolver, ignoreLocal, theme) {
}

try {
entry = pluginAPI.normalizePlugin(path, ctx.themeConfig)
entry = pluginAPI.normalizePlugin('theme', path, ctx.themeConfig)
} catch (error) {
entry = {}
}
Expand Down
10 changes: 7 additions & 3 deletions packages/@vuepress/core/lib/node/plugin-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module.exports = class PluginAPI {
plugin = pluginRaw
} else {
try {
plugin = this.normalizePlugin(pluginRaw, pluginOptions)
plugin = this.normalizePlugin('plugin', pluginRaw, pluginOptions)
} catch (e) {
logger.warn(e.message)
return this
Expand Down Expand Up @@ -116,10 +116,14 @@ module.exports = class PluginAPI {
* @api public
*/

normalizePlugin (pluginRaw, pluginOptions = {}) {
normalizePlugin (type, pluginRaw, pluginOptions = {}) {
let plugin = this._pluginResolver.resolve(pluginRaw)
if (!plugin.entry) {
throw new Error(`[vuepress] cannot resolve plugin "${pluginRaw}"`)
if (plugin.error) {
throw new Error(`[vuepress] An error was encounted in ${type} "${pluginRaw}"`)
} else {
throw new Error(`[vuepress] Cannot resolve ${type} "${pluginRaw}"`)
}
}
plugin = flattenPlugin(plugin, pluginOptions, this._pluginContext, this)
plugin.$$normalized = true
Expand Down
81 changes: 38 additions & 43 deletions packages/@vuepress/shared-utils/src/moduleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,17 @@ const SCOPE_PACKAGE_RE = /^@(.*)\/(.*)/
*/

export class CommonModule {
name: string | null
entry: string | null
shortcut: string | null
fromDep: boolean | null

constructor (
entry: string | null,
name: string | null,
shortcut: string | null,
fromDep: boolean | null,
) {
this.entry = entry
this.shortcut = shortcut
this.name = name
this.fromDep = fromDep
}
public entry: string | null,
public name: string | null,
public shortcut: string | null,
public fromDep: boolean | null,
public error?: Error
) {}
}

function getNoopModule(error?: Error) {
return new CommonModule(null, null, null, null, error)
}

export interface NormalizedModuleRequest {
Expand Down Expand Up @@ -99,17 +94,15 @@ class ModuleResolver {
}

const isStringRequest = isString(req)
const isAbsolutePath = isStringRequest && path.isAbsolute(req)

const resolved = tryChain<string, CommonModule>([
[this.resolveNonStringPackage.bind(this), !isStringRequest],
[this.resolveAbsolutePathPackage.bind(this), isStringRequest && isAbsolutePath],
[this.resolveRelativePathPackage.bind(this), isStringRequest && !isAbsolutePath],
[this.resolvePathPackage.bind(this), isStringRequest],
[this.resolveDepPackage.bind(this), isStringRequest]
], req)

if (!resolved) {
return new CommonModule(null, null, null, null /* fromDep */)
return getNoopModule()
}

return resolved
Expand All @@ -128,16 +121,20 @@ class ModuleResolver {
* Resolve non-string package, return directly.
*/

private resolveNonStringPackage (req: string) {
const { shortcut, name } = <NormalizedModuleRequest>this.normalizeRequest(req)
private resolveNonStringPackage (req: any) {
const { shortcut, name } = this.normalizeRequest(req)
return new CommonModule(req, name, shortcut, false /* fromDep */)
}

/**
* Resolve module with absolute path.
* Resolve module with absolute/relative path.
*/

resolveAbsolutePathPackage (req: string) {
resolvePathPackage (req: string) {
if (!path.isAbsolute(req)) {
req = path.resolve(this.cwd, req)
}

const normalized = fsExistsFallback([
req,
req + '.js',
Expand All @@ -149,30 +146,29 @@ class ModuleResolver {
}

const dirname = path.parse(normalized).name
const { shortcut, name } = this.normalizeRequest(dirname)
const module = this.load ? require(normalized) : normalized
return new CommonModule(module, name, shortcut, false /* fromDep */)
}

/**
* Resolve module with absolute path.
*/

private resolveRelativePathPackage (req: string) {
req = path.resolve(process.cwd(), req)
return this.resolveAbsolutePathPackage(req)
const { shortcut, name } = this.normalizeName(dirname)
try {
const module = this.load ? require(normalized) : normalized
return new CommonModule(module, name, shortcut, false /* fromDep */)
} catch (error) {
return getNoopModule(error)
}
}

/**
* Resolve module from dependency.
*/

private resolveDepPackage (req: string) {
const { shortcut, name } = this.normalizeRequest(req)
const entry = this.load
? loadModule(<string>name, this.cwd)
: resolveModule(<string>name, this.cwd)
return new CommonModule(entry, name, shortcut, true /* fromDep */)
const { shortcut, name } = this.normalizeName(req)
try {
const entry = this.load
? loadModule(<string>name, this.cwd)
: resolveModule(<string>name, this.cwd)
return new CommonModule(entry, name, shortcut, true /* fromDep */)
} catch (error) {
return getNoopModule(error)
}
}

/**
Expand All @@ -190,8 +186,8 @@ class ModuleResolver {
*/

normalizeName (req: string): NormalizedModuleRequest {
let name
let shortcut
let name = null
let shortcut = null

if (req.startsWith('@')) {
const pkg = resolveScopePackage(req)
Expand All @@ -213,7 +209,6 @@ class ModuleResolver {
name = `${this.nonScopePrefix}${shortcut}`
}

// @ts-ignore
return { name, shortcut }
}

Expand Down