@@ -15,13 +15,16 @@ const traverse = babelTraverse.default || babelTraverse;
1515 * and inserting blank comments into documentation.js's processing stream.
1616 * Through inference steps, these comments gain more information and are automatically
1717 * documented as well as we can.
18+ * @param {Object } config
19+ * @param {Object } [config.extensions] extensions to try when resolving
1820 * @param {Object } ast the babel-parsed syntax tree
1921 * @param {Object } data the name of the file
2022 * @param {Function } addComment a method that creates a new comment if necessary
2123 * @returns {Array<Object> } comments
2224 * @private
2325 */
2426export default function walkExported (
27+ config /* { extensions?: string[] } */ ,
2528 ast ,
2629 data /*: {
2730 file: string
@@ -120,7 +123,8 @@ export default function walkExported(
120123 local ,
121124 exportKind ,
122125 filename ,
123- source . value
126+ source . value ,
127+ config . extensions
124128 ) ;
125129 bindingPath = tmp . ast ;
126130 specData = tmp . data ;
@@ -181,16 +185,40 @@ function traverseExportedSubtree(path, data, addComments, overrideName) {
181185 }
182186}
183187
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.
188194 }
189195
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+
190218 let value = dataCache . get ( path ) ;
191219 if ( ! value ) {
192220 const input = fs . readFileSync ( path , 'utf-8' ) ;
193- const ast = parseToAst ( input , filePath ) ;
221+ const ast = parseToAst ( input , path ) ;
194222 value = {
195223 data : {
196224 file : path ,
@@ -209,10 +237,11 @@ function findExportDeclaration(
209237 name ,
210238 exportKind ,
211239 referrer ,
212- filename
240+ filename ,
241+ extensions
213242) {
214243 const depPath = nodePath . resolve ( nodePath . dirname ( referrer ) , filename ) ;
215- const tmp = getCachedData ( dataCache , depPath ) ;
244+ const tmp = getCachedData ( dataCache , depPath , extensions ) ;
216245 const ast = tmp . ast ;
217246 let data = tmp . data ;
218247
@@ -273,7 +302,8 @@ function findExportDeclaration(
273302 local ,
274303 exportKind ,
275304 depPath ,
276- source . value
305+ source . value ,
306+ extensions
277307 ) ;
278308 rv = tmp . ast ;
279309 data = tmp . data ;
0 commit comments