Skip to content

Commit 76ed07b

Browse files
committed
🤖 Fix emotion babel plugin configuration for Chromatic
Replace Storybook's default React plugin with emotion-configured one: - Filter out Storybook's vite:react plugin to avoid conflicts - Add custom React plugin with jsxImportSource and babel config - Exclude stories and node_modules from emotion transformation - Remove unnecessary babel.config.js file This approach is based on the recommended solution from storybookjs/builder-vite#210 for using emotion with Vite+Storybook. _Generated with `cmux`_
1 parent 8c6562d commit 76ed07b

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

.storybook/main.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { StorybookConfig } from '@storybook/react-vite';
22
import type { UserConfig } from 'vite';
3+
import react from '@vitejs/plugin-react';
34

45
const config: StorybookConfig = {
56
"stories": [
@@ -18,26 +19,37 @@ const config: StorybookConfig = {
1819
"options": {}
1920
},
2021
async viteFinal(config: UserConfig) {
21-
return {
22-
...config,
23-
plugins: config.plugins,
24-
optimizeDeps: {
25-
...config.optimizeDeps,
26-
include: [
27-
...(config.optimizeDeps?.include || []),
28-
'@emotion/react',
29-
'@emotion/styled',
30-
'@emotion/cache',
31-
],
32-
},
33-
build: {
34-
...config.build,
35-
commonjsOptions: {
36-
...config.build?.commonjsOptions,
37-
include: [/node_modules/],
22+
// Filter out Storybook's default React plugin
23+
config.plugins = (config.plugins || []).filter((plugin) => {
24+
return !(
25+
Array.isArray(plugin) &&
26+
plugin[0]?.name?.includes('vite:react')
27+
);
28+
});
29+
30+
// Add React plugin with emotion configuration
31+
config.plugins.push(
32+
react({
33+
exclude: [/\.stories\.(t|j)sx?$/, /node_modules/],
34+
jsxImportSource: '@emotion/react',
35+
babel: {
36+
plugins: ['@emotion/babel-plugin'],
3837
},
39-
},
38+
})
39+
);
40+
41+
// Optimize emotion dependencies
42+
config.optimizeDeps = {
43+
...config.optimizeDeps,
44+
include: [
45+
...(config.optimizeDeps?.include || []),
46+
'@emotion/react',
47+
'@emotion/styled',
48+
'@emotion/cache',
49+
],
4050
};
51+
52+
return config;
4153
},
4254
};
4355
export default config;

0 commit comments

Comments
 (0)