Skip to content

Commit 899ba75

Browse files
committed
make use of RESOLVE_EXTENSIONS as requested
1 parent cea2827 commit 899ba75

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

code/frameworks/react-vite/src/plugins/docgen-resolver.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class ReactDocgenResolveError extends Error {
1717

1818
// These extensions are sorted by priority
1919
// resolve() will check for files in the order these extensions are sorted
20-
const RESOLVE_EXTENSIONS = ['.js', '.ts', '.tsx', '.mjs', '.cjs', '.mts', '.cts', '.jsx'];
20+
export const RESOLVE_EXTENSIONS = ['.js', '.ts', '.tsx', '.mjs', '.cjs', '.mts', '.cts', '.jsx'];
2121

2222
export function defaultLookupModule(filename: string, basedir: string): string {
2323
const resolveOptions = {

code/frameworks/react-vite/src/plugins/react-docgen.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import {
1111
import MagicString from 'magic-string';
1212
import type { PluginOption } from 'vite';
1313
import actualNameHandler from './docgen-handlers/actualNameHandler';
14-
import { ReactDocgenResolveError, defaultLookupModule } from './docgen-resolver';
14+
import {
15+
RESOLVE_EXTENSIONS,
16+
ReactDocgenResolveError,
17+
defaultLookupModule,
18+
} from './docgen-resolver';
1519

1620
type DocObj = Documentation & { actualName: string };
1721

@@ -47,11 +51,11 @@ export function reactDocgen({
4751
importer: makeFsImporter((filename, basedir) => {
4852
const result = defaultLookupModule(filename, basedir);
4953

50-
if (!result.match(/\.(mjs|tsx?|jsx?)$/)) {
51-
throw new ReactDocgenResolveError(filename);
54+
if (RESOLVE_EXTENSIONS.find((ext) => result.endsWith(ext)) === undefined) {
55+
return result;
5256
}
5357

54-
return result;
58+
throw new ReactDocgenResolveError(filename);
5559
}),
5660
filename: id,
5761
}) as DocObj[];

code/presets/react-webpack/src/loaders/react-docgen-loader.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { Handler, NodePath, babelTypes as t, Documentation } from 'react-do
1212
import { logger } from '@storybook/node-logger';
1313

1414
import {
15+
RESOLVE_EXTENSIONS,
1516
ReactDocgenResolveError,
1617
defaultLookupModule,
1718
} from '../../../../frameworks/react-vite/src/plugins/docgen-resolver';
@@ -78,11 +79,11 @@ export default async function reactDocgenLoader(
7879
importer: makeFsImporter((filename, basedir) => {
7980
const result = defaultLookupModule(filename, basedir);
8081

81-
if (!result.match(/\.(mjs|tsx?|jsx?)$/)) {
82-
throw new ReactDocgenResolveError(filename);
82+
if (RESOLVE_EXTENSIONS.find((ext) => result.endsWith(ext)) === undefined) {
83+
return result;
8384
}
8485

85-
return result;
86+
throw new ReactDocgenResolveError(filename);
8687
}),
8788
babelOptions: {
8889
babelrc: false,

0 commit comments

Comments
 (0)