From 51de6cfde8c725a52dc7d95803390b29d9c737eb Mon Sep 17 00:00:00 2001 From: Shigma <33423008+Shigma@users.noreply.github.com> Date: Tue, 24 Sep 2019 01:07:18 +0800 Subject: [PATCH] fix($core): optimize error log (close: #1296) (#1413) --- packages/@vuepress/core/lib/node/loadTheme.js | 5 +- .../core/lib/node/plugin-api/index.js | 11 ++- .../shared-utils/src/moduleResolver.ts | 96 +++++++++---------- 3 files changed, 54 insertions(+), 58 deletions(-) diff --git a/packages/@vuepress/core/lib/node/loadTheme.js b/packages/@vuepress/core/lib/node/loadTheme.js index ac4c958716..eec97897e0 100755 --- a/packages/@vuepress/core/lib/node/loadTheme.js +++ b/packages/@vuepress/core/lib/node/loadTheme.js @@ -123,8 +123,9 @@ function resolveTheme (ctx, resolver, ignoreLocal, theme) { } try { - entry = pluginAPI.normalizePlugin(path, ctx.themeConfig) - } catch (error) { + entry = pluginAPI.normalizePlugin('theme', path, ctx.themeConfig) + } catch (e) { + logger.warn(e.message) entry = {} } diff --git a/packages/@vuepress/core/lib/node/plugin-api/index.js b/packages/@vuepress/core/lib/node/plugin-api/index.js index 6dc8a9e3fd..18dd2c362b 100644 --- a/packages/@vuepress/core/lib/node/plugin-api/index.js +++ b/packages/@vuepress/core/lib/node/plugin-api/index.js @@ -86,7 +86,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 @@ -117,10 +117,15 @@ 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) { + logger.debug(plugin.error) + throw new Error(`An error was encounted in ${type} "${pluginRaw}"`) + } else { + throw new Error(`Cannot resolve ${type} "${pluginRaw}"`) + } } plugin = flattenPlugin(plugin, pluginOptions, this._pluginContext, this) plugin.$$normalized = true diff --git a/packages/@vuepress/shared-utils/src/moduleResolver.ts b/packages/@vuepress/shared-utils/src/moduleResolver.ts index 93ab58cde4..f3bcbda88b 100644 --- a/packages/@vuepress/shared-utils/src/moduleResolver.ts +++ b/packages/@vuepress/shared-utils/src/moduleResolver.ts @@ -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 { @@ -54,22 +49,17 @@ export interface NormalizedModuleRequest { type Type = String | Number | Boolean | RegExp | Function | Object | Record | Array class ModuleResolver { - private type: string - private org: string - private allowedTypes: Type[] - private load: boolean - private cwd: string private nonScopePrefix: string private scopePrefix: string private typePrefixLength: number private prefixSlicePosition: number constructor ( - type: string, - org: string, - allowedTypes: Type[] = [String], - load = false, - cwd: string + private type: string, + private org: string, + private allowedTypes: Type[], + private load = false, + private cwd: string ) { this.type = type this.org = org @@ -103,17 +93,15 @@ class ModuleResolver { } const isStringRequest = isString(req) - const isAbsolutePath = isStringRequest && path.isAbsolute(req) const resolved = tryChain([ [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 @@ -132,16 +120,20 @@ class ModuleResolver { * Resolve non-string package, return directly. */ - private resolveNonStringPackage (req: string) { - const { shortcut, name } = 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', @@ -153,18 +145,13 @@ 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) + } } /** @@ -172,11 +159,15 @@ class ModuleResolver { */ private resolveDepPackage (req: string) { - const { shortcut, name } = this.normalizeRequest(req) - const entry = this.load - ? loadModule(name, this.cwd) - : resolveModule(name, this.cwd) - return new CommonModule(entry, name, shortcut, true /* fromDep */) + const { shortcut, name } = this.normalizeName(req) + try { + const entry = this.load + ? loadModule(name, this.cwd) + : resolveModule(name, this.cwd) + return new CommonModule(entry, name, shortcut, true /* fromDep */) + } catch (error) { + return getNoopModule(error) + } } /** @@ -194,8 +185,8 @@ class ModuleResolver { */ normalizeName (req: string): NormalizedModuleRequest { - let name - let shortcut + let name = null + let shortcut = null if (req.startsWith('@')) { const pkg = resolveScopePackage(req) @@ -217,7 +208,6 @@ class ModuleResolver { name = `${this.nonScopePrefix}${shortcut}` } - // @ts-ignore return { name, shortcut } }