Skip to content

Commit

Permalink
make use of RESOLVE_EXTENSIONS as requested
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelangen committed Feb 29, 2024
1 parent cea2827 commit 899ba75
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion code/frameworks/react-vite/src/plugins/docgen-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class ReactDocgenResolveError extends Error {

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

export function defaultLookupModule(filename: string, basedir: string): string {
const resolveOptions = {
Expand Down
12 changes: 8 additions & 4 deletions code/frameworks/react-vite/src/plugins/react-docgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
import MagicString from 'magic-string';
import type { PluginOption } from 'vite';
import actualNameHandler from './docgen-handlers/actualNameHandler';
import { ReactDocgenResolveError, defaultLookupModule } from './docgen-resolver';
import {
RESOLVE_EXTENSIONS,
ReactDocgenResolveError,
defaultLookupModule,
} from './docgen-resolver';

type DocObj = Documentation & { actualName: string };

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

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

return result;
throw new ReactDocgenResolveError(filename);
}),
filename: id,
}) as DocObj[];
Expand Down
7 changes: 4 additions & 3 deletions code/presets/react-webpack/src/loaders/react-docgen-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { Handler, NodePath, babelTypes as t, Documentation } from 'react-do
import { logger } from '@storybook/node-logger';

import {
RESOLVE_EXTENSIONS,
ReactDocgenResolveError,
defaultLookupModule,
} from '../../../../frameworks/react-vite/src/plugins/docgen-resolver';
Expand Down Expand Up @@ -78,11 +79,11 @@ export default async function reactDocgenLoader(
importer: makeFsImporter((filename, basedir) => {
const result = defaultLookupModule(filename, basedir);

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

return result;
throw new ReactDocgenResolveError(filename);
}),
babelOptions: {
babelrc: false,
Expand Down

0 comments on commit 899ba75

Please sign in to comment.