@@ -15,13 +15,16 @@ const traverse = babelTraverse.default || babelTraverse;
15
15
* and inserting blank comments into documentation.js's processing stream.
16
16
* Through inference steps, these comments gain more information and are automatically
17
17
* documented as well as we can.
18
+ * @param {Object } config
19
+ * @param {Object } [config.extensions] extensions to try when resolving
18
20
* @param {Object } ast the babel-parsed syntax tree
19
21
* @param {Object } data the name of the file
20
22
* @param {Function } addComment a method that creates a new comment if necessary
21
23
* @returns {Array<Object> } comments
22
24
* @private
23
25
*/
24
26
export default function walkExported (
27
+ config /* { extensions?: string[] } */ ,
25
28
ast ,
26
29
data /*: {
27
30
file: string
@@ -120,7 +123,8 @@ export default function walkExported(
120
123
local ,
121
124
exportKind ,
122
125
filename ,
123
- source . value
126
+ source . value ,
127
+ config . extensions
124
128
) ;
125
129
bindingPath = tmp . ast ;
126
130
specData = tmp . data ;
@@ -181,16 +185,40 @@ function traverseExportedSubtree(path, data, addComments, overrideName) {
181
185
}
182
186
}
183
187
184
- function getCachedData ( dataCache , filePath ) {
185
- let path = filePath ;
186
- if ( ! nodePath . extname ( path ) ) {
187
- path = require . resolve ( path ) ;
188
+ function resolveFile ( filePath , extensions = [ ] ) {
189
+ try {
190
+ // First try resolving the file with the default extensions.
191
+ return require . resolve ( filePath ) ;
192
+ } catch {
193
+ // If that fails, try resolving the file with the extensions passed in.
188
194
}
189
195
196
+ // Then try all other extensions in order.
197
+ for ( const extension of extensions ) {
198
+ try {
199
+ return require . resolve (
200
+ `${ filePath } ${ extension . startsWith ( '.' ) ? extension : `.${ extension } ` } `
201
+ ) ;
202
+ } catch {
203
+ continue ;
204
+ }
205
+ }
206
+
207
+ throw new Error (
208
+ `Could not resolve \`${ filePath } \` with any of the extensions: ${ [
209
+ ...require . extensions ,
210
+ ...extensions
211
+ ] . join ( ', ' ) } `
212
+ ) ;
213
+ }
214
+
215
+ function getCachedData ( dataCache , filePath , extensions ) {
216
+ const path = resolveFile ( filePath , extensions ) ;
217
+
190
218
let value = dataCache . get ( path ) ;
191
219
if ( ! value ) {
192
220
const input = fs . readFileSync ( path , 'utf-8' ) ;
193
- const ast = parseToAst ( input , filePath ) ;
221
+ const ast = parseToAst ( input , path ) ;
194
222
value = {
195
223
data : {
196
224
file : path ,
@@ -209,10 +237,11 @@ function findExportDeclaration(
209
237
name ,
210
238
exportKind ,
211
239
referrer ,
212
- filename
240
+ filename ,
241
+ extensions
213
242
) {
214
243
const depPath = nodePath . resolve ( nodePath . dirname ( referrer ) , filename ) ;
215
- const tmp = getCachedData ( dataCache , depPath ) ;
244
+ const tmp = getCachedData ( dataCache , depPath , extensions ) ;
216
245
const ast = tmp . ast ;
217
246
let data = tmp . data ;
218
247
@@ -273,7 +302,8 @@ function findExportDeclaration(
273
302
local ,
274
303
exportKind ,
275
304
depPath ,
276
- source . value
305
+ source . value ,
306
+ extensions
277
307
) ;
278
308
rv = tmp . ast ;
279
309
data = tmp . data ;
0 commit comments