Skip to content

Commit

Permalink
React JSX transform support per #12899
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Oct 27, 2020
1 parent a8bd5ef commit c805b58
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion app/react/src/server/framework-preset-react.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import path from 'path';
import { TransformOptions } from '@babel/core';

const storybookReactDirName = path.dirname(require.resolve('@storybook/react/package.json'));
// TODO: improve node_modules detection
const context = storybookReactDirName.includes('node_modules')
? path.join(storybookReactDirName, '../../') // Real life case, already in node_modules
: path.join(storybookReactDirName, '../../node_modules'); // SB Monorepo

const hasJsxRuntime = () => {
try {
require.resolve('react/jsx-runtime', { paths: [context] });
return true;
} catch (e) {
return false;
}
};

export function babelDefault(config: TransformOptions) {
const presetReactOptions = hasJsxRuntime() ? { runtime: 'automatic' } : {};
return {
...config,
presets: [
...config.presets,
require.resolve('@babel/preset-react'),
[require.resolve('@babel/preset-react'), presetReactOptions],
require.resolve('@babel/preset-flow'),
],
plugins: [...(config.plugins || []), require.resolve('babel-plugin-add-react-displayname')],
Expand Down

0 comments on commit c805b58

Please sign in to comment.