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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add storybook mdx rule to the config #140

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/common/webpack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ function createFallbackRules({isEnvProduction}: HelperOptions) {
generator: {
filename: 'assets/[name].[contenthash:8][ext]',
},
exclude: [/\.[jt]sx?$/, /\.json$/, /\.[cm]js$/, /\.ejs$/],
exclude: [/\.[jt]sx?$/, /\.json$/, /\.[cm]js$/, /\.ejs$/, /\.mdx$/],
},
];

Expand Down
8 changes: 7 additions & 1 deletion src/common/webpack/storybook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {isLibraryConfig} from '../models';
import type {HelperOptions} from './config';
import type {ClientConfig} from '../models';
import type * as Webpack from 'webpack';
import {findMdxRuleInConfig} from './utils';

type Mode = `${WebpackMode}`;

Expand All @@ -32,6 +33,8 @@ export async function configureServiceWebpackConfig(

const webpackConfig = await configureWebpackConfigForStorybook(mode, options);

const mdxRule = findMdxRuleInConfig(storybookConfig);

return {
...storybookConfig,
plugins: [...(storybookConfig.plugins ?? []), ...webpackConfig.plugins],
Expand All @@ -53,7 +56,10 @@ export async function configureServiceWebpackConfig(
},
module: {
...storybookConfig.module,
rules: webpackConfig.module.rules,
rules:
mdxRule && webpackConfig.module?.rules
? [mdxRule, ...webpackConfig.module.rules]
: webpackConfig.module.rules,
},
};
}
Expand Down
16 changes: 16 additions & 0 deletions src/common/webpack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {prettyTime} from '../logger/pretty-time';
import type webpack from 'webpack';
import type {Logger} from '../logger';

import type * as Webpack from 'webpack';

export function webpackCompilerHandlerFactory(logger: Logger, onCompilationEnd?: () => void) {
return async (err?: Error | null, stats?: webpack.Stats) => {
if (err) {
Expand Down Expand Up @@ -89,3 +91,17 @@ function readJsonConfig(pathname: string) {
throw new Error(`Couldn't read config ${pathname}`);
}
}

export const findMdxRuleInConfig = (storybookConfig: Webpack.Configuration) => {
const mdxRule = storybookConfig.module?.rules?.find((rule) => {
const test = (rule as {test: RegExp}).test;

if (!test) {
return false;
}

return test.test('.mdx');
});

return mdxRule;
};
Loading