@@ -24,22 +24,17 @@ const SCOPE_PACKAGE_RE = /^@(.*)\/(.*)/
24
24
*/
25
25
26
26
export class CommonModule {
27
- name : string | null
28
- entry : string | null
29
- shortcut : string | null
30
- fromDep : boolean | null
31
-
32
27
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 )
43
38
}
44
39
45
40
export interface NormalizedModuleRequest {
@@ -54,22 +49,17 @@ export interface NormalizedModuleRequest {
54
49
type Type = String | Number | Boolean | RegExp | Function | Object | Record < string , any > | Array < any >
55
50
56
51
class ModuleResolver {
57
- private type : string
58
- private org : string
59
- private allowedTypes : Type [ ]
60
- private load : boolean
61
- private cwd : string
62
52
private nonScopePrefix : string
63
53
private scopePrefix : string
64
54
private typePrefixLength : number
65
55
private prefixSlicePosition : number
66
56
67
57
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
73
63
) {
74
64
this . type = type
75
65
this . org = org
@@ -103,17 +93,15 @@ class ModuleResolver {
103
93
}
104
94
105
95
const isStringRequest = isString ( req )
106
- const isAbsolutePath = isStringRequest && path . isAbsolute ( req )
107
96
108
97
const resolved = tryChain < string , CommonModule > ( [
109
98
[ 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 ] ,
112
100
[ this . resolveDepPackage . bind ( this ) , isStringRequest ]
113
101
] , req )
114
102
115
103
if ( ! resolved ) {
116
- return new CommonModule ( null , null , null , null /* fromDep */ )
104
+ return getNoopModule ( )
117
105
}
118
106
119
107
return resolved
@@ -132,16 +120,20 @@ class ModuleResolver {
132
120
* Resolve non-string package, return directly.
133
121
*/
134
122
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 )
137
125
return new CommonModule ( req , name , shortcut , false /* fromDep */ )
138
126
}
139
127
140
128
/**
141
- * Resolve module with absolute path.
129
+ * Resolve module with absolute/relative path.
142
130
*/
143
131
144
- resolveAbsolutePathPackage ( req : string ) {
132
+ resolvePathPackage ( req : string ) {
133
+ if ( ! path . isAbsolute ( req ) ) {
134
+ req = path . resolve ( this . cwd , req )
135
+ }
136
+
145
137
const normalized = fsExistsFallback ( [
146
138
req ,
147
139
req + '.js' ,
@@ -153,30 +145,29 @@ class ModuleResolver {
153
145
}
154
146
155
147
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
+ }
168
155
}
169
156
170
157
/**
171
158
* Resolve module from dependency.
172
159
*/
173
160
174
161
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
+ }
180
171
}
181
172
182
173
/**
@@ -194,8 +185,8 @@ class ModuleResolver {
194
185
*/
195
186
196
187
normalizeName ( req : string ) : NormalizedModuleRequest {
197
- let name
198
- let shortcut
188
+ let name = null
189
+ let shortcut = null
199
190
200
191
if ( req . startsWith ( '@' ) ) {
201
192
const pkg = resolveScopePackage ( req )
@@ -217,7 +208,6 @@ class ModuleResolver {
217
208
name = `${ this . nonScopePrefix } ${ shortcut } `
218
209
}
219
210
220
- // @ts -ignore
221
211
return { name, shortcut }
222
212
}
223
213
0 commit comments