Skip to content

Commit

Permalink
fix($core): optimize error log (close: #1296) (#1413)
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma authored and ulivz committed Sep 23, 2019
1 parent 2e3efb4 commit 51de6cf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 58 deletions.
5 changes: 3 additions & 2 deletions packages/@vuepress/core/lib/node/loadTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
}

Expand Down
11 changes: 8 additions & 3 deletions packages/@vuepress/core/lib/node/plugin-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
96 changes: 43 additions & 53 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 All @@ -54,22 +49,17 @@ export interface NormalizedModuleRequest {
type Type = String | Number | Boolean | RegExp | Function | Object | Record<string, any> | Array<any>

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
Expand Down Expand Up @@ -103,17 +93,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 @@ -132,16 +120,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 @@ -153,30 +145,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 @@ -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)
Expand All @@ -217,7 +208,6 @@ class ModuleResolver {
name = `${this.nonScopePrefix}${shortcut}`
}

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

Expand Down

0 comments on commit 51de6cf

Please sign in to comment.