Skip to content

Commit

Permalink
Update docs + preact presets to not clobber each other.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Oct 12, 2022
1 parent f9fcb87 commit 8e0a57c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
11 changes: 10 additions & 1 deletion code/addons/docs/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ type BabelParams = {
};
function createBabelOptions({ babelOptions, mdxBabelOptions, configureJSX }: BabelParams) {
const babelPlugins = mdxBabelOptions?.plugins || babelOptions?.plugins || [];

const filteredBabelPlugins = babelPlugins.filter((p: any) => {
const name = Array.isArray(p) ? p[0] : p;
if (typeof name === 'string') {
return !name.includes('plugin-transform-react-jsx');
}
return true;
});

const jsxPlugin = [
require.resolve('@babel/plugin-transform-react-jsx'),
{ pragma: 'React.createElement', pragmaFrag: 'React.Fragment' },
];
const plugins = configureJSX ? [...babelPlugins, jsxPlugin] : babelPlugins;
const plugins = configureJSX ? [...filteredBabelPlugins, jsxPlugin] : babelPlugins;
return {
// don't use the root babelrc by default (users can override this in mdxBabelOptions)
babelrc: false,
Expand Down
16 changes: 1 addition & 15 deletions code/presets/preact-webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const babel: StorybookConfig['babelDefault'] = (config) => {
...(config.plugins || []).filter((p) => {
const name = Array.isArray(p) ? p[0] : p;
if (typeof name === 'string') {
return !name.includes('babel-plugin-transform-react-jsx');
return !name.includes('plugin-transform-react-jsx');
}
return true;
}),
Expand All @@ -23,20 +23,6 @@ export const babel: StorybookConfig['babelDefault'] = (config) => {
};

export const webpackFinal: StorybookConfig['webpackFinal'] = (config) => {
const rules = config.module?.rules || [];
const tsxRule = rules.find((rule) => (rule.test as RegExp).test?.('main.tsx'));
tsxRule.use = (tsxRule.use as any).map((entry: any) => {
let newPlugins = entry.options.plugins;
if (entry.loader?.includes('babel-loader')) {
newPlugins = (entry.options as any).plugins.map((plugin: any) => {
if (plugin[0]?.includes?.('@babel/plugin-transform-react-jsx')) {
return [plugin[0], { importSource: 'preact', runtime: 'automatic' }];
}
return plugin;
});
}
return { ...entry, options: { ...entry.options, plugins: newPlugins } };
});
return {
...config,
resolve: {
Expand Down

0 comments on commit 8e0a57c

Please sign in to comment.