Skip to content

Commit 51de6cf

Browse files
shigmaulivz
authored andcommitted
fix($core): optimize error log (close: #1296) (#1413)
1 parent 2e3efb4 commit 51de6cf

File tree

3 files changed

+54
-58
lines changed

3 files changed

+54
-58
lines changed

packages/@vuepress/core/lib/node/loadTheme.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ function resolveTheme (ctx, resolver, ignoreLocal, theme) {
123123
}
124124

125125
try {
126-
entry = pluginAPI.normalizePlugin(path, ctx.themeConfig)
127-
} catch (error) {
126+
entry = pluginAPI.normalizePlugin('theme', path, ctx.themeConfig)
127+
} catch (e) {
128+
logger.warn(e.message)
128129
entry = {}
129130
}
130131

packages/@vuepress/core/lib/node/plugin-api/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ module.exports = class PluginAPI {
8686
plugin = pluginRaw
8787
} else {
8888
try {
89-
plugin = this.normalizePlugin(pluginRaw, pluginOptions)
89+
plugin = this.normalizePlugin('plugin', pluginRaw, pluginOptions)
9090
} catch (e) {
9191
logger.warn(e.message)
9292
return this
@@ -117,10 +117,15 @@ module.exports = class PluginAPI {
117117
* @api public
118118
*/
119119

120-
normalizePlugin (pluginRaw, pluginOptions = {}) {
120+
normalizePlugin (type, pluginRaw, pluginOptions = {}) {
121121
let plugin = this._pluginResolver.resolve(pluginRaw)
122122
if (!plugin.entry) {
123-
throw new Error(`[vuepress] cannot resolve plugin "${pluginRaw}"`)
123+
if (plugin.error) {
124+
logger.debug(plugin.error)
125+
throw new Error(`An error was encounted in ${type} "${pluginRaw}"`)
126+
} else {
127+
throw new Error(`Cannot resolve ${type} "${pluginRaw}"`)
128+
}
124129
}
125130
plugin = flattenPlugin(plugin, pluginOptions, this._pluginContext, this)
126131
plugin.$$normalized = true

packages/@vuepress/shared-utils/src/moduleResolver.ts

+43-53
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,17 @@ const SCOPE_PACKAGE_RE = /^@(.*)\/(.*)/
2424
*/
2525

2626
export class CommonModule {
27-
name: string | null
28-
entry: string | null
29-
shortcut: string | null
30-
fromDep: boolean | null
31-
3227
constructor (
33-
entry: string | null,
34-
name: string | null,
35-
shortcut: string | null,
36-
fromDep: boolean | null,
37-
) {
38-
this.entry = entry
39-
this.shortcut = shortcut
40-
this.name = name
41-
this.fromDep = fromDep
42-
}
28+
public entry: string | null,
29+
public name: string | null,
30+
public shortcut: string | null,
31+
public fromDep: boolean | null,
32+
public error?: Error
33+
) {}
34+
}
35+
36+
function getNoopModule(error?: Error) {
37+
return new CommonModule(null, null, null, null, error)
4338
}
4439

4540
export interface NormalizedModuleRequest {
@@ -54,22 +49,17 @@ export interface NormalizedModuleRequest {
5449
type Type = String | Number | Boolean | RegExp | Function | Object | Record<string, any> | Array<any>
5550

5651
class ModuleResolver {
57-
private type: string
58-
private org: string
59-
private allowedTypes: Type[]
60-
private load: boolean
61-
private cwd: string
6252
private nonScopePrefix: string
6353
private scopePrefix: string
6454
private typePrefixLength: number
6555
private prefixSlicePosition: number
6656

6757
constructor (
68-
type: string,
69-
org: string,
70-
allowedTypes: Type[] = [String],
71-
load = false,
72-
cwd: string
58+
private type: string,
59+
private org: string,
60+
private allowedTypes: Type[],
61+
private load = false,
62+
private cwd: string
7363
) {
7464
this.type = type
7565
this.org = org
@@ -103,17 +93,15 @@ class ModuleResolver {
10393
}
10494

10595
const isStringRequest = isString(req)
106-
const isAbsolutePath = isStringRequest && path.isAbsolute(req)
10796

10897
const resolved = tryChain<string, CommonModule>([
10998
[this.resolveNonStringPackage.bind(this), !isStringRequest],
110-
[this.resolveAbsolutePathPackage.bind(this), isStringRequest && isAbsolutePath],
111-
[this.resolveRelativePathPackage.bind(this), isStringRequest && !isAbsolutePath],
99+
[this.resolvePathPackage.bind(this), isStringRequest],
112100
[this.resolveDepPackage.bind(this), isStringRequest]
113101
], req)
114102

115103
if (!resolved) {
116-
return new CommonModule(null, null, null, null /* fromDep */)
104+
return getNoopModule()
117105
}
118106

119107
return resolved
@@ -132,16 +120,20 @@ class ModuleResolver {
132120
* Resolve non-string package, return directly.
133121
*/
134122

135-
private resolveNonStringPackage (req: string) {
136-
const { shortcut, name } = <NormalizedModuleRequest>this.normalizeRequest(req)
123+
private resolveNonStringPackage (req: any) {
124+
const { shortcut, name } = this.normalizeRequest(req)
137125
return new CommonModule(req, name, shortcut, false /* fromDep */)
138126
}
139127

140128
/**
141-
* Resolve module with absolute path.
129+
* Resolve module with absolute/relative path.
142130
*/
143131

144-
resolveAbsolutePathPackage (req: string) {
132+
resolvePathPackage (req: string) {
133+
if (!path.isAbsolute(req)) {
134+
req = path.resolve(this.cwd, req)
135+
}
136+
145137
const normalized = fsExistsFallback([
146138
req,
147139
req + '.js',
@@ -153,30 +145,29 @@ class ModuleResolver {
153145
}
154146

155147
const dirname = path.parse(normalized).name
156-
const { shortcut, name } = this.normalizeRequest(dirname)
157-
const module = this.load ? require(normalized) : normalized
158-
return new CommonModule(module, name, shortcut, false /* fromDep */)
159-
}
160-
161-
/**
162-
* Resolve module with absolute path.
163-
*/
164-
165-
private resolveRelativePathPackage (req: string) {
166-
req = path.resolve(process.cwd(), req)
167-
return this.resolveAbsolutePathPackage(req)
148+
const { shortcut, name } = this.normalizeName(dirname)
149+
try {
150+
const module = this.load ? require(normalized) : normalized
151+
return new CommonModule(module, name, shortcut, false /* fromDep */)
152+
} catch (error) {
153+
return getNoopModule(error)
154+
}
168155
}
169156

170157
/**
171158
* Resolve module from dependency.
172159
*/
173160

174161
private resolveDepPackage (req: string) {
175-
const { shortcut, name } = this.normalizeRequest(req)
176-
const entry = this.load
177-
? loadModule(<string>name, this.cwd)
178-
: resolveModule(<string>name, this.cwd)
179-
return new CommonModule(entry, name, shortcut, true /* fromDep */)
162+
const { shortcut, name } = this.normalizeName(req)
163+
try {
164+
const entry = this.load
165+
? loadModule(<string>name, this.cwd)
166+
: resolveModule(<string>name, this.cwd)
167+
return new CommonModule(entry, name, shortcut, true /* fromDep */)
168+
} catch (error) {
169+
return getNoopModule(error)
170+
}
180171
}
181172

182173
/**
@@ -194,8 +185,8 @@ class ModuleResolver {
194185
*/
195186

196187
normalizeName (req: string): NormalizedModuleRequest {
197-
let name
198-
let shortcut
188+
let name = null
189+
let shortcut = null
199190

200191
if (req.startsWith('@')) {
201192
const pkg = resolveScopePackage(req)
@@ -217,7 +208,6 @@ class ModuleResolver {
217208
name = `${this.nonScopePrefix}${shortcut}`
218209
}
219210

220-
// @ts-ignore
221211
return { name, shortcut }
222212
}
223213

0 commit comments

Comments
 (0)