Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

React-Docgen: Make error-handling more gentle #25055

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ describe('framework-preset-react-docgen', () => {
module: {
rules: [
{
exclude: /node_modules/,
exclude: /(\.stories\.(js|jsx|ts|tsx))|(node_modules)/,
loader: '@storybook/preset-react-webpack/dist/loaders/react-docgen-loader',
options: { babelOptions: { plugins: [], presets: [] } },
options: { babelOptions: { plugins: [], presets: [] }, debug: false },
test: /\.(cjs|mjs|tsx?|jsx?)$/,
},
],
Expand Down Expand Up @@ -88,9 +88,9 @@ describe('framework-preset-react-docgen', () => {
module: {
rules: [
{
exclude: /node_modules/,
exclude: /(\.stories\.(js|jsx|ts|tsx))|(node_modules)/,
loader: '@storybook/preset-react-webpack/dist/loaders/react-docgen-loader',
options: { babelOptions: { plugins: [], presets: [] } },
options: { babelOptions: { plugins: [], presets: [] }, debug: false },
test: /\.(cjs|mjs|jsx?)$/,
},
],
Expand Down
7 changes: 5 additions & 2 deletions code/presets/react-webpack/src/framework-preset-react-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const webpackFinal: StorybookConfig['webpackFinal'] = async (
if (!hasDocsOrControls(options)) return config;

const typescriptOptions = await options.presets.apply('typescript', {} as any);
const debug = options.loglevel === 'debug';

const { reactDocgen, reactDocgenTypescriptOptions } = typescriptOptions || {};

Expand All @@ -34,8 +35,9 @@ export const webpackFinal: StorybookConfig['webpackFinal'] = async (
),
options: {
babelOptions,
debug,
},
exclude: /node_modules/,
exclude: /(\.stories\.(js|jsx|ts|tsx))|(node_modules)/,
},
],
},
Expand All @@ -60,8 +62,9 @@ export const webpackFinal: StorybookConfig['webpackFinal'] = async (
),
options: {
babelOptions,
debug,
},
exclude: /node_modules/,
exclude: /(\.stories\.(js|jsx|ts|tsx))|(node_modules)/,
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
},
],
},
Expand Down
22 changes: 19 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 @@ -9,6 +9,8 @@ import {
import MagicString from 'magic-string';
import type { LoaderContext } from 'webpack';
import type { Handler, NodePath, babelTypes as t, Documentation } from 'react-docgen';
import { logger } from '@storybook/node-logger';
import type { TransformOptions } from '@babel/core';

const { getNameOrValue, isReactForwardRefCall } = utils;

Expand Down Expand Up @@ -56,11 +58,14 @@ const defaultResolver = new docgenResolver.FindExportedDefinitionsResolver();
const defaultImporter = docgenImporters.fsImporter;
const handlers = [...defaultHandlers, actualNameHandler];

export default async function reactDocgenLoader(this: LoaderContext<any>, source: string) {
export default async function reactDocgenLoader(
this: LoaderContext<{ babelOptions: TransformOptions; debug: boolean }>,
source: string
) {
const callback = this.async();
// get options
const options = this.getOptions() || {};
const { babelOptions = {} } = options;
const { babelOptions = {}, debug = false } = options;

const { plugins, presets } = babelOptions;

Expand Down Expand Up @@ -94,7 +99,18 @@ export default async function reactDocgenLoader(this: LoaderContext<any>, source
if (error.code === ERROR_CODES.MISSING_DEFINITION) {
callback(null, source);
} else {
callback(error);
if (!debug) {
logger.warn(
`Failed to parse ${this.resourcePath} with react-docgen. Rerun Storybook with --loglevel=debug to get more info.`
);
} else {
logger.warn(
`Failed to parse ${this.resourcePath} with react-docgen. Please use the below error message and the content of the file which causes the error to report the issue to the maintainers of react-docgen. https://github.com/reactjs/react-docgen`
);
logger.error(error);
}

callback(null, source);
}
}
}