Skip to content

Commit

Permalink
Merge pull request #24993 from storybookjs/valentin/fix-react-docgen-…
Browse files Browse the repository at this point in the history
…for-nextjs

Next.js: Fix react-docgen usage with preset-env settings
  • Loading branch information
valentinpalkovic authored Nov 28, 2023
2 parents c4d5f05 + 4933c17 commit a60f3dd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
46 changes: 36 additions & 10 deletions code/presets/react-webpack/src/framework-preset-react-docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@ describe('framework-preset-react-docgen', () => {

const config = await preset.webpackFinal?.(webpackConfig, {
presets: {
apply: async () =>
({
check: false,
reactDocgen: 'react-docgen',
} as Partial<TypescriptOptions>),
apply: async (name: string) => {
if (name === 'typescript') {
return {
check: false,
reactDocgen: 'react-docgen',
} as Partial<TypescriptOptions>;
}

if (name === 'babel') {
return {
plugins: [],
presets: [],
};
}

return undefined;
},
},
presetsList: presetsListWithDocs,
} as any);
Expand All @@ -33,6 +45,7 @@ describe('framework-preset-react-docgen', () => {
{
exclude: /node_modules\/.*/,
loader: '@storybook/preset-react-webpack/dist/loaders/react-docgen-loader',
options: { babelOptions: { plugins: [], presets: [] } },
test: /\.(cjs|mjs|tsx?|jsx?)$/,
},
],
Expand All @@ -50,11 +63,23 @@ describe('framework-preset-react-docgen', () => {
const config = await preset.webpackFinal?.(webpackConfig, {
presets: {
// @ts-expect-error (not strict)
apply: async () =>
({
check: false,
reactDocgen: 'react-docgen-typescript',
} as Partial<TypescriptOptions>),
apply: async (name: string) => {
if (name === 'typescript') {
return {
check: false,
reactDocgen: 'react-docgen-typescript',
} as Partial<TypescriptOptions>;
}

if (name === 'babel') {
return {
plugins: [],
presets: [],
};
}

return undefined;
},
},
presetsList: presetsListWithDocs,
});
Expand All @@ -65,6 +90,7 @@ describe('framework-preset-react-docgen', () => {
{
exclude: /node_modules\/.*/,
loader: '@storybook/preset-react-webpack/dist/loaders/react-docgen-loader',
options: { babelOptions: { plugins: [], presets: [] } },
test: /\.(cjs|mjs|jsx?)$/,
},
],
Expand Down
9 changes: 9 additions & 0 deletions code/presets/react-webpack/src/framework-preset-react-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const webpackFinal: StorybookConfig['webpackFinal'] = async (
}

if (reactDocgen !== 'react-docgen-typescript') {
const babelOptions = await options.presets.apply('babel', {});
return {
...config,
module: {
Expand All @@ -34,6 +35,9 @@ export const webpackFinal: StorybookConfig['webpackFinal'] = async (
require.resolve,
'@storybook/preset-react-webpack/dist/loaders/react-docgen-loader'
),
options: {
babelOptions,
},
exclude: /node_modules\/.*/,
},
],
Expand All @@ -43,6 +47,8 @@ export const webpackFinal: StorybookConfig['webpackFinal'] = async (

const { ReactDocgenTypeScriptPlugin } = await import('@storybook/react-docgen-typescript-plugin');

const babelOptions = await options.presets.apply('babel', {});

return {
...config,
module: {
Expand All @@ -55,6 +61,9 @@ export const webpackFinal: StorybookConfig['webpackFinal'] = async (
require.resolve,
'@storybook/preset-react-webpack/dist/loaders/react-docgen-loader'
),
options: {
babelOptions,
},
exclude: /node_modules\/.*/,
},
],
Expand Down
11 changes: 11 additions & 0 deletions code/presets/react-webpack/src/loaders/react-docgen-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,24 @@ const handlers = [...defaultHandlers, actualNameHandler];

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

const { plugins, presets } = babelOptions;

try {
const docgenResults = parse(source, {
filename: this.resourcePath,
resolver: defaultResolver,
handlers,
importer: defaultImporter,
babelOptions: {
babelrc: false,
configFile: false,
plugins,
presets,
},
}) as DocObj[];

const magicString = new MagicString(source);
Expand Down

0 comments on commit a60f3dd

Please sign in to comment.