From 7e3345813cf7c9bdffedb0e0b066a46bd120f788 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Wed, 20 Feb 2019 19:09:52 +0800 Subject: [PATCH 1/2] handle error on loading module --- .../@vuepress/core/lib/plugin-api/index.js | 6 +- .../shared-utils/src/moduleResolver.ts | 81 +++++++++---------- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/packages/@vuepress/core/lib/plugin-api/index.js b/packages/@vuepress/core/lib/plugin-api/index.js index 83639fb542..bcea6abab4 100644 --- a/packages/@vuepress/core/lib/plugin-api/index.js +++ b/packages/@vuepress/core/lib/plugin-api/index.js @@ -114,7 +114,11 @@ module.exports = class PluginAPI { normalizePlugin (pluginRaw, pluginOptions = {}) { let plugin = this._pluginResolver.resolve(pluginRaw) if (!plugin.entry) { - console.warn(`[vuepress] cannot resolve plugin "${pluginRaw}"`) + if (plugin.error) { + console.warn(`[vuepress] an error was encounted in plugin "${pluginRaw}"`) + } else { + console.warn(`[vuepress] cannot resolve plugin "${pluginRaw}"`) + } return this } plugin = flattenPlugin(plugin, pluginOptions, this._pluginContext, this) diff --git a/packages/@vuepress/shared-utils/src/moduleResolver.ts b/packages/@vuepress/shared-utils/src/moduleResolver.ts index ecab531b3f..c41e8b334d 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 { @@ -99,17 +94,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 @@ -128,16 +121,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', @@ -149,18 +146,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) + } } /** @@ -168,11 +160,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) + } } /** @@ -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) @@ -213,7 +209,6 @@ class ModuleResolver { name = `${this.nonScopePrefix}${shortcut}` } - // @ts-ignore return { name, shortcut } } From 4b469ac980930d1c8dfe98a5a192123d573b84d5 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Wed, 20 Feb 2019 19:55:22 +0800 Subject: [PATCH 2/2] optimize error log format --- packages/@vuepress/core/lib/plugin-api/index.js | 8 ++++---- packages/@vuepress/core/lib/prepare/loadTheme.js | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/@vuepress/core/lib/plugin-api/index.js b/packages/@vuepress/core/lib/plugin-api/index.js index bcea6abab4..affe0bafc8 100644 --- a/packages/@vuepress/core/lib/plugin-api/index.js +++ b/packages/@vuepress/core/lib/plugin-api/index.js @@ -84,7 +84,7 @@ module.exports = class PluginAPI { if (isPlainObject(pluginRaw) && pluginRaw.$$normalized) { plugin = pluginRaw } else { - plugin = this.normalizePlugin(pluginRaw, pluginOptions) + plugin = this.normalizePlugin('plugin', pluginRaw, pluginOptions) } if (plugin.multiple !== true) { @@ -111,13 +111,13 @@ module.exports = class PluginAPI { * @api public */ - normalizePlugin (pluginRaw, pluginOptions = {}) { + normalizePlugin (type, pluginRaw, pluginOptions = {}) { let plugin = this._pluginResolver.resolve(pluginRaw) if (!plugin.entry) { if (plugin.error) { - console.warn(`[vuepress] an error was encounted in plugin "${pluginRaw}"`) + console.warn(`[vuepress] An error was encounted in ${type} "${pluginRaw}"`) } else { - console.warn(`[vuepress] cannot resolve plugin "${pluginRaw}"`) + console.warn(`[vuepress] Cannot resolve ${type} "${pluginRaw}"`) } return this } diff --git a/packages/@vuepress/core/lib/prepare/loadTheme.js b/packages/@vuepress/core/lib/prepare/loadTheme.js index f07804cddb..3a510e54fc 100644 --- a/packages/@vuepress/core/lib/prepare/loadTheme.js +++ b/packages/@vuepress/core/lib/prepare/loadTheme.js @@ -66,7 +66,7 @@ module.exports = async function loadTheme (ctx) { } try { - themeEntryFile = pluginAPI.normalizePlugin(themePath, ctx.themeConfig) + themeEntryFile = pluginAPI.normalizePlugin('theme', themePath, ctx.themeConfig) } catch (error) { themeEntryFile = {} } @@ -88,7 +88,7 @@ module.exports = async function loadTheme (ctx) { parentThemePath = normalizeThemePath(resolved) try { - parentThemeEntryFile = pluginAPI.normalizePlugin(parentThemePath, ctx.themeConfig) + parentThemeEntryFile = pluginAPI.normalizePlugin('theme', parentThemePath, ctx.themeConfig) } catch (error) { parentThemeEntryFile = {} }